Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mejedi
Copy link
Contributor

@mejedi mejedi commented Feb 5, 2025

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.

@mejedi mejedi requested review from dylandreimerink, florianl and a team as code owners February 5, 2025 09:32
@mejedi mejedi force-pushed the switch-to-binary-nativeendian branch from ec4f104 to bf868b1 Compare February 5, 2025 09:32
@mejedi mejedi marked this pull request as draft February 5, 2025 09:59
@mejedi mejedi force-pushed the switch-to-binary-nativeendian branch 4 times, most recently from 46516de to dfa9d1a Compare February 5, 2025 11:36
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]>
@mejedi mejedi force-pushed the switch-to-binary-nativeendian branch from dfa9d1a to 2ea765f Compare February 5, 2025 11:42
Comment on lines +5 to +19
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
}
Copy link
Contributor Author

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.

Copy link
Member

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 :)

@mejedi mejedi marked this pull request as ready for review February 5, 2025 12:05
Copy link
Member

@dylandreimerink dylandreimerink left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants