Skip to content
Extraits de code Groupes Projets
Valider eb32a329 rédigé par robocoder's avatar robocoder
Parcourir les fichiers

update sprintf.js to v0.6 (now BSD licensed)

git-svn-id: http://dev.piwik.org/svn/trunk@2966 59fd770c-687e-43c8-a1e3-f5a4ff64c105
parent 824f59a9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -139,6 +139,10 @@ THIRD-PARTY COMPONENTS AND LIBRARIES ...@@ -139,6 +139,10 @@ THIRD-PARTY COMPONENTS AND LIBRARIES
Link: https//sourceforge.net/projects/sparkline/ Link: https//sourceforge.net/projects/sparkline/
License: Dual-licensed: New BSD or GPL v2 License: Dual-licensed: New BSD or GPL v2
Name: sprintf
Link: http://www.diveintojavascript.com/projects/javascript-sprintf
License: New BSD
Name: swfObject Name: swfObject
Link: http://code.google.com/p/swfobject/ Link: http://code.google.com/p/swfobject/
License: MIT License: MIT
......
/** /**
* sprintf() for JavaScript v.0.4 sprintf() for JavaScript 0.6
*
* Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
* 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
*/
function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); } Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
All rights reserved.
function sprintf (fmt, s) { Redistribution and use in source and binary forms, with or without
var i = 0, a, f = arguments[i++], o = [], m, p, c, x; modification, are permitted provided that the following conditions are met:
while (f) { * Redistributions of source code must retain the above copyright
if (m = /^[^\x25]+/.exec(f)) o.push(m[0]); notice, this list of conditions and the following disclaimer.
else if (m = /^\x25{2}/.exec(f)) o.push('%'); * Redistributions in binary form must reproduce the above copyright
else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) { notice, this list of conditions and the following disclaimer in the
if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments."); documentation and/or other materials provided with the distribution.
if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) * Neither the name of sprintf() for JavaScript nor the
throw("Expecting number but found " + typeof(a)); names of its contributors may be used to endorse or promote products
switch (m[7]) { derived from this software without specific prior written permission.
case 'b': a = a.toString(2); break;
case 'c': a = String.fromCharCode(a); break; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
case 'd': a = parseInt(a); break; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break; DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY
case 'o': a = a.toString(8); break; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
case 'u': a = Math.abs(a); break; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
case 'x': a = a.toString(16); break; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
case 'X': a = a.toString(16).toUpperCase(); break; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
} SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; Changelog:
p = m[5] ? str_repeat(c, x) : ''; 2007.04.03 - 0.1:
o.push(m[4] ? a + p : p + a); - initial release
} 2007.09.11 - 0.2:
else throw ("Huh ?!"); - feature: added argument swapping
f = f.substring(m[0].length); 2007.09.17 - 0.3:
} - bug fix: no longer throws exception on empty paramenters (Hans Pufal)
return o.join(''); 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 <raph (at] n3rd [dot) org> (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('');
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter