浏览文件
strutil.go
28 行 · 799 B历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package strutil |
| 5 | |
| 6 | import ( |
| 7 | "io" |
| 8 | |
| 9 | "golang.org/x/text/encoding/unicode" |
| 10 | "golang.org/x/text/transform" |
| 11 | ) |
| 12 | |
| 13 | // FromUTF16le returns an io.Reader for UTF16le data. |
| 14 | // Windows uses little endian by default, use unicode.UseBOM policy to retrieve BOM from the text, |
| 15 | // and unicode.LittleEndian as a fallback. |
| 16 | func FromUTF16le(r io.Reader) io.Reader { |
| 17 | return transform.NewReader(r, unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()) |
| 18 | } |
| 19 | |
| 20 | // FromUTF16leToString reads from Unicode 16 LE encoded data from an io.Reader and returns a string. |
| 21 | func FromUTF16leToString(r io.Reader) (string, error) { |
| 22 | out, err := io.ReadAll(FromUTF16le(r)) |
| 23 | if err != nil { |
| 24 | return "", err |
| 25 | } |
| 26 | |
| 27 | return string(out), nil |
| 28 | } |

