Merge branch 'master' into topic/fix_multilocation_trial_creation
[sgn.git] / js / sprintf.js
blob0e8d02c988013b28c8497b68b5148da08a62568e
1 /**\r
2 sprintf() for JavaScript 0.7-beta1\r
3 http://www.diveintojavascript.com/projects/javascript-sprintf\r
4 \r
5 Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>\r
6 All rights reserved.\r
7 \r
8 Redistribution and use in source and binary forms, with or without\r
9 modification, are permitted provided that the following conditions are met:\r
10     * Redistributions of source code must retain the above copyright\r
11       notice, this list of conditions and the following disclaimer.\r
12     * Redistributions in binary form must reproduce the above copyright\r
13       notice, this list of conditions and the following disclaimer in the\r
14       documentation and/or other materials provided with the distribution.\r
15     * Neither the name of sprintf() for JavaScript nor the\r
16       names of its contributors may be used to endorse or promote products\r
17       derived from this software without specific prior written permission.\r
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
22 DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY\r
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31 Changelog:\r
32 2010.09.06 - 0.7-beta1\r
33   - features: vsprintf, support for named placeholders\r
34   - enhancements: format cache, reduced global namespace pollution\r
36 2010.05.22 - 0.6:\r
37  - reverted to 0.4 and fixed the bug regarding the sign of the number 0\r
38  Note:\r
39  Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)\r
40  who warned me about a bug in 0.5, I discovered that the last update was\r
41  a regress. I appologize for that.\r
43 2010.05.09 - 0.5:\r
44  - bug fix: 0 is now preceeded with a + sign\r
45  - bug fix: the sign was not at the right position on padded results (Kamal Abdali)\r
46  - switched from GPL to BSD license\r
48 2007.10.21 - 0.4:\r
49  - unit test and patch (David Baird)\r
51 2007.09.17 - 0.3:\r
52  - bug fix: no longer throws exception on empty paramenters (Hans Pufal)\r
54 2007.09.11 - 0.2:\r
55  - feature: added argument swapping\r
57 2007.04.03 - 0.1:\r
58  - initial release\r
59 **/\r
61 var sprintf = (function() {\r
62         function get_type(variable) {\r
63                 return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();\r
64         }\r
65         function str_repeat(input, multiplier) {\r
66                 for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */}\r
67                 return output.join('');\r
68         }\r
70         var str_format = function() {\r
71                 if (!str_format.cache.hasOwnProperty(arguments[0])) {\r
72                         str_format.cache[arguments[0]] = str_format.parse(arguments[0]);\r
73                 }\r
74                 return str_format.format.call(null, str_format.cache[arguments[0]], arguments);\r
75         };\r
77         str_format.format = function(parse_tree, argv) {\r
78                 var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;\r
79                 for (i = 0; i < tree_length; i++) {\r
80                         node_type = get_type(parse_tree[i]);\r
81                         if (node_type === 'string') {\r
82                                 output.push(parse_tree[i]);\r
83                         }\r
84                         else if (node_type === 'array') {\r
85                                 match = parse_tree[i]; // convenience purposes only\r
86                                 if (match[2]) { // keyword argument\r
87                                         arg = argv[cursor];\r
88                                         for (k = 0; k < match[2].length; k++) {\r
89                                                 if (!arg.hasOwnProperty(match[2][k])) {\r
90                                                         throw(sprintf('[sprintf] property "%s" does not exist', match[2][k]));\r
91                                                 }\r
92                                                 arg = arg[match[2][k]];\r
93                                         }\r
94                                 }\r
95                                 else if (match[1]) { // positional argument (explicit)\r
96                                         arg = argv[match[1]];\r
97                                 }\r
98                                 else { // positional argument (implicit)\r
99                                         arg = argv[cursor++];\r
100                                 }\r
102                                 if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) {\r
103                                         throw(sprintf('[sprintf] expecting number but found %s', get_type(arg)));\r
104                                 }\r
105                                 switch (match[8]) {\r
106                                         case 'b': arg = arg.toString(2); break;\r
107                                         case 'c': arg = String.fromCharCode(arg); break;\r
108                                         case 'd': arg = parseInt(arg, 10); break;\r
109                                         case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;\r
110                                         case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;\r
111                                         case 'o': arg = arg.toString(8); break;\r
112                                         case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;\r
113                                         case 'u': arg = Math.abs(arg); break;\r
114                                         case 'x': arg = arg.toString(16); break;\r
115                                         case 'X': arg = arg.toString(16).toUpperCase(); break;\r
116                                 }\r
117                                 arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg);\r
118                                 pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' ';\r
119                                 pad_length = match[6] - String(arg).length;\r
120                                 pad = match[6] ? str_repeat(pad_character, pad_length) : '';\r
121                                 output.push(match[5] ? arg + pad : pad + arg);\r
122                         }\r
123                 }\r
124                 return output.join('');\r
125         };\r
127         str_format.cache = {};\r
129         str_format.parse = function(fmt) {\r
130                 var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;\r
131                 while (_fmt) {\r
132                         if ((match = /^[^\x25]+/.exec(_fmt)) !== null) {\r
133                                 parse_tree.push(match[0]);\r
134                         }\r
135                         else if ((match = /^\x25{2}/.exec(_fmt)) !== null) {\r
136                                 parse_tree.push('%');\r
137                         }\r
138                         else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {\r
139                                 if (match[2]) {\r
140                                         arg_names |= 1;\r
141                                         var field_list = [], replacement_field = match[2], field_match = [];\r
142                                         if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {\r
143                                                 field_list.push(field_match[1]);\r
144                                                 while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {\r
145                                                         if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {\r
146                                                                 field_list.push(field_match[1]);\r
147                                                         }\r
148                                                         else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) {\r
149                                                                 field_list.push(field_match[1]);\r
150                                                         }\r
151                                                         else {\r
152                                                                 throw('[sprintf] huh?');\r
153                                                         }\r
154                                                 }\r
155                                         }\r
156                                         else {\r
157                                                 throw('[sprintf] huh?');\r
158                                         }\r
159                                         match[2] = field_list;\r
160                                 }\r
161                                 else {\r
162                                         arg_names |= 2;\r
163                                 }\r
164                                 if (arg_names === 3) {\r
165                                         throw('[sprintf] mixing positional and named placeholders is not (yet) supported');\r
166                                 }\r
167                                 parse_tree.push(match);\r
168                         }\r
169                         else {\r
170                                 throw('[sprintf] huh?');\r
171                         }\r
172                         _fmt = _fmt.substring(match[0].length);\r
173                 }\r
174                 return parse_tree;\r
175         };\r
177         return str_format;\r
178 })();\r
180 var vsprintf = function(fmt, argv) {\r
181         argv.unshift(fmt);\r
182         return sprintf.apply(null, argv);\r
183 };\r