add a few methods to String; support ISO-8859-1 charset

This commit is contained in:
Joel Dice 2009-08-13 09:00:07 -06:00
parent 864a28f2ce
commit ae02212dd9

View File

@ -13,6 +13,7 @@ package java.lang;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;
import java.util.Comparator;
import java.util.Formatter;
import java.util.Locale;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
@ -52,7 +53,9 @@ public final class String
throws UnsupportedEncodingException
{
this(bytes, offset, length);
if (! charsetName.equalsIgnoreCase("UTF-8")) {
if (! (charsetName.equalsIgnoreCase("UTF-8")
|| charsetName.equalsIgnoreCase("ISO-8859-1")))
{
throw new UnsupportedEncodingException(charsetName);
}
}
@ -76,10 +79,7 @@ public final class String
public String(byte[] data, String charset)
throws UnsupportedEncodingException
{
this(data);
if (! charset.equals("UTF-8")) {
throw new UnsupportedEncodingException(charset);
}
this(data, 0, data.length, charset);
}
public String(byte bytes[], int highByte, int offset, int length) {
@ -540,6 +540,10 @@ public final class String
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
@ -625,8 +629,8 @@ public final class String
return length == 0;
}
public boolean contains(String match) {
return indexOf(match) != -1;
public boolean contains(CharSequence match) {
return indexOf(match.toString()) != -1;
}
public int codePointAt(int offset) {
@ -649,6 +653,14 @@ public final class String
throw new UnsupportedOperationException();
}
public static String format(Locale locale, String format, Object ... args) {
return new Formatter(locale).format(format, args).toString();
}
public static String format(String format, Object ... args) {
return format(Locale.getDefault(), format, args);
}
// for GNU Classpath compatibility:
static char[] zeroBasedStringValue(String s) {
if (s.offset == 0) {