parse static method

Ip6Address parse(
  1. String source
)
override

Parses either Ip4Address or Ip6Address.

Implementation

static Ip6Address parse(String source) {
  final result = ByteData(16);
  final middle = source.indexOf('::');
  List<String> prefixParts = const <String>[];
  List<String> suffixParts = const <String>[];
  if (middle < 0) {
    prefixParts = source.split(':');
  } else {
    if (middle != 0) {
      prefixParts = source.substring(0, middle).split(':');
    }
    if (middle + 2 != source.length) {
      suffixParts = source.substring(middle + 2).split(':');
    }
  }
  if (prefixParts.length + suffixParts.length > 8) {
    throw ArgumentError.value(source, 'source', 'too many numbers');
  }
  var i = 0;
  for (var item in prefixParts) {
    try {
      result.setUint16(i, int.parse(item, radix: 16));
    } catch (e) {
      throw ArgumentError.value(source, 'source', "problem with '$item'");
    }
    i += 2;
  }
  i = 16 - suffixParts.length * 2;
  for (var item in suffixParts) {
    try {
      result.setUint16(i, int.parse(item, radix: 16));
    } catch (e) {
      throw ArgumentError.value(source, 'source', "problem with '$item'");
    }
    i += 2;
  }
  return Ip6Address.decode(RawReader.withByteData(result));
}