summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/saintfish/chardet/utf8.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-07-07 23:34:05 +0200
committerWim <wim@42.be>2017-07-07 23:34:05 +0200
commita0938d93869904ebf6d9938485c248b976150fac (patch)
treea12fad5acdceeec93a28efb600ca62b9fdfb40a5 /vendor/github.com/saintfish/chardet/utf8.go
parent2338c69d402ad3779f4e2a2f38ac800ceca656b9 (diff)
downloadmatterbridge-msglm-a0938d93869904ebf6d9938485c248b976150fac.tar.gz
matterbridge-msglm-a0938d93869904ebf6d9938485c248b976150fac.tar.bz2
matterbridge-msglm-a0938d93869904ebf6d9938485c248b976150fac.zip
Add go-charset and chardet to vendor
Diffstat (limited to 'vendor/github.com/saintfish/chardet/utf8.go')
-rw-r--r--vendor/github.com/saintfish/chardet/utf8.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/vendor/github.com/saintfish/chardet/utf8.go b/vendor/github.com/saintfish/chardet/utf8.go
new file mode 100644
index 00000000..ae036ad9
--- /dev/null
+++ b/vendor/github.com/saintfish/chardet/utf8.go
@@ -0,0 +1,71 @@
+package chardet
+
+import (
+ "bytes"
+)
+
+var utf8Bom = []byte{0xEF, 0xBB, 0xBF}
+
+type recognizerUtf8 struct {
+}
+
+func newRecognizer_utf8() *recognizerUtf8 {
+ return &recognizerUtf8{}
+}
+
+func (*recognizerUtf8) Match(input *recognizerInput) (output recognizerOutput) {
+ output = recognizerOutput{
+ Charset: "UTF-8",
+ }
+ hasBom := bytes.HasPrefix(input.raw, utf8Bom)
+ inputLen := len(input.raw)
+ var numValid, numInvalid uint32
+ var trailBytes uint8
+ for i := 0; i < inputLen; i++ {
+ c := input.raw[i]
+ if c&0x80 == 0 {
+ continue
+ }
+ if c&0xE0 == 0xC0 {
+ trailBytes = 1
+ } else if c&0xF0 == 0xE0 {
+ trailBytes = 2
+ } else if c&0xF8 == 0xF0 {
+ trailBytes = 3
+ } else {
+ numInvalid++
+ if numInvalid > 5 {
+ break
+ }
+ trailBytes = 0
+ }
+
+ for i++; i < inputLen; i++ {
+ c = input.raw[i]
+ if c&0xC0 != 0x80 {
+ numInvalid++
+ break
+ }
+ if trailBytes--; trailBytes == 0 {
+ numValid++
+ break
+ }
+ }
+ }
+
+ if hasBom && numInvalid == 0 {
+ output.Confidence = 100
+ } else if hasBom && numValid > numInvalid*10 {
+ output.Confidence = 80
+ } else if numValid > 3 && numInvalid == 0 {
+ output.Confidence = 100
+ } else if numValid > 0 && numInvalid == 0 {
+ output.Confidence = 80
+ } else if numValid == 0 && numInvalid == 0 {
+ // Plain ASCII
+ output.Confidence = 10
+ } else if numValid > numInvalid*10 {
+ output.Confidence = 25
+ }
+ return
+}