1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package unicode provides data and functions to test some properties of
6 // Unicode code points.
10 MaxRune
= '\U0010FFFF' // Maximum valid Unicode code point.
11 ReplacementChar
= '\uFFFD' // Represents invalid code points.
12 MaxASCII
= '\u007F' // maximum ASCII value.
13 MaxLatin1
= '\u00FF' // maximum Latin-1 value.
16 // RangeTable defines a set of Unicode code points by listing the ranges of
17 // code points within the set. The ranges are listed in two slices
18 // to save space: a slice of 16-bit ranges and a slice of 32-bit ranges.
19 // The two slices must be in sorted order and non-overlapping.
20 // Also, R32 should contain only values >= 0x10000 (1<<16).
21 type RangeTable
struct {
24 LatinOffset
int // number of entries in R16 with Hi <= MaxLatin1
27 // Range16 represents of a range of 16-bit Unicode code points. The range runs from Lo to Hi
28 // inclusive and has the specified stride.
35 // Range32 represents of a range of Unicode code points and is used when one or
36 // more of the values will not fit in 16 bits. The range runs from Lo to Hi
37 // inclusive and has the specified stride. Lo and Hi must always be >= 1<<16.
44 // CaseRange represents a range of Unicode code points for simple (one
45 // code point to one code point) case conversion.
46 // The range runs from Lo to Hi inclusive, with a fixed stride of 1. Deltas
47 // are the number to add to the code point to reach the code point for a
48 // different case for that character. They may be negative. If zero, it
49 // means the character is in the corresponding case. There is a special
50 // case representing sequences of alternating corresponding Upper and Lower
51 // pairs. It appears with a fixed Delta of
52 // {UpperLower, UpperLower, UpperLower}
53 // The constant UpperLower has an otherwise impossible delta value.
54 type CaseRange
struct {
60 // SpecialCase represents language-specific case mappings such as Turkish.
61 // Methods of SpecialCase customize (by overriding) the standard mappings.
62 type SpecialCase
[]CaseRange
64 // BUG(r): There is no mechanism for full case folding, that is, for
65 // characters that involve multiple runes in the input or output.
67 // Indices into the Delta arrays inside CaseRanges for case mapping.
75 type d
[MaxCase
]rune
// to make the CaseRanges text shorter
77 // If the Delta field of a CaseRange is UpperLower or LowerUpper, it means
78 // this CaseRange represents a sequence of the form (say)
79 // Upper Lower Upper Lower.
81 UpperLower
= MaxRune
+ 1 // (Cannot be a valid delta.)
84 // linearMax is the maximum size table for linear search for non-Latin1 rune.
85 // Derived by running 'go test -calibrate'.
88 // is16 reports whether r is in the sorted slice of 16-bit ranges.
89 func is16(ranges
[]Range16
, r
uint16) bool {
90 if len(ranges
) <= linearMax || r
<= MaxLatin1
{
91 for i
:= range ranges
{
97 return (r
-range_
.Lo
)%range_
.Stride
== 0
103 // binary search over ranges
109 if range_
.Lo
<= r
&& r
<= range_
.Hi
{
110 return (r
-range_
.Lo
)%range_
.Stride
== 0
121 // is32 reports whether r is in the sorted slice of 32-bit ranges.
122 func is32(ranges
[]Range32
, r
uint32) bool {
123 if len(ranges
) <= linearMax
{
124 for i
:= range ranges
{
130 return (r
-range_
.Lo
)%range_
.Stride
== 0
136 // binary search over ranges
142 if range_
.Lo
<= r
&& r
<= range_
.Hi
{
143 return (r
-range_
.Lo
)%range_
.Stride
== 0
154 // Is reports whether the rune is in the specified table of ranges.
155 func Is(rangeTab
*RangeTable
, r rune
) bool {
157 if len(r16
) > 0 && r
<= rune(r16
[len(r16
)-1].Hi
) {
158 return is16(r16
, uint16(r
))
161 if len(r32
) > 0 && r
>= rune(r32
[0].Lo
) {
162 return is32(r32
, uint32(r
))
167 func isExcludingLatin(rangeTab
*RangeTable
, r rune
) bool {
169 if off
:= rangeTab
.LatinOffset
; len(r16
) > off
&& r
<= rune(r16
[len(r16
)-1].Hi
) {
170 return is16(r16
[off
:], uint16(r
))
173 if len(r32
) > 0 && r
>= rune(r32
[0].Lo
) {
174 return is32(r32
, uint32(r
))
179 // IsUpper reports whether the rune is an upper case letter.
180 func IsUpper(r rune
) bool {
181 // See comment in IsGraphic.
182 if uint32(r
) <= MaxLatin1
{
183 return properties
[uint8(r
)]&pLmask
== pLu
185 return isExcludingLatin(Upper
, r
)
188 // IsLower reports whether the rune is a lower case letter.
189 func IsLower(r rune
) bool {
190 // See comment in IsGraphic.
191 if uint32(r
) <= MaxLatin1
{
192 return properties
[uint8(r
)]&pLmask
== pLl
194 return isExcludingLatin(Lower
, r
)
197 // IsTitle reports whether the rune is a title case letter.
198 func IsTitle(r rune
) bool {
202 return isExcludingLatin(Title
, r
)
205 // to maps the rune using the specified case mapping.
206 func to(_case
int, r rune
, caseRange
[]CaseRange
) rune
{
207 if _case
< 0 || MaxCase
<= _case
{
208 return ReplacementChar
// as reasonable an error as any
210 // binary search over ranges
216 if rune(cr
.Lo
) <= r
&& r
<= rune(cr
.Hi
) {
217 delta
:= rune(cr
.Delta
[_case
])
219 // In an Upper-Lower sequence, which always starts with
220 // an UpperCase letter, the real deltas always look like:
221 // {0, 1, 0} UpperCase (Lower is next)
222 // {-1, 0, -1} LowerCase (Upper, Title are previous)
223 // The characters at even offsets from the beginning of the
224 // sequence are upper case; the ones at odd offsets are lower.
225 // The correct mapping can be done by clearing or setting the low
226 // bit in the sequence offset.
227 // The constants UpperCase and TitleCase are even while LowerCase
228 // is odd so we take the low bit from _case.
229 return rune(cr
.Lo
) + ((r
-rune(cr
.Lo
))&^1 |
rune(_case
&1))
242 // To maps the rune to the specified case: UpperCase, LowerCase, or TitleCase.
243 func To(_case
int, r rune
) rune
{
244 return to(_case
, r
, CaseRanges
)
247 // ToUpper maps the rune to upper case.
248 func ToUpper(r rune
) rune
{
250 if 'a' <= r
&& r
<= 'z' {
255 return To(UpperCase
, r
)
258 // ToLower maps the rune to lower case.
259 func ToLower(r rune
) rune
{
261 if 'A' <= r
&& r
<= 'Z' {
266 return To(LowerCase
, r
)
269 // ToTitle maps the rune to title case.
270 func ToTitle(r rune
) rune
{
272 if 'a' <= r
&& r
<= 'z' { // title case is upper case for ASCII
277 return To(TitleCase
, r
)
280 // ToUpper maps the rune to upper case giving priority to the special mapping.
281 func (special SpecialCase
) ToUpper(r rune
) rune
{
282 r1
:= to(UpperCase
, r
, []CaseRange(special
))
289 // ToTitle maps the rune to title case giving priority to the special mapping.
290 func (special SpecialCase
) ToTitle(r rune
) rune
{
291 r1
:= to(TitleCase
, r
, []CaseRange(special
))
298 // ToLower maps the rune to lower case giving priority to the special mapping.
299 func (special SpecialCase
) ToLower(r rune
) rune
{
300 r1
:= to(LowerCase
, r
, []CaseRange(special
))
307 // caseOrbit is defined in tables.go as []foldPair. Right now all the
308 // entries fit in uint16, so use uint16. If that changes, compilation
309 // will fail (the constants in the composite literal will not fit in uint16)
310 // and the types here can change to uint32.
311 type foldPair
struct {
316 // SimpleFold iterates over Unicode code points equivalent under
317 // the Unicode-defined simple case folding. Among the code points
318 // equivalent to rune (including rune itself), SimpleFold returns the
319 // smallest rune >= r if one exists, or else the smallest rune >= 0.
322 // SimpleFold('A') = 'a'
323 // SimpleFold('a') = 'A'
325 // SimpleFold('K') = 'k'
326 // SimpleFold('k') = '\u212A' (Kelvin symbol, K)
327 // SimpleFold('\u212A') = 'K'
329 // SimpleFold('1') = '1'
331 func SimpleFold(r rune
) rune
{
332 // Consult caseOrbit table for special cases.
337 if rune(caseOrbit
[m
].From
) < r
{
343 if lo
< len(caseOrbit
) && rune(caseOrbit
[lo
].From
) == r
{
344 return rune(caseOrbit
[lo
].To
)
347 // No folding specified. This is a one- or two-element
348 // equivalence class containing rune and ToLower(rune)
349 // and ToUpper(rune) if they are different from rune.
350 if l
:= ToLower(r
); l
!= r
{