sprintf.js
5.57 KB
//>>built
define(["dojo/_base/kernel","dojo/_base/lang","dojo/_base/sniff","./tokenize"],function(n,h,l,m){var f=h.getObject("string",!0,dojox);f.sprintf=function(a,c){for(var d=[],e=1;e<arguments.length;e++)d.push(arguments[e]);e=new f.sprintf.Formatter(a);return e.format.apply(e,d)};f.sprintf.Formatter=function(a){this._mapped=!1;this._format=a;this._tokens=m(a,this._re,this._parseDelim,this)};h.extend(f.sprintf.Formatter,{_re:/\%(?:\(([\w_]+)\)|([1-9]\d*)\$)?([0 +\-\#]*)(\*|\d+)?(\.)?(\*|\d+)?[hlL]?([\%scdeEfFgGiouxX])/g,
_parseDelim:function(a,c,d,e,b,g,f){a&&(this._mapped=!0);return{mapping:a,intmapping:c,flags:d,_minWidth:e,period:b,_precision:g,specifier:f}},_specifiers:{b:{base:2,isInt:!0},o:{base:8,isInt:!0},x:{base:16,isInt:!0},X:{extend:["x"],toUpper:!0},d:{base:10,isInt:!0},i:{extend:["d"]},u:{extend:["d"],isUnsigned:!0},c:{setArg:function(a){if(!isNaN(a.arg)){var c=parseInt(a.arg);if(0>c||127<c)throw Error("invalid character code passed to %c in sprintf");a.arg=isNaN(c)?""+c:String.fromCharCode(c)}}},s:{setMaxWidth:function(a){a.maxWidth=
"."==a.period?a.precision:-1}},e:{isDouble:!0,doubleNotation:"e"},E:{extend:["e"],toUpper:!0},f:{isDouble:!0,doubleNotation:"f"},F:{extend:["f"]},g:{isDouble:!0,doubleNotation:"g"},G:{extend:["g"],toUpper:!0}},format:function(a){if(this._mapped&&"object"!=typeof a)throw Error("format requires a mapping");for(var c="",d=0,e=0,b;e<this._tokens.length;e++)if(b=this._tokens[e],"string"==typeof b)c+=b;else{if(this._mapped){if("undefined"==typeof a[b.mapping])throw Error("missing key "+b.mapping);b.arg=
a[b.mapping]}else{b.intmapping&&(d=parseInt(b.intmapping)-1);if(d>=arguments.length)throw Error("got "+arguments.length+" printf arguments, insufficient for '"+this._format+"'");b.arg=arguments[d++]}if(!b.compiled){b.compiled=!0;b.sign="";b.zeroPad=!1;b.rightJustify=!1;b.alternative=!1;for(var g={},f=b.flags.length;f--;){var k=b.flags.charAt(f);g[k]=!0;switch(k){case " ":b.sign=" ";break;case "+":b.sign="+";break;case "0":b.zeroPad=g["-"]?!1:!0;break;case "-":b.rightJustify=!0;b.zeroPad=!1;break;
case "#":b.alternative=!0;break;default:throw Error("bad formatting flag '"+b.flags.charAt(f)+"'");}}b.minWidth=b._minWidth?parseInt(b._minWidth):0;b.maxWidth=-1;b.toUpper=!1;b.isUnsigned=!1;b.isInt=!1;b.isDouble=!1;b.precision=1;"."==b.period&&(b.precision=b._precision?parseInt(b._precision):0);g=this._specifiers[b.specifier];if("undefined"==typeof g)throw Error("unexpected specifier '"+b.specifier+"'");g.extend&&(h.mixin(g,this._specifiers[g.extend]),delete g.extend);h.mixin(b,g)}"function"==typeof b.setArg&&
b.setArg(b);"function"==typeof b.setMaxWidth&&b.setMaxWidth(b);if("*"==b._minWidth){if(this._mapped)throw Error("* width not supported in mapped formats");b.minWidth=parseInt(arguments[d++]);if(isNaN(b.minWidth))throw Error("the argument for * width at position "+d+" is not a number in "+this._format);0>b.minWidth&&(b.rightJustify=!0,b.minWidth=-b.minWidth)}if("*"==b._precision&&"."==b.period){if(this._mapped)throw Error("* precision not supported in mapped formats");b.precision=parseInt(arguments[d++]);
if(isNaN(b.precision))throw Error("the argument for * precision at position "+d+" is not a number in "+this._format);0>b.precision&&(b.precision=1,b.period="")}b.isInt?("."==b.period&&(b.zeroPad=!1),this.formatInt(b)):b.isDouble&&("."!=b.period&&(b.precision=6),this.formatDouble(b));this.fitField(b);c+=""+b.arg}return c},_zeros10:"0000000000",_spaces10:" ",formatInt:function(a){var c=parseInt(a.arg);if(!isFinite(c)){if("number"!=typeof a.arg)throw Error("format argument '"+a.arg+"' not an integer; parseInt returned "+
c);c=0}0>c&&(a.isUnsigned||10!=a.base)&&(c=4294967295+c+1);0>c?(a.arg=(-c).toString(a.base),this.zeroPad(a),a.arg="-"+a.arg):(a.arg=c.toString(a.base),c||a.precision?this.zeroPad(a):a.arg="",a.sign&&(a.arg=a.sign+a.arg));16==a.base&&(a.alternative&&(a.arg="0x"+a.arg),a.arg=a.toUpper?a.arg.toUpperCase():a.arg.toLowerCase());8==a.base&&a.alternative&&"0"!=a.arg.charAt(0)&&(a.arg="0"+a.arg)},formatDouble:function(a){var c=parseFloat(a.arg);if(!isFinite(c)){if("number"!=typeof a.arg)throw Error("format argument '"+
a.arg+"' not a float; parseFloat returned "+c);c=0}switch(a.doubleNotation){case "e":a.arg=c.toExponential(a.precision);break;case "f":a.arg=c.toFixed(a.precision);break;case "g":a.arg=1E-4>Math.abs(c)?c.toExponential(0<a.precision?a.precision-1:a.precision):c.toPrecision(a.precision);a.alternative||(a.arg=a.arg.replace(/(\..*[^0])0*/,"$1"),a.arg=a.arg.replace(/\.0*e/,"e").replace(/\.0$/,""));break;default:throw Error("unexpected double notation '"+a.doubleNotation+"'");}a.arg=a.arg.replace(/e\+(\d)$/,
"e+0$1").replace(/e\-(\d)$/,"e-0$1");l("opera")&&(a.arg=a.arg.replace(/^\./,"0."));a.alternative&&(a.arg=a.arg.replace(/^(\d+)$/,"$1."),a.arg=a.arg.replace(/^(\d+)e/,"$1.e"));0<=c&&a.sign&&(a.arg=a.sign+a.arg);a.arg=a.toUpper?a.arg.toUpperCase():a.arg.toLowerCase()},zeroPad:function(a,c){c=2==arguments.length?c:a.precision;"string"!=typeof a.arg&&(a.arg=""+a.arg);for(var d=c-10;a.arg.length<d;)a.arg=a.rightJustify?a.arg+this._zeros10:this._zeros10+a.arg;d=c-a.arg.length;a.arg=a.rightJustify?a.arg+
this._zeros10.substring(0,d):this._zeros10.substring(0,d)+a.arg},fitField:function(a){if(0<=a.maxWidth&&a.arg.length>a.maxWidth)return a.arg.substring(0,a.maxWidth);a.zeroPad?this.zeroPad(a,a.minWidth):this.spacePad(a)},spacePad:function(a,c){c=2==arguments.length?c:a.minWidth;"string"!=typeof a.arg&&(a.arg=""+a.arg);for(var d=c-10;a.arg.length<d;)a.arg=a.rightJustify?a.arg+this._spaces10:this._spaces10+a.arg;d=c-a.arg.length;a.arg=a.rightJustify?a.arg+this._spaces10.substring(0,d):this._spaces10.substring(0,
d)+a.arg}});return f.sprintf});