vitess.io/vitess@v0.16.2/go/mysql/collations/internal/charset/helpers.go (about) 1 /* 2 Copyright 2022 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 charset 18 19 func Slice(charset Charset, input []byte, from, to int) []byte { 20 if charset, ok := charset.(interface{ Slice([]byte, int, int) []byte }); ok { 21 return charset.Slice(input, from, to) 22 } 23 iter := input 24 for i := 0; i < to; i++ { 25 r, size := charset.DecodeRune(iter) 26 if r == RuneError && size < 2 { 27 break 28 } 29 iter = iter[size:] 30 } 31 return input[:len(input)-len(iter)] 32 } 33 34 func Validate(charset Charset, input []byte) bool { 35 if charset, ok := charset.(interface{ Validate([]byte) bool }); ok { 36 return charset.Validate(input) 37 } 38 for len(input) > 0 { 39 r, size := charset.DecodeRune(input) 40 if r == RuneError && size < 2 { 41 return false 42 } 43 input = input[size:] 44 } 45 return true 46 } 47 48 func Length(charset Charset, input []byte) int { 49 if charset, ok := charset.(interface{ Length([]byte) int }); ok { 50 return charset.Length(input) 51 } 52 var count int 53 for len(input) > 0 { 54 _, size := charset.DecodeRune(input) 55 input = input[size:] 56 count++ 57 } 58 return count 59 }