Skip to content

Commit

Permalink
handle basic argument substitution in MessageFormat.format
Browse files Browse the repository at this point in the history
Thanks to Remi for an initial version of this patch.
  • Loading branch information
dicej committed Aug 11, 2012
1 parent e2416dd commit e2ff771
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions classpath/java/text/MessageFormat.java
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/Strings.java
Expand Up @@ -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.