1 2 3function sprintf( ) { 4 // Return a formatted string 5 // 6 // version: 903.3016 7 // discuss at: http://phpjs.org/functions/sprintf 8 // + original by: Ash Searle (http://hexmen.com/blog/) 9 // + namespaced by: Michael White (http://getsprink.com) 10 // + tweaked by: Jack 11 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 12 // + input by: Paulo Ricardo F. Santos 13 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 14 // + input by: Brett Zamir (http://brettz9.blogspot.com) 15 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 16 // * example 1: sprintf("%01.2f", 123.1); 17 // * returns 1: 123.10 18 // * example 2: sprintf("[%10s]", 'monkey'); 19 // * returns 2: '[ monkey]' 20 // * example 3: sprintf("[%'#10s]", 'monkey'); 21 // * returns 3: '[####monkey]' 22 var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g; 23 var a = arguments, i = 0, format = a[i++]; 24 25 // pad() 26 var pad = function(str, len, chr, leftJustify) { 27 if (!chr) chr = ' '; 28 var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr); 29 return leftJustify ? str + padding : padding + str; 30 }; 31 32 // justify() 33 var justify = function(value, prefix, leftJustify, minWidth, zeroPad, customPadChar) { 34 var diff = minWidth - value.length; 35 if (diff > 0) { 36 if (leftJustify || !zeroPad) { 37 value = pad(value, minWidth, customPadChar, leftJustify); 38 } else { 39 value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length); 40 } 41 } 42 return value; 43 }; 44 45 // formatBaseX() 46 var formatBaseX = function(value, base, prefix, leftJustify, minWidth, precision, zeroPad) { 47 // Note: casts negative numbers to positive ones 48 var number = value >>> 0; 49 prefix = prefix && number && {'2': '0b', '8': '0', '16': '0x'}[base] || ''; 50 value = prefix + pad(number.toString(base), precision || 0, '0', false); 51 return justify(value, prefix, leftJustify, minWidth, zeroPad); 52 }; 53 54 // formatString() 55 var formatString = function(value, leftJustify, minWidth, precision, zeroPad, customPadChar) { 56 if (precision != null) { 57 value = value.slice(0, precision); 58 } 59 return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar); 60 }; 61 62 // doFormat() 63 var doFormat = function(substring, valueIndex, flags, minWidth, _, precision, type) { 64 var number; 65 var prefix; 66 var method; 67 var textTransform; 68 var value; 69 70 if (substring == '%%') return '%'; 71 72 // parse flags 73 var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false, customPadChar = ' '; 74 var flagsl = flags.length; 75 for (var j = 0; flags && j < flagsl; j++) switch (flags.charAt(j)) { 76 case ' ': positivePrefix = ' '; break; 77 case '+': positivePrefix = '+'; break; 78 case '-': leftJustify = true; break; 79 case "'": customPadChar = flags.charAt(j+1); break; 80 case '0': zeroPad = true; break; 81 case '#': prefixBaseX = true; break; 82 } 83 84 // parameters may be null, undefined, empty-string or real valued 85 // we want to ignore null, undefined and empty-string values 86 if (!minWidth) { 87 minWidth = 0; 88 } else if (minWidth == '*') { 89 minWidth = +a[i++]; 90 } else if (minWidth.charAt(0) == '*') { 91 minWidth = +a[minWidth.slice(1, -1)]; 92 } else { 93 minWidth = +minWidth; 94 } 95 96 // Note: undocumented perl feature: 97 if (minWidth < 0) { 98 minWidth = -minWidth; 99 leftJustify = true; 100 } 101 102 if (!isFinite(minWidth)) { 103 throw new Error('sprintf: (minimum-)width must be finite'); 104 } 105 106 if (!precision) { 107 precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : void(0); 108 } else if (precision == '*') { 109 precision = +a[i++]; 110 } else if (precision.charAt(0) == '*') { 111 precision = +a[precision.slice(1, -1)]; 112 } else { 113 precision = +precision; 114 } 115 116 // grab value using valueIndex if required? 117 value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++]; 118 119 switch (type) { 120 case 's': return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar); 121 case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad); 122 case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad); 123 case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad); 124 case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad); 125 case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase(); 126 case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad); 127 case 'i': 128 case 'd': { 129 number = parseInt(+value); 130 prefix = number < 0 ? '-' : positivePrefix; 131 value = prefix + pad(String(Math.abs(number)), precision, '0', false); 132 return justify(value, prefix, leftJustify, minWidth, zeroPad); 133 } 134 case 'e': 135 case 'E': 136 case 'f': 137 case 'F': 138 case 'g': 139 case 'G': { 140 number = +value; 141 prefix = number < 0 ? '-' : positivePrefix; 142 method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())]; 143 textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2]; 144 value = prefix + Math.abs(number)[method](precision); 145 return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform](); 146 } 147 default: return substring; 148 } 149 }; 150 151 return format.replace(regex, doFormat); 152}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.