unicode classifies runes by Unicode category, converts between cases, and exposes the range tables that back both.
Every classification and case function takes a single rune and returns a plain bool or rune, no allocation, no error to check.
r := '5'
fmt.Println(unicode.IsDigit(r), unicode.IsLetter(r))Examples
Each Is* function checks membership in a specific Unicode category. IsLetter recognizes any script's letters, not just ASCII.
runes := []rune{'A', 'a', '5', ' ', '.', '中'}
for _, r := range runes {
fmt.Printf("%c: letter=%t upper=%t punct=%t\n", r, unicode.IsLetter(r), unicode.IsUpper(r), unicode.IsPunct(r))
}A: letter=true upper=true punct=false
a: letter=true upper=false punct=false
5: letter=false upper=false punct=false
: letter=false upper=false punct=false
.: letter=false upper=false punct=true
中: letter=true upper=false punct=falseToTitle exists separately from ToUpper because a few digraph letters have three distinct forms. For everything else the two agree.
fmt.Println(string(unicode.ToUpper('a')))
fmt.Println(string(unicode.ToLower('A')))
dz := 'dž'
fmt.Println(string(unicode.ToUpper(dz)), string(unicode.ToTitle(dz)))A
a
DŽ DžIn and Is check a rune against a RangeTable directly, the same mechanism the predicate functions use internally against their own predefined tables.
fmt.Println(unicode.In('中', unicode.Han))
fmt.Println(unicode.In('A', unicode.Han))
fmt.Println(unicode.Is(unicode.Greek, 'Ω'))
fmt.Println(unicode.IsOneOf([]*unicode.RangeTable{unicode.Han, unicode.Greek}, 'Ω'))true
false
true
trueTurkish has two distinct i/I pairs that the default Unicode casing rules collapse into one. TurkishCase keeps them separate.
fmt.Println(string(unicode.ToLower('I')))
fmt.Println(string(unicode.TurkishCase.ToLower('I')))
fmt.Println(string(unicode.ToUpper('i')))
fmt.Println(string(unicode.TurkishCase.ToUpper('i')))i
ı
I
İPredicates
| Function | Description |
|---|---|
IsControl(r rune) bool unicode.IsControl('\n') | Reports whether r is a control character |
IsDigit(r rune) bool unicode.IsDigit('7') | Reports whether r is a decimal digit |
IsGraphic(r rune) bool unicode.IsGraphic('A') | Reports whether r is a graphic character: letter, mark, number, punctuation, symbol, or space |
IsLetter(r rune) bool unicode.IsLetter('文') | Reports whether r is a letter |
IsLower(r rune) bool unicode.IsLower('a') | Reports whether r is a lowercase letter |
IsMark(r rune) bool unicode.IsMark(r) | Reports whether r is a mark character, such as a combining accent |
IsNumber(r rune) bool unicode.IsNumber('Ⅷ') | Reports whether r is a number, a wider definition than IsDigit's decimal-only check |
IsPrint(r rune) bool unicode.IsPrint(' ') | Reports whether r is printable: IsGraphic plus the space character |
IsPunct(r rune) bool unicode.IsPunct('!') | Reports whether r is a punctuation character |
IsSpace(r rune) bool unicode.IsSpace('\t') | Reports whether r is a space character under Unicode's definition |
IsSymbol(r rune) bool unicode.IsSymbol('+') | Reports whether r is a symbol character |
IsTitle(r rune) bool unicode.IsTitle('Dž') | Reports whether r is a title-case letter |
IsUpper(r rune) bool unicode.IsUpper('A') | Reports whether r is an uppercase letter |
Case conversion
| Function | Description |
|---|---|
ToUpper(r rune) rune unicode.ToUpper('a') | Returns the uppercase form of r |
ToLower(r rune) rune unicode.ToLower('A') | Returns the lowercase form of r |
ToTitle(r rune) rune unicode.ToTitle('dž') | Returns the title-case form of r, distinct from ToUpper for a handful of digraph letters |
To(_case int, r rune) rune unicode.To(unicode.UpperCase, 'a') | Maps r to the given case constant: UpperCase, LowerCase, or TitleCase |
SimpleFold(r rune) rune unicode.SimpleFold('A') | Returns the next rune in r's case-folding equivalence class, wrapping back to the start of the orbit |
(SpecialCase) ToUpper / ToLower / ToTitle(r rune) rune unicode.TurkishCase.ToLower('I') | Applies a language-specific case mapping, such as TurkishCase, instead of the default rules |
Range membership
| Function | Description |
|---|---|
Is(rangeTab *RangeTable, r rune) bool unicode.Is(unicode.Greek, 'Ω') | Reports whether r is in the given range table |
IsOneOf(ranges []*RangeTable, r rune) bool unicode.IsOneOf(tables, r) | Reports whether r is in any of the given range tables |
In(r rune, ranges ...*RangeTable) bool unicode.In('中', unicode.Han) | Reports whether r is in any of the given range tables, as a variadic call instead of a slice |
Types
| Function | Description |
|---|---|
RangeTable struct{ R16 []Range16; R32 []Range32; LatinOffset int } unicode.Han | A compact, searchable set of runes; the type behind Letter, Digit, Han, and every other predefined table |
Range16 struct{ Lo, Hi, Stride uint16 } (built by the unicode package) | A contiguous run of 16-bit code points at a fixed stride, one entry in a RangeTable |
Range32 struct{ Lo, Hi, Stride uint32 } (built by the unicode package) | Like Range16, for code points above the 16-bit range |
CaseRange struct{ Lo, Hi uint32; Delta [MaxCase]rune } (built by the unicode package) | One entry in a SpecialCase mapping table |
SpecialCase []CaseRange unicode.TurkishCase | A set of case mappings that override the default Unicode rules for a specific language |
Constants
| Function | Description |
|---|---|
MaxRune unicode.MaxRune | The highest valid Unicode code point, U+10FFFF |
ReplacementChar unicode.ReplacementChar | The rune used to represent invalid or unknown code points, U+FFFD |
MaxASCII unicode.MaxASCII | The highest ASCII code point, U+007F |
MaxLatin1 unicode.MaxLatin1 | The highest Latin-1 code point, U+00FF |
UpperCase, LowerCase, TitleCase, OtherCase unicode.To(unicode.LowerCase, r) | The case constants accepted by To |
MaxCase [unicode.MaxCase]rune{} | The number of case constants, used to size a SpecialCase's Delta array |
Predefined range tables
| Function | Description |
|---|---|
Letter, Digit, Number, Punct, Space, Symbol, Mark, Control unicode.Is(unicode.Letter, r) | General category range tables; each backs the matching Is* function |
Upper, Lower, Title unicode.Is(unicode.Upper, r) | Case-specific range tables; each backs the matching Is* function |
Categories map[string]*RangeTable unicode.Categories["Lu"] | Every general category table, keyed by its short name, such as "Lu" or "Nd" |
Scripts map[string]*RangeTable unicode.Scripts["Greek"] | Every script table, keyed by name, such as Han, Greek, or Cyrillic |
Properties map[string]*RangeTable unicode.Properties["White_Space"] | Every Unicode property table, keyed by name |
TurkishCase, AzeriCase SpecialCase unicode.TurkishCase | Predefined SpecialCase values for languages where the default casing rules are wrong |