Skip to content

Commit

Permalink
net: use stringslite package for string operations
Browse files Browse the repository at this point in the history
- Replace manual string suffix removal with stringslite.TrimSuffix in conf.go
- Use stringslite.Cut for string splitting in ParseCIDR function in ip.go
- Add stringslite import in ip.go

This change simplifies string operations and improves code readability.
  • Loading branch information
aimuz committed May 17, 2024
1 parent 2f64268 commit aef5dc5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/net/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
}

// Canonicalize the hostname by removing any trailing dot.
if stringslite.HasSuffix(hostname, ".") {
hostname = hostname[:len(hostname)-1]
}
hostname = stringslite.TrimSuffix(hostname, ".")

nss := getSystemNSS()
srcs := nss.sources["hosts"]
Expand Down
6 changes: 3 additions & 3 deletions src/net/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package net
import (
"internal/bytealg"
"internal/itoa"
"internal/stringslite"
"net/netip"
)

Expand Down Expand Up @@ -515,11 +516,10 @@ func parseIP(s string) ([16]byte, bool) {
// For example, ParseCIDR("192.0.2.1/24") returns the IP address
// 192.0.2.1 and the network 192.0.2.0/24.
func ParseCIDR(s string) (IP, *IPNet, error) {
i := bytealg.IndexByteString(s, '/')
if i < 0 {
addr, mask, found := stringslite.Cut(s, "/")
if !found {
return nil, nil, &ParseError{Type: "CIDR address", Text: s}
}
addr, mask := s[:i], s[i+1:]

ipAddr, err := netip.ParseAddr(addr)
if err != nil || ipAddr.Zone() != "" {
Expand Down

0 comments on commit aef5dc5

Please sign in to comment.