OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A parsed URI, such as a URL. | 8 * A parsed URI, such as a URL. |
9 * | 9 * |
10 * **See also:** | 10 * **See also:** |
(...skipping 2242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2253 | 2253 |
2254 /** | 2254 /** |
2255 * This is the internal implementation of JavaScript's encodeURI function. | 2255 * This is the internal implementation of JavaScript's encodeURI function. |
2256 * It encodes all characters in the string [text] except for those | 2256 * It encodes all characters in the string [text] except for those |
2257 * that appear in [canonicalTable], and returns the escaped string. | 2257 * that appear in [canonicalTable], and returns the escaped string. |
2258 */ | 2258 */ |
2259 static String _uriEncode(List<int> canonicalTable, | 2259 static String _uriEncode(List<int> canonicalTable, |
2260 String text, | 2260 String text, |
2261 {Encoding encoding: UTF8, | 2261 {Encoding encoding: UTF8, |
2262 bool spaceToPlus: false}) { | 2262 bool spaceToPlus: false}) { |
| 2263 if (_noUriEncodingNeeded(canonicalTable, text, encoding)) return text; |
| 2264 |
2263 byteToHex(byte, buffer) { | 2265 byteToHex(byte, buffer) { |
2264 const String hex = '0123456789ABCDEF'; | 2266 const String hex = '0123456789ABCDEF'; |
2265 buffer.writeCharCode(hex.codeUnitAt(byte >> 4)); | 2267 buffer.writeCharCode(hex.codeUnitAt(byte >> 4)); |
2266 buffer.writeCharCode(hex.codeUnitAt(byte & 0x0f)); | 2268 buffer.writeCharCode(hex.codeUnitAt(byte & 0x0f)); |
2267 } | 2269 } |
2268 | 2270 |
2269 // Encode the string into bytes then generate an ASCII only string | 2271 // Encode the string into bytes then generate an ASCII only string |
2270 // by percent encoding selected bytes. | 2272 // by percent encoding selected bytes. |
2271 StringBuffer result = new StringBuffer(); | 2273 StringBuffer result = new StringBuffer(); |
2272 var bytes = encoding.encode(text); | 2274 var bytes = encoding.encode(text); |
2273 for (int i = 0; i < bytes.length; i++) { | 2275 for (int i = 0; i < bytes.length; i++) { |
2274 int byte = bytes[i]; | 2276 int byte = bytes[i]; |
2275 if (byte < 128 && | 2277 if (byte < 128 && |
2276 ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) { | 2278 ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) { |
2277 result.writeCharCode(byte); | 2279 result.writeCharCode(byte); |
2278 } else if (spaceToPlus && byte == _SPACE) { | 2280 } else if (spaceToPlus && byte == _SPACE) { |
2279 result.writeCharCode(_PLUS); | 2281 result.writeCharCode(_PLUS); |
2280 } else { | 2282 } else { |
2281 result.writeCharCode(_PERCENT); | 2283 result.writeCharCode(_PERCENT); |
2282 byteToHex(byte, result); | 2284 byteToHex(byte, result); |
2283 } | 2285 } |
2284 } | 2286 } |
2285 return result.toString(); | 2287 return result.toString(); |
2286 } | 2288 } |
2287 | 2289 |
| 2290 static bool _noUriEncodingNeeded( |
| 2291 List<int> canonicalTable, String text, Encoding encoding) { |
| 2292 if (encoding != UTF8) return false; |
| 2293 for (int i = 0; i < text.length; i++) { |
| 2294 int code = text.codeUnitAt(i); |
| 2295 if (code >= 128) return false; |
| 2296 if ((canonicalTable[code >> 4] & (1 << (code & 0x0f))) == 0) return false; |
| 2297 } |
| 2298 return true; |
| 2299 } |
| 2300 |
2288 /** | 2301 /** |
2289 * Convert a byte (2 character hex sequence) in string [s] starting | 2302 * Convert a byte (2 character hex sequence) in string [s] starting |
2290 * at position [pos] to its ordinal value | 2303 * at position [pos] to its ordinal value |
2291 */ | 2304 */ |
2292 static int _hexCharPairToByte(String s, int pos) { | 2305 static int _hexCharPairToByte(String s, int pos) { |
2293 int byte = 0; | 2306 int byte = 0; |
2294 for (int i = 0; i < 2; i++) { | 2307 for (int i = 0; i < 2; i++) { |
2295 var charCode = s.codeUnitAt(pos + i); | 2308 var charCode = s.codeUnitAt(pos + i); |
2296 if (0x30 <= charCode && charCode <= 0x39) { | 2309 if (0x30 <= charCode && charCode <= 0x39) { |
2297 byte = byte * 16 + charCode - 0x30; | 2310 byte = byte * 16 + charCode - 0x30; |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2604 0xafff, // 0x30 - 0x3f 1111111111110101 | 2617 0xafff, // 0x30 - 0x3f 1111111111110101 |
2605 // @ABCDEFGHIJKLMNO | 2618 // @ABCDEFGHIJKLMNO |
2606 0xffff, // 0x40 - 0x4f 1111111111111111 | 2619 0xffff, // 0x40 - 0x4f 1111111111111111 |
2607 // PQRSTUVWXYZ _ | 2620 // PQRSTUVWXYZ _ |
2608 0x87ff, // 0x50 - 0x5f 1111111111100001 | 2621 0x87ff, // 0x50 - 0x5f 1111111111100001 |
2609 // abcdefghijklmno | 2622 // abcdefghijklmno |
2610 0xfffe, // 0x60 - 0x6f 0111111111111111 | 2623 0xfffe, // 0x60 - 0x6f 0111111111111111 |
2611 // pqrstuvwxyz ~ | 2624 // pqrstuvwxyz ~ |
2612 0x47ff]; // 0x70 - 0x7f 1111111111100010 | 2625 0x47ff]; // 0x70 - 0x7f 1111111111100010 |
2613 } | 2626 } |
OLD | NEW |