vitess.io/vitess@v0.16.2/go/mysql/collations/internal/uca/tailoring.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 uca
    18  
    19  type Reorder struct {
    20  	FromMin, FromMax uint16
    21  	ToMin, ToMax     uint16
    22  }
    23  
    24  type parametricT struct {
    25  	upperCaseFirst bool
    26  	reorder        []Reorder
    27  	reorderMin     uint16
    28  	reorderMax     uint16
    29  }
    30  
    31  func (p *parametricT) adjust(level int, weight uint16) uint16 {
    32  	if p == nil {
    33  		return weight
    34  	}
    35  	switch level {
    36  	case 0:
    37  		if weight >= p.reorderMin && weight <= p.reorderMax {
    38  			for _, reorder := range p.reorder {
    39  				if weight >= reorder.FromMin && weight <= reorder.FromMax {
    40  					return weight - reorder.FromMin + reorder.ToMin
    41  				}
    42  			}
    43  		}
    44  	case 2:
    45  		// see: https://unicode.org/reports/tr35/tr35-collation.html#Case_Untailored
    46  		if p.upperCaseFirst && weight < ' ' {
    47  			switch weight {
    48  			case 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0E, 0x11, 0x12, 0x1D:
    49  				return weight | 0x100
    50  			default:
    51  				return weight | 0x300
    52  			}
    53  		}
    54  	}
    55  	return weight
    56  }
    57  
    58  func newParametricTailoring(reorder []Reorder, upperCaseFirst bool) *parametricT {
    59  	if len(reorder) == 0 && !upperCaseFirst {
    60  		return nil
    61  	}
    62  
    63  	t := &parametricT{
    64  		upperCaseFirst: upperCaseFirst,
    65  		reorder:        reorder,
    66  		reorderMin:     ^uint16(0),
    67  		reorderMax:     0,
    68  	}
    69  
    70  	for _, r := range reorder {
    71  		if r.FromMin < t.reorderMin {
    72  			t.reorderMin = r.FromMin
    73  		}
    74  		if r.FromMax > t.reorderMax {
    75  			t.reorderMax = r.FromMax
    76  		}
    77  	}
    78  
    79  	return t
    80  }