matchPrefixedField method

List<String>? matchPrefixedField(
  1. String prefix,
  2. String rawText,
  3. String endChar,
  4. bool trim,
)

Implementation

List<String>? matchPrefixedField(
  String prefix,
  String rawText,
  String endChar,
  bool trim,
) {
  List<String>? matches;
  int i = 0;
  final int max = rawText.length;
  while (i < max) {
    i = rawText.indexOf(prefix, i);
    if (i < 0) {
      break;
    }
    i += prefix.length; // Skip past this prefix we found to start
    final int start = i; // Found the start of a match here
    bool more = true;
    while (more) {
      i = rawText.indexOf(endChar, i);
      if (i < 0) {
        // No terminating end character? uh, done. Set i such that loop terminates and break
        i = rawText.length;
        more = false;
      } else if (_countPrecedingBackslashes(rawText, i) % 2 != 0) {
        // semicolon was escaped (odd count of preceding backslashes) so continue
        i++;
      } else {
        // found a match
        matches ??= [];
        String element = unescapeBackslash(rawText.substring(start, i));
        if (trim) {
          element = element.trim();
        }
        if (element.isNotEmpty) {
          matches.add(element);
        }
        i++;
        more = false;
      }
    }
  }
  if (matches == null || matches.isEmpty) {
    return null;
  }
  return matches.toList();
}