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

Unified Diff: sdk/lib/_internal/js_runtime/lib/core_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 | « runtime/lib/uri_patch.dart ('k') | sdk/lib/core/uri.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/js_runtime/lib/core_patch.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/core_patch.dart b/sdk/lib/_internal/js_runtime/lib/core_patch.dart
index 7d6ad7c244533fc8af009361b01a016c19aae16b..a9832709b21b2fe46243d8e73e18eb8874e2c62c 100644
--- a/sdk/lib/_internal/js_runtime/lib/core_patch.dart
+++ b/sdk/lib/_internal/js_runtime/lib/core_patch.dart
@@ -527,6 +527,47 @@ class Uri {
if (uri != null) return Uri.parse(uri);
throw new UnsupportedError("'Uri.base' is not supported");
}
+
+
+ // Matches a String that _uriEncodes to itself regardless of the kind of
+ // component. This corresponds to [_unreservedTable], i.e. characters that
+ // are not encoded by any encoding table.
+ static final RegExp _needsNoEncoding = new RegExp(r'^[\-\.0-9A-Z_a-z~]*$');
+
+ /**
+ * This is the internal implementation of JavaScript's encodeURI function.
+ * It encodes all characters in the string [text] except for those
+ * that appear in [canonicalTable], and returns the escaped string.
+ */
+ @patch
+ static String _uriEncode(List<int> canonicalTable,
+ String text,
+ Encoding encoding,
+ bool spaceToPlus) {
+ if (identical(encoding, UTF8) && _needsNoEncoding.hasMatch(text)) {
+ return text;
+ }
+
+ // Encode the string into bytes then generate an ASCII only string
+ // by percent encoding selected bytes.
+ StringBuffer result = new StringBuffer();
+ var bytes = encoding.encode(text);
+ for (int i = 0; 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);
+ result.writeCharCode(hexDigits.codeUnitAt(byte >> 4));
+ result.writeCharCode(hexDigits.codeUnitAt(byte & 0x0f));
+ }
+ }
+ return result.toString();
+ }
}
@patch
« no previous file with comments | « runtime/lib/uri_patch.dart ('k') | sdk/lib/core/uri.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698