decodeString static method
Decode a packet String (JSON data)
@param {String} str @return {Object} packet @api private
Implementation
static decodeString(String str) {
var i = 0;
var endLen = str.length - 1;
// look up type
var p = <String, dynamic>{'type': num.parse(str[0])};
if (null == PacketTypes[p['type']]) return error();
// look up attachments if type binary
if (BINARY_EVENT == p['type'] || BINARY_ACK == p['type']) {
var buf = '';
while (str[++i] != '-') {
buf += str[i];
if (i == endLen) break;
}
if (buf != '${num.tryParse(buf) ?? -1}' || str[i] != '-') {
throw new ArgumentError('Illegal attachments');
}
p['attachments'] = num.parse(buf);
}
// look up namespace (if any)
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'] = '/';
}
// look up id
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;
}
// p['id'] = p['id'];
}
// look up json data
if (i < endLen - 1 && str[++i].isNotEmpty == true) {
p = tryParse(p, str.substring(i));
}
// debug('decoded %s as %j', str, p);
return p;
}