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 import "dart:convert" show ASCII; |
| 6 |
5 // VM implementation of Uri. | 7 // VM implementation of Uri. |
6 typedef Uri _UriBaseClosure(); | 8 typedef Uri _UriBaseClosure(); |
7 | 9 |
8 Uri _unsupportedUriBase() { | 10 Uri _unsupportedUriBase() { |
9 throw new UnsupportedError("'Uri.base' is not supported"); | 11 throw new UnsupportedError("'Uri.base' is not supported"); |
10 } | 12 } |
11 | 13 |
12 // _uriBaseClosure can be overwritten by the embedder to supply a different | 14 // _uriBaseClosure can be overwritten by the embedder to supply a different |
13 // value for Uri.base. | 15 // value for Uri.base. |
14 _UriBaseClosure _uriBaseClosure = _unsupportedUriBase; | 16 _UriBaseClosure _uriBaseClosure = _unsupportedUriBase; |
15 | 17 |
16 patch class Uri { | 18 patch class Uri { |
17 static final bool _isWindowsCached = _isWindowsPlatform; | 19 static final bool _isWindowsCached = _isWindowsPlatform; |
18 | 20 |
19 /* patch */ static bool get _isWindows => _isWindowsCached; | 21 /* patch */ static bool get _isWindows => _isWindowsCached; |
20 | 22 |
21 /* patch */ static Uri get base => _uriBaseClosure(); | 23 /* patch */ static Uri get base => _uriBaseClosure(); |
22 | 24 |
23 static bool get _isWindowsPlatform native "Uri_isWindowsPlatform"; | 25 static bool get _isWindowsPlatform native "Uri_isWindowsPlatform"; |
| 26 |
| 27 /* patch */ static String _uriEncode(List<int> canonicalTable, |
| 28 String text, |
| 29 Encoding encoding, |
| 30 bool spaceToPlus) { |
| 31 // First check if the text will be changed by encoding. |
| 32 int i = 0; |
| 33 if (identical(encoding, UTF8) || |
| 34 identical(encoding, LATIN1) || |
| 35 identical(encoding, ASCII)) { |
| 36 // Encoding is compatible with the original string. |
| 37 // Find first character that needs encoding. |
| 38 for (; i < text.length; i++) { |
| 39 var char = text.codeUnitAt(i); |
| 40 if (char >= 128 || |
| 41 canonicalTable[char >> 4] & (1 << (char & 0x0f)) == 0) { |
| 42 break; |
| 43 } |
| 44 } |
| 45 } |
| 46 if (i == text.length) return text; |
| 47 |
| 48 // Encode the string into bytes then generate an ASCII only string |
| 49 // by percent encoding selected bytes. |
| 50 StringBuffer result = new StringBuffer(); |
| 51 for (int j = 0; j < i; j++) { |
| 52 result.writeCharCode(text.codeUnitAt(j)); |
| 53 } |
| 54 |
| 55 // TODO(lrn): Is there a way to only encode from index i and forwards. |
| 56 var bytes = encoding.encode(text); |
| 57 for (; i < bytes.length; i++) { |
| 58 int byte = bytes[i]; |
| 59 if (byte < 128 && |
| 60 ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) { |
| 61 result.writeCharCode(byte); |
| 62 } else if (spaceToPlus && byte == _SPACE) { |
| 63 result.writeCharCode(_PLUS); |
| 64 } else { |
| 65 const String hexDigits = '0123456789ABCDEF'; |
| 66 result..writeCharCode(_PERCENT) |
| 67 ..writeCharCode(hexDigits.codeUnitAt(byte >> 4)) |
| 68 ..writeCharCode(hexDigits.codeUnitAt(byte & 0x0f)); |
| 69 } |
| 70 } |
| 71 return result.toString(); |
| 72 } |
24 } | 73 } |
OLD | NEW |