github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/mahonia/euc-kr.go (about)

     1  package mahonia
     2  
     3  // Converters for the EUC-KR encoding.
     4  
     5  import (
     6  	"unicode/utf8"
     7  )
     8  
     9  func init() {
    10  	RegisterCharset(&Charset{
    11  		Name: "EUC-KR",
    12  		Aliases: []string{
    13  			"ibm-1363",
    14  			"KS_C_5601-1987",
    15  			"KS_C_5601-1989",
    16  			"KSC_5601",
    17  			"Korean",
    18  			"iso-ir-149",
    19  			"cp1363",
    20  			"5601",
    21  			"ksc",
    22  			"windows-949",
    23  			"ibm-970",
    24  			"cp970",
    25  			"970",
    26  			"cp949",
    27  		},
    28  		NewDecoder: func() Decoder {
    29  			return decodeEucKr
    30  		},
    31  		NewEncoder: func() Encoder {
    32  			eucKrOnce.Do(reverseEucKrTable)
    33  			return encodeEucKr
    34  		},
    35  	})
    36  }
    37  
    38  func decodeEucKr(p []byte) (c rune, size int, status Status) {
    39  	if len(p) == 0 {
    40  		return 0, 0, NO_ROOM
    41  	}
    42  
    43  	b := p[0]
    44  	if b < 0x80 {
    45  		return rune(b), 1, SUCCESS
    46  	}
    47  
    48  	if len(p) < 2 {
    49  		return 0, 0, NO_ROOM
    50  	}
    51  
    52  	euc := int(b)<<8 + int(p[1])
    53  	c = rune(eucKrToUnicode[euc])
    54  
    55  	if c == 0 {
    56  		return utf8.RuneError, 2, INVALID_CHAR
    57  	}
    58  	return c, 2, SUCCESS
    59  }
    60  
    61  func encodeEucKr(p []byte, c rune) (size int, status Status) {
    62  	if len(p) == 0 {
    63  		return 0, NO_ROOM
    64  	}
    65  
    66  	if c < 0x80 {
    67  		p[0] = byte(c)
    68  		return 1, SUCCESS
    69  	}
    70  
    71  	if len(p) < 2 {
    72  		return 0, NO_ROOM
    73  	}
    74  
    75  	if c > 0xffff {
    76  		p[0] = '?'
    77  		return 1, INVALID_CHAR
    78  	}
    79  
    80  	euc := unicodeToEucKr[c]
    81  	if euc == 0 {
    82  		p[0] = '?'
    83  		return 1, INVALID_CHAR
    84  	}
    85  
    86  	p[0] = byte(euc >> 8)
    87  	p[1] = byte(euc)
    88  	return 2, SUCCESS
    89  }