| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::JavaScript3::Sprintf; | ||
| 4 | |||
| 5 | sub emit_javascript3 { | ||
| 6 | |||
| 7 | return <<'EOT'; | ||
| 8 | /** | ||
| 9 | * Copyright (c) 2010 Jakob Westhoff | ||
| 10 | * | ||
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 12 | * of this software and associated documentation files (the "Software"), to deal | ||
| 13 | * in the Software without restriction, including without limitation the rights | ||
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 15 | * copies of the Software, and to permit persons to whom the Software is | ||
| 16 | * furnished to do so, subject to the following conditions: | ||
| 17 | * | ||
| 18 | * The above copyright notice and this permission notice shall be included in | ||
| 19 | * all copies or substantial portions of the Software. | ||
| 20 | * | ||
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 27 | * THE SOFTWARE. | ||
| 28 | */ | ||
| 29 | |||
| 30 | CORE.sprintf = function(List__) { | ||
| 31 | var format = List__[0]; | ||
| 32 | var list = List__[1]; | ||
| 33 | |||
| 34 | // Check for format definition | ||
| 35 | if ( typeof format != 'string' ) { | ||
| 36 | CORE.die(["sprintf: The first arguments need to be a valid format string."]); | ||
| 37 | } | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Define the regex to match a formating string | ||
| 41 | * The regex consists of the following parts: | ||
| 42 | * percent sign to indicate the start | ||
| 43 | * (optional) sign specifier | ||
| 44 | * (optional) padding specifier | ||
| 45 | * (optional) alignment specifier | ||
| 46 | * (optional) width specifier | ||
| 47 | * (optional) precision specifier | ||
| 48 | * type specifier: | ||
| 49 | * % - literal percent sign | ||
| 50 | * b - binary number | ||
| 51 | * c - ASCII character represented by the given value | ||
| 52 | * d - signed decimal number | ||
| 53 | * f - floating point value | ||
| 54 | * o - octal number | ||
| 55 | * s - string | ||
| 56 | * x - hexadecimal number (lowercase characters) | ||
| 57 | * X - hexadecimal number (uppercase characters) | ||
| 58 | */ | ||
| 59 | var r = new RegExp( /%(\+)?([0 ]|'(.))?(-)?([0-9]+)?(\.([0-9]+))?([%bcdfosxX])/g ); | ||
| 60 | |||
| 61 | /** | ||
| 62 | * Each format string is splitted into the following parts: | ||
| 63 | * 0: Full format string | ||
| 64 | * 1: sign specifier (+) | ||
| 65 | * 2: padding specifier (0/<space>/'<any char>) | ||
| 66 | * 3: if the padding character starts with a ' this will be the real | ||
| 67 | * padding character | ||
| 68 | * 4: alignment specifier | ||
| 69 | * 5: width specifier | ||
| 70 | * 6: precision specifier including the dot | ||
| 71 | * 7: precision specifier without the dot | ||
| 72 | * 8: type specifier | ||
| 73 | */ | ||
| 74 | var parts = []; | ||
| 75 | var paramIndex = 0; | ||
| 76 | while ( part = r.exec( format ) ) { | ||
| 77 | // Check if an input value has been provided, for the current | ||
| 78 | // format string | ||
| 79 | if ( paramIndex >= list.length ) { | ||
| 80 | CORE.die(["sprintf: At least one argument was missing."]); | ||
| 81 | } | ||
| 82 | |||
| 83 | parts[parts.length] = { | ||
| 84 | /* beginning of the part in the string */ | ||
| 85 | begin: part.index, | ||
| 86 | /* end of the part in the string */ | ||
| 87 | end: part.index + part[0].length, | ||
| 88 | /* force sign */ | ||
| 89 | sign: ( part[1] == '+' ), | ||
| 90 | /* is the given data negative */ | ||
| 91 | negative: ( parseInt( list[paramIndex] ) < 0 ) ? true : false, | ||
| 92 | /* padding character (default: <space>) */ | ||
| 93 | padding: ( part[2] == undefined ) | ||
| 94 | ? ( ' ' ) /* default */ | ||
| 95 | : ( ( part[2].substring( 0, 1 ) == "'" ) | ||
| 96 | ? ( part[3] ) /* use special char */ | ||
| 97 | : ( part[2] ) /* use normal <space> or zero */ | ||
| 98 | ), | ||
| 99 | /* should the output be aligned left?*/ | ||
| 100 | alignLeft: ( part[4] == '-' ), | ||
| 101 | /* width specifier (number or false) */ | ||
| 102 | width: ( part[5] != undefined ) ? part[5] : false, | ||
| 103 | /* precision specifier (number or false) */ | ||
| 104 | precision: ( part[7] != undefined ) ? part[7] : false, | ||
| 105 | /* type specifier */ | ||
| 106 | type: part[8], | ||
| 107 | /* the given data associated with this part converted to a string */ | ||
| 108 | data: ( part[8] != '%' ) ? String ( list[paramIndex++] ) : false | ||
| 109 | }; | ||
| 110 | } | ||
| 111 | |||
| 112 | var newString = ""; | ||
| 113 | var start = 0; | ||
| 114 | // Generate our new formated string | ||
| 115 | for( var i=0; i<parts.length; ++i ) { | ||
| 116 | // Add first unformated string part | ||
| 117 | newString += format.substring( start, parts[i].begin ); | ||
| 118 | |||
| 119 | // Mark the new string start | ||
| 120 | start = parts[i].end; | ||
| 121 | |||
| 122 | // Create the appropriate preformat substitution | ||
| 123 | // This substitution is only the correct type conversion. All the | ||
| 124 | // different options and flags haven't been applied to it at this | ||
| 125 | // point | ||
| 126 | var preSubstitution = ""; | ||
| 127 | switch ( parts[i].type ) { | ||
| 128 | case '%': | ||
| 129 | preSubstitution = "%"; | ||
| 130 | break; | ||
| 131 | case 'b': | ||
| 132 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 2 ); | ||
| 133 | break; | ||
| 134 | case 'c': | ||
| 135 | preSubstitution = String.fromCharCode( Math.abs( parseInt( parts[i].data ) ) ); | ||
| 136 | break; | ||
| 137 | case 'd': | ||
| 138 | preSubstitution = String( Math.abs( parseInt( parts[i].data ) ) ); | ||
| 139 | break; | ||
| 140 | case 'f': | ||
| 141 | preSubstitution = ( parts[i].precision == false ) | ||
| 142 | ? ( String( ( Math.abs( parseFloat( parts[i].data ) ) ) ) ) | ||
| 143 | : ( Math.abs( parseFloat( parts[i].data ) ).toFixed( parts[i].precision ) ); | ||
| 144 | break; | ||
| 145 | case 'o': | ||
| 146 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 8 ); | ||
| 147 | break; | ||
| 148 | case 's': | ||
| 149 | preSubstitution = parts[i].data.substring( 0, parts[i].precision ? parts[i].precision : parts[i].data.length ); /* Cut if precision is defined */ | ||
| 150 | break; | ||
| 151 | case 'x': | ||
| 152 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 16 ).toLowerCase(); | ||
| 153 | break; | ||
| 154 | case 'X': | ||
| 155 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 16 ).toUpperCase(); | ||
| 156 | break; | ||
| 157 | default: | ||
| 158 | throw 'sprintf: Unknown type "' + parts[i].type + '" detected. This should never happen. Maybe the regex is wrong.'; | ||
| 159 | } | ||
| 160 | |||
| 161 | // The % character is a special type and does not need further processing | ||
| 162 | if ( parts[i].type == "%" ) { | ||
| 163 | newString += preSubstitution; | ||
| 164 | continue; | ||
| 165 | } | ||
| 166 | |||
| 167 | // Modify the preSubstitution by taking sign, padding and width | ||
| 168 | // into account | ||
| 169 | |||
| 170 | // Pad the string based on the given width | ||
| 171 | if ( parts[i].width != false ) { | ||
| 172 | // Padding needed? | ||
| 173 | if ( parts[i].width > preSubstitution.length ) | ||
| 174 | { | ||
| 175 | var origLength = preSubstitution.length; | ||
| 176 | for( var j = 0; j < parts[i].width - origLength; ++j ) | ||
| 177 | { | ||
| 178 | preSubstitution = ( parts[i].alignLeft == true ) | ||
| 179 | ? ( preSubstitution + parts[i].padding ) | ||
| 180 | : ( parts[i].padding + preSubstitution ); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | // Add a sign symbol if neccessary or enforced, but only if we are | ||
| 186 | // not handling a string | ||
| 187 | if ( parts[i].type == 'b' | ||
| 188 | || parts[i].type == 'd' | ||
| 189 | || parts[i].type == 'o' | ||
| 190 | || parts[i].type == 'f' | ||
| 191 | || parts[i].type == 'x' | ||
| 192 | || parts[i].type == 'X' ) { | ||
| 193 | if ( parts[i].negative == true ) { | ||
| 194 | preSubstitution = "-" + preSubstitution; | ||
| 195 | } | ||
| 196 | else if ( parts[i].sign == true ) { | ||
| 197 | preSubstitution = "+" + preSubstitution; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | // Add the substitution to the new string | ||
| 202 | newString += preSubstitution; | ||
| 203 | } | ||
| 204 | |||
| 205 | // Add the last part of the given format string, which may still be there | ||
| 206 | newString += format.substring( start, format.length ); | ||
| 207 | |||
| 208 | return newString; | ||
| 209 | }; | ||
| 210 | |||
| 211 | CORE.printf = function(List__) { | ||
| 212 | return CORE.print([ CORE.sprintf(List__) ]); | ||
| 213 | }; | ||
| 214 | |||
| 215 | EOT | ||
| 216 | } # end of emit_javascript3() | ||
| 217 | |||
| 218 | 1; | ||
| 219 |