github.com/dolthub/go-mysql-server@v0.18.0/sql/encodings/utf8mb4.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package encodings
    16  
    17  import (
    18  	"strings"
    19  	"unicode"
    20  	"unicode/utf8"
    21  )
    22  
    23  // utf8mb4Encoder is the implementation of Utf8mb4. As Go's string encoding perfectly matches `utf8mb4`, all of the
    24  // encoding and decoding functions are identity functions.
    25  type utf8mb4Encoder struct{}
    26  
    27  // Utf8mb4 represents the `utf8mb4` character set encoding.
    28  var Utf8mb4 Encoder = utf8mb4Encoder{}
    29  
    30  // Decode implements the Encoder interface.
    31  func (utf8mb4Encoder) Decode(str []byte) ([]byte, bool) {
    32  	return str, true
    33  }
    34  
    35  // Encode implements the Encoder interface.
    36  func (utf8mb4Encoder) Encode(str []byte) ([]byte, bool) {
    37  	return str, true
    38  }
    39  
    40  // EncodeReplaceUnknown implements the Encoder interface.
    41  func (utf8mb4Encoder) EncodeReplaceUnknown(str []byte) []byte {
    42  	return str
    43  }
    44  
    45  // DecodeRune implements the Encoder interface.
    46  func (utf8mb4Encoder) DecodeRune(r []byte) ([]byte, bool) {
    47  	return r, true
    48  }
    49  
    50  // EncodeRune implements the Encoder interface.
    51  func (utf8mb4Encoder) EncodeRune(r []byte) ([]byte, bool) {
    52  	return r, true
    53  }
    54  
    55  // Uppercase implements the Encoder interface.
    56  func (utf8mb4Encoder) Uppercase(str string) string {
    57  	return strings.ToUpper(str)
    58  }
    59  
    60  // Lowercase implements the Encoder interface.
    61  func (utf8mb4Encoder) Lowercase(str string) string {
    62  	return strings.ToLower(str)
    63  }
    64  
    65  // UppercaseRune implements the Encoder interface.
    66  func (utf8mb4Encoder) UppercaseRune(r rune) rune {
    67  	return unicode.ToUpper(r)
    68  }
    69  
    70  // LowercaseRune implements the Encoder interface.
    71  func (utf8mb4Encoder) LowercaseRune(r rune) rune {
    72  	return unicode.ToLower(r)
    73  }
    74  
    75  // NextRune implements the Encoder interface.
    76  func (utf8mb4Encoder) NextRune(str string) (rune, int) {
    77  	return utf8.DecodeRuneInString(str)
    78  }
    79  
    80  // IsReturnSafe implements the Encoder interface. Since the encoding and decoding functions return their input, they are
    81  // not safe to freely modify, and must be copied to preserve the original slice.
    82  func (utf8mb4Encoder) IsReturnSafe() bool {
    83  	return false
    84  }