MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / String

Method String

pkg/util/ipaddr/ipaddr.go:114–134  ·  view source on GitHub ↗

String will convert the IP to the appropriate family formatted string representation. In order to retain postgres compatibility we ensure IPv4-mapped IPv6 stays in IPv6 format, unlike net.Addr.String().

()

Source from the content-addressed store, hash-verified

112// representation. In order to retain postgres compatibility we ensure
113// IPv4-mapped IPv6 stays in IPv6 format, unlike net.Addr.String().
114func (ipAddr IPAddr) String() string {
115 ip := net.IP(uint128.Uint128(ipAddr.Addr).GetBytes())
116 isIPv4MappedIPv6 := ipAddr.Family == IPv6family && ip.Equal(ip.To4())
117 var maskSize byte
118
119 if ipAddr.Family == IPv4family {
120 maskSize = 32
121 } else {
122 maskSize = 128
123 }
124 if ipAddr.Mask == maskSize && isIPv4MappedIPv6 {
125 return "::ffff:" + ip.String()
126 } else if ipAddr.Mask == maskSize {
127 return ip.String()
128 } else if isIPv4MappedIPv6 {
129 // Due to an issue with IPv4-mapped IPv6 the mask is also reduced to an IPv4
130 // mask during net.IPNet.String, so we need to add the mask manually.
131 return "::ffff:" + ip.String() + "/" + strconv.Itoa(int(ipAddr.Mask))
132 }
133 return ip.String() + "/" + strconv.Itoa(int(ipAddr.Mask))
134}
135
136// Compare two IPAddrs. IPv4-mapped IPv6 addresses are not equal to their IPv4
137// mapping. The order of order importance goes Family > Mask > IP-bytes.

Callers 1

PrettyPrintValueEncodedFunction · 0.95

Calls 4

Uint128Struct · 0.92
StringMethod · 0.65
GetBytesMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected