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

Side by Side Diff: src/uri.js

Issue 11348349: Improve array to string conversion. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } else { 158 } else {
159 throw new $URIError("URI malformed"); 159 throw new $URIError("URI malformed");
160 } 160 }
161 } 161 }
162 } 162 }
163 } 163 }
164 if (0xD800 <= value && value <= 0xDFFF) { 164 if (0xD800 <= value && value <= 0xDFFF) {
165 throw new $URIError("URI malformed"); 165 throw new $URIError("URI malformed");
166 } 166 }
167 if (value < 0x10000) { 167 if (value < 0x10000) {
168 result[index++] = value; 168 %_TwoByteSeqStringSetChar(result, index++, value);
169 return index; 169 return index;
170 } else { 170 } else {
171 result[index++] = (value >> 10) + 0xd7c0; 171 %_TwoByteSeqStringSetChar(result, index++, (value >> 10) + 0xd7c0);
172 result[index++] = (value & 0x3ff) + 0xdc00; 172 %_TwoByteSeqStringSetChar(result, index++, (value & 0x3ff) + 0xdc00);
173 return index; 173 return index;
174 } 174 }
175 } 175 }
176 176
177 177
178 // ECMA-262, section 15.1.3 178 // ECMA-262, section 15.1.3
179 function Encode(uri, unescape) { 179 function Encode(uri, unescape) {
180 var uriLength = uri.length; 180 var uriLength = uri.length;
181 // We are going to pass result to %StringFromCharCodeArray 181 var array = new InternalArray(uriLength);
182 // which does not expect any getters/setters installed
183 // on the incoming array.
184 var result = new InternalArray(uriLength);
185 var index = 0; 182 var index = 0;
186 for (var k = 0; k < uriLength; k++) { 183 for (var k = 0; k < uriLength; k++) {
187 var cc1 = uri.charCodeAt(k); 184 var cc1 = uri.charCodeAt(k);
188 if (unescape(cc1)) { 185 if (unescape(cc1)) {
189 result[index++] = cc1; 186 array[index++] = cc1;
190 } else { 187 } else {
191 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed"); 188 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed");
192 if (cc1 < 0xD800 || cc1 > 0xDBFF) { 189 if (cc1 < 0xD800 || cc1 > 0xDBFF) {
193 index = URIEncodeSingle(cc1, result, index); 190 index = URIEncodeSingle(cc1, array, index);
194 } else { 191 } else {
195 k++; 192 k++;
196 if (k == uriLength) throw new $URIError("URI malformed"); 193 if (k == uriLength) throw new $URIError("URI malformed");
197 var cc2 = uri.charCodeAt(k); 194 var cc2 = uri.charCodeAt(k);
198 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed"); 195 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed");
199 index = URIEncodePair(cc1, cc2, result, index); 196 index = URIEncodePair(cc1, cc2, array, index);
200 } 197 }
201 } 198 }
202 } 199 }
203 return %StringFromCharCodeArray(result); 200
201 var result = %NewString(array.length, true);
202 for (var i = 0; i < array.length; i++) {
203 %_OneByteSeqStringSetChar(result, i, array[i]);
204 }
205 return result;
204 } 206 }
205 207
206 208
207 // ECMA-262, section 15.1.3 209 // ECMA-262, section 15.1.3
208 function Decode(uri, reserved) { 210 function Decode(uri, reserved) {
209 var uriLength = uri.length; 211 var uriLength = uri.length;
210 // We are going to pass result to %StringFromCharCodeArray 212 var one_byte = %NewString(uriLength, true);
211 // which does not expect any getters/setters installed
212 // on the incoming array.
213 var result = new InternalArray(uriLength);
214 var index = 0; 213 var index = 0;
215 for (var k = 0; k < uriLength; k++) { 214 var k = 0;
216 var ch = uri.charAt(k); 215
217 if (ch == '%') { 216 // Optimistically assume ascii string.
217 for ( ; k < uriLength; k++) {
218 var code = uri.charCodeAt(k);
219 if (code == 37) { // '%'
220 if (k + 2 >= uriLength) throw new $URIError("URI malformed");
221 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2));
222 if (cc >> 7) break; // Assumption wrong, two byte string.
223 if (reserved(cc)) {
224 %_OneByteSeqStringSetChar(one_byte, index++, 37); // '%'.
225 %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+1));
226 %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+2));
227 } else {
228 %_OneByteSeqStringSetChar(one_byte, index++, cc);
229 }
230 k += 2;
231 } else {
232 if (code > 0x7f) break; // Assumption wrong, two byte string.
233 %_OneByteSeqStringSetChar(one_byte, index++, code);
234 }
235 }
236
237 one_byte = TruncateString(one_byte, index);
238 if (k == uriLength) return one_byte;
239
240 // Write into two byte string.
241 var two_byte = %NewString(uriLength - k, false);
242 index = 0;
243
244 for ( ; k < uriLength; k++) {
245 var code = uri.charCodeAt(k);
246 if (code == 37) { // '%'
218 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); 247 if (k + 2 >= uriLength) throw new $URIError("URI malformed");
219 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)); 248 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k));
220 if (cc >> 7) { 249 if (cc >> 7) {
221 var n = 0; 250 var n = 0;
222 while (((cc << ++n) & 0x80) != 0) { } 251 while (((cc << ++n) & 0x80) != 0) { }
223 if (n == 1 || n > 4) throw new $URIError("URI malformed"); 252 if (n == 1 || n > 4) throw new $URIError("URI malformed");
224 var octets = new $Array(n); 253 var octets = new $Array(n);
225 octets[0] = cc; 254 octets[0] = cc;
226 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed"); 255 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed");
227 for (var i = 1; i < n; i++) { 256 for (var i = 1; i < n; i++) {
228 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed"); 257 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed");
229 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), 258 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k),
230 uri.charCodeAt(++k)); 259 uri.charCodeAt(++k));
231 } 260 }
232 index = URIDecodeOctets(octets, result, index); 261 index = URIDecodeOctets(octets, two_byte, index);
262 } else if (reserved(cc)) {
263 %_TwoByteSeqStringSetChar(two_byte, index++, 37); // '%'.
264 %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k - 1));
265 %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k));
233 } else { 266 } else {
234 if (reserved(cc)) { 267 %_TwoByteSeqStringSetChar(two_byte, index++, cc);
235 result[index++] = 37; // Char code of '%'.
236 result[index++] = uri.charCodeAt(k - 1);
237 result[index++] = uri.charCodeAt(k);
238 } else {
239 result[index++] = cc;
240 }
241 } 268 }
242 } else { 269 } else {
243 result[index++] = ch.charCodeAt(0); 270 %_TwoByteSeqStringSetChar(two_byte, index++, code);
244 } 271 }
245 } 272 }
246 result.length = index; 273
247 return %StringFromCharCodeArray(result); 274 two_byte = TruncateString(two_byte, index);
275 return one_byte + two_byte;
248 } 276 }
249 277
250 278
251 // ECMA-262 - 15.1.3.1. 279 // ECMA-262 - 15.1.3.1.
252 function URIDecode(uri) { 280 function URIDecode(uri) {
253 var reservedPredicate = function(cc) { 281 var reservedPredicate = function(cc) {
254 // #$ 282 // #$
255 if (35 <= cc && cc <= 36) return true; 283 if (35 <= cc && cc <= 36) return true;
256 // & 284 // &
257 if (cc == 38) return true; 285 if (cc == 38) return true;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 "escape", URIEscape, 443 "escape", URIEscape,
416 "unescape", URIUnescape, 444 "unescape", URIUnescape,
417 "decodeURI", URIDecode, 445 "decodeURI", URIDecode,
418 "decodeURIComponent", URIDecodeComponent, 446 "decodeURIComponent", URIDecodeComponent,
419 "encodeURI", URIEncode, 447 "encodeURI", URIEncode,
420 "encodeURIComponent", URIEncodeComponent 448 "encodeURIComponent", URIEncodeComponent
421 )); 449 ));
422 } 450 }
423 451
424 SetUpUri(); 452 SetUpUri();
OLDNEW
« src/string.js ('K') | « src/string.js ('k') | src/x64/codegen-x64.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698