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

Unified Diff: sdk/lib/core/encode_decode.dart

Issue 16019002: Merge the dart:uri library into dart:core and update the Uri class (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add missing files Created 7 years, 7 months 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
Index: sdk/lib/core/encode_decode.dart
diff --git a/sdk/lib/core/encode_decode.dart b/sdk/lib/core/encode_decode.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d236acf9c79598eb686d2fbfd4ba4406d546e7d9
--- /dev/null
+++ b/sdk/lib/core/encode_decode.dart
@@ -0,0 +1,123 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// 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.
+
+part of dart.core;
+
+// Frequently used character codes.
+class _CharCode {
+ //static const int HT = 9;
+ //static const int LF = 10;
+ //static const int CR = 13;
+ //static const int SP = 32;
+ //static const int AMPERSAND = 38;
+ //static const int COMMA = 44;
+ //static const int DASH = 45;
+ //static const int SLASH = 47;
+ //static const int ZERO = 48;
+ //static const int ONE = 49;
+ //static const int COLON = 58;
+ //static const int SEMI_COLON = 59;
+ //static const int EQUAL = 61;
+ static const int PERCENT = 0x25;
+ static const int ZERO = 0x30;
+ static const int NINE = 0x39;
+ static const int UPPER_CASE_A = 0x41;
+ static const int UPPER_CASE_F = 0x46;
+ static const int LOWER_CASE_A = 0x61;
+ static const int LOWER_CASE_F = 0x66;
+}
+
+/**
+ * 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.
+ */
+String _uriEncode(List<int> canonicalTable,
+ String text,
+ {bool spaceToPlus: false}) {
+ final String hex = '0123456789ABCDEF';
+ var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v & 0x0f]}';
floitsch 2013/05/24 20:37:27 This shouldn't be a 'var' but a normal nested func
Søren Gjesse 2013/05/27 10:47:21 Done.
Lasse Reichstein Nielsen 2013/05/28 07:26:23 Seems inefficient to create a string, and then wri
+ StringBuffer result = new StringBuffer();
+ for (int i = 0; i < text.length; i++) {
+ int ch = text.codeUnitAt(i);
+ if (ch < 128 && ((canonicalTable[ch >> 4] & (1 << (ch & 0x0f))) != 0)) {
+ result.write(text[i]);
+ } else if (spaceToPlus && text[i] == " ") {
+ result.write("+");
+ } else {
+ if (ch >= 0xD800 && ch < 0xDC00) {
+ // Low surrogate. We expect a next char high surrogate.
+ ++i;
+ int nextCh = text.length == i ? 0 : text.codeUnitAt(i);
+ if (nextCh >= 0xDC00 && nextCh < 0xE000) {
+ // convert the pair to a U+10000 codepoint
+ ch = 0x10000 + ((ch - 0xD800) << 10) + (nextCh - 0xDC00);
+ } else {
+ throw new ArgumentError('Malformed URI');
+ }
+ }
+ for (int codepoint in codepointsToUtf8([ch])) {
+ result.write(byteToHex(codepoint));
+ }
+ }
+ }
+ return result.toString();
+}
+
+/**
+ * Convert a byte (2 character hex sequence) in string [s] starting
+ * at position [pos] to its ordinal value
+ */
+int _hexCharPairToByte(String s, int pos) {
+ int byte = 0;
+ for (int i = 0; i < 2; i++) {
+ var charCode = s.codeUnitAt(pos + i);
+ if (0x30 <= charCode && charCode <= 0x39) {
+ byte = byte * 16 + charCode - 0x30;
+ } else {
+ // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66).
+ charCode |= 0x20;
+ if (0x61 <= charCode && charCode <= 0x66) {
+ byte = byte * 16 + charCode - 0x57;
+ } else {
+ throw new ArgumentError("Invalid URL encoding");
+ }
+ }
+ }
+ return byte;
+}
+
+/**
+ * A JavaScript-like decodeURI function. It unescapes the string [text] and
+ * returns the unescaped string.
+ */
+String _uriDecode(String text, {bool plusToSpace: false}) {
+ StringBuffer result = new StringBuffer();
+ List<int> codepoints = new List<int>();
+ for (int i = 0; i < text.length;) {
+ String ch = text[i];
+ if (ch != '%') {
+ if (plusToSpace && ch == '+') {
+ result.write(" ");
+ } else {
+ result.write(ch);
+ }
+ i++;
+ } else {
+ codepoints.clear();
+ while (ch == '%') {
+ if (++i > text.length - 2) {
+ throw new ArgumentError('Truncated URI');
+ }
+ codepoints.add(_hexCharPairToByte(text, i));
+ i += 2;
+ if (i == text.length)
+ break;
+ ch = text[i];
+ }
+ result.write(decodeUtf8(codepoints));
+ }
+ }
+ return result.toString();
+}

Powered by Google App Engine
This is Rietveld 408576698