You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently code is scattered with type-checks on the parsed input being a String or a List. Having a buffer could avoid this problem and enable further extensibility on the data being parsed.
/// An immutable parse buffer.
abstract class Buffer<E, S> {
///
factory Buffer.from(Object input) {
if (input is String) {
return new _StringBuffer(input, input.length);
} else if (input is List) {
return new _ListBuffer(input, input.length);
}
}
/// Return the underlying data structure.
S data;
/// Return the size of the buffer.
int get size;
/// Return the element at position [value].
E operator [](int index);
/// Return the sequence from [start] to [stop].
S sequence(int start, int stop);
}
class _StringBuffer implements Buffer<int, String> {
@override
final String data;
@override
final int length;
_StringBuffer(this.data, this.length);
@override
int operator [](int index) => data.codeUnitAt(index);
@override
String sequence(int startIndex, int stopIndex) => data.substring(startIndex, stopIndex);
}
class _ListBuffer<T> implements Buffer<T, List<T>> {
@override
final List<T> data;
@override
final int length;
_ListBuffer(this.data, this.length);
@override
T operator [](int index) => data[index];
@override
List<T> sequence(int startIndex, int stopIndex) => data.sublist(startIndex, stopIndex);
}
The text was updated successfully, but these errors were encountered:
Currently code is scattered with type-checks on the parsed input being a
String
or aList
. Having a buffer could avoid this problem and enable further extensibility on the data being parsed.The text was updated successfully, but these errors were encountered: