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
|
|
|
|
// means the character is in the corresponding case.
|
|
|
|
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 (
|
|
|
|
UpperLower = 1;
|
|
|
|
LowerUpper = -1;
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
rune &^= ' ';
|
|
|
|
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
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return rune + int(r.Delta[_case]);
|
|
|
|
}
|
|
|
|
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' {
|
|
|
|
rune &^= ' '
|
|
|
|
}
|
|
|
|
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' {
|
|
|
|
rune |= ' '
|
|
|
|
}
|
|
|
|
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
|
|
|
|
rune &^= ' '
|
|
|
|
}
|
|
|
|
return rune
|
|
|
|
}
|
|
|
|
return To(TitleCase, rune);
|
|
|
|
}
|