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

Unified Diff: sdk/lib/utf/utf8.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/utf8.dart
diff --git a/sdk/lib/utf/utf8.dart b/sdk/lib/utf/utf8.dart
index f9ef9fbe969f3a01beddc3534faf6dceedc1389b..6b9573f200c89488470fc64df419bf53d6fb6e25 100644
--- a/sdk/lib/utf/utf8.dart
+++ b/sdk/lib/utf/utf8.dart
@@ -136,8 +136,8 @@ class IterableUtf8Decoder extends Iterable<int> {
IterableUtf8Decoder(this.bytes, [this.offset = 0, this.length = null,
this.replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]);
- Utf8Decoder iterator() => new Utf8Decoder(bytes, offset, length,
- replacementCodepoint);
+ Utf8Decoder get iterator =>
+ new Utf8Decoder(bytes, offset, length, replacementCodepoint);
}
/**
@@ -151,18 +151,19 @@ class IterableUtf8Decoder extends Iterable<int> {
class Utf8Decoder implements Iterator<int> {
final _ListRangeIterator utf8EncodedBytesIterator;
final int replacementCodepoint;
+ int _current = -1;
Utf8Decoder(List<int> utf8EncodedBytes, [int offset = 0, int length,
this.replacementCodepoint =
UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
- utf8EncodedBytesIterator = (new _ListRange(utf8EncodedBytes, offset,
- length)).iterator();
+ utf8EncodedBytesIterator =
+ (new _ListRange(utf8EncodedBytes, offset, length)).iterator;
Utf8Decoder._fromListRangeIterator(_ListRange source, [
this.replacementCodepoint =
UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
- utf8EncodedBytesIterator = source.iterator();
+ utf8EncodedBytesIterator = source.iterator;
/** Decode the remaininder of the characters in this decoder
* into a [List<int>].
@@ -170,8 +171,8 @@ class Utf8Decoder implements Iterator<int> {
List<int> decodeRest() {
List<int> codepoints = new List<int>(utf8EncodedBytesIterator.remaining);
int i = 0;
- while (hasNext) {
- codepoints[i++] = next();
+ while (moveNext()) {
+ codepoints[i++] = current;
}
if (i == codepoints.length) {
return codepoints;
@@ -182,24 +183,37 @@ class Utf8Decoder implements Iterator<int> {
}
}
- bool get hasNext => utf8EncodedBytesIterator.hasNext;
+ int get current {
+ if (_current == -1) {
+ // TODO(floitsch): bad error message.
+ throw new StateError("No more elements");
+ }
+ return _current;
+ }
+
+ bool moveNext() {
+ _current = -1;
+
+ if (!utf8EncodedBytesIterator.moveNext()) return false;
- int next() {
- int value = utf8EncodedBytesIterator.next();
+ int value = utf8EncodedBytesIterator.current;
int additionalBytes = 0;
if (value < 0) {
if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
+ return true;
} else {
throw new ArgumentError(
"Invalid UTF8 at ${utf8EncodedBytesIterator.position}");
}
} else if (value <= _UTF8_ONE_BYTE_MAX) {
- return value;
+ _current = value;
+ return true;
} else if (value < _UTF8_FIRST_BYTE_OF_TWO_BASE) {
if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
+ return true;
} else {
throw new ArgumentError(
"Invalid UTF8 at ${utf8EncodedBytesIterator.position}");
@@ -220,14 +234,15 @@ class Utf8Decoder implements Iterator<int> {
value -= _UTF8_FIRST_BYTE_OF_SIX_BASE;
additionalBytes = 5;
} else if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
+ return true;
} else {
throw new ArgumentError(
"Invalid UTF8 at ${utf8EncodedBytesIterator.position}");
}
int j = 0;
- while (j < additionalBytes && utf8EncodedBytesIterator.hasNext) {
- int nextValue = utf8EncodedBytesIterator.next();
+ while (j < additionalBytes && utf8EncodedBytesIterator.moveNext()) {
+ int nextValue = utf8EncodedBytesIterator.current;
if (nextValue > _UTF8_ONE_BYTE_MAX &&
nextValue < _UTF8_FIRST_BYTE_OF_TWO_BASE) {
value = ((value << 6) | (nextValue & _UTF8_LO_SIX_BIT_MASK));
@@ -249,9 +264,11 @@ class Utf8Decoder implements Iterator<int> {
(additionalBytes == 3 && value > _UTF8_THREE_BYTE_MAX);
bool inRange = value <= UNICODE_VALID_RANGE_MAX;
if (validSequence && nonOverlong && inRange) {
- return value;
+ _current = value;
+ return true;
} else if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
+ return true;
} else {
throw new ArgumentError(
"Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}");

Powered by Google App Engine
This is Rietveld 408576698