Skip to content

Commit

Permalink
use Hex instead of AllHex
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Sep 17, 2020
1 parent f13641e commit b23648b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
25 changes: 2 additions & 23 deletions ipaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,31 +264,10 @@ func (ip IPAddress) Bits() string {
return ip.toBinary(".")
}

// Hex is the the same AllHex, but does not contain the prefix 0.
//
// For example, "a0b0c0d".
func (ip IPAddress) Hex() string {
iszero := true
_len := len(ip.ip)
ss := make([]string, 0, _len)
for i := 0; i < _len; i++ {
if ip.ip[i] == 0 {
if iszero {
continue
}
}
if iszero {
iszero = false
}
ss = append(ss, fmt.Sprintf("%02x", ip.ip[i]))
}
return strings.Join(ss, "")
}

// AllHex returns the hexadecimal format of the IP address.
// Hex returns the hexadecimal format of the IP address.
//
// For example, "0a0b0c0d".
func (ip IPAddress) AllHex() string {
func (ip IPAddress) Hex() string {
_len := len(ip.ip)
ss := make([]string, 0, _len)
for i := 0; i < _len; i++ {
Expand Down
4 changes: 2 additions & 2 deletions ipaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func ExampleIPAddress_Network() {
}

func ExampleIPAddress_Hex() {
fmt.Println(MustNewIPAddress("192.168.10.10").Hex())
fmt.Println(MustNewIPAddress("10.11.12.13").Hex())
fmt.Println(MustNewIPAddress("fe80::").Hex())

// Output:
// c0a80a0a
// 0a0b0c0d
// fe800000000000000000000000000000
}

Expand Down

0 comments on commit b23648b

Please sign in to comment.