1 40 42 package com.google.gwt.dev.js.rhino; 43 44 49 50 public class ScriptRuntime { 51 52 public static double NaN = 0.0d / 0.0; 53 54 public static String numberToString(double d, int base) { 55 if (d != d) 56 return "NaN"; 57 if (d == Double.POSITIVE_INFINITY) 58 return "Infinity"; 59 if (d == Double.NEGATIVE_INFINITY) 60 return "-Infinity"; 61 if (d == 0.0) 62 return "0"; 63 64 if ((base < 2) || (base > 36)) { 65 throw new Error (Context.getMessage1("msg.bad.radix", 66 Integer.toString(base))); 67 } 68 69 if (base != 10) { 70 return DToA.JS_dtobasestr(base, d); 71 } else { 72 StringBuffer result = new StringBuffer (); 73 DToA.JS_dtostr(result, DToA.DTOSTR_STANDARD, 0, d); 74 return result.toString(); 75 } 76 77 } 78 79 82 static double stringToNumber(String s, int start, int radix) { 83 char digitMax = '9'; 84 char lowerCaseBound = 'a'; 85 char upperCaseBound = 'A'; 86 int len = s.length(); 87 if (radix < 10) { 88 digitMax = (char) ('0' + radix - 1); 89 } 90 if (radix > 10) { 91 lowerCaseBound = (char) ('a' + radix - 10); 92 upperCaseBound = (char) ('A' + radix - 10); 93 } 94 int end; 95 double sum = 0.0; 96 for (end=start; end < len; end++) { 97 char c = s.charAt(end); 98 int newDigit; 99 if ('0' <= c && c <= digitMax) 100 newDigit = c - '0'; 101 else if ('a' <= c && c < lowerCaseBound) 102 newDigit = c - 'a' + 10; 103 else if ('A' <= c && c < upperCaseBound) 104 newDigit = c - 'A' + 10; 105 else 106 break; 107 sum = sum*radix + newDigit; 108 } 109 if (start == end) { 110 return NaN; 111 } 112 if (sum >= 9007199254740992.0) { 113 if (radix == 10) { 114 119 try { 120 return Double.valueOf(s.substring(start, end)).doubleValue(); 121 } catch (NumberFormatException nfe) { 122 return NaN; 123 } 124 } else if (radix == 2 || radix == 4 || radix == 8 || 125 radix == 16 || radix == 32) 126 { 127 137 BinaryDigitReader bdr = new BinaryDigitReader(radix, s, start, end); 138 int bit; 139 sum = 0.0; 140 141 142 do { 143 bit = bdr.getNextBinaryDigit(); 144 } while (bit == 0); 145 146 if (bit == 1) { 147 148 sum = 1.0; 149 for (int j = 52; j != 0; j--) { 150 bit = bdr.getNextBinaryDigit(); 151 if (bit < 0) 152 return sum; 153 sum = sum*2 + bit; 154 } 155 156 int bit54 = bdr.getNextBinaryDigit(); 157 if (bit54 >= 0) { 158 double factor = 2.0; 159 int sticky = 0; 160 int bit3; 161 162 while ((bit3 = bdr.getNextBinaryDigit()) >= 0) { 163 sticky |= bit3; 164 factor *= 2; 165 } 166 sum += bit54 & (bit | sticky); 167 sum *= factor; 168 } 169 } 170 } 171 172 } 173 return sum; 174 } 175 176 180 public static String escapeString(String s) { 181 182 StringBuffer sb = null; 183 184 for(int i = 0, L = s.length(); i != L; ++i) { 185 int c = s.charAt(i); 186 187 if (' ' <= c && c <= '~' && c != '"' && c != '\\') { 188 if (sb != null) { 191 sb.append((char)c); 192 } 193 continue; 194 } 195 if (sb == null) { 196 sb = new StringBuffer (L + 3); 197 sb.append(s); 198 sb.setLength(i); 199 } 200 201 int escape = -1; 202 switch (c) { 203 case '\b': escape = 'b'; break; 204 case '\f': escape = 'f'; break; 205 case '\n': escape = 'n'; break; 206 case '\r': escape = 'r'; break; 207 case '\t': escape = 't'; break; 208 case 0xb: escape = 'v'; break; case '"': escape = '"'; break; 210 case ' ': escape = ' '; break; 211 case '\\': escape = '\\'; break; 212 } 213 if (escape >= 0) { 214 sb.append('\\'); 216 sb.append((char)escape); 217 } else { 218 int hexSize; 219 if (c < 256) { 220 sb.append("\\x"); 222 hexSize = 2; 223 } else { 224 sb.append("\\u"); 226 hexSize = 4; 227 } 228 for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { 230 int digit = 0xf & (c >> shift); 231 int hc = (digit < 10) ? '0' + digit : 'a' - 10 + digit; 232 sb.append((char)hc); 233 } 234 } 235 } 236 237 return (sb == null) ? s : sb.toString(); 238 } 239 240 } 241 | Popular Tags |