|
1 | 1 | /**
|
2 |
| - * sprintf() for JavaScript v.0.4 |
3 |
| - * |
4 |
| - * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/> |
5 |
| - * Thanks to David Baird (unit test and patch). |
6 |
| - * |
7 |
| - * This program is free software; you can redistribute it and/or modify it under |
8 |
| - * the terms of the GNU General Public License as published by the Free Software |
9 |
| - * Foundation; either version 2 of the License, or (at your option) any later |
10 |
| - * version. |
11 |
| - * |
12 |
| - * This program is distributed in the hope that it will be useful, but WITHOUT |
13 |
| - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
14 |
| - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
15 |
| - * details. |
16 |
| - * |
17 |
| - * You should have received a copy of the GNU General Public License along with |
18 |
| - * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
19 |
| - * Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
| - */ |
| 2 | +sprintf() for JavaScript 0.5 |
21 | 3 |
|
22 |
| -function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); } |
| 4 | +Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com> |
| 5 | +All rights reserved. |
23 | 6 |
|
24 |
| -function sprintf () { |
25 |
| - var i = 0, a, f = arguments[i++], o = [], m, p, c, x; |
26 |
| - while (f) { |
27 |
| - if (m = /^[^\x25]+/.exec(f)) o.push(m[0]); |
28 |
| - else if (m = /^\x25{2}/.exec(f)) o.push('%'); |
29 |
| - else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) { |
30 |
| - if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments."); |
31 |
| - if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) |
32 |
| - throw("Expecting number but found " + typeof(a)); |
33 |
| - switch (m[7]) { |
34 |
| - case 'b': a = a.toString(2); break; |
35 |
| - case 'c': a = String.fromCharCode(a); break; |
36 |
| - case 'd': a = parseInt(a); break; |
37 |
| - case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break; |
38 |
| - case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break; |
39 |
| - case 'o': a = a.toString(8); break; |
40 |
| - case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break; |
41 |
| - case 'u': a = Math.abs(a); break; |
42 |
| - case 'x': a = a.toString(16); break; |
43 |
| - case 'X': a = a.toString(16).toUpperCase(); break; |
44 |
| - } |
45 |
| - a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a); |
46 |
| - c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' '; |
47 |
| - x = m[5] - String(a).length; |
48 |
| - p = m[5] ? str_repeat(c, x) : ''; |
49 |
| - o.push(m[4] ? a + p : p + a); |
50 |
| - } |
51 |
| - else throw ("Huh ?!"); |
52 |
| - f = f.substring(m[0].length); |
53 |
| - } |
54 |
| - return o.join(''); |
| 7 | +Redistribution and use in source and binary forms, with or without |
| 8 | +modification, are permitted provided that the following conditions are met: |
| 9 | + * Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | + * Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | + * Neither the name of sprintf() for JavaScript nor the |
| 15 | + names of its contributors may be used to endorse or promote products |
| 16 | + derived from this software without specific prior written permission. |
| 17 | +
|
| 18 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY |
| 22 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +
|
| 29 | +
|
| 30 | +Changelog: |
| 31 | +2007.04.03 - 0.1: |
| 32 | + - initial release |
| 33 | +2007.09.11 - 0.2: |
| 34 | + - feature: added argument swapping |
| 35 | +2007.09.17 - 0.3: |
| 36 | + - bug fix: no longer throws exception on empty paramenters (Hans Pufal) |
| 37 | +2007.10.21 - 0.4: |
| 38 | + - unit test and patch (David Baird) |
| 39 | +2010.05.09 - 0.5: |
| 40 | + - bug fix: 0 is now preceeded with a + sign |
| 41 | + - bug fix: the sign was not at the right position on padded results (Kamal Abdali) |
| 42 | + - switched from GPL to BSD license |
| 43 | +**/ |
| 44 | + |
| 45 | +function str_repeat(i, m) { |
| 46 | + for (var o = []; m > 0; o[--m] = i); |
| 47 | + return(o.join("")); |
| 48 | +} |
| 49 | + |
| 50 | +function sprintf() { |
| 51 | + var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = ''; |
| 52 | + while (f) { |
| 53 | + if (m = /^[^\x25]+/.exec(f)) { |
| 54 | + o.push(m[0]); |
| 55 | + } |
| 56 | + else if (m = /^\x25{2}/.exec(f)) { |
| 57 | + o.push("%"); |
| 58 | + } |
| 59 | + else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) { |
| 60 | + if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) { |
| 61 | + throw("Too few arguments."); |
| 62 | + } |
| 63 | + if (/[^s]/.test(m[7]) && (typeof(a) != "number")) { |
| 64 | + throw("Expecting number but found " + typeof(a)); |
| 65 | + } |
| 66 | + switch (m[7]) { |
| 67 | + case 'b': a = a.toString(2); break; |
| 68 | + case 'c': a = String.fromCharCode(a); break; |
| 69 | + case 'd': a = parseInt(a); break; |
| 70 | + case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break; |
| 71 | + case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break; |
| 72 | + case 'o': a = a.toString(8); break; |
| 73 | + case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break; |
| 74 | + case 'u': a = Math.abs(a); break; |
| 75 | + case 'x': a = a.toString(16); break; |
| 76 | + case 'X': a = a.toString(16).toUpperCase(); break; |
| 77 | + } |
| 78 | + if (/[def]/.test(m[7])) { |
| 79 | + s = (a >= 0 ? (m[2] ? '+' : '') : '-'); |
| 80 | + a = Math.abs(a); |
| 81 | + } |
| 82 | + c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' '; |
| 83 | + x = m[5] - String(a).length - s.length; |
| 84 | + p = m[5] ? str_repeat(c, x) : ''; |
| 85 | + o.push(s + (m[4] ? a + p : p + a)); |
| 86 | + } |
| 87 | + else { |
| 88 | + throw("Huh ?!"); |
| 89 | + } |
| 90 | + f = f.substring(m[0].length); |
| 91 | + } |
| 92 | + return o.join(""); |
55 | 93 | }
|
0 commit comments