convertToUTF8 converts data from the given charset to UTF-8. If charset is empty, "utf-8", or "us-ascii", the data is returned as-is.
(data []byte, charset string)
| 364 | // convertToUTF8 converts data from the given charset to UTF-8. |
| 365 | // If charset is empty, "utf-8", or "us-ascii", the data is returned as-is. |
| 366 | func convertToUTF8(data []byte, charset string) []byte { |
| 367 | charset = strings.ToLower(strings.TrimSpace(charset)) |
| 368 | if charset == "" || charset == "utf-8" || charset == "us-ascii" { |
| 369 | return data |
| 370 | } |
| 371 | enc, err := ianaindex.MIME.Encoding(charset) |
| 372 | if err != nil || enc == nil { |
| 373 | return data |
| 374 | } |
| 375 | decoded, err := enc.NewDecoder().Bytes(data) |
| 376 | if err != nil { |
| 377 | return data |
| 378 | } |
| 379 | return decoded |
| 380 | } |
no outgoing calls