/*
** sprintf.js -- POSIX sprintf(3) style formatting function for JavaScript
** Copyright (c) 2006-2007 Ralf S. Engelschall <rse@engelschall.com>
** Partly based on Public Domain code by Jan Moesen <http://jan.moesen.nu/>
** Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
** $LastChangedDate$
** $LastChangedRevision$
*/
/* make sure the ECMAScript 3.0 Number.toFixed() method is available */
if (typeof Number.prototype.toFixed != "undefined") {
(function(){
/* see http://www.jibbering.com/faq/#FAQ4_6 for details */
function Stretch(Q, L, c) {
var S = Q
if (c.length > 0)
while (S.length < L)
S = c+S;
return S;
}
function StrU(X, M, N) { /* X >= 0.0 */
var T, S;
S = new String(Math.round(X * Number("1e"+N)));
if (S.search && S.search(/\D/) != -1)
return ''+X;
with (new String(Stretch(S, M+N, '0')))
return substring(0, T=(length-N)) + '.' + substring(T);
}
function Sign(X) {
return X < 0 ? '-' : '';
}
function StrS(X, M, N) {
return Sign(X)+StrU(Math.abs(X), M, N);
}
Number.prototype.toFixed = function (n) { return StrS(this, 1, n) };
})();
}
/* the sprintf() function */
sprintf = function () {
/* argument sanity checking */
if (!arguments || arguments.length < 1)
alert("sprintf:ERROR: not enough arguments");
/* initialize processing queue */
var argumentnum = 0;
var done = "", todo = arguments[argumentnum++];
/* parse still to be done format string */
var m;
while (m = /^([^%]*)%(\d+$)?([#0 +'-]+)?(\*|\d+)?(\.\*|\.\d+)?([%diouxXfFcs])(.*)$/.exec(todo)) {
var pProlog = m[1],
pAccess = m[2],
pFlags = m[3],
pMinLength = m[4],
pPrecision = m[5],
pType = m[6],
pEpilog = m[7];
/* determine substitution */
var subst;
if (pType == '%')
/* special case: escaped percent character */
subst = '%';
else {
/* parse padding and justify aspects of flags */
var padWith = ' ';
var justifyRight = true;
if (pFlags) {
if (pFlags.indexOf('0') >= 0)
padWith = '0';
if (pFlags.indexOf('-') >= 0) {
padWith = ' ';
justifyRight = false;
}
}
else
pFlags = "";
/* determine minimum length */
var minLength = -1;
if (pMinLength) {
if (pMinLength == "*") {
var access = argumentnum++;
if (access >= arguments.length)
alert("sprintf:ERROR: not enough arguments");
minLength = arguments[access];
}
else
minLength = parseInt(pMinLength, 10);
}
/* determine precision */
var precision = -1;
if (pPrecision) {
if (pPrecision == ".*") {
var access = argumentnum++;
if (access >= arguments.length)
alert("sprintf:ERROR: not enough arguments");
precision = arguments[access];
}
else
precision = parseInt(pPrecision.substring(1), 10);
}
/* determine how to fetch argument */
var access = argumentnum++;
if (pAccess)
access = parseInt(pAccess.substring(0, pAccess.length - 1), 10);
if (access >= arguments.length)
alert("sprintf:ERROR: not enough arguments");
/* dispatch into expansions according to type */
var prefix = "";
switch (pType) {
case 'd':
case 'i':
subst = arguments[access];
if (typeof subst != "number")
subst = 0;
subst = subst.toString(10);
if (pFlags.indexOf('#') >= 0 && subst >= 0)
subst = "+" + subst;
if (pFlags.indexOf(' ') >= 0 && subst >= 0)
subst = " " + subst;
break;
case 'o':
subst = arguments[access];
if (typeof subst != "number")
subst = 0;
subst = subst.toString(8);
break;
case 'u':
subst = arguments[access];
if (typeof subst != "number")
subst = 0;
subst = Math.abs(subst);
subst = subst.toString(10);
break;
![]() PS打造Sinsuo非主流效果+颓废美化照片 | ![]() 非主流照片制作教程之大眼睛MM | ![]() 非主流的黑白色彩 | ![]() 非主流3HAN风格照片的制作方法 |
![]() 非主流PS教程-简单的瘦脸方法 | ![]() 梦幻非主流照片效果![PS图效] | ![]() 一种非主流照片处理.方法 | ![]() 5步教会你非主流RAY风格的照片制作 |