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

Side by Side 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, 3 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 unified diff | Download patch | Annotate | Revision Log
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 patch class String { 5 patch class String {
6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes,
7 return _StringBase.createFromCharCodes(charCodes); 7 [int start = 0, int end]) {
8 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
8 } 9 }
9 10
10 /* patch */ factory String.fromCharCode(int charCode) { 11 /* patch */ factory String.fromCharCode(int charCode) {
11 if (charCode >= 0) { 12 if (charCode >= 0) {
12 if (charCode <= 0xff) { 13 if (charCode <= 0xff) {
13 return _OneByteString._allocate(1).._setAt(0, charCode); 14 return _OneByteString._allocate(1).._setAt(0, charCode);
14 } 15 }
15 if (charCode <= 0xffff) { 16 if (charCode <= 0xffff) {
16 return _StringBase._createFromCodePoints(new _List(1)..[0] = charCode); 17 return _StringBase._createFromCodePoints(new _List(1)..[0] = charCode,
18 0, 1);
17 } 19 }
18 if (charCode <= 0x10ffff) { 20 if (charCode <= 0x10ffff) {
19 var low = 0xDC00 | (charCode & 0x3ff); 21 var low = 0xDC00 | (charCode & 0x3ff);
20 int bits = charCode - 0x10000; 22 int bits = charCode - 0x10000;
21 var high = 0xD800 | (bits >> 10); 23 var high = 0xD800 | (bits >> 10);
22 return _StringBase._createFromCodePoints(new _List(2)..[0] = high 24 return _StringBase._createFromCodePoints(new _List(2)..[0] = high
23 ..[1] = low); 25 ..[1] = low,
26 0, 2);
24 } 27 }
25 } 28 }
26 throw new RangeError.range(charCode, 0, 0x10ffff); 29 throw new RangeError.range(charCode, 0, 0x10ffff);
27 } 30 }
28 31
29 /* patch */ const factory String.fromEnvironment(String name, 32 /* patch */ const factory String.fromEnvironment(String name,
30 {String defaultValue}) 33 {String defaultValue})
31 native "String_fromEnvironment"; 34 native "String_fromEnvironment";
32 } 35 }
33 36
(...skipping 10 matching lines...) Expand all
44 } 47 }
45 48
46 Type get runtimeType => String; 49 Type get runtimeType => String;
47 50
48 int get hashCode native "String_getHashCode"; 51 int get hashCode native "String_getHashCode";
49 52
50 /** 53 /**
51 * Create the most efficient string representation for specified 54 * Create the most efficient string representation for specified
52 * [codePoints]. 55 * [codePoints].
53 */ 56 */
54 static String createFromCharCodes(Iterable<int> charCodes) { 57 static String createFromCharCodes(Iterable<int> charCodes,
58 int start, int end) {
59 if (start < 0) throw new RangeError.value(start);
55 if (charCodes != null) { 60 if (charCodes != null) {
56 // TODO(srdjan): Also skip copying of wide typed arrays. 61 // TODO(srdjan): Also skip copying of wide typed arrays.
57 final ccid = ClassID.getID(charCodes); 62 final ccid = ClassID.getID(charCodes);
58 bool isOneByteString = false; 63 bool isOneByteString = false;
59 if ((ccid != ClassID.cidArray) && 64 if ((ccid != ClassID.cidArray) &&
60 (ccid != ClassID.cidGrowableObjectArray) && 65 (ccid != ClassID.cidGrowableObjectArray) &&
61 (ccid != ClassID.cidImmutableArray)) { 66 (ccid != ClassID.cidImmutableArray)) {
62 if ((charCodes is Uint8List) || (charCodes is Int8List)) { 67 if ((charCodes is Uint8List) || (charCodes is Int8List)) {
63 isOneByteString = true; 68 isOneByteString = true;
64 } else { 69 } else {
65 charCodes = new List<int>.from(charCodes, growable: false); 70 charCodes = new List.from(charCodes, growable: false);
66 } 71 }
67 } 72 }
68 final len = charCodes.length; 73 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.
74 end = charCodes.length;
75 }
76 if (end <= start) return "";
77 final len = end - start;
69 if (!isOneByteString) { 78 if (!isOneByteString) {
70 for (int i = 0; i < len; i++) { 79 for (int i = start; i < end; i++) {
71 int e = charCodes[i]; 80 int e = charCodes[i];
72 if (e is! _Smi) throw new ArgumentError(e); 81 if (e is! _Smi) throw new ArgumentError(e);
73 // Is e Latin1? 82 // Is e Latin1?
74 if ((e < 0) || (e > 0xFF)) { 83 if ((e < 0) || (e > 0xFF)) {
75 return _createFromCodePoints(charCodes); 84 return _createFromCodePoints(charCodes, start, end);
76 } 85 }
77 } 86 }
78 } 87 }
79 // Allocate a one byte string. When the list is 128 entries or longer, 88 // Allocate a one byte string. When the list is 128 entries or longer,
80 // it's faster to perform a runtime-call. 89 // it's faster to perform a runtime-call.
81 if (len >= 128) { 90 if (len >= 128) {
82 return _OneByteString._allocateFromOneByteList(charCodes); 91 return _OneByteString._allocateFromOneByteList(charCodes, start, end);
83 } 92 }
84 var s = _OneByteString._allocate(len); 93 var s = _OneByteString._allocate(len);
85 for (int i = 0; i < len; i++) { 94 for (int i = 0; i < len; i++) {
86 s._setAt(i, charCodes[i]); 95 s._setAt(i, charCodes[start + i]);
87 } 96 }
88 return s; 97 return s;
89 } 98 }
90 return _createFromCodePoints(charCodes); 99 // charCodes is null.
100 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
91 } 101 }
92 102
93 static String _createFromCodePoints(List<int> codePoints) 103 static String _createFromCodePoints(List<int> codePoints, int start, int end)
94 native "StringBase_createFromCodePoints"; 104 native "StringBase_createFromCodePoints";
95 105
96 String operator [](int index) native "String_charAt"; 106 String operator [](int index) native "String_charAt";
97 107
98 int codeUnitAt(int index) native "String_codeUnitAt"; 108 int codeUnitAt(int index) native "String_codeUnitAt";
99 109
100 int get length native "String_getLength"; 110 int get length native "String_getLength";
101 111
102 bool get isEmpty { 112 bool get isEmpty {
103 return this.length == 0; 113 return this.length == 0;
(...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 return result; 919 return result;
910 } 920 }
911 return this; 921 return this;
912 } 922 }
913 923
914 // Allocates a string of given length, expecting its content to be 924 // Allocates a string of given length, expecting its content to be
915 // set using _setAt. 925 // set using _setAt.
916 static _OneByteString _allocate(int length) native "OneByteString_allocate"; 926 static _OneByteString _allocate(int length) native "OneByteString_allocate";
917 927
918 928
919 static _OneByteString _allocateFromOneByteList(List<int> list) 929 static _OneByteString _allocateFromOneByteList(List<int> list,
930 int start, int end)
920 native "OneByteString_allocateFromOneByteList"; 931 native "OneByteString_allocateFromOneByteList";
921 932
922 // This is internal helper method. Code point value must be a valid 933 // This is internal helper method. Code point value must be a valid
923 // Latin1 value (0..0xFF), index must be valid. 934 // Latin1 value (0..0xFF), index must be valid.
924 void _setAt(int index, int codePoint) native "OneByteString_setAt"; 935 void _setAt(int index, int codePoint) native "OneByteString_setAt";
925 } 936 }
926 937
927 938
928 class _TwoByteString extends _StringBase implements String { 939 class _TwoByteString extends _StringBase implements String {
929 factory _TwoByteString._uninstantiable() { 940 factory _TwoByteString._uninstantiable() {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 class _CodeUnits extends Object with ListMixin<int>, 1021 class _CodeUnits extends Object with ListMixin<int>,
1011 UnmodifiableListMixin<int> { 1022 UnmodifiableListMixin<int> {
1012 /** The string that this is the code units of. */ 1023 /** The string that this is the code units of. */
1013 String _string; 1024 String _string;
1014 1025
1015 _CodeUnits(this._string); 1026 _CodeUnits(this._string);
1016 1027
1017 int get length => _string.length; 1028 int get length => _string.length;
1018 int operator[](int i) => _string.codeUnitAt(i); 1029 int operator[](int i) => _string.codeUnitAt(i);
1019 } 1030 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698