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

     1  package mahonia
     2  
     3  // Converters for GBK encoding.
     4  
     5  func init() {
     6  	RegisterCharset(&Charset{
     7  		Name:    "GBK",
     8  		Aliases: []string{"GB2312"}, // GBK is a superset of GB2312.
     9  		NewDecoder: func() Decoder {
    10  			return decodeGBKRune
    11  		},
    12  		NewEncoder: func() Encoder {
    13  			return encodeGBKRune
    14  		},
    15  	})
    16  }
    17  
    18  func decodeGBKRune(p []byte) (r rune, size int, status Status) {
    19  	if len(p) == 0 {
    20  		status = NO_ROOM
    21  		return
    22  	}
    23  
    24  	b := p[0]
    25  	if b < 128 {
    26  		return rune(b), 1, SUCCESS
    27  	}
    28  
    29  	if len(p) < 2 {
    30  		status = NO_ROOM
    31  		return
    32  	}
    33  
    34  	c := uint16(p[0])<<8 + uint16(p[1])
    35  	r = rune(gbkToUnicode[c])
    36  	if r == 0 {
    37  		r = gbkToUnicodeExtra[c]
    38  	}
    39  
    40  	if r != 0 {
    41  		return r, 2, SUCCESS
    42  	}
    43  
    44  	return 0xfffd, 1, INVALID_CHAR
    45  }
    46  
    47  func encodeGBKRune(p []byte, r rune) (size int, status Status) {
    48  	if len(p) == 0 {
    49  		status = NO_ROOM
    50  		return
    51  	}
    52  
    53  	if r < 128 {
    54  		p[0] = byte(r)
    55  		return 1, SUCCESS
    56  	}
    57  
    58  	if len(p) < 2 {
    59  		status = NO_ROOM
    60  		return
    61  	}
    62  
    63  	var c uint16
    64  	if r < 0x10000 {
    65  		c = unicodeToGBK[r]
    66  	} else {
    67  		c = unicodeToGBKExtra[r]
    68  	}
    69  
    70  	if c != 0 {
    71  		p[0] = byte(c >> 8)
    72  		p[1] = byte(c)
    73  		return 2, SUCCESS
    74  	}
    75  
    76  	p[0] = 0x1a
    77  	return 1, INVALID_CHAR
    78  }