omq/encodings.go
2024-03-10 12:45:02 +05:00

37 lines
721 B
Go

package main
import (
"fmt"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
)
type Encoding string
const (
EncodingUtf8 Encoding = "UTF-8"
EncodingWin1251 Encoding = "Win-1251"
)
func (e Encoding) Encoder() (*encoding.Encoder, error) {
switch e {
case EncodingWin1251:
return charmap.Windows1251.NewEncoder(), nil
case EncodingUtf8:
return &encoding.Encoder{Transformer: new(DummyTransformer)}, nil
}
return nil, fmt.Errorf("unknown encoding: %s", string(e))
}
type DummyTransformer struct{}
func (e *DummyTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
copy(dst, src)
return len(src), len(src), nil
}
func (e *DummyTransformer) Reset() {}