| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::JavaScript2::Sprintf; | ||
| 4 | |||
| 5 | sub emit_javascript2 { | ||
| 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__.shift(); | ||
| 32 | var list = List__; | ||
| 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 | * (optional) vector flag | ||
| 49 | * type specifier: | ||
| 50 | * % - literal percent sign | ||
| 51 | * b - binary number | ||
| 52 | * c - ASCII character represented by the given value | ||
| 53 | * d - signed decimal number | ||
| 54 | * u - an unsigned integer, in decimal | ||
| 55 | * f - floating point value | ||
| 56 | * o - octal number | ||
| 57 | * s - string | ||
| 58 | * x - hexadecimal number (lowercase characters) | ||
| 59 | * X - hexadecimal number (uppercase characters) | ||
| 60 | */ | ||
| 61 | var r = new RegExp( /%(\+)?([0 ]|'(.))?(-)?([0-9]+)?(\.([0-9]+))?(v)?([%bcdufosxX])/g ); | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Each format string is splitted into the following parts: | ||
| 65 | * 0: Full format string | ||
| 66 | * 1: sign specifier (+) | ||
| 67 | * 2: padding specifier (0/<space>/'<any char>) | ||
| 68 | * 3: if the padding character starts with a ' this will be the real | ||
| 69 | * padding character | ||
| 70 | * 4: alignment specifier | ||
| 71 | * 5: width specifier | ||
| 72 | * 6: precision specifier including the dot | ||
| 73 | * 7: precision specifier without the dot | ||
| 74 | * 8: vector flag | ||
| 75 | * 9: type specifier | ||
| 76 | */ | ||
| 77 | var parts = []; | ||
| 78 | var paramIndex = 0; | ||
| 79 | var part; | ||
| 80 | while ( part = r.exec( format ) ) { | ||
| 81 | // Check if an input value has been provided, for the current | ||
| 82 | // format string | ||
| 83 | if ( paramIndex >= list.length ) { | ||
| 84 | CORE.die(["sprintf: At least one argument was missing."]); | ||
| 85 | } | ||
| 86 | |||
| 87 | parts[parts.length] = { | ||
| 88 | /* beginning of the part in the string */ | ||
| 89 | begin: part.index, | ||
| 90 | /* end of the part in the string */ | ||
| 91 | end: part.index + part[0].length, | ||
| 92 | /* force sign */ | ||
| 93 | sign: ( part[1] == '+' ), | ||
| 94 | /* is the given data negative */ | ||
| 95 | negative: ( parseInt( list[paramIndex] ) < 0 ) ? true : false, | ||
| 96 | /* padding character (default: <space>) */ | ||
| 97 | padding: ( part[2] == undefined ) | ||
| 98 | ? ( ' ' ) /* default */ | ||
| 99 | : ( ( part[2].substring( 0, 1 ) == "'" ) | ||
| 100 | ? ( part[3] ) /* use special char */ | ||
| 101 | : ( part[2] ) /* use normal <space> or zero */ | ||
| 102 | ), | ||
| 103 | /* should the output be aligned left?*/ | ||
| 104 | alignLeft: ( part[4] == '-' ), | ||
| 105 | /* width specifier (number or false) */ | ||
| 106 | width: ( part[5] != undefined ) ? part[5] : false, | ||
| 107 | /* precision specifier (number or false) */ | ||
| 108 | precision: ( part[7] != undefined ) ? part[7] : false, | ||
| 109 | /* vector flag */ | ||
| 110 | vector_flag: ( part[8] != undefined ) ? part[8] : false, | ||
| 111 | /* type specifier */ | ||
| 112 | type: part[9], | ||
| 113 | /* the given data associated with this part converted to a string */ | ||
| 114 | data: ( part[8] != undefined ) // vector_flag | ||
| 115 | ? list[paramIndex++] | ||
| 116 | : ( part[9] != '%' ) | ||
| 117 | ? String ( list[paramIndex++] ) | ||
| 118 | : false | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | var newString = ""; | ||
| 123 | var start = 0; | ||
| 124 | // Generate our new formated string | ||
| 125 | for( var i=0; i<parts.length; ++i ) { | ||
| 126 | // Add first unformated string part | ||
| 127 | newString += format.substring( start, parts[i].begin ); | ||
| 128 | |||
| 129 | // Mark the new string start | ||
| 130 | start = parts[i].end; | ||
| 131 | |||
| 132 | // Create the appropriate preformat substitution | ||
| 133 | // This substitution is only the correct type conversion. All the | ||
| 134 | // different options and flags haven't been applied to it at this | ||
| 135 | // point | ||
| 136 | var preSubstitution = ""; | ||
| 137 | switch ( parts[i].type ) { | ||
| 138 | case '%': | ||
| 139 | preSubstitution = "%"; | ||
| 140 | break; | ||
| 141 | case 'b': | ||
| 142 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 2 ); | ||
| 143 | break; | ||
| 144 | case 'c': | ||
| 145 | preSubstitution = String.fromCharCode( Math.abs( parseInt( parts[i].data ) ) ); | ||
| 146 | break; | ||
| 147 | case 'd': | ||
| 148 | if (parts[i].vector_flag) { | ||
| 149 | preSubstitution = p5cget('Perlito5::Runtime::Sprintf', 'sprintf_vd')([ parts[i].data ], ""); | ||
| 150 | parts[i].type = "s"; | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | preSubstitution = String( Math.abs( parseInt( parts[i].data ) ) ); | ||
| 154 | } | ||
| 155 | break; | ||
| 156 | case 'u': | ||
| 157 | // preSubstitution = String( Math.abs( parseInt( parts[i].data ) ) ); | ||
| 158 | preSubstitution = String( Math.abs( parseFloat( parts[i].data ) ) ); | ||
| 159 | break; | ||
| 160 | case 'f': | ||
| 161 | preSubstitution = ( parts[i].precision == false ) | ||
| 162 | ? ( String( ( Math.abs( parseFloat( parts[i].data ) ) ) ) ) | ||
| 163 | : ( Math.abs( parseFloat( parts[i].data ) ).toFixed( parts[i].precision ) ); | ||
| 164 | break; | ||
| 165 | case 'o': | ||
| 166 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 8 ); | ||
| 167 | break; | ||
| 168 | case 's': | ||
| 169 | preSubstitution = parts[i].data.substring( 0, parts[i].precision ? parts[i].precision : parts[i].data.length ); /* Cut if precision is defined */ | ||
| 170 | break; | ||
| 171 | case 'x': | ||
| 172 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 16 ).toLowerCase(); | ||
| 173 | break; | ||
| 174 | case 'X': | ||
| 175 | preSubstitution = Math.abs( parseInt( parts[i].data ) ).toString( 16 ).toUpperCase(); | ||
| 176 | break; | ||
| 177 | default: | ||
| 178 | throw 'sprintf: Unknown type "' + parts[i].type + '" detected. This should never happen. Maybe the regex is wrong.'; | ||
| 179 | } | ||
| 180 | |||
| 181 | // The % character is a special type and does not need further processing | ||
| 182 | if ( parts[i].type == "%" ) { | ||
| 183 | newString += preSubstitution; | ||
| 184 | continue; | ||
| 185 | } | ||
| 186 | |||
| 187 | // Modify the preSubstitution by taking sign, padding and width | ||
| 188 | // into account | ||
| 189 | |||
| 190 | // Pad the string based on the given width | ||
| 191 | if ( parts[i].width != false ) { | ||
| 192 | // Padding needed? | ||
| 193 | if ( parts[i].width > preSubstitution.length ) | ||
| 194 | { | ||
| 195 | var origLength = preSubstitution.length; | ||
| 196 | for( var j = 0; j < parts[i].width - origLength; ++j ) | ||
| 197 | { | ||
| 198 | preSubstitution = ( parts[i].alignLeft == true ) | ||
| 199 | ? ( preSubstitution + parts[i].padding ) | ||
| 200 | : ( parts[i].padding + preSubstitution ); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | // Add a sign symbol if neccessary or enforced, but only if we are | ||
| 206 | // not handling a string | ||
| 207 | if ( parts[i].type == 'b' | ||
| 208 | || parts[i].type == 'd' | ||
| 209 | || parts[i].type == 'o' | ||
| 210 | || parts[i].type == 'f' | ||
| 211 | || parts[i].type == 'x' | ||
| 212 | || parts[i].type == 'X' ) { | ||
| 213 | if ( parts[i].negative == true ) { | ||
| 214 | preSubstitution = "-" + preSubstitution; | ||
| 215 | } | ||
| 216 | else if ( parts[i].sign == true ) { | ||
| 217 | preSubstitution = "+" + preSubstitution; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | // Add the substitution to the new string | ||
| 222 | newString += preSubstitution; | ||
| 223 | } | ||
| 224 | |||
| 225 | // Add the last part of the given format string, which may still be there | ||
| 226 | newString += format.substring( start, format.length ); | ||
| 227 | |||
| 228 | return newString; | ||
| 229 | }; | ||
| 230 | |||
| 231 | EOT | ||
| 232 | } # end of emit_javascript2() | ||
| 233 | |||
| 234 | 1; | ||
| 235 |