@@ -966,27 +966,28 @@ func (ip Addr) StringExpanded() string {
966
966
return string (ret )
967
967
}
968
968
969
+ // AppendText implements the [encoding.TextAppender] interface,
970
+ // It is the same as [Addr.AppendTo].
971
+ func (ip Addr ) AppendText (b []byte ) ([]byte , error ) {
972
+ return ip .AppendTo (b ), nil
973
+ }
974
+
969
975
// MarshalText implements the [encoding.TextMarshaler] interface,
970
976
// The encoding is the same as returned by [Addr.String], with one exception:
971
977
// If ip is the zero [Addr], the encoding is the empty string.
972
978
func (ip Addr ) MarshalText () ([]byte , error ) {
979
+ var maxCap int
973
980
switch ip .z {
974
981
case z0 :
975
- return []byte ("" ), nil
976
982
case z4 :
977
- max := len ("255.255.255.255" )
978
- b := make ([]byte , 0 , max )
979
- return ip .appendTo4 (b ), nil
983
+ maxCap = len ("255.255.255.255" )
980
984
default :
985
+ maxCap = len ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0" )
981
986
if ip .Is4In6 () {
982
- max := len ("::ffff:255.255.255.255%enp5s0" )
983
- b := make ([]byte , 0 , max )
984
- return ip .appendTo4In6 (b ), nil
987
+ maxCap = len ("::ffff:255.255.255.255%enp5s0" )
985
988
}
986
- max := len ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0" )
987
- b := make ([]byte , 0 , max )
988
- return ip .appendTo6 (b ), nil
989
989
}
990
+ return ip .AppendText (make ([]byte , 0 , maxCap ))
990
991
}
991
992
992
993
// UnmarshalText implements the encoding.TextUnmarshaler interface.
@@ -1004,30 +1005,37 @@ func (ip *Addr) UnmarshalText(text []byte) error {
1004
1005
return err
1005
1006
}
1006
1007
1007
- func ( ip Addr ) marshalBinaryWithTrailingBytes ( trailingBytes int ) [] byte {
1008
- var b []byte
1008
+ // AppendBinary implements the [encoding.BinaryAppender] interface.
1009
+ func ( ip Addr ) AppendBinary ( b []byte ) ([] byte , error ) {
1009
1010
switch ip .z {
1010
1011
case z0 :
1011
- b = make ([]byte , trailingBytes )
1012
1012
case z4 :
1013
- b = make ([]byte , 4 + trailingBytes )
1014
- byteorder .BePutUint32 (b , uint32 (ip .addr .lo ))
1013
+ b = byteorder .BeAppendUint32 (b , uint32 (ip .addr .lo ))
1015
1014
default :
1016
- z := ip .Zone ()
1017
- b = make ([]byte , 16 + len (z )+ trailingBytes )
1018
- byteorder .BePutUint64 (b [:8 ], ip .addr .hi )
1019
- byteorder .BePutUint64 (b [8 :], ip .addr .lo )
1020
- copy (b [16 :], z )
1015
+ b = byteorder .BeAppendUint64 (b , ip .addr .hi )
1016
+ b = byteorder .BeAppendUint64 (b , ip .addr .lo )
1017
+ b = append (b , ip .Zone ()... )
1018
+ }
1019
+ return b , nil
1020
+ }
1021
+
1022
+ func (ip Addr ) marshalBinarySize () int {
1023
+ switch ip .z {
1024
+ case z0 :
1025
+ return 0
1026
+ case z4 :
1027
+ return 4
1028
+ default :
1029
+ return 16 + len (ip .Zone ())
1021
1030
}
1022
- return b
1023
1031
}
1024
1032
1025
1033
// MarshalBinary implements the [encoding.BinaryMarshaler] interface.
1026
1034
// It returns a zero-length slice for the zero [Addr],
1027
1035
// the 4-byte form for an IPv4 address,
1028
1036
// and the 16-byte form with zone appended for an IPv6 address.
1029
1037
func (ip Addr ) MarshalBinary () ([]byte , error ) {
1030
- return ip .marshalBinaryWithTrailingBytes ( 0 ), nil
1038
+ return ip .AppendBinary ( make ([] byte , 0 , ip . marshalBinarySize ()))
1031
1039
}
1032
1040
1033
1041
// UnmarshalBinary implements the [encoding.BinaryUnmarshaler] interface.
@@ -1198,21 +1206,25 @@ func (p AddrPort) AppendTo(b []byte) []byte {
1198
1206
return b
1199
1207
}
1200
1208
1209
+ // AppendText implements the [encoding.TextAppender] interface. The
1210
+ // encoding is the same as returned by [AddrPort.AppendTo].
1211
+ func (p AddrPort ) AppendText (b []byte ) ([]byte , error ) {
1212
+ return p .AppendTo (b ), nil
1213
+ }
1214
+
1201
1215
// MarshalText implements the [encoding.TextMarshaler] interface. The
1202
1216
// encoding is the same as returned by [AddrPort.String], with one exception: if
1203
1217
// p.Addr() is the zero [Addr], the encoding is the empty string.
1204
1218
func (p AddrPort ) MarshalText () ([]byte , error ) {
1205
- var max int
1219
+ var maxCap int
1206
1220
switch p .ip .z {
1207
1221
case z0 :
1208
1222
case z4 :
1209
- max = len ("255.255.255.255:65535" )
1223
+ maxCap = len ("255.255.255.255:65535" )
1210
1224
default :
1211
- max = len ("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0]:65535" )
1225
+ maxCap = len ("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0]:65535" )
1212
1226
}
1213
- b := make ([]byte , 0 , max )
1214
- b = p .AppendTo (b )
1215
- return b , nil
1227
+ return p .AppendText (make ([]byte , 0 , maxCap ))
1216
1228
}
1217
1229
1218
1230
// UnmarshalText implements the encoding.TextUnmarshaler
@@ -1228,13 +1240,22 @@ func (p *AddrPort) UnmarshalText(text []byte) error {
1228
1240
return err
1229
1241
}
1230
1242
1243
+ // AppendBinary implements the [encoding.BinaryAppendler] interface.
1244
+ // It returns [Addr.AppendBinary] with an additional two bytes appended
1245
+ // containing the port in little-endian.
1246
+ func (p AddrPort ) AppendBinary (b []byte ) ([]byte , error ) {
1247
+ b , err := p .Addr ().AppendBinary (b )
1248
+ if err != nil {
1249
+ return nil , err
1250
+ }
1251
+ return byteorder .LeAppendUint16 (b , p .Port ()), nil
1252
+ }
1253
+
1231
1254
// MarshalBinary implements the [encoding.BinaryMarshaler] interface.
1232
1255
// It returns [Addr.MarshalBinary] with an additional two bytes appended
1233
1256
// containing the port in little-endian.
1234
1257
func (p AddrPort ) MarshalBinary () ([]byte , error ) {
1235
- b := p .Addr ().marshalBinaryWithTrailingBytes (2 )
1236
- byteorder .LePutUint16 (b [len (b )- 2 :], p .Port ())
1237
- return b , nil
1258
+ return p .AppendBinary (make ([]byte , 0 , p .Addr ().marshalBinarySize ()+ 2 ))
1238
1259
}
1239
1260
1240
1261
// UnmarshalBinary implements the [encoding.BinaryUnmarshaler] interface.
@@ -1487,21 +1508,25 @@ func (p Prefix) AppendTo(b []byte) []byte {
1487
1508
return b
1488
1509
}
1489
1510
1511
+ // AppendText implements the [encoding.TextAppender] interface.
1512
+ // It is the same as [Prefix.AppendTo].
1513
+ func (p Prefix ) AppendText (b []byte ) ([]byte , error ) {
1514
+ return p .AppendTo (b ), nil
1515
+ }
1516
+
1490
1517
// MarshalText implements the [encoding.TextMarshaler] interface,
1491
1518
// The encoding is the same as returned by [Prefix.String], with one exception:
1492
1519
// If p is the zero value, the encoding is the empty string.
1493
1520
func (p Prefix ) MarshalText () ([]byte , error ) {
1494
- var max int
1521
+ var maxCap int
1495
1522
switch p .ip .z {
1496
1523
case z0 :
1497
1524
case z4 :
1498
- max = len ("255.255.255.255/32" )
1525
+ maxCap = len ("255.255.255.255/32" )
1499
1526
default :
1500
- max = len ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0/128" )
1527
+ maxCap = len ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0/128" )
1501
1528
}
1502
- b := make ([]byte , 0 , max )
1503
- b = p .AppendTo (b )
1504
- return b , nil
1529
+ return p .AppendText (make ([]byte , 0 , maxCap ))
1505
1530
}
1506
1531
1507
1532
// UnmarshalText implements the encoding.TextUnmarshaler interface.
@@ -1517,13 +1542,23 @@ func (p *Prefix) UnmarshalText(text []byte) error {
1517
1542
return err
1518
1543
}
1519
1544
1545
+ // AppendBinary implements the [encoding.AppendMarshaler] interface.
1546
+ // It returns [Addr.AppendBinary] with an additional byte appended
1547
+ // containing the prefix bits.
1548
+ func (p Prefix ) AppendBinary (b []byte ) ([]byte , error ) {
1549
+ b , err := p .Addr ().withoutZone ().AppendBinary (b )
1550
+ if err != nil {
1551
+ return nil , err
1552
+ }
1553
+ return append (b , uint8 (p .Bits ())), nil
1554
+ }
1555
+
1520
1556
// MarshalBinary implements the [encoding.BinaryMarshaler] interface.
1521
1557
// It returns [Addr.MarshalBinary] with an additional byte appended
1522
1558
// containing the prefix bits.
1523
1559
func (p Prefix ) MarshalBinary () ([]byte , error ) {
1524
- b := p .Addr ().withoutZone ().marshalBinaryWithTrailingBytes (1 )
1525
- b [len (b )- 1 ] = uint8 (p .Bits ())
1526
- return b , nil
1560
+ // without the zone the max length is 16, plus an additional byte is 17
1561
+ return p .AppendBinary (make ([]byte , 0 , p .Addr ().withoutZone ().marshalBinarySize ()+ 1 ))
1527
1562
}
1528
1563
1529
1564
// UnmarshalBinary implements the [encoding.BinaryUnmarshaler] interface.
0 commit comments