OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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.uri; | 5 part of dart.uri; |
6 | 6 |
7 /** | 7 /** |
8 * Javascript-like URI encode/decode functions. | 8 * Javascript-like URI encode/decode functions. |
9 * The documentation here borrows heavily from the original Javascript | 9 * The documentation here borrows heavily from the original Javascript |
10 * doumentation on MDN at: | 10 * doumentation on MDN at: |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 /** | 107 /** |
108 * This is the internal implementation of JavaScript's encodeURI function. | 108 * This is the internal implementation of JavaScript's encodeURI function. |
109 * It encodes all characters in the string [text] except for those | 109 * It encodes all characters in the string [text] except for those |
110 * that appear in [canonicalTable], and returns the escaped string. | 110 * that appear in [canonicalTable], and returns the escaped string. |
111 */ | 111 */ |
112 String _uriEncode(List<int> canonicalTable, String text) { | 112 String _uriEncode(List<int> canonicalTable, String text) { |
113 final String hex = '0123456789ABCDEF'; | 113 final String hex = '0123456789ABCDEF'; |
114 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v & 0x0f]}'; | 114 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v & 0x0f]}'; |
115 StringBuffer result = new StringBuffer(); | 115 StringBuffer result = new StringBuffer(); |
116 for (int i = 0; i < text.length; i++) { | 116 for (int i = 0; i < text.length; i++) { |
117 int ch = text.charCodeAt(i); | 117 int ch = text.codeUnitAt(i); |
118 if (ch < 128 && ((canonicalTable[ch >> 4] & (1 << (ch & 0x0f))) != 0)) { | 118 if (ch < 128 && ((canonicalTable[ch >> 4] & (1 << (ch & 0x0f))) != 0)) { |
119 result.add(text[i]); | 119 result.add(text[i]); |
120 } else if (text[i] == " ") { | 120 } else if (text[i] == " ") { |
121 result.add("+"); | 121 result.add("+"); |
122 } else { | 122 } else { |
123 if (ch >= 0xD800 && ch < 0xDC00) { | 123 if (ch >= 0xD800 && ch < 0xDC00) { |
124 // Low surrogate. We expect a next char high surrogate. | 124 // Low surrogate. We expect a next char high surrogate. |
125 ++i; | 125 ++i; |
126 int nextCh = text.length == i ? 0 : text.charCodeAt(i); | 126 int nextCh = text.length == i ? 0 : text.codeUnitAt(i); |
127 if (nextCh >= 0xDC00 && nextCh < 0xE000) { | 127 if (nextCh >= 0xDC00 && nextCh < 0xE000) { |
128 // convert the pair to a U+10000 codepoint | 128 // convert the pair to a U+10000 codepoint |
129 ch = 0x10000 + ((ch-0xD800) << 10) + (nextCh - 0xDC00); | 129 ch = 0x10000 + ((ch - 0xD800) << 10) + (nextCh - 0xDC00); |
130 } else { | 130 } else { |
131 throw new ArgumentError('Malformed URI'); | 131 throw new ArgumentError('Malformed URI'); |
132 } | 132 } |
133 } | 133 } |
134 for (int codepoint in codepointsToUtf8([ch])) { | 134 for (int codepoint in codepointsToUtf8([ch])) { |
135 result.add(byteToHex(codepoint)); | 135 result.add(byteToHex(codepoint)); |
136 } | 136 } |
137 } | 137 } |
138 } | 138 } |
139 return result.toString(); | 139 return result.toString(); |
140 } | 140 } |
141 | 141 |
142 /** | 142 /** |
143 * Convert a byte (2 character hex sequence) in string [s] starting | 143 * Convert a byte (2 character hex sequence) in string [s] starting |
144 * at position [pos] to its ordinal value | 144 * at position [pos] to its ordinal value |
145 */ | 145 */ |
146 | |
147 int _hexCharPairToByte(String s, int pos) { | 146 int _hexCharPairToByte(String s, int pos) { |
148 int byte = 0; | 147 int byte = 0; |
149 for (int i = 0; i < 2; i++) { | 148 for (int i = 0; i < 2; i++) { |
150 var charCode = s.charCodeAt(pos + i); | 149 var charCode = s.codeUnitAt(pos + i); |
151 if (0x30 <= charCode && charCode <= 0x39) { | 150 if (0x30 <= charCode && charCode <= 0x39) { |
152 byte = byte * 16 + charCode - 0x30; | 151 byte = byte * 16 + charCode - 0x30; |
153 } else { | 152 } else { |
154 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66). | 153 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66). |
155 charCode |= 0x20; | 154 charCode |= 0x20; |
156 if (0x61 <= charCode && charCode <= 0x66) { | 155 if (0x61 <= charCode && charCode <= 0x66) { |
157 byte = byte * 16 + charCode - 0x57; | 156 byte = byte * 16 + charCode - 0x57; |
158 } else { | 157 } else { |
159 throw new ArgumentError("Invalid URL encoding"); | 158 throw new ArgumentError("Invalid URL encoding"); |
160 } | 159 } |
(...skipping 29 matching lines...) Expand all Loading... |
190 if (i == text.length) | 189 if (i == text.length) |
191 break; | 190 break; |
192 ch = text[i]; | 191 ch = text[i]; |
193 } | 192 } |
194 result.add(decodeUtf8(codepoints)); | 193 result.add(decodeUtf8(codepoints)); |
195 } | 194 } |
196 } | 195 } |
197 return result.toString(); | 196 return result.toString(); |
198 } | 197 } |
199 | 198 |
OLD | NEW |