update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled

This commit is contained in:
2025-12-08 16:16:23 +01:00
commit c251f174ed
1349 changed files with 194301 additions and 0 deletions

33
utils/ioutils/ioutils.go Normal file
View File

@@ -0,0 +1,33 @@
package ioutils
import (
"io"
"os"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// UTF8Reader wraps an io.Reader to handle Byte Order Mark (BOM) properly.
// It strips UTF-8 BOM if present, and converts UTF-16 (LE/BE) to UTF-8.
// This is particularly useful for reading user-provided text files (like LRC lyrics,
// playlists) that may have been created on Windows, which often adds BOM markers.
//
// Reference: https://en.wikipedia.org/wiki/Byte_order_mark
func UTF8Reader(r io.Reader) io.Reader {
return transform.NewReader(r, unicode.BOMOverride(unicode.UTF8.NewDecoder()))
}
// UTF8ReadFile reads the named file and returns its contents as a byte slice,
// automatically handling BOM markers. It's similar to os.ReadFile but strips
// UTF-8 BOM and converts UTF-16 encoded files to UTF-8.
func UTF8ReadFile(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := UTF8Reader(file)
return io.ReadAll(reader)
}

View File

@@ -0,0 +1,117 @@
package ioutils
import (
"bytes"
"io"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestIOUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "IO Utils Suite")
}
var _ = Describe("UTF8Reader", func() {
Context("when reading text with UTF-8 BOM", func() {
It("strips the UTF-8 BOM marker", func() {
// UTF-8 BOM is EF BB BF
input := []byte{0xEF, 0xBB, 0xBF, 'h', 'e', 'l', 'l', 'o'}
reader := UTF8Reader(bytes.NewReader(input))
output, err := io.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal("hello"))
})
It("strips UTF-8 BOM from multi-line text", func() {
// Test with the actual LRC file format
input := []byte{0xEF, 0xBB, 0xBF, '[', '0', '0', ':', '0', '0', '.', '0', '0', ']', ' ', 't', 'e', 's', 't'}
reader := UTF8Reader(bytes.NewReader(input))
output, err := io.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal("[00:00.00] test"))
})
})
Context("when reading text without BOM", func() {
It("passes through unchanged", func() {
input := []byte("hello world")
reader := UTF8Reader(bytes.NewReader(input))
output, err := io.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal("hello world"))
})
})
Context("when reading UTF-16 LE encoded text", func() {
It("converts to UTF-8 and strips BOM", func() {
// UTF-16 LE BOM (FF FE) followed by "hi" in UTF-16 LE
input := []byte{0xFF, 0xFE, 'h', 0x00, 'i', 0x00}
reader := UTF8Reader(bytes.NewReader(input))
output, err := io.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal("hi"))
})
})
Context("when reading UTF-16 BE encoded text", func() {
It("converts to UTF-8 and strips BOM", func() {
// UTF-16 BE BOM (FE FF) followed by "hi" in UTF-16 BE
input := []byte{0xFE, 0xFF, 0x00, 'h', 0x00, 'i'}
reader := UTF8Reader(bytes.NewReader(input))
output, err := io.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal("hi"))
})
})
Context("when reading empty content", func() {
It("returns empty string", func() {
reader := UTF8Reader(bytes.NewReader([]byte{}))
output, err := io.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal(""))
})
})
})
var _ = Describe("UTF8ReadFile", func() {
Context("when reading a file with UTF-8 BOM", func() {
It("strips the BOM marker", func() {
// Use the actual fixture from issue #4631
contents, err := UTF8ReadFile("../../tests/fixtures/bom-test.lrc")
Expect(err).ToNot(HaveOccurred())
// Should NOT start with BOM
Expect(contents[0]).ToNot(Equal(byte(0xEF)))
// Should start with '['
Expect(contents[0]).To(Equal(byte('[')))
Expect(string(contents)).To(HavePrefix("[00:00.00]"))
})
})
Context("when reading a file without BOM", func() {
It("reads the file normally", func() {
contents, err := UTF8ReadFile("../../tests/fixtures/test.lrc")
Expect(err).ToNot(HaveOccurred())
// Should contain the expected content
Expect(string(contents)).To(ContainSubstring("We're no strangers to love"))
})
})
Context("when reading a non-existent file", func() {
It("returns an error", func() {
_, err := UTF8ReadFile("../../tests/fixtures/nonexistent.lrc")
Expect(err).To(HaveOccurred())
})
})
})