vitess.io/vitess@v0.16.2/go/mysql/collations/internal/charset/simplifiedchinese/gb2312.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package simplifiedchinese
    18  
    19  import (
    20  	"unicode/utf8"
    21  
    22  	"vitess.io/vitess/go/mysql/collations/internal/charset/types"
    23  )
    24  
    25  type Charset_gb2312 struct{}
    26  
    27  func (Charset_gb2312) Name() string {
    28  	return "gb2312"
    29  }
    30  
    31  func (Charset_gb2312) IsSuperset(other types.Charset) bool {
    32  	switch other.(type) {
    33  	case Charset_gb2312:
    34  		return true
    35  	default:
    36  		return false
    37  	}
    38  }
    39  
    40  func (Charset_gb2312) SupportsSupplementaryChars() bool {
    41  	return false
    42  }
    43  
    44  func (Charset_gb2312) EncodeRune(dst []byte, r rune) int {
    45  	switch {
    46  	case r < utf8.RuneSelf:
    47  		dst[0] = byte(r)
    48  		return 1
    49  	case gb2312Encode0min <= r && r <= gb2312Encode0max:
    50  		if r = rune(gb2312Encode0[r-gb2312Encode0min]); r != 0 {
    51  			goto writeGB
    52  		}
    53  	case gb2312Encode1min <= r && r <= gb2312Encode1max:
    54  		if r = rune(gb2312Encode1[r-gb2312Encode1min]); r != 0 {
    55  			goto writeGB
    56  		}
    57  	case gb2312Encode2min <= r && r <= gb2312Encode2max:
    58  		if r = rune(gb2312Encode2[r-gb2312Encode2min]); r != 0 {
    59  			goto writeGB
    60  		}
    61  	case gb2312Encode3min <= r && r <= gb2312Encode3max:
    62  		if r = rune(gb2312Encode3[r-gb2312Encode3min]); r != 0 {
    63  			goto writeGB
    64  		}
    65  	case gb2312Encode4min <= r && r <= gb2312Encode4max:
    66  		if r = rune(gb2312Encode4[r-gb2312Encode4min]); r != 0 {
    67  			goto writeGB
    68  		}
    69  	case gb2312Encode5min <= r && r <= gb2312Encode5max:
    70  		if r = rune(gb2312Encode5[r-gb2312Encode5min]); r != 0 {
    71  			goto writeGB
    72  		}
    73  	case gb2312Encode6min <= r && r <= gb2312Encode6max:
    74  		if r = rune(gb2312Encode6[r-gb2312Encode6min]); r != 0 {
    75  			goto writeGB
    76  		}
    77  	case gb2312Encode7min <= r && r <= gb2312Encode7max:
    78  		if r = rune(gb2312Encode7[r-gb2312Encode7min]); r != 0 {
    79  			goto writeGB
    80  		}
    81  	case gb2312Encode8min <= r && r <= gb2312Encode8max:
    82  		if r = rune(gb2312Encode8[r-gb2312Encode8min]); r != 0 {
    83  			goto writeGB
    84  		}
    85  	case gb2312Encode9min <= r && r <= gb2312Encode9max:
    86  		if r = rune(gb2312Encode9[r-gb2312Encode9min]); r != 0 {
    87  			goto writeGB
    88  		}
    89  	}
    90  	return -1
    91  
    92  writeGB:
    93  	r |= 0x8080
    94  	dst[0] = byte(r >> 8)
    95  	dst[1] = byte(r)
    96  	return 2
    97  }
    98  
    99  func (Charset_gb2312) DecodeRune(src []byte) (rune, int) {
   100  	if len(src) < 1 {
   101  		return utf8.RuneError, 0
   102  	}
   103  
   104  	c0 := src[0]
   105  	if c0 < utf8.RuneSelf {
   106  		return rune(c0), 1
   107  	}
   108  
   109  	if len(src) < 2 {
   110  		return utf8.RuneError, 1
   111  	}
   112  
   113  	r := (uint16(c0)<<8 | uint16(src[1])) & 0x7f7f
   114  	switch {
   115  	case gb2312Decode0min <= r && r <= gb2312Decode0max:
   116  		r = gb2312Decode0[r-gb2312Decode0min]
   117  	case gb2312Decode1min <= r && r <= gb2312Decode1max:
   118  		r = gb2312Decode1[r-gb2312Decode1min]
   119  	case gb2312Decode2min <= r && r <= gb2312Decode2max:
   120  		r = gb2312Decode2[r-gb2312Decode2min]
   121  	default:
   122  		r = 0
   123  	}
   124  	if r == 0 {
   125  		return utf8.RuneError, 2
   126  	}
   127  	return rune(r), 2
   128  }