MutableStringMatchesIterable class
class MutableStringMatchesIterable extends IterableBase<MutableStringMatch> implements Iterator<MutableStringMatch> { final MutableString mutableString; final RegExp regExp; MutableStringMatch _current; int lastIndex = 0; MutableStringMatchesIterable(MutableString this.mutableString, RegExp this.regExp); Iterator<MutableStringMatch> get iterator => this; @override bool moveNext() { if (mutableString.string.length < this.lastIndex) { return false; //TODO <= ? } Match match = regExp.firstMatch(mutableString.string.substring(lastIndex)); if (match == null) { return false; } _current = new MutableStringMatch(this, match, match.start + lastIndex, match.end + lastIndex, mutableString.string); lastIndex += match.end; return true; } @override MutableStringMatch get current => _current; }
Extends
IterableBase<MutableStringMatch> > MutableStringMatchesIterable
Implements
Iterator<MutableStringMatch>
Constructors
new MutableStringMatchesIterable(MutableString mutableString, RegExp regExp) #
Creates a new Object
instance.
Object
instances have no meaningful state, and are only useful
through their identity. An Object
instance is equal to itself
only.
MutableStringMatchesIterable(MutableString this.mutableString, RegExp this.regExp);
Properties
final MutableStringMatch current #
Returns the current element.
Return null
if the iterator has not yet been moved to the first
element, or if the iterator has been moved after the last element of the
Iterable
.
@override MutableStringMatch get current => _current;
final E first #
Returns the first element.
If this
is empty throws a StateError
. Otherwise this method is
equivalent to this.elementAt(0)
E get first { Iterator it = iterator; if (!it.moveNext()) { throw new StateError("No elements"); } return it.current; }
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty => !iterator.moveNext();
final bool isNotEmpty #
Returns true if there is at least one element in this collection.
bool get isNotEmpty => !isEmpty;
final Iterator<MutableStringMatch> iterator #
Returns an Iterator that iterates over this Iterable object.
Iterator<MutableStringMatch> get iterator => this;
final E last #
E get last { Iterator it = iterator; if (!it.moveNext()) { throw new StateError("No elements"); } E result; do { result = it.current; } while(it.moveNext()); return result; }
int lastIndex #
int lastIndex = 0
final int length #
Returns the number of elements in this
.
Counting all elements may be involve running through all elements and can therefore be slow.
int get length { assert(this is! EfficientLength); int count = 0; Iterator it = iterator; while (it.moveNext()) { count++; } return count; }
final MutableString mutableString #
final MutableString mutableString
final RegExp regExp #
final RegExp regExp
final E single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError
.
E get single { Iterator it = iterator; if (!it.moveNext()) throw new StateError("No elements"); E result = it.current; if (it.moveNext()) throw new StateError("More than one element"); return result; }
Methods
bool any(bool f(E element)) #
Returns true if one element of this collection satisfies the
predicate test
. Returns false otherwise.
bool any(bool f(E element)) { for (E element in this) { if (f(element)) return true; } return false; }
bool contains(Object element) #
Returns true if the collection contains an element equal to element.
bool contains(Object element) { for (E e in this) { if (e == element) return true; } return false; }
E elementAt(int index) #
Returns the indexth element.
If this
has fewer than
index elements throws a RangeError
.
Note: if this
does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this
.
E elementAt(int index) { if (index is! int || index < 0) throw new RangeError.value(index); int remaining = index; for (E element in this) { if (remaining == 0) return element; remaining--; } throw new RangeError.value(index); }
bool every(bool f(E element)) #
Returns true if every elements of this collection satisify the
predicate test
. Returns false
otherwise.
bool every(bool f(E element)) { for (E element in this) { if (!f(element)) return false; } return true; }
Iterable expand(Iterable f(E element)) #
Expands each element of this Iterable
into zero or more elements.
The resulting Iterable runs through the elements returned by f for each element of this, in order.
The returned Iterable
is lazy, and calls
f for each element
of this every time it's iterated.
Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this, f);
dynamic firstWhere(bool test(E value), {Object orElse()}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError
is
thrown.
dynamic firstWhere(bool test(E value), { Object orElse() }) { for (E element in this) { if (test(element)) return element; } if (orElse != null) return orElse(); throw new StateError("No matching element"); }
dynamic fold(initialValue, combine(previousValue, E element)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
dynamic fold(var initialValue, dynamic combine(var previousValue, E element)) { var value = initialValue; for (E element in this) value = combine(value, element); return value; }
void forEach(void f(E element)) #
Applies the function f to each element of this collection.
void forEach(void f(E element)) { for (E element in this) f(element); }
String join([String separator = ""]) #
Converts each element to a String
and concatenates the strings.
Converts each element to a String
by calling Object.toString
on it.
Then concatenates the strings, optionally separated by the
separator
string.
String join([String separator = ""]) { Iterator<E> iterator = this.iterator; if (!iterator.moveNext()) return ""; StringBuffer buffer = new StringBuffer(); if (separator == null || separator == "") { do { buffer.write("${iterator.current}"); } while (iterator.moveNext()); } else { buffer.write("${iterator.current}"); while (iterator.moveNext()) { buffer.write(separator); buffer.write("${iterator.current}"); } } return buffer.toString(); }
dynamic lastWhere(bool test(E value), {Object orElse()}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError
is
thrown.
dynamic lastWhere(bool test(E value), { Object orElse() }) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { result = element; foundMatching = true; } } if (foundMatching) return result; if (orElse != null) return orElse(); throw new StateError("No matching element"); }
Iterable map(f(E element)) #
Returns a lazy Iterable
where each element e
of this
is replaced
by the result of f(e)
.
This method returns a view of the mapped elements. As long as the
returned Iterable
is not iterated over, the supplied function
f will
not be invoked. The transformed elements will not be cached. Iterating
multiple times over the the returned Iterable
will invoke the supplied
function
f multiple times on the same element.
Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
bool moveNext() #
Moves to the next element. Returns true if current contains the next element. Returns false, if no element was left.
It is safe to invoke moveNext even when the iterator is already positioned after the last element. In this case moveNext has no effect.
@override bool moveNext() { if (mutableString.string.length < this.lastIndex) { return false; //TODO <= ? } Match match = regExp.firstMatch(mutableString.string.substring(lastIndex)); if (match == null) { return false; } _current = new MutableStringMatch(this, match, match.start + lastIndex, match.end + lastIndex, mutableString.string); lastIndex += match.end; return true; }
E reduce(E combine(E value, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
E reduce(E combine(E value, E element)) { Iterator<E> iterator = this.iterator; if (!iterator.moveNext()) { throw new StateError("No elements"); } E value = iterator.current; while (iterator.moveNext()) { value = combine(value, iterator.current); } return value; }
E singleWhere(bool test(E value)) #
Returns the single element that satisfies
test. If no or more than one
element match then a StateError
is thrown.
E singleWhere(bool test(E value)) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { if (foundMatching) { throw new StateError("More than one matching element"); } result = element; foundMatching = true; } } if (foundMatching) return result; throw new StateError("No matching element"); }
Iterable<E> skip(int n) #
Returns an Iterable that skips the first n elements.
If this
has fewer than
n elements, then the resulting Iterable is
empty.
It is an error if n is negative.
Iterable<E> skip(int n) { return new SkipIterable<E>(this, n); }
Iterable<E> skipWhile(bool test(E value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable iterates over all elements of this
.
As long as the iterator's elements satisfy
test they are
discarded. Once an element does not satisfy the
test the iterator stops
testing and uses every later element unconditionally. That is, the elements
of the returned Iterable are the elements of this
starting from the
first element that does not satisfy
test.
Iterable<E> skipWhile(bool test(E value)) { return new SkipWhileIterable<E>(this, test); }
Iterable<E> take(int n) #
Returns an Iterable
with at most
n elements.
The returned Iterable
may contain fewer than
n elements, if this
contains fewer than
n elements.
It is an error if n is negative.
Iterable<E> take(int n) { return new TakeIterable<E>(this, n); }
Iterable<E> takeWhile(bool test(E value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable starts iterating over the elements of this
.
When the iterator encounters an element e
that does not satisfy
test,
it discards e
and moves into the finished state. That is, it does not
get or provide any more elements.
Iterable<E> takeWhile(bool test(E value)) { return new TakeWhileIterable<E>(this, test); }
List<E> toList({bool growable: true}) #
Creates a List
containing the elements of this Iterable
.
The elements are in iteration order. The list is fixed-length if growable is false.
List<E> toList({ bool growable: true }) => new List<E>.from(this, growable: growable);
Set<E> toSet() #
Creates a Set
containing the elements of this Iterable
.
Set<E> toSet() => new Set<E>.from(this);
String toString() #
Returns a string representation of (some of) the elements of this
.
Elements are represented by their own toString
results.
The representation always contains the first three elements. If there are less than a hundred elements in the iterable, it also contains the last two elements.
If the resulting string isn't above 80 characters, more elements are included from the start of the iterable.
The conversion may omit calling toString
on some elements if they
are known to now occur in the output, and it may stop iterating after
a hundred elements.
String toString() => _iterableToString(this);
Iterable<E> where(bool f(E element)) #
Returns a lazy Iterable
with all elements that satisfy the
predicate test
.
This method returns a view of the mapped elements. As long as the
returned Iterable
is not iterated over, the supplied function test
will
not be invoked. Iterating will not cache results, and thus iterating
multiple times over the returned Iterable
will invoke the supplied
function test
multiple times on the same element.
Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);