From cd151081c008722868062b522ae2076b4df8d312 Mon Sep 17 00:00:00 2001 From: b5f0d6c3 Date: Fri, 6 May 2022 14:08:24 +0800 Subject: update mkvlib:add utf-16le support --- mkvlib/utils.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'mkvlib/utils.go') diff --git a/mkvlib/utils.go b/mkvlib/utils.go index 590ed2e..85ef657 100644 --- a/mkvlib/utils.go +++ b/mkvlib/utils.go @@ -3,6 +3,9 @@ package mkvlib import ( "errors" "fmt" + "github.com/gogs/chardet" + "golang.org/x/text/encoding/unicode" + "golang.org/x/text/transform" "io" "math/rand" "os" @@ -213,3 +216,13 @@ func randomStr(l int) string { } return string(result) } + +func toUTF8(data []byte) string { + d := chardet.NewTextDetector() + if r, err := d.DetectBest(data); err == nil { + if r.Charset == "UTF-16LE" { + data, _, _ = transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder(), data) + } + } + return string(data) +} -- cgit v1.2.1