vitess.io/vitess@v0.16.2/go/mysql/collations/internal/charset/eightbit/8bit.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 eightbit
    18  
    19  import (
    20  	"unicode/utf8"
    21  
    22  	"vitess.io/vitess/go/mysql/collations/internal/charset/types"
    23  )
    24  
    25  type UnicodeMapping struct {
    26  	From, To uint16
    27  	Range    []byte `json:"Tab"`
    28  }
    29  
    30  type Charset_8bit struct {
    31  	Name_       string
    32  	ToUnicode   *[256]uint16
    33  	FromUnicode []UnicodeMapping
    34  }
    35  
    36  func (e *Charset_8bit) Name() string {
    37  	return e.Name_
    38  }
    39  
    40  func (e *Charset_8bit) IsSuperset(other types.Charset) bool {
    41  	switch other := other.(type) {
    42  	case *Charset_8bit:
    43  		return e.Name_ == other.Name_
    44  	default:
    45  		return false
    46  	}
    47  }
    48  
    49  func (e *Charset_8bit) SupportsSupplementaryChars() bool {
    50  	return false
    51  }
    52  
    53  func (e *Charset_8bit) DecodeRune(bytes []byte) (rune, int) {
    54  	if len(bytes) < 1 {
    55  		return utf8.RuneError, 0
    56  	}
    57  	return rune(e.ToUnicode[bytes[0]]), 1
    58  }
    59  
    60  func (e *Charset_8bit) EncodeRune(dst []byte, r rune) int {
    61  	if r > 0xFFFF {
    62  		return -1
    63  	}
    64  	cp := uint16(r)
    65  	for _, mapping := range e.FromUnicode {
    66  		if cp >= mapping.From && cp <= mapping.To {
    67  			dst[0] = mapping.Range[cp-mapping.From]
    68  			if dst[0] == 0 && cp != 0 {
    69  				return -1
    70  			}
    71  			return 1
    72  		}
    73  	}
    74  	return -1
    75  }
    76  
    77  func (Charset_8bit) Length(src []byte) int {
    78  	return len(src)
    79  }