source: issm/trunk-jpl/externalpackages/javascript/src/sprintf.js@ 19790

Last change on this file since 19790 was 19790, checked in by Eric.Larour, 9 years ago

CHG: needed for javascript issm build.

File size: 6.1 KB
Line 
1function sprintf() {
2 // discuss at: http://phpjs.org/functions/sprintf/
3 // original by: Ash Searle (http://hexmen.com/blog/)
4 // improved by: Michael White (http://getsprink.com)
5 // improved by: Jack
6 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
7 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
8 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
9 // improved by: Dj
10 // improved by: Allidylls
11 // input by: Paulo Freitas
12 // input by: Brett Zamir (http://brett-zamir.me)
13 // example 1: sprintf("%01.2f", 123.1);
14 // returns 1: 123.10
15 // example 2: sprintf("[%10s]", 'monkey');
16 // returns 2: '[ monkey]'
17 // example 3: sprintf("[%'#10s]", 'monkey');
18 // returns 3: '[####monkey]'
19 // example 4: sprintf("%d", 123456789012345);
20 // returns 4: '123456789012345'
21 // example 5: sprintf('%-03s', 'E');
22 // returns 5: 'E00'
23
24 var regex = /%%|%(\d+\$)?([\-+\'#0 ]*)(\*\d+\$|\*|\d+)?(?:\.(\*\d+\$|\*|\d+))?([scboxXuideEfFgG])/g;
25 var a = arguments;
26 var i = 0;
27 var format = a[i++];
28
29 // pad()
30 var pad = function(str, len, chr, leftJustify) {
31 if (!chr) {
32 chr = ' ';
33 }
34 var padding = (str.length >= len) ? '' : new Array(1 + len - str.length >>> 0)
35 .join(chr);
36 return leftJustify ? str + padding : padding + str;
37 };
38
39 // justify()
40 var justify = function(value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
41 var diff = minWidth - value.length;
42 if (diff > 0) {
43 if (leftJustify || !zeroPad) {
44 value = pad(value, minWidth, customPadChar, leftJustify);
45 } else {
46 value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
47 }
48 }
49 return value;
50 };
51
52 // formatBaseX()
53 var formatBaseX = function(value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
54 // Note: casts negative numbers to positive ones
55 var number = value >>> 0;
56 prefix = (prefix && number && {
57 '2' : '0b',
58 '8' : '0',
59 '16' : '0x'
60 }[base]) || '';
61 value = prefix + pad(number.toString(base), precision || 0, '0', false);
62 return justify(value, prefix, leftJustify, minWidth, zeroPad);
63 };
64
65 // formatString()
66 var formatString = function(value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
67 if (precision !== null && precision !== undefined) {
68 value = value.slice(0, precision);
69 }
70 return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
71 };
72
73 // doFormat()
74 var doFormat = function(substring, valueIndex, flags, minWidth, precision, type) {
75 var number, prefix, method, textTransform, value;
76
77 if (substring === '%%') {
78 return '%';
79 }
80
81 // parse flags
82 var leftJustify = false;
83 var positivePrefix = '';
84 var zeroPad = false;
85 var prefixBaseX = false;
86 var customPadChar = ' ';
87 var flagsl = flags.length;
88 var j;
89 for (j = 0; flags && j < flagsl; j++) {
90 switch (flags.charAt(j)) {
91 case ' ':
92 positivePrefix = ' ';
93 break;
94 case '+':
95 positivePrefix = '+';
96 break;
97 case '-':
98 leftJustify = true;
99 break;
100 case "'":
101 customPadChar = flags.charAt(j + 1);
102 break;
103 case '0':
104 zeroPad = true;
105 customPadChar = '0';
106 break;
107 case '#':
108 prefixBaseX = true;
109 break;
110 }
111 }
112
113 // parameters may be null, undefined, empty-string or real valued
114 // we want to ignore null, undefined and empty-string values
115 if (!minWidth) {
116 minWidth = 0;
117 } else if (minWidth === '*') {
118 minWidth = +a[i++];
119 } else if (minWidth.charAt(0) === '*') {
120 minWidth = +a[minWidth.slice(1, -1)];
121 } else {
122 minWidth = +minWidth;
123 }
124
125 // Note: undocumented perl feature:
126 if (minWidth < 0) {
127 minWidth = -minWidth;
128 leftJustify = true;
129 }
130
131 if (!isFinite(minWidth)) {
132 throw new Error('sprintf: (minimum-)width must be finite');
133 }
134
135 if (!precision) {
136 precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type === 'd') ? 0 : undefined;
137 } else if (precision === '*') {
138 precision = +a[i++];
139 } else if (precision.charAt(0) === '*') {
140 precision = +a[precision.slice(1, -1)];
141 } else {
142 precision = +precision;
143 }
144
145 // grab value using valueIndex if required?
146 value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
147
148 switch (type) {
149 case 's':
150 return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
151 case 'c':
152 return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
153 case 'b':
154 return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
155 case 'o':
156 return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
157 case 'x':
158 return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
159 case 'X':
160 return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad)
161 .toUpperCase();
162 case 'u':
163 return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
164 case 'i':
165 case 'd':
166 number = +value || 0;
167 // Plain Math.round doesn't just truncate
168 number = Math.round(number - number % 1);
169 prefix = number < 0 ? '-' : positivePrefix;
170 value = prefix + pad(String(Math.abs(number)), precision, '0', false);
171 return justify(value, prefix, leftJustify, minWidth, zeroPad);
172 case 'e':
173 case 'E':
174 case 'f': // Should handle locales (as per setlocale)
175 case 'F':
176 case 'g':
177 case 'G':
178 number = +value;
179 prefix = number < 0 ? '-' : positivePrefix;
180 method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
181 textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
182 value = prefix + Math.abs(number)[method](precision);
183 return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
184 default:
185 return substring;
186 }
187 };
188
189 return format.replace(regex, doFormat);
190}
Note: See TracBrowser for help on using the repository browser.