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

Unified Diff: runtime/lib/string_patch.dart

Issue 515183002: Make String.fromCharCodes take start/end. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't say that end must be greater than start. Passing the actual length should be the same as pass… Created 6 years, 4 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: runtime/lib/string_patch.dart
diff --git a/runtime/lib/string_patch.dart b/runtime/lib/string_patch.dart
index 3ac78a8fd9404faa5724cd771666eb61053110d5..4c90fcb4ea5aeecd83e781d1bcf18bd2e982302f 100644
--- a/runtime/lib/string_patch.dart
+++ b/runtime/lib/string_patch.dart
@@ -3,8 +3,9 @@
// BSD-style license that can be found in the LICENSE file.
patch class String {
- /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) {
- return _StringBase.createFromCharCodes(charCodes);
+ /* patch */ factory String.fromCharCodes(Iterable<int> charCodes,
+ [int start = 0, int end]) {
+ return _StringBase.createFromCharCodes(charCodes, start, end);
srdjan 2014/09/17 15:31:58 Adding optional arguments will slow down this fact
Lasse Reichstein Nielsen 2014/09/18 09:03:00 Will do. Alternatively, we could have a String
}
/* patch */ factory String.fromCharCode(int charCode) {
@@ -13,14 +14,16 @@ patch class String {
return _OneByteString._allocate(1).._setAt(0, charCode);
}
if (charCode <= 0xffff) {
- return _StringBase._createFromCodePoints(new _List(1)..[0] = charCode);
+ return _StringBase._createFromCodePoints(new _List(1)..[0] = charCode,
+ 0, 1);
}
if (charCode <= 0x10ffff) {
var low = 0xDC00 | (charCode & 0x3ff);
int bits = charCode - 0x10000;
var high = 0xD800 | (bits >> 10);
return _StringBase._createFromCodePoints(new _List(2)..[0] = high
- ..[1] = low);
+ ..[1] = low,
+ 0, 2);
}
}
throw new RangeError.range(charCode, 0, 0x10ffff);
@@ -51,7 +54,9 @@ class _StringBase {
* Create the most efficient string representation for specified
* [codePoints].
*/
- static String createFromCharCodes(Iterable<int> charCodes) {
+ static String createFromCharCodes(Iterable<int> charCodes,
+ int start, int end) {
+ if (start < 0) throw new RangeError.value(start);
if (charCodes != null) {
// TODO(srdjan): Also skip copying of wide typed arrays.
final ccid = ClassID.getID(charCodes);
@@ -62,35 +67,40 @@ class _StringBase {
if ((charCodes is Uint8List) || (charCodes is Int8List)) {
isOneByteString = true;
} else {
- charCodes = new List<int>.from(charCodes, growable: false);
+ charCodes = new List.from(charCodes, growable: false);
}
}
- final len = charCodes.length;
+ if (end == null || end > charCodes.length) {
srdjan 2014/09/17 15:31:58 Please use parentheses
Lasse Reichstein Nielsen 2014/09/18 09:03:00 Done.
+ end = charCodes.length;
+ }
+ if (end <= start) return "";
+ final len = end - start;
if (!isOneByteString) {
- for (int i = 0; i < len; i++) {
+ for (int i = start; i < end; i++) {
int e = charCodes[i];
if (e is! _Smi) throw new ArgumentError(e);
// Is e Latin1?
if ((e < 0) || (e > 0xFF)) {
- return _createFromCodePoints(charCodes);
+ return _createFromCodePoints(charCodes, start, end);
}
}
}
// Allocate a one byte string. When the list is 128 entries or longer,
// it's faster to perform a runtime-call.
if (len >= 128) {
- return _OneByteString._allocateFromOneByteList(charCodes);
+ return _OneByteString._allocateFromOneByteList(charCodes, start, end);
}
var s = _OneByteString._allocate(len);
for (int i = 0; i < len; i++) {
- s._setAt(i, charCodes[i]);
+ s._setAt(i, charCodes[start + i]);
}
return s;
}
- return _createFromCodePoints(charCodes);
+ // charCodes is null.
+ throw new ArgumentError(charCodes);
srdjan 2014/09/17 15:31:58 Why not in the start: if (charCodes == null) throw
Lasse Reichstein Nielsen 2014/09/18 09:03:00 A naive attempt at putting the common branch first
}
- static String _createFromCodePoints(List<int> codePoints)
+ static String _createFromCodePoints(List<int> codePoints, int start, int end)
native "StringBase_createFromCodePoints";
String operator [](int index) native "String_charAt";
@@ -916,7 +926,8 @@ class _OneByteString extends _StringBase implements String {
static _OneByteString _allocate(int length) native "OneByteString_allocate";
- static _OneByteString _allocateFromOneByteList(List<int> list)
+ static _OneByteString _allocateFromOneByteList(List<int> list,
+ int start, int end)
native "OneByteString_allocateFromOneByteList";
// This is internal helper method. Code point value must be a valid

Powered by Google App Engine
This is Rietveld 408576698