parseTupleRegExp function

List<String> parseTupleRegExp(
  1. String text
)

returns a list of strings from a tuple string

'(a, b, c)' => 'a','b','c' '((a, b), c)' => '(a, b)', 'c' '(a, (b, c))' => 'a', '(b, c)' '(a, (b, c), d)' => 'a', '(b, c)', 'd' '(a, (b, c), (d, e))' => 'a', '(b, c)', '(d, e)' '((a, b), (c, d))' => '(a, b)', '(c, d)'

Implementation

List<String> parseTupleRegExp(String text) {
  text = text.substring(1, text.length - 1);

  final List<String> result = <String>[];
  String buffer = '';

  int count = 0;

  for (var index = 0; index < text.length; index++) {
    final String char = text[index];
    if (char == '(') {
      count++;
    } else if (char == ')') {
      count--;
    }

    if (count == 0 &&
        char == ',' &&
        index != text.length - 1 &&
        text[index + 1] == ' ') {
      result.add(buffer);
      buffer = '';
      index++;
    } else {
      buffer += char;
    }
  }
  result.add(buffer);
  return result;
}