2009-01-14 15:05:00 -07: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.
|
|
|
|
|
2009-03-06 04:22:02 -07:00
|
|
|
// This package provides data and functions to test some properties of Unicode code points.
|
|
|
|
// It is rudimentary but will improve.
|
2009-01-14 15:05:00 -07:00
|
|
|
package unicode
|
|
|
|
|
2009-03-06 04:22:02 -07:00
|
|
|
// The representation of a range of Unicode code points. The range runs from Lo to Hi
|
|
|
|
// inclusive and has the specified stride.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Range struct {
|
2009-08-29 00:05:16 -06:00
|
|
|
Lo int;
|
|
|
|
Hi int;
|
|
|
|
Stride int;
|
2009-01-14 15:05:00 -07:00
|
|
|
}
|
|
|
|
|
2009-08-29 00:05:16 -06:00
|
|
|
// The representation of a range of Unicode code points for 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
|
2009-08-30 15:02:42 -06:00
|
|
|
// 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 the usual Lo and Hi values and a Delta of
|
|
|
|
// {0, UpperLower, 0}
|
|
|
|
// The constant UpperLower has (meaningful) value 1. The lower case
|
|
|
|
// letters in such sequences are assumed; were they present they would
|
|
|
|
// have a Delta of
|
|
|
|
// {LowerUpper, 0, LowerUpper}
|
|
|
|
// where LowerUpper has value -1.
|
2009-08-29 00:05:16 -06:00
|
|
|
type CaseRange struct {
|
|
|
|
Lo int;
|
|
|
|
Hi int;
|
|
|
|
Delta d;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indices into the Delta arrays inside CaseRanges for case mapping.
|
|
|
|
const (
|
|
|
|
UpperCase = iota;
|
|
|
|
LowerCase;
|
|
|
|
TitleCase;
|
|
|
|
MaxCase;
|
|
|
|
)
|
|
|
|
type d [MaxCase]int32 // to make the CaseRanges text shorter
|
|
|
|
|
|
|
|
// If the Delta field of a CaseRange is UpperLower or LowerUpper, it means
|
|
|
|
// this CaseRange represents a sequence of the form (say)
|
|
|
|
// Upper Lower Upper Lower.
|
|
|
|
const (
|
2009-08-30 15:02:42 -06:00
|
|
|
MaxChar = 0x10FFFF;
|
|
|
|
UpperLower = MaxChar + 2; // cannot be a valid delta
|
|
|
|
LowerUpper = MaxChar + 3;
|
2009-08-29 00:05:16 -06:00
|
|
|
)
|
|
|
|
|
2009-03-06 04:22:02 -07:00
|
|
|
// Is tests whether rune is in the specified table of ranges.
|
2009-01-20 15:40:40 -07:00
|
|
|
func Is(ranges []Range, rune int) bool {
|
2009-01-14 15:05:00 -07:00
|
|
|
// common case: rune is ASCII or Latin-1
|
|
|
|
if rune < 0x100 {
|
2009-08-26 17:53:07 -06:00
|
|
|
for i, r := range ranges {
|
2009-03-06 04:22:02 -07:00
|
|
|
if rune > r.Hi {
|
2009-01-14 15:05:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
2009-03-06 04:22:02 -07:00
|
|
|
if rune < r.Lo {
|
2009-01-14 15:05:00 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-06 04:22:02 -07:00
|
|
|
return (rune - r.Lo) % r.Stride == 0;
|
2009-01-14 15:05:00 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// binary search over ranges
|
|
|
|
lo := 0;
|
|
|
|
hi := len(ranges);
|
|
|
|
for lo < hi {
|
|
|
|
m := lo + (hi - lo)/2;
|
|
|
|
r := ranges[m];
|
2009-03-06 04:22:02 -07:00
|
|
|
if r.Lo <= rune && rune <= r.Hi {
|
|
|
|
return (rune - r.Lo) % r.Stride == 0;
|
2009-01-14 15:05:00 -07:00
|
|
|
}
|
2009-03-06 04:22:02 -07:00
|
|
|
if rune < r.Lo {
|
2009-01-14 15:05:00 -07:00
|
|
|
hi = m;
|
|
|
|
} else {
|
|
|
|
lo = m+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-26 17:01:31 -06:00
|
|
|
// IsUpper reports whether the rune is an upper case letter.
|
2009-01-20 15:40:40 -07:00
|
|
|
func IsUpper(rune int) bool {
|
2009-08-26 17:53:07 -06:00
|
|
|
if rune < 0x80 { // quick ASCII check
|
|
|
|
return 'A' <= rune && rune <= 'Z';
|
|
|
|
}
|
2009-01-14 15:05:00 -07:00
|
|
|
return Is(Upper, rune);
|
|
|
|
}
|
|
|
|
|
2009-08-26 17:01:31 -06:00
|
|
|
// IsLower reports whether the rune is a lower case letter.
|
|
|
|
func IsLower(rune int) bool {
|
2009-08-26 17:53:07 -06:00
|
|
|
if rune < 0x80 { // quick ASCII check
|
|
|
|
return 'a' <= rune && rune <= 'z';
|
|
|
|
}
|
2009-08-26 17:01:31 -06:00
|
|
|
return Is(Lower, rune);
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTitle reports whether the rune is a title case letter.
|
|
|
|
func IsTitle(rune int) bool {
|
2009-08-26 17:53:07 -06:00
|
|
|
if rune < 0x80 { // quick ASCII check
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-26 17:01:31 -06:00
|
|
|
return Is(Title, rune);
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:22:02 -07:00
|
|
|
// IsLetter reports whether the rune is a letter.
|
2009-01-20 15:40:40 -07:00
|
|
|
func IsLetter(rune int) bool {
|
2009-08-26 17:53:07 -06:00
|
|
|
if rune < 0x80 { // quick ASCII check
|
2009-08-30 15:02:42 -06:00
|
|
|
rune &^= 'a'-'A';
|
2009-08-26 17:53:07 -06:00
|
|
|
return 'A' <= rune && rune <= 'Z';
|
|
|
|
}
|
2009-01-14 15:05:00 -07:00
|
|
|
return Is(Letter, rune);
|
|
|
|
}
|
2009-08-29 00:05:16 -06:00
|
|
|
|
2009-08-30 15:02:42 -06:00
|
|
|
// In an Upper-Lower sequence, which always starts with an UpperCase letter,
|
|
|
|
// the real deltas always look like:
|
|
|
|
// 0 1 0
|
|
|
|
// -1 0 -1
|
|
|
|
// This is a single-dimensioned array addressed by the case shifted up one bit
|
|
|
|
// (the column of this table) or'ed with the low bit of the position in
|
|
|
|
// the sequence (the row of the table).
|
|
|
|
var ulDelta = [8]int{
|
|
|
|
(UpperCase<<1) | 0: 0,
|
|
|
|
(UpperCase<<1) | 1: -1,
|
|
|
|
(LowerCase<<1) | 0: 1,
|
|
|
|
(LowerCase<<1) | 1: 0,
|
|
|
|
(TitleCase<<1) | 0: 0,
|
|
|
|
(TitleCase<<1) | 1: -1,
|
|
|
|
}
|
|
|
|
|
2009-08-29 00:05:16 -06:00
|
|
|
// To maps the rune to the specified case, UpperCase, LowerCase, or TitleCase
|
|
|
|
func To(_case int, rune int) int {
|
|
|
|
if _case < 0 || MaxCase <= _case {
|
|
|
|
return 0xFFFD // as reasonable an error as any
|
|
|
|
}
|
|
|
|
// binary search over ranges
|
|
|
|
lo := 0;
|
|
|
|
hi := len(CaseRanges);
|
|
|
|
for lo < hi {
|
|
|
|
m := lo + (hi - lo)/2;
|
|
|
|
r := CaseRanges[m];
|
|
|
|
if r.Lo <= rune && rune <= r.Hi {
|
2009-08-30 15:02:42 -06:00
|
|
|
delta := int(r.Delta[_case]);
|
|
|
|
if delta > MaxChar {
|
|
|
|
// Somewhere inside an UpperLower sequence. Use
|
|
|
|
// the precomputed delta table to get our offset.
|
|
|
|
delta = ulDelta[((_case<<1) | ((rune-r.Lo)&1))];
|
|
|
|
}
|
|
|
|
return rune + delta;
|
2009-08-29 00:05:16 -06:00
|
|
|
}
|
|
|
|
if rune < r.Lo {
|
|
|
|
hi = m;
|
|
|
|
} else {
|
|
|
|
lo = m+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rune;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToUpper maps the rune to upper case
|
|
|
|
func ToUpper(rune int) int {
|
|
|
|
if rune < 0x80 { // quick ASCII check
|
|
|
|
if 'a' <= rune && rune <= 'z' {
|
2009-08-30 15:02:42 -06:00
|
|
|
rune -= 'a'-'A'
|
2009-08-29 00:05:16 -06:00
|
|
|
}
|
|
|
|
return rune
|
|
|
|
}
|
|
|
|
return To(UpperCase, rune);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToLower maps the rune to lower case
|
|
|
|
func ToLower(rune int) int {
|
|
|
|
if rune < 0x80 { // quick ASCII check
|
|
|
|
if 'A' <= rune && rune <= 'Z' {
|
2009-08-30 15:02:42 -06:00
|
|
|
rune += 'a'-'A'
|
2009-08-29 00:05:16 -06:00
|
|
|
}
|
|
|
|
return rune
|
|
|
|
}
|
|
|
|
return To(LowerCase, rune);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToTitle maps the rune to title case
|
|
|
|
func ToTitle(rune int) int {
|
|
|
|
if rune < 0x80 { // quick ASCII check
|
|
|
|
if 'a' <= rune && rune <= 'z' { // title case is upper case for ASCII
|
2009-08-30 15:02:42 -06:00
|
|
|
rune -= 'a'-'A'
|
2009-08-29 00:05:16 -06:00
|
|
|
}
|
|
|
|
return rune
|
|
|
|
}
|
|
|
|
return To(TitleCase, rune);
|
|
|
|
}
|