3 * Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
4 * and Contributors (http://phpjs.org/authors)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // discuss at: http://phpjs.org/functions/sprintf/
25 // original by: Ash Searle (http://hexmen.com/blog/)
26 // improved by: Michael White (http://getsprink.com)
28 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
29 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
30 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
32 // improved by: Allidylls
33 // input by: Paulo Freitas
34 // input by: Brett Zamir (http://brett-zamir.me)
35 // example 1: sprintf("%01.2f", 123.1);
37 // example 2: sprintf("[%10s]", 'monkey');
38 // returns 2: '[ monkey]'
39 // example 3: sprintf("[%'#10s]", 'monkey');
40 // returns 3: '[####monkey]'
41 // example 4: sprintf("%d", 123456789012345);
42 // returns 4: '123456789012345'
43 // example 5: sprintf('%-03s', 'E');
46 var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuideEfFgG])/g;
52 var pad = function (str, len, chr, leftJustify) {
56 var padding = (str.length >= len) ? '' : new Array(1 + len - str.length >>> 0)
58 return leftJustify ? str + padding : padding + str;
62 var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
63 var diff = minWidth - value.length;
65 if (leftJustify || !zeroPad) {
66 value = pad(value, minWidth, customPadChar, leftJustify);
68 value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
75 var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
76 // Note: casts negative numbers to positive ones
77 var number = value >>> 0;
78 prefix = prefix && number && {
83 value = prefix + pad(number.toString(base), precision || 0, '0', false);
84 return justify(value, prefix, leftJustify, minWidth, zeroPad);
88 var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
89 if (precision != null) {
90 value = value.slice(0, precision);
92 return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
96 var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
97 var number, prefix, method, textTransform, value;
99 if (substring === '%%') {
104 var leftJustify = false;
105 var positivePrefix = '';
107 var prefixBaseX = false;
108 var customPadChar = ' ';
109 var flagsl = flags.length;
110 for (var j = 0; flags && j < flagsl; j++) {
111 switch (flags.charAt(j)) {
113 positivePrefix = ' ';
116 positivePrefix = '+';
122 customPadChar = flags.charAt(j + 1);
134 // parameters may be null, undefined, empty-string or real valued
135 // we want to ignore null, undefined and empty-string values
138 } else if (minWidth === '*') {
140 } else if (minWidth.charAt(0) == '*') {
141 minWidth = +a[minWidth.slice(1, -1)];
143 minWidth = +minWidth;
146 // Note: undocumented perl feature:
148 minWidth = -minWidth;
152 if (!isFinite(minWidth)) {
153 throw new Error('sprintf: (minimum-)width must be finite');
157 precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type === 'd') ? 0 : undefined;
158 } else if (precision === '*') {
160 } else if (precision.charAt(0) == '*') {
161 precision = +a[precision.slice(1, -1)];
163 precision = +precision;
166 // grab value using valueIndex if required?
167 value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
171 return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
173 return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
175 return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
177 return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
179 return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
181 return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad)
184 return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
187 number = +value || 0;
188 // Plain Math.round doesn't just truncate
189 number = Math.round(number - number % 1);
190 prefix = number < 0 ? '-' : positivePrefix;
191 value = prefix + pad(String(Math.abs(number)), precision, '0', false);
192 return justify(value, prefix, leftJustify, minWidth, zeroPad);
195 case 'f': // Should handle locales (as per setlocale)
200 prefix = number < 0 ? '-' : positivePrefix;
201 method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
202 textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
203 value = prefix + Math.abs(number)[method](precision);
204 return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
210 return format.replace(regex, doFormat);