Skip to content

Commit

Permalink
just return the 0 to make this hotpath faster
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Feb 28, 2025
1 parent a1279a5 commit 85b5d50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
12 changes: 2 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,8 @@ func ParseIp(ip string, ipv4Mask, ipv6Mask int) (string, string) {
// For IPv4 addresses
if parsedIP.To4() != nil {
mask := net.CIDRMask(ipv4Mask, 32)
network := parsedIP.Mask(mask)
subnet := []string{}
for _, octet := range strings.Split(network.String(), ".") {
if octet == "0" {
break
}
subnet = append(subnet, octet)
}

return ip, strings.Join(subnet, ".")
subnet := parsedIP.Mask(mask)
return ip, subnet.String()
}

// For IPv6 addresses
Expand Down
10 changes: 5 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,35 +141,35 @@ func TestParseIp(t *testing.T) {
ip: "192.168.1.1",
ipv4Mask: 8,
wantFull: "192.168.1.1",
wantSubnet: "192",
wantSubnet: "192.0.0.0",
},
{
name: "IPv4 /10",
ip: "192.168.1.1",
ipv4Mask: 10,
wantFull: "192.168.1.1",
wantSubnet: "192.128",
wantSubnet: "192.128.0.0",
},
{
name: "IPv4 /16",
ip: "192.168.1.1",
ipv4Mask: 16,
wantFull: "192.168.1.1",
wantSubnet: "192.168",
wantSubnet: "192.168.0.0",
},
{
name: "IPv4 /20",
ip: "192.168.1.1",
ipv4Mask: 20,
wantFull: "192.168.1.1",
wantSubnet: "192.168",
wantSubnet: "192.168.0.0",
},
{
name: "IPv4 /24",
ip: "192.168.1.1",
ipv4Mask: 24,
wantFull: "192.168.1.1",
wantSubnet: "192.168.1",
wantSubnet: "192.168.1.0",
},
{
name: "IPv4 /32",
Expand Down

0 comments on commit 85b5d50

Please sign in to comment.