summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/text/encoding/simplifiedchinese
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/text/encoding/simplifiedchinese')
-rw-r--r--vendor/golang.org/x/text/encoding/simplifiedchinese/gbk.go60
-rw-r--r--vendor/golang.org/x/text/encoding/simplifiedchinese/hzgb2312.go49
2 files changed, 51 insertions, 58 deletions
diff --git a/vendor/golang.org/x/text/encoding/simplifiedchinese/gbk.go b/vendor/golang.org/x/text/encoding/simplifiedchinese/gbk.go
index e0b15bbc..b89c45b0 100644
--- a/vendor/golang.org/x/text/encoding/simplifiedchinese/gbk.go
+++ b/vendor/golang.org/x/text/encoding/simplifiedchinese/gbk.go
@@ -5,7 +5,6 @@
package simplifiedchinese
import (
- "errors"
"unicode/utf8"
"golang.org/x/text/encoding"
@@ -40,11 +39,6 @@ var gbk18030 = internal.Encoding{
identifier.GB18030,
}
-var (
- errInvalidGB18030 = errors.New("simplifiedchinese: invalid GB18030 encoding")
- errInvalidGBK = errors.New("simplifiedchinese: invalid GBK encoding")
-)
-
type gbkDecoder struct {
transform.NopResetter
gb18030 bool
@@ -66,8 +60,12 @@ loop:
case c0 < 0xff:
if nSrc+1 >= len(src) {
- err = transform.ErrShortSrc
- break loop
+ if !atEOF {
+ err = transform.ErrShortSrc
+ break loop
+ }
+ r, size = utf8.RuneError, 1
+ goto write
}
c1 := src[nSrc+1]
switch {
@@ -77,18 +75,24 @@ loop:
c1 -= 0x41
case d.gb18030 && 0x30 <= c1 && c1 < 0x40:
if nSrc+3 >= len(src) {
- err = transform.ErrShortSrc
- break loop
+ if !atEOF {
+ err = transform.ErrShortSrc
+ break loop
+ }
+ // The second byte here is always ASCII, so we can set size
+ // to 1 in all cases.
+ r, size = utf8.RuneError, 1
+ goto write
}
c2 := src[nSrc+2]
if c2 < 0x81 || 0xff <= c2 {
- err = errInvalidGB18030
- break loop
+ r, size = utf8.RuneError, 1
+ goto write
}
c3 := src[nSrc+3]
if c3 < 0x30 || 0x3a <= c3 {
- err = errInvalidGB18030
- break loop
+ r, size = utf8.RuneError, 1
+ goto write
}
size = 4
r = ((rune(c0-0x81)*10+rune(c1-0x30))*126+rune(c2-0x81))*10 + rune(c3-0x30)
@@ -109,17 +113,13 @@ loop:
r -= 189000
if 0 <= r && r < 0x100000 {
r += 0x10000
- goto write
- }
- err = errInvalidGB18030
- break loop
- default:
- if d.gb18030 {
- err = errInvalidGB18030
} else {
- err = errInvalidGBK
+ r, size = utf8.RuneError, 1
}
- break loop
+ goto write
+ default:
+ r, size = utf8.RuneError, 1
+ goto write
}
r, size = '\ufffd', 2
if i := int(c0-0x81)*190 + int(c1); i < len(decode) {
@@ -130,12 +130,7 @@ loop:
}
default:
- if d.gb18030 {
- err = errInvalidGB18030
- } else {
- err = errInvalidGBK
- }
- break loop
+ r, size = utf8.RuneError, 1
}
write:
@@ -145,13 +140,6 @@ loop:
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
- if atEOF && err == transform.ErrShortSrc {
- if d.gb18030 {
- err = errInvalidGB18030
- } else {
- err = errInvalidGBK
- }
- }
return nDst, nSrc, err
}
diff --git a/vendor/golang.org/x/text/encoding/simplifiedchinese/hzgb2312.go b/vendor/golang.org/x/text/encoding/simplifiedchinese/hzgb2312.go
index 85de6b1e..eb3157f0 100644
--- a/vendor/golang.org/x/text/encoding/simplifiedchinese/hzgb2312.go
+++ b/vendor/golang.org/x/text/encoding/simplifiedchinese/hzgb2312.go
@@ -5,7 +5,6 @@
package simplifiedchinese
import (
- "errors"
"unicode/utf8"
"golang.org/x/text/encoding"
@@ -31,8 +30,6 @@ func hzGB2312NewEncoder() transform.Transformer {
return new(hzGB2312Encoder)
}
-var errInvalidHZGB2312 = errors.New("simplifiedchinese: invalid HZ-GB2312 encoding")
-
const (
asciiState = iota
gbState
@@ -50,14 +47,18 @@ loop:
for ; nSrc < len(src); nSrc += size {
c0 := src[nSrc]
if c0 >= utf8.RuneSelf {
- err = errInvalidHZGB2312
- break loop
+ r, size = utf8.RuneError, 1
+ goto write
}
if c0 == '~' {
if nSrc+1 >= len(src) {
- err = transform.ErrShortSrc
- break loop
+ if !atEOF {
+ err = transform.ErrShortSrc
+ break loop
+ }
+ r = utf8.RuneError
+ goto write
}
size = 2
switch src[nSrc+1] {
@@ -78,8 +79,8 @@ loop:
case '\n':
continue
default:
- err = errInvalidHZGB2312
- break loop
+ r = utf8.RuneError
+ goto write
}
}
@@ -87,33 +88,37 @@ loop:
r, size = rune(c0), 1
} else {
if nSrc+1 >= len(src) {
- err = transform.ErrShortSrc
- break loop
+ if !atEOF {
+ err = transform.ErrShortSrc
+ break loop
+ }
+ r, size = utf8.RuneError, 1
+ goto write
}
+ size = 2
c1 := src[nSrc+1]
if c0 < 0x21 || 0x7e <= c0 || c1 < 0x21 || 0x7f <= c1 {
- err = errInvalidHZGB2312
- break loop
- }
-
- r, size = '\ufffd', 2
- if i := int(c0-0x01)*190 + int(c1+0x3f); i < len(decode) {
+ // error
+ } else if i := int(c0-0x01)*190 + int(c1+0x3f); i < len(decode) {
r = rune(decode[i])
- if r == 0 {
- r = '\ufffd'
+ if r != 0 {
+ goto write
}
}
+ if c1 > utf8.RuneSelf {
+ // Be consistent and always treat non-ASCII as a single error.
+ size = 1
+ }
+ r = utf8.RuneError
}
+ write:
if nDst+utf8.RuneLen(r) > len(dst) {
err = transform.ErrShortDst
break loop
}
nDst += utf8.EncodeRune(dst[nDst:], r)
}
- if atEOF && err == transform.ErrShortSrc {
- err = errInvalidHZGB2312
- }
return nDst, nSrc, err
}