MessageSequence.parse constructor

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

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

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

Implementation

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