decodeString static method

dynamic decodeString(
  1. String str
)

Implementation

static decodeString(String str) {
  var i = 0;
  var endLen = str.length - 1;

  var p = <String, dynamic>{'type': num.parse(str[0])};

  if (null == packetTypes[p['type']]) return error();

  if (binaryEventValue == p['type'] || binaryAckValue == p['type']) {
    var buf = '';
    while (str[++i] != '-') {
      buf += str[i];
      if (i == endLen) break;
    }
    if (buf != '${num.tryParse(buf) ?? -1}' || str[i] != '-') {
      throw ArgumentError('Illegal attachments');
    }
    p['attachments'] = num.parse(buf);
  }

  if (str.length > i + 1 && '/' == str[i + 1]) {
    p['nsp'] = '';
    while (++i > 0) {
      var c = str[i];
      if (',' == c) break;
      p['nsp'] += c;
      if (i == endLen) break;
    }
  } else {
    p['nsp'] = '/';
  }

  var next = i < endLen - 1 ? str[i + 1] : null;
  if (next?.isNotEmpty == true && '${num.tryParse(next!)}' == next) {
    p['id'] = '';
    while (++i > 0) {
      var c = str[i];
      if ('${num.tryParse(c)}' != c) {
        --i;
        break;
      }
      p['id'] += str[i];
      if (i == endLen - 1) break;
    }
  }

  if (i < endLen - 1 && str[++i].isNotEmpty == true) {
    p = tryParse(p, str.substring(i));
  }

  return p;
}