collapseFrom method

String collapseFrom(
  1. String sequence,
  2. String replacement
)

Replaces each group of consecutive matched characters in sequence with the specified replacement.

Implementation

String collapseFrom(String sequence, String replacement) {
  final result = <int>[];
  final iterator = _runeIteratorAt(sequence, 0);
  while (iterator.moveNext()) {
    if (match(iterator.current)) {
      while (iterator.moveNext() && match(iterator.current)) {}
      result.addAll(replacement.runes);
      iterator.movePrevious();
    } else {
      result.add(iterator.current);
    }
  }
  return String.fromCharCodes(result);
}