C++版本
bool checkIpV4(const char *address) {
if(address == nullptr || reinterpret_cast<const char *>(*address) == "" )
return false;
unsigned short holdplace = 0;
unsigned char count = 0, c = 0, p = 0XFF;
while (c = (unsigned char)*address++) {
if (c == '.') {
if (++count > 3 || p == 0XFF)
return false;
holdplace = 0;
p = 0XFF;
continue;
}
c -= '0';
holdplace *= 10;
holdplace += c;
if (holdplace > 0XFF || c > 9)
return false;
p = c;
}
return holdplace <= 0XFF && p != 0XFF;
}
GO版本
func CheckIPV4Address(address *string) bool {
if address == nil || *address == "" {
return false
}
for _, val := range *address{
if val == '.'{
if Count++; Count > 3 || P == 0xFF {
return false
}
Holdplace = 0
P = 0xFF
continue
}
val -= '0'
Holdplace *= 10
Holdplace += val
if Holdplace > 0xFF || val > 9 {
return false
}
P = uint(val)
}
return Holdplace <= 0XFF && P != 0XFF
}
func CheckIPV4Addres(address *string) bool {
if address == nil || *address == "" {
return false
}
buff := strings.Split(*address,".")
if len(buff) != 4 {
return false
}
for _, val := range buff {
if address,err := strconv.Atoi(val); err != nil || address > 0XFF {
return false
}
}
return true
}