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

Unified Diff: sdk/lib/_internal/compiler/js_lib/core_patch.dart

Issue 605213002: Change restrictions on start/end on String.fromCharCodes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address commments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/lib/string_patch.dart ('k') | sdk/lib/core/string.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/js_lib/core_patch.dart
diff --git a/sdk/lib/_internal/compiler/js_lib/core_patch.dart b/sdk/lib/_internal/compiler/js_lib/core_patch.dart
index a26ee926e08377f9b1f570e7ca9b910bf7293e9c..6d327fa05e8d8c499a4a772918accd257b0b9e2c 100644
--- a/sdk/lib/_internal/compiler/js_lib/core_patch.dart
+++ b/sdk/lib/_internal/compiler/js_lib/core_patch.dart
@@ -272,28 +272,24 @@ class String {
@patch
factory String.fromCharCodes(Iterable<int> charCodes,
[int start = 0, int end]) {
- if (start < 0) throw new RangeError.value(start);
- List list;
- if (charCodes is JSArray) {
- list = charCodes;
- // If possible, recognize typed lists too.
- int len = list.length;
- if (end == null || end > len) end = len;
- if (start > 0 || end < len) {
- if (start >= end) return "";
- list = list.sublist(start, end);
- }
- } else {
- if (end != null) {
- if (start >= end) return "";
- charCodes = charCodes.take(end);
- }
- if (start > 0) charCodes = charCodes.skip(start);
- list = charCodes.toList(); // Try the iterable's own toList first.
- if (list is! JSArray) {
- // If that fails, force a JSArray.
- list = new List.from(list);
- }
+ // If possible, recognize typed lists too.
+ if (charCodes is! JSArray) {
+ return _stringFromIterable(charCodes, start, end);
+ }
+
+ List list = charCodes;
+ int len = list.length;
+ if (start < 0 || start > len) {
+ throw new RangeError.range(start, 0, len);
+ }
+ if (end == null) {
+ end = len;
+ } else if (end < start || end > len) {
+ throw new RangeError.range(end, start, len);
+ }
+
+ if (start > 0 || end < len) {
+ list = list.sublist(start, end);
}
return Primitives.stringFromCharCodes(list);
}
@@ -308,6 +304,32 @@ class String {
throw new UnsupportedError(
'String.fromEnvironment can only be used as a const constructor');
}
+
+ static String _stringFromIterable(Iterable<int> charCodes,
+ int start, int end) {
+ if (start < 0) throw new RangeError.range(start, 0, charCodes.length);
+ if (end != null && end < start) {
+ throw new RangeError.value(end, start, charCodes.length);
+ }
+ var it = charCodes.iterator;
+ for (int i = 0; i < start; i++) {
+ if (!it.moveNext()) {
+ throw new RangeError.range(start, 0, i);
+ }
+ }
+ var list = [];
+ if (end == null) {
+ while (it.moveNext()) list.add(it.current);
+ } else {
+ for (int i = start; i < end; i++) {
+ if (!it.moveNext()) {
+ throw new RangeError.range(end, start, i);
+ }
+ list.add(it.current);
+ }
+ }
+ return Primitives.stringFromCharCodes(list);
+ }
}
@patch
« no previous file with comments | « runtime/lib/string_patch.dart ('k') | sdk/lib/core/string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698