| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * [StringBase] contains common methods used by concrete String implementations, | 6 * [StringBase] contains common methods used by concrete String implementations, |
| 7 * e.g., OneByteString. | 7 * e.g., OneByteString. |
| 8 */ | 8 */ |
| 9 class StringBase { | 9 class StringBase { |
| 10 | 10 |
| 11 int hashCode() native "String_hashCode"; | 11 int hashCode() native "String_hashCode"; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Create the most efficient string representation for specified | 14 * Create the most efficient string representation for specified |
| 15 * [codePoints]. | 15 * [codePoints]. |
| 16 */ | 16 */ |
| 17 static String createFromCharCodes(List<int> charCodes) { | 17 static String createFromCharCodes(List<int> charCodes) { |
| 18 ObjectArray objectArray; | 18 ObjectArray objectArray; |
| 19 if (charCodes is ObjectArray) { | 19 if (charCodes is ObjectArray) { |
| 20 objectArray = charCodes; | 20 objectArray = charCodes; |
| 21 } else { | 21 } else { |
| 22 int len = charCodes.length; | 22 int len = charCodes.length; |
| 23 objectArray = new Array(len); | 23 objectArray = new ObjectArray(len); |
| 24 for (int i = 0; i < len; i++) { | 24 for (int i = 0; i < len; i++) { |
| 25 objectArray[i] = charCodes[i]; | 25 objectArray[i] = charCodes[i]; |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 return _createFromCodePoints(objectArray); | 28 return _createFromCodePoints(objectArray); |
| 29 } | 29 } |
| 30 | 30 |
| 31 static String _createFromCodePoints(ObjectArray<int> codePoints) | 31 static String _createFromCodePoints(ObjectArray<int> codePoints) |
| 32 native "StringBase_createFromCodePoints"; | 32 native "StringBase_createFromCodePoints"; |
| 33 | 33 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 throw new IndexOutOfRangeException(endIndex); | 144 throw new IndexOutOfRangeException(endIndex); |
| 145 } | 145 } |
| 146 if (startIndex > endIndex) { | 146 if (startIndex > endIndex) { |
| 147 throw new IndexOutOfRangeException(startIndex); | 147 throw new IndexOutOfRangeException(startIndex); |
| 148 } | 148 } |
| 149 return substringUnchecked_(startIndex, endIndex); | 149 return substringUnchecked_(startIndex, endIndex); |
| 150 } | 150 } |
| 151 | 151 |
| 152 String substringUnchecked_(int startIndex, int endIndex) { | 152 String substringUnchecked_(int startIndex, int endIndex) { |
| 153 int len = endIndex - startIndex; | 153 int len = endIndex - startIndex; |
| 154 Array<int> charCodes = new Array<int>(len); | 154 List<int> charCodes = new List<int>(len); |
| 155 for (int i = 0; i < len; i++) { | 155 for (int i = 0; i < len; i++) { |
| 156 charCodes[i] = this.charCodeAt(startIndex + i); | 156 charCodes[i] = this.charCodeAt(startIndex + i); |
| 157 } | 157 } |
| 158 return StringBase.createFromCharCodes(charCodes); | 158 return StringBase.createFromCharCodes(charCodes); |
| 159 } | 159 } |
| 160 | 160 |
| 161 String trim() { | 161 String trim() { |
| 162 int len = this.length; | 162 int len = this.length; |
| 163 int first = 0; | 163 int first = 0; |
| 164 for (; first < len; first++) { | 164 for (; first < len; first++) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 * Returns concatenated string. | 245 * Returns concatenated string. |
| 246 */ | 246 */ |
| 247 String operator +(Object obj) { | 247 String operator +(Object obj) { |
| 248 return this.concat(obj.toString()); | 248 return this.concat(obj.toString()); |
| 249 } | 249 } |
| 250 | 250 |
| 251 /** | 251 /** |
| 252 * Convert all objects in [values] to strings and concat them | 252 * Convert all objects in [values] to strings and concat them |
| 253 * into a result string. | 253 * into a result string. |
| 254 */ | 254 */ |
| 255 static String _interpolate(Array values) { | 255 static String _interpolate(List values) { |
| 256 int numValues = values.length; | 256 int numValues = values.length; |
| 257 Array<String> stringArray = new Array<String>(numValues); | 257 List<String> stringList = new List<String>(numValues); |
| 258 int resultLength = 0; | 258 int resultLength = 0; |
| 259 for (int i = 0; i < numValues; i++) { | 259 for (int i = 0; i < numValues; i++) { |
| 260 String str = values[i].toString(); | 260 String str = values[i].toString(); |
| 261 resultLength += str.length; | 261 resultLength += str.length; |
| 262 stringArray[i] = str; | 262 stringList[i] = str; |
| 263 } | 263 } |
| 264 Array<int> codepoints = new Array<int>(resultLength); | 264 List<int> codepoints = new List<int>(resultLength); |
| 265 int intArrayIx = 0; | 265 int intArrayIx = 0; |
| 266 for (int i = 0; i < numValues; i++) { | 266 for (int i = 0; i < numValues; i++) { |
| 267 String str = stringArray[i]; | 267 String str = stringList[i]; |
| 268 int strLength = str.length; | 268 int strLength = str.length; |
| 269 for (int k = 0; k < strLength; k++) { | 269 for (int k = 0; k < strLength; k++) { |
| 270 codepoints[intArrayIx++] = str.charCodeAt(k); | 270 codepoints[intArrayIx++] = str.charCodeAt(k); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 return StringBase.createFromCharCodes(codepoints); | 273 return StringBase.createFromCharCodes(codepoints); |
| 274 } | 274 } |
| 275 | 275 |
| 276 Iterable<Match> allMatches(String str) { | 276 Iterable<Match> allMatches(String str) { |
| 277 GrowableObjectArray<Match> result = new GrowableObjectArray<Match>(); | 277 List<Match> result = new List<Match>(); |
| 278 if (this.isEmpty()) return result; | 278 if (this.isEmpty()) return result; |
| 279 int length = this.length; | 279 int length = this.length; |
| 280 | 280 |
| 281 int ix = 0; | 281 int ix = 0; |
| 282 while (ix < str.length) { | 282 while (ix < str.length) { |
| 283 int foundIx = str.indexOf(this, ix); | 283 int foundIx = str.indexOf(this, ix); |
| 284 if (foundIx < 0) break; | 284 if (foundIx < 0) break; |
| 285 result.add(new _StringMatch(foundIx, str, this)); | 285 result.add(new _StringMatch(foundIx, str, this)); |
| 286 ix = foundIx + length; | 286 ix = foundIx + length; |
| 287 } | 287 } |
| 288 return result; | 288 return result; |
| 289 } | 289 } |
| 290 | 290 |
| 291 Array<String> split(Pattern pattern) { | 291 List<String> split(Pattern pattern) { |
| 292 if (pattern is RegExp) { | 292 if (pattern is RegExp) { |
| 293 throw "Unimplemented split with RegExp"; | 293 throw "Unimplemented split with RegExp"; |
| 294 } | 294 } |
| 295 GrowableObjectArray<String> result = new GrowableObjectArray<String>(); | 295 List<String> result = new List<String>(); |
| 296 if (pattern.isEmpty()) { | 296 if (pattern.isEmpty()) { |
| 297 for (int i = 0; i < this.length; i++) { | 297 for (int i = 0; i < this.length; i++) { |
| 298 result.add(this.substring(i, i+1)); | 298 result.add(this.substring(i, i+1)); |
| 299 } | 299 } |
| 300 return result; | 300 return result; |
| 301 } | 301 } |
| 302 int ix = 0; | 302 int ix = 0; |
| 303 while (ix < this.length) { | 303 while (ix < this.length) { |
| 304 int foundIx = this.indexOf(pattern, ix); | 304 int foundIx = this.indexOf(pattern, ix); |
| 305 if (foundIx < 0) { | 305 if (foundIx < 0) { |
| 306 // Not found, add remaining. | 306 // Not found, add remaining. |
| 307 result.add(this.substring(ix, this.length)); | 307 result.add(this.substring(ix, this.length)); |
| 308 break; | 308 break; |
| 309 } | 309 } |
| 310 result.add(this.substring(ix, foundIx)); | 310 result.add(this.substring(ix, foundIx)); |
| 311 ix = foundIx + pattern.length; | 311 ix = foundIx + pattern.length; |
| 312 } | 312 } |
| 313 if (ix == this.length) { | 313 if (ix == this.length) { |
| 314 result.add(""); | 314 result.add(""); |
| 315 } | 315 } |
| 316 return result; | 316 return result; |
| 317 } | 317 } |
| 318 | 318 |
| 319 Array<String> splitChars() { | 319 List<String> splitChars() { |
| 320 int len = this.length; | 320 int len = this.length; |
| 321 final result = new Array<String>(len); | 321 final result = new List<String>(len); |
| 322 for (int i = 0; i < len; i++) { | 322 for (int i = 0; i < len; i++) { |
| 323 result[i] = this[i]; | 323 result[i] = this[i]; |
| 324 } | 324 } |
| 325 return result; | 325 return result; |
| 326 } | 326 } |
| 327 | 327 |
| 328 Array<int> charCodes() { | 328 List<int> charCodes() { |
| 329 int len = this.length; | 329 int len = this.length; |
| 330 final result = new Array<int>(len); | 330 final result = new List<int>(len); |
| 331 for (int i = 0; i < len; i++) { | 331 for (int i = 0; i < len; i++) { |
| 332 result[i] = this.charCodeAt(i); | 332 result[i] = this.charCodeAt(i); |
| 333 } | 333 } |
| 334 return result; | 334 return result; |
| 335 } | 335 } |
| 336 | 336 |
| 337 String toLowerCase() { | 337 String toLowerCase() { |
| 338 final int aCode = "A".charCodeAt(0); | 338 final int aCode = "A".charCodeAt(0); |
| 339 final int zCode = "Z".charCodeAt(0); | 339 final int zCode = "Z".charCodeAt(0); |
| 340 final int delta = aCode - "a".charCodeAt(0); | 340 final int delta = aCode - "a".charCodeAt(0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 351 static String _convert(String str, int startCode, int endCode, int delta) { | 351 static String _convert(String str, int startCode, int endCode, int delta) { |
| 352 final int len = str.length; | 352 final int len = str.length; |
| 353 int i = 0; | 353 int i = 0; |
| 354 // Check if we can just return the string. | 354 // Check if we can just return the string. |
| 355 for (; i < len; i++) { | 355 for (; i < len; i++) { |
| 356 int code = str.charCodeAt(i); | 356 int code = str.charCodeAt(i); |
| 357 if ((startCode <= code) && (code <= endCode)) break; | 357 if ((startCode <= code) && (code <= endCode)) break; |
| 358 } | 358 } |
| 359 if (i == len) return str; | 359 if (i == len) return str; |
| 360 | 360 |
| 361 Array<int> charCodes = new Array<int>(len); | 361 List<int> charCodes = new List<int>(len); |
| 362 for (i = 0; i < len; i++) { | 362 for (i = 0; i < len; i++) { |
| 363 int code = str.charCodeAt(i); | 363 int code = str.charCodeAt(i); |
| 364 if ((startCode <= code) && (code <= endCode)) { | 364 if ((startCode <= code) && (code <= endCode)) { |
| 365 code = code - delta; | 365 code = code - delta; |
| 366 } | 366 } |
| 367 charCodes[i] = code; | 367 charCodes[i] = code; |
| 368 } | 368 } |
| 369 return StringBase.createFromCharCodes(charCodes); | 369 return StringBase.createFromCharCodes(charCodes); |
| 370 } | 370 } |
| 371 | 371 |
| 372 | 372 |
| 373 | 373 |
| 374 // Implementations of Strings methods follow below. | 374 // Implementations of Strings methods follow below. |
| 375 static String join(Array<String> strings, String separator) { | 375 static String join(List<String> strings, String separator) { |
| 376 final int length = strings.length; | 376 final int length = strings.length; |
| 377 if (length === 0) { | 377 if (length === 0) { |
| 378 return ""; | 378 return ""; |
| 379 } | 379 } |
| 380 | 380 |
| 381 Array strings_array = strings; | 381 List stringsList = strings; |
| 382 if (separator.length != 0) { | 382 if (separator.length != 0) { |
| 383 strings_array = new Array(2 * length - 1); | 383 stringsList = new List(2 * length - 1); |
| 384 strings_array[0] = strings[0]; | 384 stringsList[0] = strings[0]; |
| 385 int j = 1; | 385 int j = 1; |
| 386 for (int i = 1; i < length; i++) { | 386 for (int i = 1; i < length; i++) { |
| 387 strings_array[j++] = separator; | 387 stringsList[j++] = separator; |
| 388 strings_array[j++] = strings[i]; | 388 stringsList[j++] = strings[i]; |
| 389 } | 389 } |
| 390 } | 390 } |
| 391 return concatAll(strings_array); | 391 return concatAll(stringsList); |
| 392 } | 392 } |
| 393 | 393 |
| 394 static String concatAll(Array<String> strings) { | 394 static String concatAll(List<String> strings) { |
| 395 ObjectArray strings_array; | 395 ObjectArray stringsArray; |
| 396 if (strings is ObjectArray) { | 396 if (strings is ObjectArray) { |
| 397 strings_array = strings; | 397 stringsArray = strings; |
| 398 } else { | 398 } else { |
| 399 int len = strings.length; | 399 int len = strings.length; |
| 400 strings_array = new Array(len); | 400 stringsArray = new ObjectArray(len); |
| 401 for (int i = 0; i < len; i++) { | 401 for (int i = 0; i < len; i++) { |
| 402 strings_array[i] = strings[i]; | 402 stringsArray[i] = strings[i]; |
| 403 } | 403 } |
| 404 } | 404 } |
| 405 return _concatAll(strings_array); | 405 return _concatAll(stringsArray); |
| 406 } | 406 } |
| 407 | 407 |
| 408 static String _concatAll(ObjectArray<String> strings) | 408 static String _concatAll(ObjectArray<String> strings) |
| 409 native "Strings_concatAll"; | 409 native "Strings_concatAll"; |
| 410 } | 410 } |
| 411 | 411 |
| 412 | 412 |
| 413 class OneByteString extends StringBase implements String { | 413 class OneByteString extends StringBase implements String { |
| 414 // Checks for one-byte whitespaces only. | 414 // Checks for one-byte whitespaces only. |
| 415 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 415 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 String operator[](int g) => group(g); | 456 String operator[](int g) => group(g); |
| 457 int groupCount() => 0; | 457 int groupCount() => 0; |
| 458 | 458 |
| 459 String group(int group) { | 459 String group(int group) { |
| 460 if (group != 0) { | 460 if (group != 0) { |
| 461 throw new IndexOutOfRangeException(group); | 461 throw new IndexOutOfRangeException(group); |
| 462 } | 462 } |
| 463 return pattern; | 463 return pattern; |
| 464 } | 464 } |
| 465 | 465 |
| 466 Array<String> groups(Array<int> groups) { | 466 List<String> groups(List<int> groups) { |
| 467 Array<String> result = new Array<String>(); | 467 List<String> result = new List<String>(); |
| 468 for (int g in groups) { | 468 for (int g in groups) { |
| 469 result.add(group(g)); | 469 result.add(group(g)); |
| 470 } | 470 } |
| 471 return result; | 471 return result; |
| 472 } | 472 } |
| 473 | 473 |
| 474 final int _start; | 474 final int _start; |
| 475 final String str; | 475 final String str; |
| 476 final String pattern; | 476 final String pattern; |
| 477 } | 477 } |
| OLD | NEW |