root / trunk / web / dojo / dojox / string / sprintf.js @ 12
History | View | Annotate | Download (6.96 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.string.sprintf"]){ |
9 |
dojo._hasResource["dojox.string.sprintf"]=true; |
10 |
dojo.provide("dojox.string.sprintf");
|
11 |
dojo.require("dojox.string.tokenize");
|
12 |
dojox.string.sprintf=function(_1,_2){ |
13 |
for(var _3=[],i=1;i<arguments.length;i++){ |
14 |
_3.push(arguments[i]);
|
15 |
} |
16 |
var _4=new dojox.string.sprintf.Formatter(_1); |
17 |
return _4.format.apply(_4,_3);
|
18 |
}; |
19 |
dojox.string.sprintf.Formatter=function(_5){ |
20 |
var _6=[];
|
21 |
this._mapped=false; |
22 |
this._format=_5;
|
23 |
this._tokens=dojox.string.tokenize(_5,this._re,this._parseDelim,this); |
24 |
}; |
25 |
dojo.extend(dojox.string.sprintf.Formatter,{_re:/\%(?:\(([\w_]+)\)|([1-9]\d*)\$)?([0 +\-\#]*)(\*|\d+)?(\.)?(\*|\d+)?[hlL]?([\%scdeEfFgGiouxX])/g,_parseDelim:function(_7,_8,_9,_a,_b,_c,_d){ |
26 |
if(_7){
|
27 |
this._mapped=true; |
28 |
} |
29 |
return {mapping:_7,intmapping:_8,flags:_9,_minWidth:_a,period:_b,_precision:_c,specifier:_d}; |
30 |
},_specifiers:{b:{base:2,isInt:true},o:{base:8,isInt:true},x:{base:16,isInt:true},X:{extend:["x"],toUpper:true},d:{base:10,isInt:true},i:{extend:["d"]},u:{extend:["d"],isUnsigned:true},c:{setArg:function(_e){ |
31 |
if(!isNaN(_e.arg)){
|
32 |
var _f=parseInt(_e.arg);
|
33 |
if(_f<0||_f>127){ |
34 |
throw new Error("invalid character code passed to %c in sprintf"); |
35 |
} |
36 |
_e.arg=isNaN(_f)?""+_f:String.fromCharCode(_f);
|
37 |
} |
38 |
}},s:{setMaxWidth:function(_10){ |
39 |
_10.maxWidth=(_10.period==".")?_10.precision:-1; |
40 |
}},e:{isDouble:true,doubleNotation:"e"},E:{extend:["e"],toUpper:true},f:{isDouble:true,doubleNotation:"f"},F:{extend:["f"]},g:{isDouble:true,doubleNotation:"g"},G:{extend:["g"],toUpper:true}},format:function(_11){ |
41 |
if(this._mapped&&typeof _11!="object"){ |
42 |
throw new Error("format requires a mapping"); |
43 |
} |
44 |
var str=""; |
45 |
var _12=0; |
46 |
for(var i=0,_13;i<this._tokens.length;i++){ |
47 |
_13=this._tokens[i];
|
48 |
if(typeof _13=="string"){ |
49 |
str+=_13; |
50 |
}else{
|
51 |
if(this._mapped){ |
52 |
if(typeof _11[_13.mapping]=="undefined"){ |
53 |
throw new Error("missing key "+_13.mapping); |
54 |
} |
55 |
_13.arg=_11[_13.mapping]; |
56 |
}else{
|
57 |
if(_13.intmapping){
|
58 |
var _12=parseInt(_13.intmapping)-1; |
59 |
} |
60 |
if(_12>=arguments.length){ |
61 |
throw new Error("got "+arguments.length+" printf arguments, insufficient for '"+this._format+"'"); |
62 |
} |
63 |
_13.arg=arguments[_12++];
|
64 |
} |
65 |
if(!_13.compiled){
|
66 |
_13.compiled=true;
|
67 |
_13.sign="";
|
68 |
_13.zeroPad=false;
|
69 |
_13.rightJustify=false;
|
70 |
_13.alternative=false;
|
71 |
var _14={};
|
72 |
for(var fi=_13.flags.length;fi--;){ |
73 |
var _15=_13.flags.charAt(fi);
|
74 |
_14[_15]=true;
|
75 |
switch(_15){
|
76 |
case " ": |
77 |
_13.sign=" ";
|
78 |
break;
|
79 |
case "+": |
80 |
_13.sign="+";
|
81 |
break;
|
82 |
case "0": |
83 |
_13.zeroPad=(_14["-"])?false:true; |
84 |
break;
|
85 |
case "-": |
86 |
_13.rightJustify=true;
|
87 |
_13.zeroPad=false;
|
88 |
break;
|
89 |
case "#": |
90 |
_13.alternative=true;
|
91 |
break;
|
92 |
default:
|
93 |
throw Error("bad formatting flag '"+_13.flags.charAt(fi)+"'"); |
94 |
} |
95 |
} |
96 |
_13.minWidth=(_13._minWidth)?parseInt(_13._minWidth):0;
|
97 |
_13.maxWidth=-1;
|
98 |
_13.toUpper=false;
|
99 |
_13.isUnsigned=false;
|
100 |
_13.isInt=false;
|
101 |
_13.isDouble=false;
|
102 |
_13.precision=1;
|
103 |
if(_13.period=="."){ |
104 |
if(_13._precision){
|
105 |
_13.precision=parseInt(_13._precision); |
106 |
}else{
|
107 |
_13.precision=0;
|
108 |
} |
109 |
} |
110 |
var _16=this._specifiers[_13.specifier]; |
111 |
if(typeof _16=="undefined"){ |
112 |
throw new Error("unexpected specifier '"+_13.specifier+"'"); |
113 |
} |
114 |
if(_16.extend){
|
115 |
dojo.mixin(_16,this._specifiers[_16.extend]);
|
116 |
delete _16.extend;
|
117 |
} |
118 |
dojo.mixin(_13,_16); |
119 |
} |
120 |
if(typeof _13.setArg=="function"){ |
121 |
_13.setArg(_13); |
122 |
} |
123 |
if(typeof _13.setMaxWidth=="function"){ |
124 |
_13.setMaxWidth(_13); |
125 |
} |
126 |
if(_13._minWidth=="*"){ |
127 |
if(this._mapped){ |
128 |
throw new Error("* width not supported in mapped formats"); |
129 |
} |
130 |
_13.minWidth=parseInt(arguments[_12++]);
|
131 |
if(isNaN(_13.minWidth)){
|
132 |
throw new Error("the argument for * width at position "+_12+" is not a number in "+this._format); |
133 |
} |
134 |
if(_13.minWidth<0){ |
135 |
_13.rightJustify=true;
|
136 |
_13.minWidth=-_13.minWidth; |
137 |
} |
138 |
} |
139 |
if(_13._precision=="*"&&_13.period=="."){ |
140 |
if(this._mapped){ |
141 |
throw new Error("* precision not supported in mapped formats"); |
142 |
} |
143 |
_13.precision=parseInt(arguments[_12++]);
|
144 |
if(isNaN(_13.precision)){
|
145 |
throw Error("the argument for * precision at position "+_12+" is not a number in "+this._format); |
146 |
} |
147 |
if(_13.precision<0){ |
148 |
_13.precision=1;
|
149 |
_13.period="";
|
150 |
} |
151 |
} |
152 |
if(_13.isInt){
|
153 |
if(_13.period=="."){ |
154 |
_13.zeroPad=false;
|
155 |
} |
156 |
this.formatInt(_13);
|
157 |
}else{
|
158 |
if(_13.isDouble){
|
159 |
if(_13.period!="."){ |
160 |
_13.precision=6;
|
161 |
} |
162 |
this.formatDouble(_13);
|
163 |
} |
164 |
} |
165 |
this.fitField(_13);
|
166 |
str+=""+_13.arg;
|
167 |
} |
168 |
} |
169 |
return str;
|
170 |
},_zeros10:"0000000000",_spaces10:" ",formatInt:function(_17){ |
171 |
var i=parseInt(_17.arg);
|
172 |
if(!isFinite(i)){
|
173 |
if(typeof _17.arg!="number"){ |
174 |
throw new Error("format argument '"+_17.arg+"' not an integer; parseInt returned "+i); |
175 |
} |
176 |
i=0;
|
177 |
} |
178 |
if(i<0&&(_17.isUnsigned||_17.base!=10)){ |
179 |
i=4294967295+i+1; |
180 |
} |
181 |
if(i<0){ |
182 |
_17.arg=(-i).toString(_17.base); |
183 |
this.zeroPad(_17);
|
184 |
_17.arg="-"+_17.arg;
|
185 |
}else{
|
186 |
_17.arg=i.toString(_17.base); |
187 |
if(!i&&!_17.precision){
|
188 |
_17.arg="";
|
189 |
}else{
|
190 |
this.zeroPad(_17);
|
191 |
} |
192 |
if(_17.sign){
|
193 |
_17.arg=_17.sign+_17.arg; |
194 |
} |
195 |
} |
196 |
if(_17.base==16){ |
197 |
if(_17.alternative){
|
198 |
_17.arg="0x"+_17.arg;
|
199 |
} |
200 |
_17.arg=_17.toUpper?_17.arg.toUpperCase():_17.arg.toLowerCase(); |
201 |
} |
202 |
if(_17.base==8){ |
203 |
if(_17.alternative&&_17.arg.charAt(0)!="0"){ |
204 |
_17.arg="0"+_17.arg;
|
205 |
} |
206 |
} |
207 |
},formatDouble:function(_18){ |
208 |
var f=parseFloat(_18.arg);
|
209 |
if(!isFinite(f)){
|
210 |
if(typeof _18.arg!="number"){ |
211 |
throw new Error("format argument '"+_18.arg+"' not a float; parseFloat returned "+f); |
212 |
} |
213 |
f=0;
|
214 |
} |
215 |
switch(_18.doubleNotation){
|
216 |
case "e": |
217 |
_18.arg=f.toExponential(_18.precision); |
218 |
break;
|
219 |
case "f": |
220 |
_18.arg=f.toFixed(_18.precision); |
221 |
break;
|
222 |
case "g": |
223 |
if(Math.abs(f)<0.0001){ |
224 |
_18.arg=f.toExponential(_18.precision>0?_18.precision-1:_18.precision); |
225 |
}else{
|
226 |
_18.arg=f.toPrecision(_18.precision); |
227 |
} |
228 |
if(!_18.alternative){
|
229 |
_18.arg=_18.arg.replace(/(\..*[^0])0*/,"$1"); |
230 |
_18.arg=_18.arg.replace(/\.0*e/,"e").replace(/\.0$/,""); |
231 |
} |
232 |
break;
|
233 |
default:
|
234 |
throw new Error("unexpected double notation '"+_18.doubleNotation+"'"); |
235 |
} |
236 |
_18.arg=_18.arg.replace(/e\+(\d)$/,"e+0$1").replace(/e\-(\d)$/,"e-0$1"); |
237 |
if(dojo.isOpera){
|
238 |
_18.arg=_18.arg.replace(/^\./,"0."); |
239 |
} |
240 |
if(_18.alternative){
|
241 |
_18.arg=_18.arg.replace(/^(\d+)$/,"$1."); |
242 |
_18.arg=_18.arg.replace(/^(\d+)e/,"$1.e"); |
243 |
} |
244 |
if(f>=0&&_18.sign){ |
245 |
_18.arg=_18.sign+_18.arg; |
246 |
} |
247 |
_18.arg=_18.toUpper?_18.arg.toUpperCase():_18.arg.toLowerCase(); |
248 |
},zeroPad:function(_19,_1a){ |
249 |
_1a=(arguments.length==2)?_1a:_19.precision; |
250 |
if(typeof _19.arg!="string"){ |
251 |
_19.arg=""+_19.arg;
|
252 |
} |
253 |
var _1b=_1a-10; |
254 |
while(_19.arg.length<_1b){
|
255 |
_19.arg=(_19.rightJustify)?_19.arg+this._zeros10:this._zeros10+_19.arg; |
256 |
} |
257 |
var pad=_1a-_19.arg.length;
|
258 |
_19.arg=(_19.rightJustify)?_19.arg+this._zeros10.substring(0,pad):this._zeros10.substring(0,pad)+_19.arg; |
259 |
},fitField:function(_1c){ |
260 |
if(_1c.maxWidth>=0&&_1c.arg.length>_1c.maxWidth){ |
261 |
return _1c.arg.substring(0,_1c.maxWidth); |
262 |
} |
263 |
if(_1c.zeroPad){
|
264 |
this.zeroPad(_1c,_1c.minWidth);
|
265 |
return;
|
266 |
} |
267 |
this.spacePad(_1c);
|
268 |
},spacePad:function(_1d,_1e){ |
269 |
_1e=(arguments.length==2)?_1e:_1d.minWidth; |
270 |
if(typeof _1d.arg!="string"){ |
271 |
_1d.arg=""+_1d.arg;
|
272 |
} |
273 |
var _1f=_1e-10; |
274 |
while(_1d.arg.length<_1f){
|
275 |
_1d.arg=(_1d.rightJustify)?_1d.arg+this._spaces10:this._spaces10+_1d.arg; |
276 |
} |
277 |
var pad=_1e-_1d.arg.length;
|
278 |
_1d.arg=(_1d.rightJustify)?_1d.arg+this._spaces10.substring(0,pad):this._spaces10.substring(0,pad)+_1d.arg; |
279 |
}}); |
280 |
} |