vitess.io/vitess@v0.16.2/go/mysql/collations/internal/charset/unicode/utf32.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 unicode
    18  
    19  import (
    20  	"unicode/utf8"
    21  
    22  	"vitess.io/vitess/go/mysql/collations/internal/charset/types"
    23  )
    24  
    25  type Charset_utf32 struct{}
    26  
    27  func (Charset_utf32) Name() string {
    28  	return "utf32"
    29  }
    30  
    31  func (Charset_utf32) IsSuperset(other types.Charset) bool {
    32  	switch other.(type) {
    33  	case Charset_utf32:
    34  		return true
    35  	default:
    36  		return false
    37  	}
    38  }
    39  
    40  func (Charset_utf32) EncodeRune(dst []byte, r rune) int {
    41  	_ = dst[3]
    42  
    43  	dst[0] = uint8(r >> 24)
    44  	dst[1] = uint8(r >> 16)
    45  	dst[2] = uint8(r >> 8)
    46  	dst[3] = uint8(r)
    47  	return 4
    48  }
    49  
    50  func (Charset_utf32) DecodeRune(p []byte) (rune, int) {
    51  	if len(p) < 4 {
    52  		return utf8.RuneError, 0
    53  	}
    54  	return (rune(p[0]) << 24) | (rune(p[1]) << 16) | (rune(p[2]) << 8) | rune(p[3]), 4
    55  }
    56  
    57  func (Charset_utf32) SupportsSupplementaryChars() bool {
    58  	return true
    59  }
    60  
    61  func (Charset_utf32) CharLen(src []byte) int {
    62  	cnt := len(src)
    63  	if cnt%4 != 0 {
    64  		return cnt/4 + 1
    65  	}
    66  	return cnt / 4
    67  }