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-03-06 04:22:02 -07:00
|
|
|
Lo int;
|
|
|
|
Hi int;
|
|
|
|
Stride int;
|
2009-01-14 15:05:00 -07: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
|
|
|
|
rune &^= ' ';
|
|
|
|
return 'A' <= rune && rune <= 'Z';
|
|
|
|
}
|
2009-01-14 15:05:00 -07:00
|
|
|
return Is(Letter, rune);
|
|
|
|
}
|