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

     1  package mahonia
     2  
     3  import (
     4  	"unicode/utf8"
     5  )
     6  
     7  // Converters for the EUC-JP encoding
     8  
     9  func init() {
    10  	RegisterCharset(&Charset{
    11  		Name:    "EUC-JP",
    12  		Aliases: []string{"extended_unix_code_packed_format_for_japanese", "cseucpkdfmtjapanese"},
    13  		NewDecoder: func() Decoder {
    14  			return decodeEucJP
    15  		},
    16  		NewEncoder: func() Encoder {
    17  			jis0208Table.Reverse()
    18  			jis0212Table.Reverse()
    19  			return encodeEucJP
    20  		},
    21  	})
    22  }
    23  
    24  func decodeEucJP(p []byte) (c rune, size int, status Status) {
    25  	if len(p) == 0 {
    26  		return 0, 0, NO_ROOM
    27  	}
    28  
    29  	b := p[0]
    30  	switch {
    31  	case b < 0x80:
    32  		return rune(b), 1, SUCCESS
    33  
    34  	case b == 0x8e:
    35  		if len(p) < 2 {
    36  			return 0, 0, NO_ROOM
    37  		}
    38  		b2 := p[1]
    39  		if b2 < 0xa1 || b2 > 0xdf {
    40  			return utf8.RuneError, 1, INVALID_CHAR
    41  		}
    42  		return rune(b2) + (0xff61 - 0xa1), 2, SUCCESS
    43  
    44  	case b == 0x8f:
    45  		if len(p) < 3 {
    46  			return 0, 0, NO_ROOM
    47  		}
    48  		c, size, status = jis0212Table.DecodeHigh(p[1:3])
    49  		if status == SUCCESS {
    50  			size = 3
    51  		}
    52  		return
    53  
    54  	case 0xa1 <= b && b <= 0xfe:
    55  		return jis0208Table.DecodeHigh(p)
    56  	}
    57  
    58  	return utf8.RuneError, 1, INVALID_CHAR
    59  }
    60  
    61  func encodeEucJP(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  	if 0xff61 <= c && c <= 0xff9f {
    81  		p[0] = 0x8e
    82  		p[1] = byte(c - (0xff61 - 0xa1))
    83  		return 2, SUCCESS
    84  	}
    85  
    86  	size, status = jis0208Table.EncodeHigh(p, c)
    87  	if status == SUCCESS {
    88  		return size, status
    89  	}
    90  
    91  	size, status = jis0212Table.EncodeHigh(p[1:], c)
    92  	switch status {
    93  	case SUCCESS:
    94  		p[0] = 0x8f
    95  		return size + 1, SUCCESS
    96  
    97  	case INVALID_CHAR:
    98  		p[0] = '?'
    99  		return 1, INVALID_CHAR
   100  	}
   101  	return size, status
   102  }