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