decodeFromIndex function

dynamic decodeFromIndex(
  1. String str
)

Implementation

dynamic decodeFromIndex(String str) {
  String strToParse = "";
  _Encoder? encoder;
  Queue<List<dynamic>> valueStack = Queue();
  Queue<_Encoder> collectionStack = Queue();

  for (final c in str.split("")) {
    // no encoder, mean a fresh start
    if (encoder == null) {
      // encounter delimiter means end collection
      // else build the new encoder
      if (c == RESERVED_DELIMITER) {
        final group = collectionStack.removeLast();
        final values = valueStack.removeLast();
        final result = group.decode(values);
        // if the root is collection type, return as result
        // else add it the parent collection value
        if (collectionStack.isEmpty) {
          return result;
        } else {
          valueStack.last.add(result);
        }
        continue;
      }
      encoder = _getEncoderByCollationType(c);
      // if the encoder is collection type, add to stack and start collection
      if (encoder is _ArrEncoder || encoder is _ObjEncoder) {
        valueStack.add([]);
        collectionStack.addLast(encoder);
        strToParse = '';
        encoder = null;
      }
      continue;
    }

    // reached the end of string, time to run encoder
    if (c == RESERVED_DELIMITER) {
      final value = encoder.decode(strToParse);

      // if the value is for the collection, add to stack
      // else return
      if (collectionStack.isNotEmpty) {
        valueStack.last.add(value);
      } else {
        return value;
      }
      strToParse = '';
      encoder = null;
      continue;
    }
    strToParse += c;
  }
}