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) {
  var i = 0;
  final list = <int>[];
  final codeUnits = sequence.codeUnits;
  final replacementCodes = replacement.codeUnits;
  while (i < codeUnits.length) {
    final codeUnit = codeUnits[i];
    if (match(codeUnit)) {
      do {
        i++;
      } while (i < codeUnits.length && match(codeUnits[i]));
      list.addAll(replacementCodes);
    } else {
      list.add(codeUnit);
      i++;
    }
  }
  return String.fromCharCodes(list);
}