Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 part of dart.core; | |
| 6 | |
| 7 // Frequently used character codes. | |
| 8 class _CharCode { | |
| 9 //static const int HT = 9; | |
| 10 //static const int LF = 10; | |
| 11 //static const int CR = 13; | |
| 12 //static const int SP = 32; | |
| 13 //static const int AMPERSAND = 38; | |
| 14 //static const int COMMA = 44; | |
| 15 //static const int DASH = 45; | |
| 16 //static const int SLASH = 47; | |
| 17 //static const int ZERO = 48; | |
| 18 //static const int ONE = 49; | |
| 19 //static const int COLON = 58; | |
| 20 //static const int SEMI_COLON = 59; | |
| 21 //static const int EQUAL = 61; | |
| 22 static const int PERCENT = 0x25; | |
| 23 static const int ZERO = 0x30; | |
| 24 static const int NINE = 0x39; | |
| 25 static const int UPPER_CASE_A = 0x41; | |
| 26 static const int UPPER_CASE_F = 0x46; | |
| 27 static const int LOWER_CASE_A = 0x61; | |
| 28 static const int LOWER_CASE_F = 0x66; | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * This is the internal implementation of JavaScript's encodeURI function. | |
| 33 * It encodes all characters in the string [text] except for those | |
| 34 * that appear in [canonicalTable], and returns the escaped string. | |
| 35 */ | |
| 36 String _uriEncode(List<int> canonicalTable, | |
| 37 String text, | |
| 38 {bool spaceToPlus: false}) { | |
| 39 final String hex = '0123456789ABCDEF'; | |
| 40 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v & 0x0f]}'; | |
|
floitsch
2013/05/24 20:37:27
This shouldn't be a 'var' but a normal nested func
Søren Gjesse
2013/05/27 10:47:21
Done.
Lasse Reichstein Nielsen
2013/05/28 07:26:23
Seems inefficient to create a string, and then wri
| |
| 41 StringBuffer result = new StringBuffer(); | |
| 42 for (int i = 0; i < text.length; i++) { | |
| 43 int ch = text.codeUnitAt(i); | |
| 44 if (ch < 128 && ((canonicalTable[ch >> 4] & (1 << (ch & 0x0f))) != 0)) { | |
| 45 result.write(text[i]); | |
| 46 } else if (spaceToPlus && text[i] == " ") { | |
| 47 result.write("+"); | |
| 48 } else { | |
| 49 if (ch >= 0xD800 && ch < 0xDC00) { | |
| 50 // Low surrogate. We expect a next char high surrogate. | |
| 51 ++i; | |
| 52 int nextCh = text.length == i ? 0 : text.codeUnitAt(i); | |
| 53 if (nextCh >= 0xDC00 && nextCh < 0xE000) { | |
| 54 // convert the pair to a U+10000 codepoint | |
| 55 ch = 0x10000 + ((ch - 0xD800) << 10) + (nextCh - 0xDC00); | |
| 56 } else { | |
| 57 throw new ArgumentError('Malformed URI'); | |
| 58 } | |
| 59 } | |
| 60 for (int codepoint in codepointsToUtf8([ch])) { | |
| 61 result.write(byteToHex(codepoint)); | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 return result.toString(); | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Convert a byte (2 character hex sequence) in string [s] starting | |
| 70 * at position [pos] to its ordinal value | |
| 71 */ | |
| 72 int _hexCharPairToByte(String s, int pos) { | |
| 73 int byte = 0; | |
| 74 for (int i = 0; i < 2; i++) { | |
| 75 var charCode = s.codeUnitAt(pos + i); | |
| 76 if (0x30 <= charCode && charCode <= 0x39) { | |
| 77 byte = byte * 16 + charCode - 0x30; | |
| 78 } else { | |
| 79 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66). | |
| 80 charCode |= 0x20; | |
| 81 if (0x61 <= charCode && charCode <= 0x66) { | |
| 82 byte = byte * 16 + charCode - 0x57; | |
| 83 } else { | |
| 84 throw new ArgumentError("Invalid URL encoding"); | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 return byte; | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * A JavaScript-like decodeURI function. It unescapes the string [text] and | |
| 93 * returns the unescaped string. | |
| 94 */ | |
| 95 String _uriDecode(String text, {bool plusToSpace: false}) { | |
| 96 StringBuffer result = new StringBuffer(); | |
| 97 List<int> codepoints = new List<int>(); | |
| 98 for (int i = 0; i < text.length;) { | |
| 99 String ch = text[i]; | |
| 100 if (ch != '%') { | |
| 101 if (plusToSpace && ch == '+') { | |
| 102 result.write(" "); | |
| 103 } else { | |
| 104 result.write(ch); | |
| 105 } | |
| 106 i++; | |
| 107 } else { | |
| 108 codepoints.clear(); | |
| 109 while (ch == '%') { | |
| 110 if (++i > text.length - 2) { | |
| 111 throw new ArgumentError('Truncated URI'); | |
| 112 } | |
| 113 codepoints.add(_hexCharPairToByte(text, i)); | |
| 114 i += 2; | |
| 115 if (i == text.length) | |
| 116 break; | |
| 117 ch = text[i]; | |
| 118 } | |
| 119 result.write(decodeUtf8(codepoints)); | |
| 120 } | |
| 121 } | |
| 122 return result.toString(); | |
| 123 } | |
| OLD | NEW |