| 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.codeUnitAt(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.write(text[i]); |
| 120 } else if (text[i] == " ") { | 120 } else if (text[i] == " ") { |
| 121 result.add("+"); | 121 result.write("+"); |
| 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.codeUnitAt(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.write(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 */ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 166 * A JavaScript-like decodeURI function. It unescapes the string [text] and | 166 * A JavaScript-like decodeURI function. It unescapes the string [text] and |
| 167 * returns the unescaped string. | 167 * returns the unescaped string. |
| 168 */ | 168 */ |
| 169 String _uriDecode(String text) { | 169 String _uriDecode(String text) { |
| 170 StringBuffer result = new StringBuffer(); | 170 StringBuffer result = new StringBuffer(); |
| 171 List<int> codepoints = new List<int>(); | 171 List<int> codepoints = new List<int>(); |
| 172 for (int i = 0; i < text.length;) { | 172 for (int i = 0; i < text.length;) { |
| 173 String ch = text[i]; | 173 String ch = text[i]; |
| 174 if (ch != '%') { | 174 if (ch != '%') { |
| 175 if (ch == '+') { | 175 if (ch == '+') { |
| 176 result.add(" "); | 176 result.write(" "); |
| 177 } else { | 177 } else { |
| 178 result.add(ch); | 178 result.write(ch); |
| 179 } | 179 } |
| 180 i++; | 180 i++; |
| 181 } else { | 181 } else { |
| 182 codepoints.clear(); | 182 codepoints.clear(); |
| 183 while (ch == '%') { | 183 while (ch == '%') { |
| 184 if (++i > text.length - 2) { | 184 if (++i > text.length - 2) { |
| 185 throw new ArgumentError('Truncated URI'); | 185 throw new ArgumentError('Truncated URI'); |
| 186 } | 186 } |
| 187 codepoints.add(_hexCharPairToByte(text, i)); | 187 codepoints.add(_hexCharPairToByte(text, i)); |
| 188 i += 2; | 188 i += 2; |
| 189 if (i == text.length) | 189 if (i == text.length) |
| 190 break; | 190 break; |
| 191 ch = text[i]; | 191 ch = text[i]; |
| 192 } | 192 } |
| 193 result.add(decodeUtf8(codepoints)); | 193 result.write(decodeUtf8(codepoints)); |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 return result.toString(); | 196 return result.toString(); |
| 197 } | 197 } |
| 198 | 198 |
| OLD | NEW |