-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipaddress.go
49 lines (44 loc) · 872 Bytes
/
ipaddress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package strings
import (
"math"
"strconv"
"strings"
)
func ValidIpAddress(s string) []string {
var r, p = []string{}, make([]string, 4)
first:
for i := 1; i < min(4, len(s)); i++ {
p[0] = s[:i]
if !validPart(p[0]) {
continue first
}
second:
for j := 1; j < min(4, len(s)-i); j++ {
p[1] = s[i : i+j]
if !validPart(p[1]) {
continue second
}
for k := 1; k < min(4, len(s)-i-j); k++ {
p[2], p[3] = s[i+j:i+j+k], s[i+j+k:]
if validPart(p[2]) && validPart(p[3]) {
r = append(r, strings.Join(p, "."))
}
}
}
}
return r
}
func min(x, y int) int {
return int(math.Min(float64(x), float64(y)))
}
func validPart(s string) bool {
// "00", "000'", "01", etc. are not valid, but '0' is valid
if len(s) == 1 {
return true
}
if s[0] == '0' {
return false
}
i, err := strconv.Atoi(s)
return err == nil && i <= 255
}