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

Unified Diff: sdk/lib/utf/utf32.dart

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Address comments. Created 8 years, 1 month 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: sdk/lib/utf/utf32.dart
diff --git a/sdk/lib/utf/utf32.dart b/sdk/lib/utf/utf32.dart
index 5180f2d17234efacfa2949669d5a80fdcd5e4c83..fc0ea013f3103028656084e33c6a289576357d89 100644
--- a/sdk/lib/utf/utf32.dart
+++ b/sdk/lib/utf/utf32.dart
@@ -185,7 +185,7 @@ class IterableUtf32Decoder extends Iterable<int> {
IterableUtf32Decoder._(this.codeunitsProvider);
- Utf32BytesDecoder iterator() => codeunitsProvider();
+ Utf32BytesDecoder get iterator => codeunitsProvider();
}
/**
@@ -194,6 +194,7 @@ class IterableUtf32Decoder extends Iterable<int> {
class Utf32BytesDecoder implements _ListRangeIterator {
final _ListRangeIterator utf32EncodedBytesIterator;
final int replacementCodepoint;
+ int _current = -1;
Utf32BytesDecoder._fromListRangeIterator(
this.utf32EncodedBytesIterator, this.replacementCodepoint);
@@ -219,19 +220,27 @@ class Utf32BytesDecoder implements _ListRangeIterator {
List<int> decodeRest() {
List<int> codeunits = new List<int>(remaining);
int i = 0;
- while (hasNext) {
- codeunits[i++] = next();
+ while (moveNext()) {
+ codeunits[i++] = current;
}
return codeunits;
}
- bool get hasNext => utf32EncodedBytesIterator.hasNext;
+ int get current {
+ if (_current == -1) {
+ // TODO(floitsch): bad error message.
+ throw new StateError("No more elements");
+ }
+ return _current;
+ }
- int next() {
+ bool moveNext() {
+ _current = -1;
if (utf32EncodedBytesIterator.remaining < 4) {
utf32EncodedBytesIterator.skip(utf32EncodedBytesIterator.remaining);
if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
+ return true;
} else {
throw new ArgumentError(
"Invalid UTF32 at ${utf32EncodedBytesIterator.position}");
@@ -239,9 +248,11 @@ class Utf32BytesDecoder implements _ListRangeIterator {
} else {
int codepoint = decode();
if (_validCodepoint(codepoint)) {
- return codepoint;
+ _current = codepoint;
+ return true;
} else if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
+ return true;
} else {
throw new ArgumentError(
"Invalid UTF32 at ${utf32EncodedBytesIterator.position}");
@@ -272,18 +283,23 @@ class Utf32beBytesDecoder extends Utf32BytesDecoder {
Utf32beBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0,
int length, bool stripBom = true,
int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
- super._fromListRangeIterator((new _ListRange(utf32EncodedBytes, offset,
- length)).iterator(), replacementCodepoint) {
+ super._fromListRangeIterator(
+ (new _ListRange(utf32EncodedBytes, offset, length)).iterator,
+ replacementCodepoint) {
if (stripBom && hasUtf32beBom(utf32EncodedBytes, offset, length)) {
skip();
}
}
int decode() {
- int value = utf32EncodedBytesIterator.next();
- value = (value << 8) + utf32EncodedBytesIterator.next();
- value = (value << 8) + utf32EncodedBytesIterator.next();
- value = (value << 8) + utf32EncodedBytesIterator.next();
+ utf32EncodedBytesIterator.moveNext();
+ int value = utf32EncodedBytesIterator.current;
+ utf32EncodedBytesIterator.moveNext();
+ value = (value << 8) + utf32EncodedBytesIterator.current;
+ utf32EncodedBytesIterator.moveNext();
+ value = (value << 8) + utf32EncodedBytesIterator.current;
+ utf32EncodedBytesIterator.moveNext();
+ value = (value << 8) + utf32EncodedBytesIterator.current;
return value;
}
}
@@ -296,18 +312,23 @@ class Utf32leBytesDecoder extends Utf32BytesDecoder {
Utf32leBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0,
int length, bool stripBom = true,
int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
- super._fromListRangeIterator((new _ListRange(utf32EncodedBytes, offset,
- length)).iterator(), replacementCodepoint) {
+ super._fromListRangeIterator(
+ (new _ListRange(utf32EncodedBytes, offset, length)).iterator,
+ replacementCodepoint) {
if (stripBom && hasUtf32leBom(utf32EncodedBytes, offset, length)) {
skip();
}
}
int decode() {
- int value = (utf32EncodedBytesIterator.next());
- value += (utf32EncodedBytesIterator.next() << 8);
- value += (utf32EncodedBytesIterator.next() << 16);
- value += (utf32EncodedBytesIterator.next() << 24);
+ utf32EncodedBytesIterator.moveNext();
+ int value = utf32EncodedBytesIterator.current;
+ utf32EncodedBytesIterator.moveNext();
+ value += (utf32EncodedBytesIterator.current << 8);
+ utf32EncodedBytesIterator.moveNext();
+ value += (utf32EncodedBytesIterator.current << 16);
+ utf32EncodedBytesIterator.moveNext();
+ value += (utf32EncodedBytesIterator.current << 24);
return value;
}
}
« runtime/vm/intrinsifier.h ('K') | « sdk/lib/utf/utf16.dart ('k') | sdk/lib/utf/utf8.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698