Files
local-server/pkg/utf16/utf16.go
T

30 lines
465 B
Go
Raw Normal View History

2022-05-31 11:52:26 +02:00
package utf16
import (
"encoding/binary"
2026-07-17 11:33:07 +02:00
"fmt"
2022-05-31 11:52:26 +02:00
"unicode/utf8"
)
func FromUTF8(data []byte) (res []byte) {
2026-07-17 11:33:07 +02:00
endian := binary.LittleEndian
2022-05-31 11:52:26 +02:00
res = make([]byte, (len(data)+1)*2)
2026-07-17 11:33:07 +02:00
res = res[:2]
endian.PutUint16(res, 0xfeff)
2022-05-31 11:52:26 +02:00
for len(data) > 0 {
r, size := utf8.DecodeRune(data)
if r > 65535 {
2026-07-17 11:33:07 +02:00
panic(fmt.Errorf("r=0x%x > 0xffff", r))
}
2022-05-31 11:52:26 +02:00
slen := len(res)
res = res[:slen+2]
endian.PutUint16(res[slen:], uint16(r))
data = data[size:]
}
return
}