-
Notifications
You must be signed in to change notification settings - Fork 713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace internal.NativeEndian with stdlib #1669
base: main
Are you sure you want to change the base?
Conversation
ec4f104
to
bf868b1
Compare
46516de
to
dfa9d1a
Compare
Yet another attempt at replacing internal.NativeEndian with NativeEndian from "encoding/binary". The conversion is mostly a straight-forward replacement. The only challenge are checks whether a ByteOrder is Big/Little/NativeEndian. In stdlib, NativeEndian is a distinct type hence never compares equal to BigEndian or LittleEndian. Introduce internal.EqualByteOrder(bo1, bo2). It works by passing []byte{0x12, 0x34} through bo1.Uint16() and bo2.Uint16() and comparing the results. Signed-off-by: Nick Zavaritsky <[email protected]>
dfa9d1a
to
2ea765f
Compare
func endianMark(bo binary.ByteOrder) uint16 { return bo.Uint16([]byte{0x12, 0x34}) } | ||
|
||
func EqualByteOrder(bo1, bo2 binary.ByteOrder) bool { | ||
return endianMark(bo1) == endianMark(bo2) | ||
} | ||
|
||
func NormalizeByteOrder(bo binary.ByteOrder) binary.ByteOrder { | ||
if EqualByteOrder(bo, binary.BigEndian) { | ||
return binary.BigEndian | ||
} | ||
if EqualByteOrder(bo, binary.LittleEndian) { | ||
return binary.LittleEndian | ||
} | ||
return bo | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One might argue that EqualByteOrder
is not stringent enough. Indeed, one can implement e.g. PDPEndian
. EqualByteOrder
will claim that PDPEndian
is equal to LittleEndian
.
However, there's neither Golang nor eBPF available on PDP 11. "Weird" byte orders are probably just a theoretical concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I think this is good enough. I do not expect PDP 11 related issues :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems alright to me. Thank you!
Yet another attempt at replacing
internal.NativeEndian
withNativeEndian
from"encoding/binary"
. The conversion is mostly a straight-forward replacement. The only challenge are checks whether a ByteOrder is Big/Little/NativeEndian. In stdlib, NativeEndian is a distinct type hence never compares equal to BigEndian or LittleEndian.Introduce
internal.EqualByteOrder(bo1, bo2)
. It works by passing[]byte{0x12, 0x34}
throughbo1.Uint16()
andbo2.Uint16()
and comparing the results.