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

Unified Diff: sdk/lib/utf/utf_core.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/utf_core.dart
diff --git a/sdk/lib/utf/utf_core.dart b/sdk/lib/utf/utf_core.dart
index 03723b97493b5df626b5bf0fc6cd613842d2afb8..86d84e36a62e854facda10a963a26ac9aa8b164a 100644
--- a/sdk/lib/utf/utf_core.dart
+++ b/sdk/lib/utf/utf_core.dart
@@ -68,8 +68,10 @@ const int UNICODE_UTF16_LO_MASK = 0x3ff;
* Encode code points as UTF16 code units.
*/
List<int> _codepointsToUtf16CodeUnits(
- List<int> codepoints, [int offset = 0, int length,
- int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
+ List<int> codepoints,
+ [int offset = 0,
+ int length,
+ int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
_ListRange listRange = new _ListRange(codepoints, offset, length);
int encodedLength = 0;
@@ -114,13 +116,13 @@ List<int> _utf16CodeUnitsToCodepoints(
List<int> utf16CodeUnits, [int offset = 0, int length,
int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
_ListRangeIterator source =
- (new _ListRange(utf16CodeUnits, offset, length)).iterator();
+ (new _ListRange(utf16CodeUnits, offset, length)).iterator;
Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder
.fromListRangeIterator(source, replacementCodepoint);
List<int> codepoints = new List<int>(source.remaining);
int i = 0;
- while (decoder.hasNext) {
- codepoints[i++] = decoder.next();
+ while (decoder.moveNext()) {
+ codepoints[i++] = decoder.current;
}
if (i == codepoints.length) {
return codepoints;
@@ -140,26 +142,36 @@ List<int> _utf16CodeUnitsToCodepoints(
class Utf16CodeUnitDecoder implements Iterator<int> {
final _ListRangeIterator utf16CodeUnitIterator;
final int replacementCodepoint;
+ int _current = -1;
Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length,
int this.replacementCodepoint =
UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
- utf16CodeUnitIterator = (new _ListRange(utf16CodeUnits, offset, length))
- .iterator();
+ utf16CodeUnitIterator =
+ (new _ListRange(utf16CodeUnits, offset, length)).iterator;
Utf16CodeUnitDecoder.fromListRangeIterator(
_ListRangeIterator this.utf16CodeUnitIterator,
int this.replacementCodepoint);
- Iterator<int> iterator() => this;
+ Iterator<int> get iterator => this;
+
+ int get current {
+ if (_current == -1) {
+ // TODO(floitsch): bad error message.
+ throw new StateError("No more elements");
+ }
+ return _current;
+ }
- bool get hasNext => utf16CodeUnitIterator.hasNext;
+ bool moveNext() {
+ _current = -1;
+ if (!utf16CodeUnitIterator.moveNext()) return false;
- int next() {
- int value = utf16CodeUnitIterator.next();
+ int value = utf16CodeUnitIterator.current;
if (value < 0) {
if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
} else {
throw new ArgumentError(
"Invalid UTF16 at ${utf16CodeUnitIterator.position}");
@@ -167,35 +179,36 @@ class Utf16CodeUnitDecoder implements Iterator<int> {
} else if (value < UNICODE_UTF16_RESERVED_LO ||
(value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) {
// transfer directly
- return value;
+ _current = value;
} else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE &&
- utf16CodeUnitIterator.hasNext) {
+ utf16CodeUnitIterator.moveNext()) {
// merge surrogate pair
- int nextValue = utf16CodeUnitIterator.next();
+ int nextValue = utf16CodeUnitIterator.current;
if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE &&
nextValue <= UNICODE_UTF16_RESERVED_HI) {
value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10;
value += UNICODE_UTF16_OFFSET +
(nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE);
- return value;
+ _current = value;
} else {
if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE &&
nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) {
utf16CodeUnitIterator.backup();
}
if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
} else {
throw new ArgumentError(
"Invalid UTF16 at ${utf16CodeUnitIterator.position}");
}
}
} else if (replacementCodepoint != null) {
- return replacementCodepoint;
+ _current = replacementCodepoint;
} else {
throw new ArgumentError(
"Invalid UTF16 at ${utf16CodeUnitIterator.position}");
}
+ return true;
}
}
@@ -224,7 +237,7 @@ class _ListRange extends Iterable {
}
}
- _ListRangeIterator iterator() =>
+ _ListRangeIterator get iterator =>
new _ListRangeIteratorImpl(_source, _offset, _offset + _length);
int get length => _length;
@@ -236,8 +249,8 @@ class _ListRange extends Iterable {
* and move forward/backward within the iterator.
*/
abstract class _ListRangeIterator implements Iterator<int> {
- bool hasNext;
- int next();
+ bool moveNext();
+ int get current;
int get position;
void backup([by]);
int get remaining;
@@ -249,11 +262,12 @@ class _ListRangeIteratorImpl implements _ListRangeIterator {
int _offset;
final int _end;
- _ListRangeIteratorImpl(this._source, this._offset, this._end);
+ _ListRangeIteratorImpl(this._source, int offset, this._end)
+ : _offset = offset - 1;
- bool get hasNext => _offset < _end;
+ int get current => _source[_offset];
- int next() => _source[_offset++];
+ bool moveNext() => ++_offset < _end;
int get position => _offset;
@@ -261,7 +275,7 @@ class _ListRangeIteratorImpl implements _ListRangeIterator {
_offset -= by;
}
- int get remaining => _end - _offset;
+ int get remaining => _end - _offset - 1;
void skip([int count = 1]) {
_offset += count;

Powered by Google App Engine
This is Rietveld 408576698