omq/encodings.go

37 lines
721 B
Go
Raw Permalink Normal View History

2023-11-17 20:34:20 +05:00
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)
2024-03-10 12:45:02 +05:00
return len(src), len(src), nil
2023-11-17 20:34:20 +05:00
}
2024-03-10 12:45:02 +05:00
2023-11-17 20:34:20 +05:00
func (e *DummyTransformer) Reset() {}