MCPcopy
hub / github.com/spf13/pflag / ParseIPv4Mask

Function ParseIPv4Mask

ipmask.go:33–57  ·  view source on GitHub ↗

ParseIPv4Mask written in IP form (e.g. 255.255.255.0). This function should really belong to the net package.

(s string)

Source from the content-addressed store, hash-verified

31// ParseIPv4Mask written in IP form (e.g. 255.255.255.0).
32// This function should really belong to the net package.
33func ParseIPv4Mask(s string) net.IPMask {
34 mask := net.ParseIP(s)
35 if mask == nil {
36 if len(s) != 8 {
37 return nil
38 }
39 // net.IPMask.String() actually outputs things like ffffff00
40 // so write a horrible parser for that as well :-(
41 m := []int{}
42 for i := 0; i < 4; i++ {
43 b := "0x" + s[2*i:2*i+2]
44 d, err := strconv.ParseInt(b, 0, 0)
45 if err != nil {
46 return nil
47 }
48 m = append(m, int(d))
49 }
50 s := fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
51 mask = net.ParseIP(s)
52 if mask == nil {
53 return nil
54 }
55 }
56 return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
57}
58
59func parseIPv4Mask(sval string) (interface{}, error) {
60 mask := ParseIPv4Mask(sval)

Callers 3

SetMethod · 0.85
parseIPv4MaskFunction · 0.85
testParseFunction · 0.85

Calls

no outgoing calls

Tested by 1

testParseFunction · 0.68