From b23648b9fb66e4299b7eb18c0e32827d8203866e Mon Sep 17 00:00:00 2001 From: xgfone Date: Thu, 17 Sep 2020 21:36:15 +0800 Subject: [PATCH] use Hex instead of AllHex --- ipaddr.go | 25 ++----------------------- ipaddr_test.go | 4 ++-- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/ipaddr.go b/ipaddr.go index 32bf122..818644a 100644 --- a/ipaddr.go +++ b/ipaddr.go @@ -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++ { diff --git a/ipaddr_test.go b/ipaddr_test.go index a8d839b..787e399 100644 --- a/ipaddr_test.go +++ b/ipaddr_test.go @@ -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 }