vitess.io/vitess@v0.16.2/go/mysql/collations/internal/charset/japanese/ujis.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 _ "unsafe" 22 23 "vitess.io/vitess/go/mysql/collations/internal/charset/types" 24 ) 25 26 func ujisEncodeRune(dst []byte, r rune, table208, table212 *[65536]uint16) int { 27 _ = dst[2] 28 29 switch { 30 case r < utf8.RuneSelf: 31 dst[0] = byte(r) 32 return 1 33 34 case r >= 0xff61 && r <= 0xff9f: 35 dst[0] = 0x8e 36 dst[1] = uint8(r - (0xff61 - 0xa1)) 37 return 2 38 39 case r <= 0xFFFF: 40 uj := table208[r] 41 if uj != 0 { 42 dst[0] = byte(uj >> 8) 43 dst[1] = byte(uj) 44 return 2 45 } 46 47 uj = table212[r] 48 if uj != 0 { 49 dst[0] = 0x8f 50 dst[1] = byte(uj >> 8) 51 dst[2] = byte(uj) 52 return 3 53 } 54 } 55 return -1 56 } 57 58 func ujisDecodeRune(src []byte, table208, table212 *[65536]uint16) (rune, int) { 59 if len(src) < 1 { 60 return utf8.RuneError, 0 61 } 62 63 switch c0 := src[0]; { 64 case c0 < utf8.RuneSelf: 65 return rune(c0), 1 66 67 case c0 == 0x8e: 68 if len(src) < 2 { 69 return utf8.RuneError, 1 70 } 71 c1 := src[1] 72 switch { 73 case c1 < 0xa1: 74 return utf8.RuneError, 1 75 case c1 > 0xdf: 76 if c1 == 0xff { 77 return utf8.RuneError, 1 78 } 79 return utf8.RuneError, 2 80 default: 81 return rune(c1) + (0xff61 - 0xa1), 2 82 } 83 case c0 == 0x8f: 84 if len(src) < 3 { 85 if len(src) == 2 && 0xa1 <= src[1] && src[1] < 0xfe { 86 return utf8.RuneError, 2 87 } 88 return utf8.RuneError, 1 89 } 90 c1 := src[1] 91 if c1 < 0xa1 || 0xfe < c1 { 92 return utf8.RuneError, 1 93 } 94 c2 := src[2] 95 if c2 < 0xa1 || 0xfe < c2 { 96 return utf8.RuneError, 2 97 } 98 r := rune(table212[uint16(c1)<<8|uint16(c2)]) 99 if r == 0 { 100 r = utf8.RuneError 101 } 102 return r, 3 103 104 case 0xa1 <= c0 && c0 <= 0xfe: 105 if len(src) < 2 { 106 return utf8.RuneError, 1 107 } 108 c1 := src[1] 109 if c1 < 0xa1 || 0xfe < c1 { 110 return utf8.RuneError, 1 111 } 112 r := rune(table208[uint16(c0)<<8|uint16(c1)]) 113 if r == 0 { 114 r = utf8.RuneError 115 } 116 return r, 2 117 118 default: 119 return utf8.RuneError, 1 120 } 121 } 122 123 type Charset_ujis struct{} 124 125 func (Charset_ujis) Name() string { 126 return "ujis" 127 } 128 129 func (Charset_ujis) IsSuperset(other types.Charset) bool { 130 switch other.(type) { 131 case Charset_ujis: 132 return true 133 default: 134 return false 135 } 136 } 137 138 func (Charset_ujis) SupportsSupplementaryChars() bool { 139 return false 140 } 141 142 func (Charset_ujis) EncodeRune(dst []byte, r rune) int { 143 return ujisEncodeRune(dst, r, &table_jis208Encode, &table_jis212Encode) 144 } 145 146 func (Charset_ujis) DecodeRune(src []byte) (rune, int) { 147 return ujisDecodeRune(src, &table_jis208Decode, &table_jis212Decode) 148 } 149 150 type Charset_eucjpms struct{} 151 152 func (Charset_eucjpms) Name() string { 153 return "eucjpms" 154 } 155 156 func (Charset_eucjpms) IsSuperset(other types.Charset) bool { 157 switch other.(type) { 158 case Charset_eucjpms: 159 return true 160 default: 161 return false 162 } 163 } 164 165 func (Charset_eucjpms) SupportsSupplementaryChars() bool { 166 return false 167 } 168 169 func (Charset_eucjpms) EncodeRune(dst []byte, r rune) int { 170 return ujisEncodeRune(dst, r, &table_jis208_eucjpmsEncode, &table_jis212_eucjpmsEncode) 171 } 172 173 func (Charset_eucjpms) DecodeRune(src []byte) (rune, int) { 174 return ujisDecodeRune(src, &table_jis208_eucjpmsDecode, &table_jis212_eucjpmsDecode) 175 }