1 String._toFormattedString = function String$_toFormattedString(useLocale, args) {
2 var result = '';
3 var format = args[0];
4 for (var i=0;;) {
5 var open = format.indexOf('{', i);
6 var close = format.indexOf('}', i);
7 if ((open < 0) && (close < 0)) {
8 result += format.slice(i);
9 break;
10 }
11 if ((close > 0) && ((close < open) || (open < 0))) {
12 if (format.charAt(close + 1) !== '}') {
13 throw Error.argument('format', Sys.Res.stringFormatBraceMismatch);
14 }
15 result += format.slice(i, close + 1);
16 i = close + 2;
17 continue;
18 }
19 result += format.slice(i, open);
20 i = open + 1;
21 if (format.charAt(i) === '{') {
22 result += '{';
23 i++;
24 continue;
25 }
26 if (close < 0) throw Error.argument('format', Sys.Res.stringFormatBraceMismatch);
27 var brace = format.substring(i, close);
28 var colonIndex = brace.indexOf(':');
29 var argNumber = parseInt((colonIndex < 0)? brace : brace.substring(0, colonIndex), 10) + 1;
30 if (isNaN(argNumber)) throw Error.argument('format', Sys.Res.stringFormatInvalid);
31 var argFormat = (colonIndex < 0)? '' : brace.substring(colonIndex + 1);
32 var arg = args[argNumber];
33 if (typeof(arg) === "undefined" || arg === null) {
34 arg = '';
35 }
36 if (arg.toFormattedString) {
37 result += arg.toFormattedString(argFormat);
38 }
39 else if (useLocale && arg.localeFormat) {
40 result += arg.localeFormat(argFormat);
41 }
42 else if (arg.format) {
43 result += arg.format(argFormat);
44 }
45 else
46 result += arg.toString();
47 i = close + 1;
48 }
49 return result;
50 }