| 1 |
package charset |
| 2 |
|
| 3 |
import "golang.org/x/text/encoding/charmap" |
| 4 |
|
| 5 |
func mustConvert(b []byte, err error) []byte { |
| 6 |
if err != nil { |
| 7 |
panic(err) |
| 8 |
} |
| 9 |
return b |
| 10 |
} |
| 11 |
|
| 12 |
// ToUTF8 decodes the provided buffer from the ISO8859_1 encoding to UTF8. |
| 13 |
// The result is returned as string. |
| 14 |
// Any error from the decoder will cause a panic. |
| 15 |
// |
| 16 |
// This function hasn't change its signature as it is used in almost any package. |
| 17 |
// To address the issues with panicing errors this package will most likely get |
| 18 |
// a new decoding function which return possible decoding errors. |
| 19 |
func ToUTF8(iso8859_1Buf []byte) string { |
| 20 |
decoder := charmap.ISO8859_1.NewDecoder() |
| 21 |
// TODO: propagate errors |
| 22 |
return string(mustConvert(decoder.Bytes(iso8859_1Buf))) |
| 23 |
} |
| 24 |
|
| 25 |
// ToISO8859_1 encodes the provided utf8String to the ISO8859_1 encoding. |
| 26 |
// The result is returned as byte slice. |
| 27 |
// Any error from the encoder will cause a panic. |
| 28 |
// |
| 29 |
// This function hasn't change its signature as it is used in almost any package. |
| 30 |
// To address the issues with panicing errors this package will most likely get |
| 31 |
// a new encoding function which return possible encoding errors. |
| 32 |
func ToISO8859_1(utf8String string) []byte { |
| 33 |
encoder := charmap.ISO8859_1.NewEncoder() |
| 34 |
// TODO: propagate errors |
| 35 |
return mustConvert(encoder.Bytes([]byte(utf8String))) |
| 36 |
} |