parse static method

MessageSequence parse(
  1. String text, {
  2. bool isUidSequence = false,
})

Generates a sequence based on the specified inpput text like 1:10,21,73:79.

Set isUidSequence to true in case this sequence consists of UIDs.

Implementation

static MessageSequence parse(String text, {bool isUidSequence = false}) {
  final sequence = MessageSequence(isUidSequence: isUidSequence);
  final chunks = text.split(',');
  if (chunks[0] == 'NIL') {
    sequence._isNilSequence = true;
    sequence._text = null;
  } else {
    for (final chunk in chunks) {
      final id = int.tryParse(chunk);
      if (id != null) {
        sequence.add(id);
      } else if (chunk == '*') {
        sequence.addLast();
      } else if (chunk.endsWith(':*')) {
        final idText = chunk.substring(0, chunk.length - ':*'.length);
        final id = int.tryParse(idText);
        if (id != null) {
          sequence.addRangeToLast(id);
        } else {
          throw StateError('expect id in $idText for <$chunk> in $text');
        }
      } else {
        final colonIndex = chunk.indexOf(':');
        if (colonIndex == -1) {
          throw StateError('expect colon in  <$chunk> / $text');
        }
        final start = int.tryParse(chunk.substring(0, colonIndex));
        final end = int.tryParse(chunk.substring(colonIndex + 1));
        if (start == null || end == null) {
          throw StateError('expect range in  <$chunk> / $text');
        }
        sequence.addRange(start, end);
      }
    }
  }
  return sequence;
}