aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary
diff options
context:
space:
mode:
authorcuiweixie <cuiweixie@gmail.com>2023-01-25 11:22:29 +0800
committerGopher Robot <gobot@golang.org>2023-01-27 18:17:20 +0000
commit4b3726e99bec62e4a8b8e9cecc478b51ce0d4636 (patch)
treeeff88a8a92e83d3d1f85b3fd5959e8dd25ef6f38 /src/encoding/binary
parent092671423cd95eaa6df93eb29442fef41504d097 (diff)
downloadgo-4b3726e99bec62e4a8b8e9cecc478b51ce0d4636.tar.xz
encoding/binary: add var NativeEndian
Updates #57237 Change-Id: I149c8b7eeac91b87b5810250f96d48ca87135807 Reviewed-on: https://go-review.googlesource.com/c/go/+/463218 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: xie cui <523516579@qq.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/encoding/binary')
-rw-r--r--src/encoding/binary/binary_test.go10
-rw-r--r--src/encoding/binary/native_endian_big.go14
-rw-r--r--src/encoding/binary/native_endian_little.go14
3 files changed, 38 insertions, 0 deletions
diff --git a/src/encoding/binary/binary_test.go b/src/encoding/binary/binary_test.go
index 09d08f5ee3..341cd86766 100644
--- a/src/encoding/binary/binary_test.go
+++ b/src/encoding/binary/binary_test.go
@@ -13,6 +13,7 @@ import (
"strings"
"sync"
"testing"
+ "unsafe"
)
type Struct struct {
@@ -831,3 +832,12 @@ func BenchmarkWriteSlice1000Uint8s(b *testing.B) {
Write(w, BigEndian, slice)
}
}
+
+func TestNativeEndian(t *testing.T) {
+ const val = 0x12345678
+ i := uint32(val)
+ s := unsafe.Slice((*byte)(unsafe.Pointer(&i)), unsafe.Sizeof(i))
+ if v := NativeEndian.Uint32(s); v != val {
+ t.Errorf("NativeEndian.Uint32 returned %#x, expected %#x", v, val)
+ }
+}
diff --git a/src/encoding/binary/native_endian_big.go b/src/encoding/binary/native_endian_big.go
new file mode 100644
index 0000000000..1a24354f4b
--- /dev/null
+++ b/src/encoding/binary/native_endian_big.go
@@ -0,0 +1,14 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build armbe || arm64be || m68k || mips || mips64 || mips64p32 || ppc || ppc64 || s390 || s390x || shbe || sparc || sparc64
+
+package binary
+
+type nativeEndian struct {
+ bigEndian
+}
+
+// NativeEndian is the native-endian implementation of ByteOrder and AppendByteOrder.
+var NativeEndian nativeEndian
diff --git a/src/encoding/binary/native_endian_little.go b/src/encoding/binary/native_endian_little.go
new file mode 100644
index 0000000000..67b41ae0a2
--- /dev/null
+++ b/src/encoding/binary/native_endian_little.go
@@ -0,0 +1,14 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build 386 || amd64 || amd64p32 || alpha || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || nios2 || ppc64le || riscv || riscv64 || sh || wasm
+
+package binary
+
+type nativeEndian struct {
+ littleEndian
+}
+
+// NativeEndian is the native-endian implementation of ByteOrder and AppendByteOrder.
+var NativeEndian nativeEndian