Chromium Code Reviews| Index: runtime/lib/uri_patch.dart |
| diff --git a/runtime/lib/uri_patch.dart b/runtime/lib/uri_patch.dart |
| index 93abe0da637cd45411b2e75b69930fdea7b926ab..2664b9b385930f312a94e9a7e737be58246a65fb 100644 |
| --- a/runtime/lib/uri_patch.dart |
| +++ b/runtime/lib/uri_patch.dart |
| @@ -2,6 +2,8 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| +import "dart:convert" show ASCII; |
| + |
| // VM implementation of Uri. |
| typedef Uri _UriBaseClosure(); |
| @@ -21,4 +23,51 @@ patch class Uri { |
| /* patch */ static Uri get base => _uriBaseClosure(); |
| static bool get _isWindowsPlatform native "Uri_isWindowsPlatform"; |
| + |
| + /* patch */ static String _uriEncode(List<int> canonicalTable, |
| + String text, |
| + Encoding encoding, |
| + bool spaceToPlus) { |
| + // First check if the text will be changed by encoding. |
| + int i = 0; |
| + if (identical(encoding, UTF8) || |
|
Ivan Posva
2015/11/05 06:24:57
Is it really identical that you are looking for or
Lasse Reichstein Nielsen
2015/11/05 08:06:49
It's the same thing since the UTF8 codec object do
|
| + identical(encoding, LATIN1) || |
| + identical(encoding, ASCII)) { |
| + // Encoding is compatible with the original string. |
| + // Find first character that needs encoding. |
| + for (; i < text.length; i++) { |
| + var char = text.codeUnitAt(i); |
| + if (char >= 128 || |
| + canonicalTable[char >> 4] & (1 << (char & 0x0f)) == 0) { |
| + break; |
| + } |
| + } |
| + } |
|
Lasse Reichstein Nielsen
2015/11/04 10:15:53
New code replacing the RegExp test with more preci
|
| + if (i == text.length) return text; |
| + |
| + // Encode the string into bytes then generate an ASCII only string |
| + // by percent encoding selected bytes. |
| + StringBuffer result = new StringBuffer(); |
| + for (int j = 0; j < i; j++) { |
| + result.writeCharCode(text.codeUnitAt(j)); |
| + } |
| + |
| + // TODO(lrn): Is there a way to only encode from index i and forwards. |
| + var bytes = encoding.encode(text); |
| + for (; i < bytes.length; i++) { |
| + int byte = bytes[i]; |
| + if (byte < 128 && |
| + ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) { |
| + result.writeCharCode(byte); |
| + } else if (spaceToPlus && byte == _SPACE) { |
| + result.writeCharCode(_PLUS); |
| + } else { |
| + const String hexDigits = '0123456789ABCDEF'; |
| + result..writeCharCode(_PERCENT) |
| + ..writeCharCode(hexDigits.codeUnitAt(byte >> 4)) |
| + ..writeCharCode(hexDigits.codeUnitAt(byte & 0x0f)); |
| + } |
| + } |
| + return result.toString(); |
| + } |
| } |