Replaced double.infinity with DeltaIterator.maxLength

pull/332/head
Xin Yao 4 years ago
parent 138d89e5fc
commit 3b4e5d8a0a
  1. 29
      lib/src/models/quill_delta.dart

@ -551,7 +551,7 @@ class Delta {
var index = 0; var index = 0;
final opIterator = DeltaIterator(this); final opIterator = DeltaIterator(this);
final actualEnd = end ?? double.infinity; final actualEnd = end ?? DeltaIterator.maxLength;
while (index < actualEnd && opIterator.hasNext) { while (index < actualEnd && opIterator.hasNext) {
Operation op; Operation op;
@ -602,10 +602,12 @@ class Delta {
class DeltaIterator { class DeltaIterator {
DeltaIterator(this.delta) : _modificationCount = delta._modificationCount; DeltaIterator(this.delta) : _modificationCount = delta._modificationCount;
static const int maxLength = 1073741824;
final Delta delta; final Delta delta;
final int _modificationCount; final int _modificationCount;
int _index = 0; int _index = 0;
num _offset = 0; int _offset = 0;
bool get isNextInsert => nextOperationKey == Operation.insertKey; bool get isNextInsert => nextOperationKey == Operation.insertKey;
@ -621,24 +623,31 @@ class DeltaIterator {
} }
} }
bool get hasNext => peekLength() < double.infinity; bool get hasNext => peekLength() < maxLength;
/// Returns length of next operation without consuming it. /// Returns length of next operation without consuming it.
/// ///
/// Returns [double.infinity] if there is no more operations left to iterate. /// Returns [maxLength] if there is no more operations left to iterate.
num peekLength() { int peekLength() {
if (_index < delta.length) { if (_index < delta.length) {
final operation = delta._operations[_index]; final operation = delta._operations[_index];
return operation.length! - _offset; return operation.length! - _offset;
} }
return double.infinity; return maxLength;
} }
/// Consumes and returns next operation. /// Consumes and returns next operation.
/// ///
/// Optional [length] specifies maximum length of operation to return. Note /// Optional [length] specifies maximum length of operation to return. Note
/// that actual length of returned operation may be less than specified value. /// that actual length of returned operation may be less than specified value.
Operation next([int length = 4294967296]) { ///
/// If this iterator reached the end of the Delta then returns a retain
/// operation with its length set to [maxLength].
// TODO: Note that we used double.infinity as the default value for length here
// but this can now cause a type error since operation length is
// expected to be an int. Changing default length to [maxLength] is
// a workaround to avoid breaking changes.
Operation next([int length = maxLength]) {
if (_modificationCount != delta._modificationCount) { if (_modificationCount != delta._modificationCount) {
throw ConcurrentModificationError(delta); throw ConcurrentModificationError(delta);
} }
@ -656,13 +665,13 @@ class DeltaIterator {
_offset += actualLength; _offset += actualLength;
} }
final opData = op.isInsert && op.data is String final opData = op.isInsert && op.data is String
? (op.data as String).substring( ? (op.data as String)
_currentOffset as int, _currentOffset + (actualLength as int)) .substring(_currentOffset, _currentOffset + actualLength)
: op.data; : op.data;
final opIsNotEmpty = final opIsNotEmpty =
opData is String ? opData.isNotEmpty : true; // embeds are never empty opData is String ? opData.isNotEmpty : true; // embeds are never empty
final opLength = opData is String ? opData.length : 1; final opLength = opData is String ? opData.length : 1;
final opActualLength = opIsNotEmpty ? opLength : actualLength as int; final opActualLength = opIsNotEmpty ? opLength : actualLength;
return Operation._(opKey, opActualLength, opData, opAttributes); return Operation._(opKey, opActualLength, opData, opAttributes);
} }
return Operation.retain(length); return Operation.retain(length);

Loading…
Cancel
Save