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