diff --git a/common/stringsupport/string_convert.go b/common/stringsupport/string_convert.go index 337588479..53ed2ec69 100644 --- a/common/stringsupport/string_convert.go +++ b/common/stringsupport/string_convert.go @@ -2,62 +2,14 @@ package stringsupport import ( "bytes" - "io/ioutil" + "io" "strconv" "strings" - "golang.org/x/text/encoding" "golang.org/x/text/encoding/japanese" "golang.org/x/text/transform" ) -// StringConverter is a small helper for encoding/decoding strings. -type StringConverter struct { - Encoding encoding.Encoding -} - -// Decode decodes the given bytes as the set encoding. -func (sc *StringConverter) Decode(data []byte) (string, error) { - decoded, err := ioutil.ReadAll(transform.NewReader(bytes.NewBuffer(data), sc.Encoding.NewDecoder())) - - if err != nil { - return "", err - } - - return string(decoded), nil -} - -// MustDecode decodes the given bytes as the set encoding. Panics on decode failure. -func (sc *StringConverter) MustDecode(data []byte) string { - decoded, err := sc.Decode(data) - if err != nil { - panic(err) - } - - return decoded -} - -// Encode encodes the given string as the set encoding. -func (sc *StringConverter) Encode(data string) ([]byte, error) { - encoded, err := ioutil.ReadAll(transform.NewReader(bytes.NewBuffer([]byte(data)), sc.Encoding.NewEncoder())) - - if err != nil { - return nil, err - } - - return encoded, nil -} - -// MustEncode encodes the given string as the set encoding. Panics on encode failure. -func (sc *StringConverter) MustEncode(data string) []byte { - encoded, err := sc.Encode(data) - if err != nil { - panic(err) - } - - return encoded -} - func UTF8ToSJIS(x string) []byte { e := japanese.ShiftJIS.NewEncoder() xt, _, err := transform.String(e, x) @@ -69,7 +21,7 @@ func UTF8ToSJIS(x string) []byte { func SJISToUTF8(b []byte) string { d := japanese.ShiftJIS.NewDecoder() - result, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(b), d)) + result, err := io.ReadAll(transform.NewReader(bytes.NewReader(b), d)) if err != nil { panic(err) }