mirror of
https://github.com/soypat/lneto.git
synced 2026-08-20 14:39:02 +00:00
Further improvements to httphi API (#173)
* add Exchange.WriteBodyString * Mux.MaxPathValues and other improvements * Mux PathValue improvemnt and fixes * MuxSlice more method muxing improvements * diagram out interesting approach to form parsing for clanker * refactor RequestParseForm and achieve greatness in API design * explicit naming of headerCapacityKV value in kvBuffer.Reset * fix Mux bug not matching paths correctly; httpraw HTTP V1 naming applied * rename many examples,use httphi in examples,remove useless maxAwaitingConn field * add ipv4.String * add ipv4 UnspecifiedAddr and BroadcastAddr * add ethernet.String
This commit is contained in:
@@ -25,6 +25,15 @@ const (
|
||||
MinimumMTU = MinimumFrameLength - sizeHeaderNoVLAN
|
||||
)
|
||||
|
||||
// String returns the colon-separated hexadecimal text representation of a hardware address.
|
||||
func String(hwAddr [6]byte) string {
|
||||
// See net/netip's (Addr).string4 pattern.
|
||||
var buf [maxAddrStringLen]byte
|
||||
return string(AppendAddr(buf[:0], hwAddr))
|
||||
}
|
||||
|
||||
const maxAddrStringLen = len("ff:ff:ff:ff:ff:ff")
|
||||
|
||||
// AppendAddr appends the text representation of the hardware address to the destination buffer.
|
||||
func AppendAddr(dst []byte, hwAddr [6]byte) []byte {
|
||||
for i, b := range hwAddr {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package ethernet
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
addr [6]byte
|
||||
want string
|
||||
}{
|
||||
{addr: [6]byte{}, want: "00:00:00:00:00:00"},
|
||||
{addr: [6]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, want: "ff:ff:ff:ff:ff:ff"},
|
||||
{addr: [6]byte{0xde, 0xad, 0xbe, 0xef, 0x00, 0x01}, want: "de:ad:be:ef:00:01"},
|
||||
{addr: [6]byte{0x01, 0x00, 0x5e, 0x7f, 0x00, 0x0f}, want: "01:00:5e:7f:00:0f"},
|
||||
} {
|
||||
got := String(tc.addr)
|
||||
if got != tc.want {
|
||||
t.Errorf("String(%v): got %q, want %q", tc.addr, got, tc.want)
|
||||
}
|
||||
if want := net.HardwareAddr(tc.addr[:]).String(); got != want {
|
||||
t.Errorf("String(%v) disagrees with net: got %q, want %q", tc.addr, got, want)
|
||||
}
|
||||
if got := string(AppendAddr(nil, tc.addr)); got != tc.want {
|
||||
t.Errorf("AppendAddr(%v): got %q, want %q", tc.addr, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestString_singleAlloc(t *testing.T) {
|
||||
addr := [6]byte{0xde, 0xad, 0xbe, 0xef, 0x00, 0x01}
|
||||
var sink string
|
||||
allocs := testing.AllocsPerRun(100, func() {
|
||||
sink = String(addr)
|
||||
})
|
||||
_ = sink
|
||||
// Only the returned string allocates; the scratch buffer must stay on the stack.
|
||||
if allocs != 1 {
|
||||
t.Errorf("expected 1 alloc, got %v", allocs)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user