-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic: Detect char-encoding and convert to UTF-8
- Loading branch information
1 parent
97b43ec
commit fd9bd11
Showing
5 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: go | ||
|
||
go: | ||
- 1.9.2 | ||
- 1.9.x | ||
|
||
env: | ||
- DEP_VERSION="0.3.2" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package mklink | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
|
||
"github.com/saintfish/chardet" | ||
"golang.org/x/text/encoding/japanese" | ||
"golang.org/x/text/transform" | ||
) | ||
|
||
//CharEncode is type of character encoding | ||
type CharEncode int | ||
|
||
const ( | ||
//CharUnknown is unknown character | ||
CharUnknown CharEncode = iota | ||
//CharUTF8 is UTF-8 | ||
CharUTF8 | ||
//CharISO8859_1 is ISO-8859-1 | ||
CharISO8859_1 | ||
//CharShiftJIS is Shift-JIS | ||
CharShiftJIS | ||
//CharEUCJP is EUC-JP | ||
CharEUCJP | ||
//CharISO2022JP is ISO-2022-JP | ||
CharISO2022JP | ||
) | ||
|
||
var ( | ||
charEncodeMap = map[CharEncode]string{ | ||
CharUTF8: "UTF-8", | ||
CharISO8859_1: "ISO-8859-1", | ||
CharShiftJIS: "Shift_JIS", | ||
CharEUCJP: "EUC-JP", | ||
CharISO2022JP: "ISO-2022-JP", | ||
} | ||
) | ||
|
||
//TypeofCharEncode returns CharEncode from string | ||
func TypeofCharEncode(s string) CharEncode { | ||
for key, value := range charEncodeMap { | ||
if value == s { | ||
return key | ||
} | ||
} | ||
return CharUnknown | ||
} | ||
|
||
func (e CharEncode) String() string { | ||
if name, ok := charEncodeMap[e]; ok { | ||
return name | ||
} | ||
return "unknown" | ||
|
||
} | ||
|
||
//DetectCharEncode returns character encoding | ||
func DetectCharEncode(body []byte) CharEncode { | ||
det := chardet.NewTextDetector() | ||
res, err := det.DetectBest(body) | ||
if err != nil { | ||
return CharUnknown | ||
} | ||
//fmt.Println(res.Charset) | ||
return TypeofCharEncode(res.Charset) | ||
} | ||
|
||
//ToUTF8 returns string with UTF-8 encoding | ||
func ToUTF8(body []byte) string { | ||
var trans transform.Transformer | ||
switch DetectCharEncode(body) { | ||
case CharUTF8, CharISO8859_1: | ||
return string(body) | ||
case CharShiftJIS: | ||
trans = japanese.ShiftJIS.NewDecoder() | ||
case CharEUCJP: | ||
trans = japanese.EUCJP.NewDecoder() | ||
case CharISO2022JP: | ||
trans = japanese.ISO2022JP.NewDecoder() | ||
default: | ||
return "" | ||
} | ||
r := transform.NewReader(bytes.NewReader(body), trans) | ||
buf := new(bytes.Buffer) | ||
io.Copy(buf, r) | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters