toString method
Dotted quad for IPv4; RFC 5952 form for IPv6.
Implementation
@override
String toString() {
if (isIPv4) return _bytes.join('.');
final List<int> groups = <int>[
for (int i = 0; i < 16; i += 2) (_bytes[i] << 8) | _bytes[i + 1],
];
// The longest run of two or more zero groups, the first on a tie.
int bestStart = -1;
int bestLength = 1;
for (int i = 0; i < 8;) {
if (groups[i] != 0) {
i++;
continue;
}
int j = i;
while (j < 8 && groups[j] == 0) {
j++;
}
if (j - i > bestLength) {
bestStart = i;
bestLength = j - i;
}
i = j;
}
String hex(Iterable<int> part) =>
part.map((int g) => g.toRadixString(16)).join(':');
if (bestStart < 0) return hex(groups);
return '${hex(groups.take(bestStart))}::'
'${hex(groups.skip(bestStart + bestLength))}';
}