Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2250)

Unified Diff: runtime/lib/uri_patch.dart

Issue 1416373007: Split _uriEncode into JS and VM versions. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Inline hex-function. Fix layout. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/js_runtime/lib/core_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) ||
+ 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;
+ }
+ }
+ }
+ 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();
+ }
}
« no previous file with comments | « no previous file | sdk/lib/_internal/js_runtime/lib/core_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698