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

Side by Side Diff: sdk/lib/core/uri.dart

Issue 1425373005: Don't copy text in _uriEncode if not necessary. Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « sdk/lib/core/core.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * A parsed URI, such as a URL. 8 * A parsed URI, such as a URL.
9 * 9 *
10 * **See also:** 10 * **See also:**
(...skipping 2242 matching lines...) Expand 10 before | Expand all | Expand 10 after
2253 2253
2254 /** 2254 /**
2255 * This is the internal implementation of JavaScript's encodeURI function. 2255 * This is the internal implementation of JavaScript's encodeURI function.
2256 * It encodes all characters in the string [text] except for those 2256 * It encodes all characters in the string [text] except for those
2257 * that appear in [canonicalTable], and returns the escaped string. 2257 * that appear in [canonicalTable], and returns the escaped string.
2258 */ 2258 */
2259 static String _uriEncode(List<int> canonicalTable, 2259 static String _uriEncode(List<int> canonicalTable,
2260 String text, 2260 String text,
2261 {Encoding encoding: UTF8, 2261 {Encoding encoding: UTF8,
2262 bool spaceToPlus: false}) { 2262 bool spaceToPlus: false}) {
2263 byteToHex(byte, buffer) {
2264 const String hex = '0123456789ABCDEF';
2265 buffer.writeCharCode(hex.codeUnitAt(byte >> 4));
2266 buffer.writeCharCode(hex.codeUnitAt(byte & 0x0f));
2267 }
2268
2269 // Encode the string into bytes then generate an ASCII only string
2270 // by percent encoding selected bytes.
2271 StringBuffer result = new StringBuffer();
2272 var bytes = encoding.encode(text); 2263 var bytes = encoding.encode(text);
2264 StringBuffer result;
2265 int start = 0;
2273 for (int i = 0; i < bytes.length; i++) { 2266 for (int i = 0; i < bytes.length; i++) {
2274 int byte = bytes[i]; 2267 int byte = bytes[i];
2275 if (byte < 128 && 2268 if (byte < 128 &&
2276 ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) { 2269 ((canonicalTable[byte >> 4] & (1 << (byte & 0x0f))) != 0)) {
2277 result.writeCharCode(byte); 2270 continue;
2278 } else if (spaceToPlus && byte == _SPACE) { 2271 }
2272 result ??= new StringBuffer();
2273 for (int j = start; j < i; j++) {
2274 result.writeCharCode(bytes[j]);
2275 }
2276 if (spaceToPlus && byte == _SPACE) {
2279 result.writeCharCode(_PLUS); 2277 result.writeCharCode(_PLUS);
2280 } else { 2278 } else {
2281 result.writeCharCode(_PERCENT); 2279 result.writeCharCode(_PERCENT);
2282 byteToHex(byte, result); 2280 const String hex = '0123456789ABCDEF';
2281 result.writeCharCode(hex.codeUnitAt(byte >> 4));
2282 result.writeCharCode(hex.codeUnitAt(byte & 0x0f));
2283 }
2284 start = i + 1;
2285 }
2286 if (result == null) {
2287 if (identical(encoding, ASCII) ||
2288 identical(encoding, LATIN1) ||
2289 identical(encoding, UTF8)) {
2290 return text;
2291 }
2292 return new String.fromCharCodes(bytes);
2293 }
2294 if (start < bytes.length) {
2295 for (int j = start; j < bytes.length; j++) {
2296 result.writeCharCode(bytes[j]);
2283 } 2297 }
2284 } 2298 }
2285 return result.toString(); 2299 return result.toString();
2286 } 2300 }
2287 2301
2288 /** 2302 /**
2289 * Convert a byte (2 character hex sequence) in string [s] starting 2303 * Convert a byte (2 character hex sequence) in string [s] starting
2290 * at position [pos] to its ordinal value 2304 * at position [pos] to its ordinal value
2291 */ 2305 */
2292 static int _hexCharPairToByte(String s, int pos) { 2306 static int _hexCharPairToByte(String s, int pos) {
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
2604 0xafff, // 0x30 - 0x3f 1111111111110101 2618 0xafff, // 0x30 - 0x3f 1111111111110101
2605 // @ABCDEFGHIJKLMNO 2619 // @ABCDEFGHIJKLMNO
2606 0xffff, // 0x40 - 0x4f 1111111111111111 2620 0xffff, // 0x40 - 0x4f 1111111111111111
2607 // PQRSTUVWXYZ _ 2621 // PQRSTUVWXYZ _
2608 0x87ff, // 0x50 - 0x5f 1111111111100001 2622 0x87ff, // 0x50 - 0x5f 1111111111100001
2609 // abcdefghijklmno 2623 // abcdefghijklmno
2610 0xfffe, // 0x60 - 0x6f 0111111111111111 2624 0xfffe, // 0x60 - 0x6f 0111111111111111
2611 // pqrstuvwxyz ~ 2625 // pqrstuvwxyz ~
2612 0x47ff]; // 0x70 - 0x7f 1111111111100010 2626 0x47ff]; // 0x70 - 0x7f 1111111111100010
2613 } 2627 }
OLDNEW
« no previous file with comments | « sdk/lib/core/core.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698