...

Source file src/golang.org/x/text/encoding/korean/euckr.go

Documentation: golang.org/x/text/encoding/korean

     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 korean
     6  
     7  import (
     8  	"unicode/utf8"
     9  
    10  	"golang.org/x/text/encoding"
    11  	"golang.org/x/text/encoding/internal"
    12  	"golang.org/x/text/encoding/internal/identifier"
    13  	"golang.org/x/text/transform"
    14  )
    15  
    16  // All is a list of all defined encodings in this package.
    17  var All = []encoding.Encoding{EUCKR}
    18  
    19  // EUCKR is the EUC-KR encoding, also known as Code Page 949.
    20  var EUCKR encoding.Encoding = &eucKR
    21  
    22  var eucKR = internal.Encoding{
    23  	&internal.SimpleEncoding{eucKRDecoder{}, eucKREncoder{}},
    24  	"EUC-KR",
    25  	identifier.EUCKR,
    26  }
    27  
    28  type eucKRDecoder struct{ transform.NopResetter }
    29  
    30  func (eucKRDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
    31  	r, size := 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  				break
    46  			}
    47  			c1 := src[nSrc+1]
    48  			size = 2
    49  			if c0 < 0xc7 {
    50  				r = 178 * rune(c0-0x81)
    51  				switch {
    52  				case 0x41 <= c1 && c1 < 0x5b:
    53  					r += rune(c1) - (0x41 - 0*26)
    54  				case 0x61 <= c1 && c1 < 0x7b:
    55  					r += rune(c1) - (0x61 - 1*26)
    56  				case 0x81 <= c1 && c1 < 0xff:
    57  					r += rune(c1) - (0x81 - 2*26)
    58  				default:
    59  					goto decError
    60  				}
    61  			} else if 0xa1 <= c1 && c1 < 0xff {
    62  				r = 178*(0xc7-0x81) + rune(c0-0xc7)*94 + rune(c1-0xa1)
    63  			} else {
    64  				goto decError
    65  			}
    66  			if int(r) < len(decode) {
    67  				r = rune(decode[r])
    68  				if r != 0 {
    69  					break
    70  				}
    71  			}
    72  		decError:
    73  			r = utf8.RuneError
    74  			if c1 < utf8.RuneSelf {
    75  				size = 1
    76  			}
    77  
    78  		default:
    79  			r, size = utf8.RuneError, 1
    80  			break
    81  		}
    82  
    83  		if nDst+utf8.RuneLen(r) > len(dst) {
    84  			err = transform.ErrShortDst
    85  			break
    86  		}
    87  		nDst += utf8.EncodeRune(dst[nDst:], r)
    88  	}
    89  	return nDst, nSrc, err
    90  }
    91  
    92  type eucKREncoder struct{ transform.NopResetter }
    93  
    94  func (eucKREncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
    95  	r, size := rune(0), 0
    96  	for ; nSrc < len(src); nSrc += size {
    97  		r = rune(src[nSrc])
    98  
    99  		// Decode a 1-byte rune.
   100  		if r < utf8.RuneSelf {
   101  			size = 1
   102  
   103  			if nDst >= len(dst) {
   104  				err = transform.ErrShortDst
   105  				break
   106  			}
   107  			dst[nDst] = uint8(r)
   108  			nDst++
   109  			continue
   110  
   111  		} else {
   112  			// Decode a multi-byte rune.
   113  			r, size = utf8.DecodeRune(src[nSrc:])
   114  			if size == 1 {
   115  				// All valid runes of size 1 (those below utf8.RuneSelf) were
   116  				// handled above. We have invalid UTF-8 or we haven't seen the
   117  				// full character yet.
   118  				if !atEOF && !utf8.FullRune(src[nSrc:]) {
   119  					err = transform.ErrShortSrc
   120  					break
   121  				}
   122  			}
   123  
   124  			// func init checks that the switch covers all tables.
   125  			switch {
   126  			case encode0Low <= r && r < encode0High:
   127  				if r = rune(encode0[r-encode0Low]); r != 0 {
   128  					goto write2
   129  				}
   130  			case encode1Low <= r && r < encode1High:
   131  				if r = rune(encode1[r-encode1Low]); r != 0 {
   132  					goto write2
   133  				}
   134  			case encode2Low <= r && r < encode2High:
   135  				if r = rune(encode2[r-encode2Low]); r != 0 {
   136  					goto write2
   137  				}
   138  			case encode3Low <= r && r < encode3High:
   139  				if r = rune(encode3[r-encode3Low]); r != 0 {
   140  					goto write2
   141  				}
   142  			case encode4Low <= r && r < encode4High:
   143  				if r = rune(encode4[r-encode4Low]); r != 0 {
   144  					goto write2
   145  				}
   146  			case encode5Low <= r && r < encode5High:
   147  				if r = rune(encode5[r-encode5Low]); r != 0 {
   148  					goto write2
   149  				}
   150  			case encode6Low <= r && r < encode6High:
   151  				if r = rune(encode6[r-encode6Low]); r != 0 {
   152  					goto write2
   153  				}
   154  			}
   155  			err = internal.ErrASCIIReplacement
   156  			break
   157  		}
   158  
   159  	write2:
   160  		if nDst+2 > len(dst) {
   161  			err = transform.ErrShortDst
   162  			break
   163  		}
   164  		dst[nDst+0] = uint8(r >> 8)
   165  		dst[nDst+1] = uint8(r)
   166  		nDst += 2
   167  		continue
   168  	}
   169  	return nDst, nSrc, err
   170  }
   171  
   172  func init() {
   173  	// Check that the hard-coded encode switch covers all tables.
   174  	if numEncodeTables != 7 {
   175  		panic("bad numEncodeTables")
   176  	}
   177  }
   178  

View as plain text