package utf16

import (
	"encoding/binary"
	"fmt"
	"unicode/utf8"
)

func FromUTF8(data []byte) (res []byte) {
	endian := binary.LittleEndian

	res = make([]byte, (len(data)+1)*2)

	res = res[:2]
	endian.PutUint16(res, 0xfeff)

	for len(data) > 0 {
		r, size := utf8.DecodeRune(data)
		if r > 65535 {
			panic(fmt.Errorf("r=0x%x > 0xffff", r))
		}

		slen := len(res)
		res = res[:slen+2]
		endian.PutUint16(res[slen:], uint16(r))
		data = data[size:]
	}
	return
}
