|
22 | 22 | }
|
23 | 23 |
|
24 | 24 | function sprintf(key) {
|
25 |
| - var cache = sprintf.cache |
26 |
| - if (!(cache[key])) { |
27 |
| - cache[key] = sprintf.parse(key) |
28 |
| - } |
29 |
| - return sprintf.format.call(null, cache[key], arguments) |
| 25 | + // `arguments` is not an array, but should be fine for this call |
| 26 | + return sprintf_format(sprintf_parse(key), arguments) |
| 27 | + } |
| 28 | + |
| 29 | + function vsprintf(fmt, argv) { |
| 30 | + return sprintf.apply(null, [fmt].concat(argv || [])) |
30 | 31 | }
|
31 | 32 |
|
32 |
| - sprintf.format = function(parse_tree, argv) { |
| 33 | + function sprintf_format(parse_tree, argv) { |
33 | 34 | var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, match, pad, pad_character, pad_length, is_positive, sign
|
34 | 35 | for (i = 0; i < tree_length; i++) {
|
35 | 36 | if (typeof parse_tree[i] === 'string') {
|
|
138 | 139 | return output
|
139 | 140 | }
|
140 | 141 |
|
141 |
| - sprintf.cache = Object.create(null) |
| 142 | + var sprintf_cache = Object.create(null) |
| 143 | + |
| 144 | + function sprintf_parse(fmt) { |
| 145 | + if (sprintf_cache[fmt]) { |
| 146 | + return sprintf_cache[fmt] |
| 147 | + } |
142 | 148 |
|
143 |
| - sprintf.parse = function(fmt) { |
144 | 149 | var _fmt = fmt, match, parse_tree = [], arg_names = 0
|
145 | 150 | while (_fmt) {
|
146 | 151 | if ((match = re.text.exec(_fmt)) !== null) {
|
147 |
| - parse_tree[parse_tree.length] = match[0] |
| 152 | + parse_tree.push(match[0]) |
148 | 153 | }
|
149 | 154 | else if ((match = re.modulo.exec(_fmt)) !== null) {
|
150 |
| - parse_tree[parse_tree.length] = '%' |
| 155 | + parse_tree.push('%') |
151 | 156 | }
|
152 | 157 | else if ((match = re.placeholder.exec(_fmt)) !== null) {
|
153 | 158 | if (match[2]) {
|
154 | 159 | arg_names |= 1
|
155 | 160 | var field_list = [], replacement_field = match[2], field_match = []
|
156 | 161 | if ((field_match = re.key.exec(replacement_field)) !== null) {
|
157 |
| - field_list[field_list.length] = field_match[1] |
| 162 | + field_list.push(field_match[1]) |
158 | 163 | while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
|
159 | 164 | if ((field_match = re.key_access.exec(replacement_field)) !== null) {
|
160 |
| - field_list[field_list.length] = field_match[1] |
| 165 | + field_list.push(field_match[1]) |
161 | 166 | }
|
162 | 167 | else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
|
163 |
| - field_list[field_list.length] = field_match[1] |
| 168 | + field_list.push(field_match[1]) |
164 | 169 | }
|
165 | 170 | else {
|
166 | 171 | throw new SyntaxError("[sprintf] failed to parse named argument key")
|
|
178 | 183 | if (arg_names === 3) {
|
179 | 184 | throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported")
|
180 | 185 | }
|
181 |
| - parse_tree[parse_tree.length] = match |
| 186 | + parse_tree.push(match) |
182 | 187 | }
|
183 | 188 | else {
|
184 | 189 | throw new SyntaxError("[sprintf] unexpected placeholder")
|
185 | 190 | }
|
186 | 191 | _fmt = _fmt.substring(match[0].length)
|
187 | 192 | }
|
188 |
| - return parse_tree |
189 |
| - } |
190 |
| - |
191 |
| - var vsprintf = function(fmt, argv, _argv) { |
192 |
| - _argv = [fmt].concat(argv || []) |
193 |
| - return sprintf.apply(null, _argv) |
| 193 | + return sprintf_cache[fmt] = parse_tree |
194 | 194 | }
|
195 | 195 |
|
196 | 196 | /**
|
|
213 | 213 | })
|
214 | 214 | }
|
215 | 215 | }
|
216 |
| -})(typeof window === 'undefined' ? this : window); |
| 216 | +})(this); |
0 commit comments