From ae02212dd983f30149b0e1a4b565694b4d1bffb4 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 13 Aug 2009 09:00:07 -0600 Subject: [PATCH] add a few methods to String; support ISO-8859-1 charset --- classpath/java/lang/String.java | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 1ecb53629c..2233e4500b 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -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) {