2022-09-18 12:43:16 +05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/dimchansky/utfbom"
|
|
|
|
"golang.org/x/text/encoding/charmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Encoding int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Utf8 Encoding = iota
|
|
|
|
Win1251
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e Encoding) Translate(r io.Reader) (io.Reader, error) {
|
|
|
|
switch e {
|
|
|
|
case Utf8:
|
|
|
|
return utfbom.SkipOnly(r), nil
|
|
|
|
case Win1251:
|
|
|
|
return charmap.Windows1251.NewDecoder().Reader(r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown encoding id = %d", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Encoding) MarshalText() (text []byte, err error) {
|
|
|
|
switch e {
|
|
|
|
case Utf8:
|
|
|
|
return []byte("utf8"), nil
|
|
|
|
case Win1251:
|
|
|
|
return []byte("win1251"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown encoding id = %d", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoding) UnmarshalText(text []byte) error {
|
|
|
|
switch string(text) {
|
|
|
|
case "utf8":
|
|
|
|
*e = Utf8
|
2022-09-21 21:36:40 +05:00
|
|
|
return nil
|
2022-09-18 12:43:16 +05:00
|
|
|
case "win1251":
|
|
|
|
*e = Win1251
|
2022-09-21 21:36:40 +05:00
|
|
|
return nil
|
2022-09-18 12:43:16 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("unknown encoding: %s", string(text))
|
|
|
|
}
|