vitess.io/vitess@v0.16.2/go/mysql/collations/internal/charset/japanese/sjis.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 japanese
    18  
    19  import (
    20  	"unicode/utf8"
    21  
    22  	"vitess.io/vitess/go/mysql/collations/internal/charset/types"
    23  )
    24  
    25  type Charset_sjis struct{}
    26  
    27  func (Charset_sjis) Name() string {
    28  	return "sjis"
    29  }
    30  
    31  func (Charset_sjis) IsSuperset(other types.Charset) bool {
    32  	switch other.(type) {
    33  	case Charset_sjis:
    34  		return true
    35  	default:
    36  		return false
    37  	}
    38  }
    39  
    40  func (Charset_sjis) SupportsSupplementaryChars() bool {
    41  	return false
    42  }
    43  
    44  func encodeSJIS(dst []byte, r rune, table *[65536]uint16) int {
    45  	_ = dst[1]
    46  
    47  	if r > 0xFFFF {
    48  		return -1
    49  	}
    50  
    51  	var sj = uint16(r)
    52  	if sj < utf8.RuneSelf {
    53  		if sj == 0x5c && table == &table_sjisEncode {
    54  			// COMPAT: this appears to be a difference between SJIS and CP32,
    55  			// all the other characters map properly
    56  			sj = 0x815f
    57  			goto write2
    58  		}
    59  		goto write1
    60  	}
    61  	if sj = table[r]; sj == 0 {
    62  		return -1
    63  	}
    64  	if sj > 0xFF {
    65  		goto write2
    66  	}
    67  
    68  write1:
    69  	dst[0] = byte(sj)
    70  	return 1
    71  
    72  write2:
    73  	dst[0] = byte(sj >> 8)
    74  	dst[1] = byte(sj)
    75  	return 2
    76  }
    77  
    78  func (Charset_sjis) EncodeRune(dst []byte, r rune) int {
    79  	return encodeSJIS(dst, r, &table_sjisEncode)
    80  }
    81  
    82  func decodeSJIS(src []byte, table *[65536]uint16) (rune, int) {
    83  	if len(src) < 1 {
    84  		return utf8.RuneError, 0
    85  	}
    86  	c0 := src[0]
    87  	if c0 < utf8.RuneSelf {
    88  		return rune(c0), 1
    89  	}
    90  	if c0 >= 0xA1 && c0 <= 0xDF {
    91  		return rune(table[c0]), 1
    92  	}
    93  	if len(src) >= 2 {
    94  		sj := uint16(c0)<<8 | uint16(src[1])
    95  		if cp := table[sj]; cp != 0 {
    96  			return rune(cp), 2
    97  		}
    98  	}
    99  	return utf8.RuneError, 2
   100  }
   101  
   102  func (Charset_sjis) DecodeRune(src []byte) (rune, int) {
   103  	return decodeSJIS(src, &table_sjisDecode)
   104  }
   105  
   106  type Charset_cp932 struct{}
   107  
   108  func (Charset_cp932) Name() string {
   109  	return "cp932"
   110  }
   111  
   112  func (Charset_cp932) SupportsSupplementaryChars() bool {
   113  	return false
   114  }
   115  
   116  func (Charset_cp932) IsSuperset(other types.Charset) bool {
   117  	switch other.(type) {
   118  	case Charset_cp932:
   119  		return true
   120  	default:
   121  		return false
   122  	}
   123  }
   124  
   125  func (Charset_cp932) EncodeRune(dst []byte, r rune) int {
   126  	return encodeSJIS(dst, r, &table_cp932Encode)
   127  }
   128  
   129  func (Charset_cp932) DecodeRune(src []byte) (rune, int) {
   130  	return decodeSJIS(src, &table_cp932Decode)
   131  }