2010-12-03 05:34:57 +01:00
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
|
// Package unicode provides data and functions to test some properties of
|
|
|
|
|
// Unicode code points.
|
2010-12-03 05:34:57 +01:00
|
|
|
|
package unicode
|
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
|
// Tables are regenerated each time we update the Unicode version.
|
|
|
|
|
//go:generate go run maketables.go -tables=all -output tables.go
|
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
|
const (
|
2012-01-12 02:31:45 +01:00
|
|
|
|
MaxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
|
|
|
|
ReplacementChar = '\uFFFD' // Represents invalid code points.
|
|
|
|
|
MaxASCII = '\u007F' // maximum ASCII value.
|
|
|
|
|
MaxLatin1 = '\u00FF' // maximum Latin-1 value.
|
2010-12-03 05:34:57 +01:00
|
|
|
|
)
|
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
|
// RangeTable defines a set of Unicode code points by listing the ranges of
|
|
|
|
|
// code points within the set. The ranges are listed in two slices
|
|
|
|
|
// to save space: a slice of 16-bit ranges and a slice of 32-bit ranges.
|
|
|
|
|
// The two slices must be in sorted order and non-overlapping.
|
|
|
|
|
// Also, R32 should contain only values >= 0x10000 (1<<16).
|
|
|
|
|
type RangeTable struct {
|
2012-10-23 06:31:11 +02:00
|
|
|
|
R16 []Range16
|
|
|
|
|
R32 []Range32
|
|
|
|
|
LatinOffset int // number of entries in R16 with Hi <= MaxLatin1
|
2011-09-16 17:47:21 +02:00
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
|
// Range16 represents of a range of 16-bit Unicode code points. The range runs from Lo to Hi
|
2010-12-03 05:34:57 +01:00
|
|
|
|
// inclusive and has the specified stride.
|
2011-09-16 17:47:21 +02:00
|
|
|
|
type Range16 struct {
|
|
|
|
|
Lo uint16
|
|
|
|
|
Hi uint16
|
|
|
|
|
Stride uint16
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Range32 represents of a range of Unicode code points and is used when one or
|
|
|
|
|
// more of the values will not fit in 16 bits. The range runs from Lo to Hi
|
|
|
|
|
// inclusive and has the specified stride. Lo and Hi must always be >= 1<<16.
|
|
|
|
|
type Range32 struct {
|
|
|
|
|
Lo uint32
|
|
|
|
|
Hi uint32
|
|
|
|
|
Stride uint32
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CaseRange represents a range of Unicode code points for simple (one
|
|
|
|
|
// code point to one code point) case conversion.
|
|
|
|
|
// The range runs from Lo to Hi inclusive, with a fixed stride of 1. Deltas
|
|
|
|
|
// are the number to add to the code point to reach the code point for a
|
|
|
|
|
// different case for that character. They may be negative. If zero, it
|
|
|
|
|
// means the character is in the corresponding case. There is a special
|
|
|
|
|
// case representing sequences of alternating corresponding Upper and Lower
|
|
|
|
|
// pairs. It appears with a fixed Delta of
|
|
|
|
|
// {UpperLower, UpperLower, UpperLower}
|
|
|
|
|
// The constant UpperLower has an otherwise impossible delta value.
|
|
|
|
|
type CaseRange struct {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
Lo uint32
|
|
|
|
|
Hi uint32
|
2010-12-03 05:34:57 +01:00
|
|
|
|
Delta d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SpecialCase represents language-specific case mappings such as Turkish.
|
|
|
|
|
// Methods of SpecialCase customize (by overriding) the standard mappings.
|
|
|
|
|
type SpecialCase []CaseRange
|
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
|
// BUG(r): There is no mechanism for full case folding, that is, for
|
|
|
|
|
// characters that involve multiple runes in the input or output.
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
|
|
// Indices into the Delta arrays inside CaseRanges for case mapping.
|
|
|
|
|
const (
|
|
|
|
|
UpperCase = iota
|
|
|
|
|
LowerCase
|
|
|
|
|
TitleCase
|
|
|
|
|
MaxCase
|
|
|
|
|
)
|
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
|
type d [MaxCase]rune // to make the CaseRanges text shorter
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
|
// If the Delta field of a CaseRange is UpperLower, it means
|
2010-12-03 05:34:57 +01:00
|
|
|
|
// this CaseRange represents a sequence of the form (say)
|
|
|
|
|
// Upper Lower Upper Lower.
|
|
|
|
|
const (
|
|
|
|
|
UpperLower = MaxRune + 1 // (Cannot be a valid delta.)
|
|
|
|
|
)
|
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
|
// linearMax is the maximum size table for linear search for non-Latin1 rune.
|
|
|
|
|
// Derived by running 'go test -calibrate'.
|
|
|
|
|
const linearMax = 18
|
|
|
|
|
|
|
|
|
|
// is16 reports whether r is in the sorted slice of 16-bit ranges.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func is16(ranges []Range16, r uint16) bool {
|
2012-10-23 06:31:11 +02:00
|
|
|
|
if len(ranges) <= linearMax || r <= MaxLatin1 {
|
|
|
|
|
for i := range ranges {
|
|
|
|
|
range_ := &ranges[i]
|
|
|
|
|
if r < range_.Lo {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if r <= range_.Hi {
|
|
|
|
|
return (r-range_.Lo)%range_.Stride == 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
|
// binary search over ranges
|
|
|
|
|
lo := 0
|
|
|
|
|
hi := len(ranges)
|
|
|
|
|
for lo < hi {
|
|
|
|
|
m := lo + (hi-lo)/2
|
2012-10-23 06:31:11 +02:00
|
|
|
|
range_ := &ranges[m]
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if range_.Lo <= r && r <= range_.Hi {
|
|
|
|
|
return (r-range_.Lo)%range_.Stride == 0
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if r < range_.Lo {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
hi = m
|
|
|
|
|
} else {
|
|
|
|
|
lo = m + 1
|
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
return false
|
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
|
// is32 reports whether r is in the sorted slice of 32-bit ranges.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func is32(ranges []Range32, r uint32) bool {
|
2012-10-23 06:31:11 +02:00
|
|
|
|
if len(ranges) <= linearMax {
|
|
|
|
|
for i := range ranges {
|
|
|
|
|
range_ := &ranges[i]
|
|
|
|
|
if r < range_.Lo {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if r <= range_.Hi {
|
|
|
|
|
return (r-range_.Lo)%range_.Stride == 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
|
// binary search over ranges
|
|
|
|
|
lo := 0
|
|
|
|
|
hi := len(ranges)
|
|
|
|
|
for lo < hi {
|
|
|
|
|
m := lo + (hi-lo)/2
|
2011-12-02 20:34:41 +01:00
|
|
|
|
range_ := ranges[m]
|
|
|
|
|
if range_.Lo <= r && r <= range_.Hi {
|
|
|
|
|
return (r-range_.Lo)%range_.Stride == 0
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if r < range_.Lo {
|
2010-12-03 05:34:57 +01:00
|
|
|
|
hi = m
|
|
|
|
|
} else {
|
|
|
|
|
lo = m + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
|
// Is reports whether the rune is in the specified table of ranges.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func Is(rangeTab *RangeTable, r rune) bool {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
r16 := rangeTab.R16
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if len(r16) > 0 && r <= rune(r16[len(r16)-1].Hi) {
|
|
|
|
|
return is16(r16, uint16(r))
|
2011-09-16 17:47:21 +02:00
|
|
|
|
}
|
|
|
|
|
r32 := rangeTab.R32
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if len(r32) > 0 && r >= rune(r32[0].Lo) {
|
|
|
|
|
return is32(r32, uint32(r))
|
2011-09-16 17:47:21 +02:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
|
func isExcludingLatin(rangeTab *RangeTable, r rune) bool {
|
|
|
|
|
r16 := rangeTab.R16
|
|
|
|
|
if off := rangeTab.LatinOffset; len(r16) > off && r <= rune(r16[len(r16)-1].Hi) {
|
|
|
|
|
return is16(r16[off:], uint16(r))
|
|
|
|
|
}
|
|
|
|
|
r32 := rangeTab.R32
|
|
|
|
|
if len(r32) > 0 && r >= rune(r32[0].Lo) {
|
|
|
|
|
return is32(r32, uint32(r))
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
|
// IsUpper reports whether the rune is an upper case letter.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func IsUpper(r rune) bool {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
// See comment in IsGraphic.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if uint32(r) <= MaxLatin1 {
|
2012-11-21 08:03:38 +01:00
|
|
|
|
return properties[uint8(r)]&pLmask == pLu
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
return isExcludingLatin(Upper, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsLower reports whether the rune is a lower case letter.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func IsLower(r rune) bool {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
// See comment in IsGraphic.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if uint32(r) <= MaxLatin1 {
|
2012-11-21 08:03:38 +01:00
|
|
|
|
return properties[uint8(r)]&pLmask == pLl
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
return isExcludingLatin(Lower, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsTitle reports whether the rune is a title case letter.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func IsTitle(r rune) bool {
|
|
|
|
|
if r <= MaxLatin1 {
|
2010-12-03 05:34:57 +01:00
|
|
|
|
return false
|
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
return isExcludingLatin(Title, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// to maps the rune using the specified case mapping.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func to(_case int, r rune, caseRange []CaseRange) rune {
|
2010-12-03 05:34:57 +01:00
|
|
|
|
if _case < 0 || MaxCase <= _case {
|
|
|
|
|
return ReplacementChar // as reasonable an error as any
|
|
|
|
|
}
|
|
|
|
|
// binary search over ranges
|
|
|
|
|
lo := 0
|
|
|
|
|
hi := len(caseRange)
|
|
|
|
|
for lo < hi {
|
|
|
|
|
m := lo + (hi-lo)/2
|
2011-12-02 20:34:41 +01:00
|
|
|
|
cr := caseRange[m]
|
|
|
|
|
if rune(cr.Lo) <= r && r <= rune(cr.Hi) {
|
|
|
|
|
delta := rune(cr.Delta[_case])
|
2010-12-03 05:34:57 +01:00
|
|
|
|
if delta > MaxRune {
|
|
|
|
|
// In an Upper-Lower sequence, which always starts with
|
|
|
|
|
// an UpperCase letter, the real deltas always look like:
|
|
|
|
|
// {0, 1, 0} UpperCase (Lower is next)
|
|
|
|
|
// {-1, 0, -1} LowerCase (Upper, Title are previous)
|
|
|
|
|
// The characters at even offsets from the beginning of the
|
|
|
|
|
// sequence are upper case; the ones at odd offsets are lower.
|
|
|
|
|
// The correct mapping can be done by clearing or setting the low
|
|
|
|
|
// bit in the sequence offset.
|
|
|
|
|
// The constants UpperCase and TitleCase are even while LowerCase
|
|
|
|
|
// is odd so we take the low bit from _case.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return rune(cr.Lo) + ((r-rune(cr.Lo))&^1 | rune(_case&1))
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r + delta
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if r < rune(cr.Lo) {
|
2010-12-03 05:34:57 +01:00
|
|
|
|
hi = m
|
|
|
|
|
} else {
|
|
|
|
|
lo = m + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To maps the rune to the specified case: UpperCase, LowerCase, or TitleCase.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func To(_case int, r rune) rune {
|
|
|
|
|
return to(_case, r, CaseRanges)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToUpper maps the rune to upper case.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func ToUpper(r rune) rune {
|
|
|
|
|
if r <= MaxASCII {
|
|
|
|
|
if 'a' <= r && r <= 'z' {
|
|
|
|
|
r -= 'a' - 'A'
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return To(UpperCase, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToLower maps the rune to lower case.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func ToLower(r rune) rune {
|
|
|
|
|
if r <= MaxASCII {
|
|
|
|
|
if 'A' <= r && r <= 'Z' {
|
|
|
|
|
r += 'a' - 'A'
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return To(LowerCase, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToTitle maps the rune to title case.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func ToTitle(r rune) rune {
|
|
|
|
|
if r <= MaxASCII {
|
|
|
|
|
if 'a' <= r && r <= 'z' { // title case is upper case for ASCII
|
|
|
|
|
r -= 'a' - 'A'
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return To(TitleCase, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToUpper maps the rune to upper case giving priority to the special mapping.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func (special SpecialCase) ToUpper(r rune) rune {
|
|
|
|
|
r1 := to(UpperCase, r, []CaseRange(special))
|
|
|
|
|
if r1 == r {
|
|
|
|
|
r1 = ToUpper(r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r1
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToTitle maps the rune to title case giving priority to the special mapping.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func (special SpecialCase) ToTitle(r rune) rune {
|
|
|
|
|
r1 := to(TitleCase, r, []CaseRange(special))
|
|
|
|
|
if r1 == r {
|
|
|
|
|
r1 = ToTitle(r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r1
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToLower maps the rune to lower case giving priority to the special mapping.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func (special SpecialCase) ToLower(r rune) rune {
|
|
|
|
|
r1 := to(LowerCase, r, []CaseRange(special))
|
|
|
|
|
if r1 == r {
|
|
|
|
|
r1 = ToLower(r)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return r1
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
|
|
// caseOrbit is defined in tables.go as []foldPair. Right now all the
|
|
|
|
|
// entries fit in uint16, so use uint16. If that changes, compilation
|
|
|
|
|
// will fail (the constants in the composite literal will not fit in uint16)
|
|
|
|
|
// and the types here can change to uint32.
|
|
|
|
|
type foldPair struct {
|
|
|
|
|
From uint16
|
|
|
|
|
To uint16
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SimpleFold iterates over Unicode code points equivalent under
|
|
|
|
|
// the Unicode-defined simple case folding. Among the code points
|
|
|
|
|
// equivalent to rune (including rune itself), SimpleFold returns the
|
2014-07-19 10:53:52 +02:00
|
|
|
|
// smallest rune > r if one exists, or else the smallest rune >= 0.
|
2011-09-16 17:47:21 +02:00
|
|
|
|
//
|
|
|
|
|
// For example:
|
|
|
|
|
// SimpleFold('A') = 'a'
|
|
|
|
|
// SimpleFold('a') = 'A'
|
|
|
|
|
//
|
|
|
|
|
// SimpleFold('K') = 'k'
|
|
|
|
|
// SimpleFold('k') = '\u212A' (Kelvin symbol, K)
|
|
|
|
|
// SimpleFold('\u212A') = 'K'
|
|
|
|
|
//
|
|
|
|
|
// SimpleFold('1') = '1'
|
|
|
|
|
//
|
2011-12-02 20:34:41 +01:00
|
|
|
|
func SimpleFold(r rune) rune {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
// Consult caseOrbit table for special cases.
|
|
|
|
|
lo := 0
|
|
|
|
|
hi := len(caseOrbit)
|
|
|
|
|
for lo < hi {
|
|
|
|
|
m := lo + (hi-lo)/2
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if rune(caseOrbit[m].From) < r {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
lo = m + 1
|
|
|
|
|
} else {
|
|
|
|
|
hi = m
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if lo < len(caseOrbit) && rune(caseOrbit[lo].From) == r {
|
|
|
|
|
return rune(caseOrbit[lo].To)
|
2011-09-16 17:47:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No folding specified. This is a one- or two-element
|
|
|
|
|
// equivalence class containing rune and ToLower(rune)
|
|
|
|
|
// and ToUpper(rune) if they are different from rune.
|
2011-12-02 20:34:41 +01:00
|
|
|
|
if l := ToLower(r); l != r {
|
2011-09-16 17:47:21 +02:00
|
|
|
|
return l
|
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
|
return ToUpper(r)
|
2011-09-16 17:47:21 +02:00
|
|
|
|
}
|