You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/08/11 11:12:19 UTC
svn commit: r430732 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/lang/String.java
test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java
Author: tellison
Date: Fri Aug 11 02:12:18 2006
New Revision: 430732
URL: http://svn.apache.org/viewvc?rev=430732&view=rev
Log:
Apply patch HARMONY-1152 ([luni] missing methods j.l.String.format())
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java?rev=430732&r1=430731&r2=430732&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java Fri Aug 11 02:12:18 2006
@@ -18,6 +18,7 @@
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Comparator;
+import java.util.Formatter;
import java.util.Locale;
import java.util.regex.Pattern;
@@ -1965,6 +1966,56 @@
return r - offset;
}
+ /**
+ * Returns a printf-style formatted string, using the supplied format and
+ * arguments. This function is a shortcut to
+ * <code>format(Locale.getDefault(), format, args)</code>.
+ *
+ * @param format
+ * a format string
+ * @param args
+ * arguments to replace format specifiers, may be none
+ * @throws NullPointerException
+ * if the format is null
+ * @throws IllegalArgumentException
+ * if the format is invalid
+ * @return The formatted string
+ * @since 1.5
+ * @see java.util.Formatter
+ */
+ public static String format(String format, Object... args) {
+ return format(Locale.getDefault(), format, args);
+ }
+
+ /**
+ * Returns a printf-style formatted string, using the supplied format and
+ * arguments, accordingly to the specified locale.
+ *
+ * @param loc
+ * the locale to apply; <code>null</code> value means no
+ * localization
+ * @param format
+ * a format string
+ * @param args
+ * arguments to replace format specifiers, may be none
+ * @throws NullPointerException
+ * if the format is null
+ * @throws IllegalArgumentException
+ * if the format is invalid
+ * @return The formatted string
+ * @since 1.5
+ * @see java.util.Formatter
+ */
+ public static String format(Locale loc, String format, Object... args) {
+ if (format == null) {
+ throw new NullPointerException("null format argument");
+ }
+ int bufferSize = format.length()
+ + (args == null ? 0 : args.length * 10);
+ Formatter f = new Formatter(new StringBuilder(bufferSize), loc);
+ return f.format(format, args).toString();
+ }
+
/*
* An implementation of a String.indexOf that is supposed to perform
* substantially better than the default algorithm if the the "needle" (the
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java?rev=430732&r1=430731&r2=430732&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java Fri Aug 11 02:12:18 2006
@@ -893,6 +893,19 @@
// Test for method java.lang.String java.lang.String.contentEquals(CharSequence cs)
assertFalse("Incorrect result of compare", "qwerty".contentEquals(""));
}
+
+ /**
+ * @tests java.lang.String#format(Locale, String, Object[])
+ */
+ public void test_format() {
+ assertEquals("13% of sum is 0x11",
+ String.format("%d%% of %s is 0x%x", 13, "sum", 17));
+ assertEquals("empty format", "", String.format("", 123, this));
+ try {
+ String.format(null);
+ fail("NPE is expected on null format");
+ } catch (NullPointerException ok){}
+ }
/**
* Sets up the fixture, for example, open a network connection. This method