Permalink
Browse files

handle basic argument substitution in MessageFormat.format

Thanks to Remi for an initial version of this patch.
  • Loading branch information...
1 parent e2416dd commit e2ff771baa47ba85066055101525162b1dc49c3b @dicej dicej committed Aug 11, 2012
Showing with 15 additions and 2 deletions.
  1. +8 −2 classpath/java/text/MessageFormat.java
  2. +7 −0 test/Strings.java
@@ -28,8 +28,14 @@ public MessageFormat(String pattern) {
public StringBuffer format(Object[] args, StringBuffer target,
FieldPosition p)
{
- // todo
- return target.append(pattern);
+ // todo: handle other format substitutions and escapes, and make
+ // this more efficient:
+ String result = pattern;
+ int length = args.length;
+ for (int i = 0; i < length; i++) {
+ result = result.replace("{" + i + "}", String.valueOf(args[i]));
+ }
+ return target.append(result);
}
public StringBuffer format(Object args, StringBuffer target, FieldPosition p)
View
@@ -139,5 +139,12 @@ public static void main(String[] args) throws Exception {
testDecode(false);
testDecode(true);
+
+ expect
+ (java.text.MessageFormat.format
+ ("{0} enjoy {1} {2}. do {4}? {4} do?",
+ "I", "grape", "nuts", "foobar",
+ new Object() { public String toString() { return "you"; } })
+ .equals("I enjoy grape nuts. do you? you do?"));
}
}

0 comments on commit e2ff771

Please sign in to comment.