From eb32a329f893e0a2edcf355aece0f95b03db2bad Mon Sep 17 00:00:00 2001 From: robocoder Date: Fri, 20 Aug 2010 14:57:31 +0000 Subject: update sprintf.js to v0.6 (now BSD licensed) git-svn-id: http://dev.piwik.org/svn/trunk@2966 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- LEGALNOTICE | 4 ++ libs/javascript/sprintf.js | 143 +++++++++++++++++++++++++++++---------------- 2 files changed, 96 insertions(+), 51 deletions(-) diff --git a/LEGALNOTICE b/LEGALNOTICE index dbf2a28196..a9544a96cd 100644 --- a/LEGALNOTICE +++ b/LEGALNOTICE @@ -139,6 +139,10 @@ THIRD-PARTY COMPONENTS AND LIBRARIES Link: https//sourceforge.net/projects/sparkline/ License: Dual-licensed: New BSD or GPL v2 + Name: sprintf + Link: http://www.diveintojavascript.com/projects/javascript-sprintf + License: New BSD + Name: swfObject Link: http://code.google.com/p/swfobject/ License: MIT diff --git a/libs/javascript/sprintf.js b/libs/javascript/sprintf.js index 96f8182415..8e0739ae4b 100644 --- a/libs/javascript/sprintf.js +++ b/libs/javascript/sprintf.js @@ -1,55 +1,96 @@ /** - * sprintf() for JavaScript v.0.4 - * - * Copyright (c) 2007 Alexandru Marasteanu - * Thanks to David Baird (unit test and patch). - * - * This program is free software; you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation; either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 Temple - * Place, Suite 330, Boston, MA 02111-1307 USA - */ +sprintf() for JavaScript 0.6 -function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); } +Copyright (c) Alexandru Marasteanu +All rights reserved. -function sprintf (fmt, s) { - var i = 0, a, f = arguments[i++], o = [], m, p, c, x; - while (f) { - if (m = /^[^\x25]+/.exec(f)) o.push(m[0]); - else if (m = /^\x25{2}/.exec(f)) o.push('%'); - else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) { - if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments."); - if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) - throw("Expecting number but found " + typeof(a)); - switch (m[7]) { - case 'b': a = a.toString(2); break; - case 'c': a = String.fromCharCode(a); break; - case 'd': a = parseInt(a); break; - case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break; - case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break; - case 'o': a = a.toString(8); break; - case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break; - case 'u': a = Math.abs(a); break; - case 'x': a = a.toString(16); break; - case 'X': a = a.toString(16).toUpperCase(); break; - } - a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a); - c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' '; - x = m[5] - String(a).length; - p = m[5] ? str_repeat(c, x) : ''; - o.push(m[4] ? a + p : p + a); - } - else throw ("Huh ?!"); - f = f.substring(m[0].length); - } - return o.join(''); +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of sprintf() for JavaScript nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +Changelog: +2007.04.03 - 0.1: + - initial release +2007.09.11 - 0.2: + - feature: added argument swapping +2007.09.17 - 0.3: + - bug fix: no longer throws exception on empty paramenters (Hans Pufal) +2007.10.21 - 0.4: + - unit test and patch (David Baird) +2010.05.09 - 0.5: + - bug fix: 0 is now preceeded with a + sign + - bug fix: the sign was not at the right position on padded results (Kamal Abdali) + - switched from GPL to BSD license +2010.05.22 - 0.6: + - reverted to 0.4 and fixed the bug regarding the sign of the number 0 + Note: + Thanks to Raphael Pigulla (http://www.n3rd.org/) + who warned me about a bug in 0.5, I discovered that the last update was + a regress. I appologize for that. +**/ + +function str_repeat(i, m) { + for (var o = []; m > 0; o[--m] = i); + return o.join(''); +} + +function sprintf() { + var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = ''; + while (f) { + if (m = /^[^\x25]+/.exec(f)) { + o.push(m[0]); + } + else if (m = /^\x25{2}/.exec(f)) { + o.push('%'); + } + else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) { + if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) { + throw('Too few arguments.'); + } + if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) { + throw('Expecting number but found ' + typeof(a)); + } + switch (m[7]) { + case 'b': a = a.toString(2); break; + case 'c': a = String.fromCharCode(a); break; + case 'd': a = parseInt(a); break; + case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break; + case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break; + case 'o': a = a.toString(8); break; + case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break; + case 'u': a = Math.abs(a); break; + case 'x': a = a.toString(16); break; + case 'X': a = a.toString(16).toUpperCase(); break; + } + a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a); + c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' '; + x = m[5] - String(a).length - s.length; + p = m[5] ? str_repeat(c, x) : ''; + o.push(s + (m[4] ? a + p : p + a)); + } + else { + throw('Huh ?!'); + } + f = f.substring(m[0].length); + } + return o.join(''); } -- cgit v1.2.3