github.com/go-xe2/third@v1.0.3/golang.org/x/text/encoding/traditionalchinese/big5.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package traditionalchinese
     6  
     7  import (
     8  	"unicode/utf8"
     9  
    10  	"github.com/go-xe2/third/golang.org/x/text/encoding"
    11  	"github.com/go-xe2/third/golang.org/x/text/encoding/internal"
    12  	"github.com/go-xe2/third/golang.org/x/text/encoding/internal/identifier"
    13  	"github.com/go-xe2/third/golang.org/x/text/transform"
    14  )
    15  
    16  // All is a list of all defined encodings in this package.
    17  var All = []encoding.Encoding{Big5}
    18  
    19  // Big5 is the Big5 encoding, also known as Code Page 950.
    20  var Big5 encoding.Encoding = &big5
    21  
    22  var big5 = internal.Encoding{
    23  	&internal.SimpleEncoding{big5Decoder{}, big5Encoder{}},
    24  	"Big5",
    25  	identifier.Big5,
    26  }
    27  
    28  type big5Decoder struct{ transform.NopResetter }
    29  
    30  func (big5Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
    31  	r, size, s := rune(0), 0, ""
    32  loop:
    33  	for ; nSrc < len(src); nSrc += size {
    34  		switch c0 := src[nSrc]; {
    35  		case c0 < utf8.RuneSelf:
    36  			r, size = rune(c0), 1
    37  
    38  		case 0x81 <= c0 && c0 < 0xff:
    39  			if nSrc+1 >= len(src) {
    40  				if !atEOF {
    41  					err = transform.ErrShortSrc
    42  					break loop
    43  				}
    44  				r, size = utf8.RuneError, 1
    45  				goto write
    46  			}
    47  			c1 := src[nSrc+1]
    48  			switch {
    49  			case 0x40 <= c1 && c1 < 0x7f:
    50  				c1 -= 0x40
    51  			case 0xa1 <= c1 && c1 < 0xff:
    52  				c1 -= 0x62
    53  			case c1 < 0x40:
    54  				r, size = utf8.RuneError, 1
    55  				goto write
    56  			default:
    57  				r, size = utf8.RuneError, 2
    58  				goto write
    59  			}
    60  			r, size = '\ufffd', 2
    61  			if i := int(c0-0x81)*157 + int(c1); i < len(decode) {
    62  				if 1133 <= i && i < 1167 {
    63  					// The two-rune special cases for LATIN CAPITAL / SMALL E WITH CIRCUMFLEX
    64  					// AND MACRON / CARON are from http://encoding.spec.whatwg.org/#big5
    65  					switch i {
    66  					case 1133:
    67  						s = "\u00CA\u0304"
    68  						goto writeStr
    69  					case 1135:
    70  						s = "\u00CA\u030C"
    71  						goto writeStr
    72  					case 1164:
    73  						s = "\u00EA\u0304"
    74  						goto writeStr
    75  					case 1166:
    76  						s = "\u00EA\u030C"
    77  						goto writeStr
    78  					}
    79  				}
    80  				r = rune(decode[i])
    81  				if r == 0 {
    82  					r = '\ufffd'
    83  				}
    84  			}
    85  
    86  		default:
    87  			r, size = utf8.RuneError, 1
    88  		}
    89  
    90  	write:
    91  		if nDst+utf8.RuneLen(r) > len(dst) {
    92  			err = transform.ErrShortDst
    93  			break loop
    94  		}
    95  		nDst += utf8.EncodeRune(dst[nDst:], r)
    96  		continue loop
    97  
    98  	writeStr:
    99  		if nDst+len(s) > len(dst) {
   100  			err = transform.ErrShortDst
   101  			break loop
   102  		}
   103  		nDst += copy(dst[nDst:], s)
   104  		continue loop
   105  	}
   106  	return nDst, nSrc, err
   107  }
   108  
   109  type big5Encoder struct{ transform.NopResetter }
   110  
   111  func (big5Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
   112  	r, size := rune(0), 0
   113  	for ; nSrc < len(src); nSrc += size {
   114  		r = rune(src[nSrc])
   115  
   116  		// Decode a 1-byte rune.
   117  		if r < utf8.RuneSelf {
   118  			size = 1
   119  			if nDst >= len(dst) {
   120  				err = transform.ErrShortDst
   121  				break
   122  			}
   123  			dst[nDst] = uint8(r)
   124  			nDst++
   125  			continue
   126  
   127  		} else {
   128  			// Decode a multi-byte rune.
   129  			r, size = utf8.DecodeRune(src[nSrc:])
   130  			if size == 1 {
   131  				// All valid runes of size 1 (those below utf8.RuneSelf) were
   132  				// handled above. We have invalid UTF-8 or we haven't seen the
   133  				// full character yet.
   134  				if !atEOF && !utf8.FullRune(src[nSrc:]) {
   135  					err = transform.ErrShortSrc
   136  					break
   137  				}
   138  			}
   139  		}
   140  
   141  		if r >= utf8.RuneSelf {
   142  			// func init checks that the switch covers all tables.
   143  			switch {
   144  			case encode0Low <= r && r < encode0High:
   145  				if r = rune(encode0[r-encode0Low]); r != 0 {
   146  					goto write2
   147  				}
   148  			case encode1Low <= r && r < encode1High:
   149  				if r = rune(encode1[r-encode1Low]); r != 0 {
   150  					goto write2
   151  				}
   152  			case encode2Low <= r && r < encode2High:
   153  				if r = rune(encode2[r-encode2Low]); r != 0 {
   154  					goto write2
   155  				}
   156  			case encode3Low <= r && r < encode3High:
   157  				if r = rune(encode3[r-encode3Low]); r != 0 {
   158  					goto write2
   159  				}
   160  			case encode4Low <= r && r < encode4High:
   161  				if r = rune(encode4[r-encode4Low]); r != 0 {
   162  					goto write2
   163  				}
   164  			case encode5Low <= r && r < encode5High:
   165  				if r = rune(encode5[r-encode5Low]); r != 0 {
   166  					goto write2
   167  				}
   168  			case encode6Low <= r && r < encode6High:
   169  				if r = rune(encode6[r-encode6Low]); r != 0 {
   170  					goto write2
   171  				}
   172  			case encode7Low <= r && r < encode7High:
   173  				if r = rune(encode7[r-encode7Low]); r != 0 {
   174  					goto write2
   175  				}
   176  			}
   177  			err = internal.ErrASCIIReplacement
   178  			break
   179  		}
   180  
   181  	write2:
   182  		if nDst+2 > len(dst) {
   183  			err = transform.ErrShortDst
   184  			break
   185  		}
   186  		dst[nDst+0] = uint8(r >> 8)
   187  		dst[nDst+1] = uint8(r)
   188  		nDst += 2
   189  		continue
   190  	}
   191  	return nDst, nSrc, err
   192  }
   193  
   194  func init() {
   195  	// Check that the hard-coded encode switch covers all tables.
   196  	if numEncodeTables != 8 {
   197  		panic("bad numEncodeTables")
   198  	}
   199  }