Skip to content

Commit 6830e4d

Browse files
committed
feat: implement byteorder put
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 24adfc4 commit 6830e4d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "internal/byteorder"
4+
5+
func main() {
6+
buf := make([]byte, 8)
7+
byteorder.BEPutUint64(buf, 0x0102030405060708)
8+
println("BEPutUint64: buf[0]=", buf[0], "buf[7]=", buf[7])
9+
10+
buf32 := make([]byte, 4)
11+
byteorder.BEPutUint32(buf32, 0x01020304)
12+
println("BEPutUint32: buf[0]=", buf32[0], "buf[3]=", buf32[3])
13+
14+
println("byteorder put test finished")
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
"allowImportingTsExtensions": true,
4+
"lib": [
5+
"es2022",
6+
"esnext.disposable",
7+
"dom"
8+
],
9+
"module": "nodenext",
10+
"moduleResolution": "nodenext",
11+
"noEmit": true,
12+
"paths": {
13+
"*": [
14+
"./*"
15+
],
16+
"@goscript/*": [
17+
"../../../gs/*",
18+
"../../../compliance/deps/*"
19+
],
20+
"@goscript/github.com/aperturerobotics/goscript/compliance/tests/byteorder_put/*": [
21+
"./*"
22+
]
23+
},
24+
"sourceMap": true,
25+
"target": "es2022"
26+
},
27+
"extends": "../../../tsconfig.json",
28+
"files": []
29+
}

gs/internal/byteorder/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,43 @@ export function LEUint64(b: $.Bytes): number {
3838
let high = LEUint32($.goSlice(b, 4, undefined))
3939
return low + high * 0x100000000
4040
}
41+
42+
// Big Endian Put functions
43+
export function BEPutUint16(b: $.Bytes, v: number): void {
44+
b![0] = (v >> 8) & 0xff
45+
b![1] = v & 0xff
46+
}
47+
48+
export function BEPutUint32(b: $.Bytes, v: number): void {
49+
b![0] = (v >> 24) & 0xff
50+
b![1] = (v >> 16) & 0xff
51+
b![2] = (v >> 8) & 0xff
52+
b![3] = v & 0xff
53+
}
54+
55+
export function BEPutUint64(b: $.Bytes, v: number): void {
56+
const high = Math.floor(v / 0x100000000)
57+
const low = v >>> 0
58+
BEPutUint32(b, high)
59+
BEPutUint32($.goSlice(b, 4, undefined), low)
60+
}
61+
62+
// Little Endian Put functions
63+
export function LEPutUint16(b: $.Bytes, v: number): void {
64+
b![0] = v & 0xff
65+
b![1] = (v >> 8) & 0xff
66+
}
67+
68+
export function LEPutUint32(b: $.Bytes, v: number): void {
69+
b![0] = v & 0xff
70+
b![1] = (v >> 8) & 0xff
71+
b![2] = (v >> 16) & 0xff
72+
b![3] = (v >> 24) & 0xff
73+
}
74+
75+
export function LEPutUint64(b: $.Bytes, v: number): void {
76+
const low = v >>> 0
77+
const high = Math.floor(v / 0x100000000)
78+
LEPutUint32(b, low)
79+
LEPutUint32($.goSlice(b, 4, undefined), high)
80+
}

0 commit comments

Comments
 (0)