isProbablyIpv4 function
Implementation
bool isProbablyIpv4(String hostname) {
// Cannot be shorter than 1.1.1.1
if (hostname.length < 7) {
return false;
}
// Cannot be longer than: 255.255.255.255
if (hostname.length > 15) {
return false;
}
int numberOfDots = 0;
for (int i = 0; i < hostname.length; i++) {
int code = hostname.codeUnitAt(i);
if (code == 46) {
// '.'
numberOfDots++;
} else if (code < 48 || code > 57) {
// '0' and '9'
return false;
}
}
return (numberOfDots == 3 &&
hostname.codeUnitAt(0) != 46 && // '.'
hostname.codeUnitAt(hostname.length - 1) != 46 // '.'
);
}