@@ -23,32 +23,6 @@ import (
23
23
// UTF8BOM is the utf-8 byte-order marker
24
24
var UTF8BOM = []byte {'\xef' , '\xbb' , '\xbf' }
25
25
26
- // ToUTF8WithErr converts content to UTF8 encoding
27
- func ToUTF8WithErr (content []byte ) (string , error ) {
28
- charsetLabel , err := DetectEncoding (content )
29
- if err != nil {
30
- return "" , err
31
- } else if charsetLabel == "UTF-8" {
32
- return string (RemoveBOMIfPresent (content )), nil
33
- }
34
-
35
- encoding , _ := charset .Lookup (charsetLabel )
36
- if encoding == nil {
37
- return string (content ), fmt .Errorf ("Unknown encoding: %s" , charsetLabel )
38
- }
39
-
40
- // If there is an error, we concatenate the nicely decoded part and the
41
- // original left over. This way we won't lose much data.
42
- result , n , err := transform .Bytes (encoding .NewDecoder (), content )
43
- if err != nil {
44
- result = append (result , content [n :]... )
45
- }
46
-
47
- result = RemoveBOMIfPresent (result )
48
-
49
- return string (result ), err
50
- }
51
-
52
26
// ToUTF8WithFallbackReader detects the encoding of content and coverts to UTF-8 reader if possible
53
27
func ToUTF8WithFallbackReader (rd io.Reader ) io.Reader {
54
28
var buf = make ([]byte , 2048 )
@@ -76,6 +50,32 @@ func ToUTF8WithFallbackReader(rd io.Reader) io.Reader {
76
50
)
77
51
}
78
52
53
+ // ToUTF8WithErr converts content to UTF8 encoding
54
+ func ToUTF8WithErr (content []byte ) (string , error ) {
55
+ charsetLabel , err := DetectEncoding (content )
56
+ if err != nil {
57
+ return "" , err
58
+ } else if charsetLabel == "UTF-8" {
59
+ return string (RemoveBOMIfPresent (content )), nil
60
+ }
61
+
62
+ encoding , _ := charset .Lookup (charsetLabel )
63
+ if encoding == nil {
64
+ return string (content ), fmt .Errorf ("Unknown encoding: %s" , charsetLabel )
65
+ }
66
+
67
+ // If there is an error, we concatenate the nicely decoded part and the
68
+ // original left over. This way we won't lose much data.
69
+ result , n , err := transform .Bytes (encoding .NewDecoder (), content )
70
+ if err != nil {
71
+ result = append (result , content [n :]... )
72
+ }
73
+
74
+ result = RemoveBOMIfPresent (result )
75
+
76
+ return string (result ), err
77
+ }
78
+
79
79
// ToUTF8WithFallback detects the encoding of content and coverts to UTF-8 if possible
80
80
func ToUTF8WithFallback (content []byte ) []byte {
81
81
bs , _ := ioutil .ReadAll (ToUTF8WithFallbackReader (bytes .NewReader (content )))
0 commit comments