miller/pkg/lib/latin1.go
John Kerl 268a96d002
Export library code in pkg/ (#1391)
* Export library code in `pkg/`

* new doc page
2023-09-10 17:15:13 -04:00

38 lines
744 B
Go

package lib
import (
"bytes"
"fmt"
"unicode/utf8"
)
func TryLatin1ToUTF8(input string) (string, error) {
var buffer bytes.Buffer
for _, b := range []byte(input) {
// 0x00-0xff map to 0x0000-0xffff
buffer.WriteRune(rune(b))
}
output := buffer.String()
return output, nil
}
func TryUTF8ToLatin1(input string) (string, error) {
var buffer bytes.Buffer
bytes := []byte(input)
for len(bytes) > 0 {
r, size := utf8.DecodeRune(bytes)
if r < 0x0080 {
buffer.WriteByte(byte(r))
} else if r >= 0x80 && r <= 0x00ff {
buffer.WriteByte(byte(r))
} else {
return "", fmt.Errorf("character 0x%08x (%v) is not encodable as Latin-1", int(r), r)
}
bytes = bytes[size:]
}
output := buffer.String()
return output, nil
}