What Unicode knows about a character: category, script, writing direction, case, decomposition and composition.
import unicode
Unicode gives every character of every script in the world a number, called a code point: "ش" is 1588 and "A" is 65. For any code point, the unicode package says what kind of character it is: letter or digit, which script it belongs to, whether it is written right to left or left to right, and what its upper and lower case forms are.
Most of the time the str package is enough, and it uses this one underneath. This package is for when you are inspecting text character by character yourself, in a parser, an editor or a text cleaning tool. The data is generated from the official Unicode database, version 16.
To get the code points of a string, use str.CodePoints.
Two things to keep in mind:
•
A code point is not always what a reader sees as one letter. An accented letter can be two code points and an emoji family seven; str.Graphemes works at that level.
•
ToUpper, ToLower and ToTitle are the simple one to one mappings, so a character whose upper case is two letters keeps its form: ß stays ß. To compare text without case, use the full fold behind str.EqualFold rather than comparing upper cased copies.
Counting kinds of characters
A loop over str.CodePoints with the Is... functions sorts every character into a class. The classes come from the Unicode database, so they work for every script at once: the Persian digits count as digits, the Persian comma ، as punctuation and the kasra on سلامِ as a mark.
unicode-guide-stats.salam
import unicode
importstrfuncmain:
text := "Salam 2026! سلامِ ۱۴۰۵، café (ok)"
cps := str.CodePoints(text)
defer cps.Free()
mut letters := 0mut digits := 0mut spaces := 0mut punct := 0mut marks := 0each cp in cps:
if unicode.IsLetter(cp):
letters++
else unicode.IsDigit(cp):
digits++
else unicode.IsSpace(cp):
spaces++
else unicode.IsPunct(cp):
punct++
else unicode.IsMark(cp):
marks++
endendprintln"code points:", cps.Len()
println"letters:", letters, "digits:", digits, "spaces:", spaces
println"punctuation:", punct, "marks:", marks
end
The order of the tests matters only for characters that belong to two groups; letters, digits, spaces, punctuation and marks do not overlap. For finer detail, Category returns one of the 30 general categories, compared against the constants such as CatLo, and CategoryName its two letter code.
Guessing the direction of a line
When a page shows text written by users, each paragraph needs dir="rtl" or dir="ltr". The usual rule, the one browsers apply for dir="auto", is to take the direction of the first strong character. BidiClass gives that class: BcL for left to right, BcR for Hebrew and BcAL for Arabic script.
unicode-guide-direction.salam
import unicode
importstrfuncdirection(text: str): str:
cps := str.CodePoints(text)
defer cps.Free()
each cp in cps:
bc := unicode.BidiClass(cp)
if bc == unicode.BcL:
ret"ltr"endif bc == unicode.BcR or bc == unicode.BcAL:
ret"rtl"endendret"neutral"endfuncmain:
each line in ["Hello", "سلام دنیا", "2026: سال نو", "(42) Answer", "!!! 123", "שלום"]:
printlndirection(line), "|", line
endend
Output
ltr | Hello
rtl | سلام دنیا
rtl | 2026: سال نو
ltr | (42) Answer
neutral | !!! 123
rtl | שלום
Digits, spaces and punctuation are weak or neutral, so 2026: سال نو is right to left because the first letter is Persian. A line with no letters at all has no direction of its own; give it the direction of the surrounding page. For laying out a whole mixed paragraph, see bidi, which runs the complete algorithm.
Spotting mixed scripts in a user name
Some letters in different scripts look the same: the Cyrillic а is not the Latin a, yet pаypal with a Cyrillic letter looks exactly like the real name. A sign up form can refuse names that mix scripts. Script gives the script of each code point; skip ScriptCommon (digits, punctuation, _) and ScriptInherited (combining marks), which belong to every script.
unicode-guide-scripts.salam
import unicode
importstrfuncscripts_in(name: str): Vector<str>:
mut found := Vector {} asVector<str>
cps := str.CodePoints(name)
defer cps.Free()
each cp in cps:
sc := unicode.Script(cp)
if sc == unicode.ScriptCommon or sc == unicode.ScriptInherited:
continueend
script := unicode.ScriptName(cp)
mut seen := falseeach s in found:
if s == script:
seen = trueendendifnot seen:
found.Push(script)
endendret found
endfuncmain:
each name in ["paypal", "pаypal", "علی_2000", "Reza۱۳۷۰"]:
s := scripts_in(name)
verdict := s.Len() > 1 ? "mixed, check it" : "ok"println name, s, verdict
s.Free()
endend
Output
paypal ["Latin"] ok
pаypal ["Latin", "Cyrillic"] mixed, check it
علی_2000 ["Arabic"] ok
Reza۱۳۷۰ ["Latin", "Arabic"] mixed, check it
The last name shows a trap: the Persian digits ۱۳۷۰ belong to the Arabic script, while the ASCII digits are Common. Whether a Latin name with Persian digits should pass is your decision; the function only reports the facts. The а escape writes the Cyrillic letter into the source so you can see it is there.
Removing accents and vowel marks
For a loose search, café should match cafe. Canonical decomposition splits é into e and a combining accent; drop every mark and keep the rest. DecompLen says how many parts a code point decomposes into, zero when it does not, and DecompAt returns each part.
unicode-guide-marks.salam
import unicode
importstrfuncstrip_marks(text: str): str:
mut b := str.NewBuilder()
deferstr.BufFree(b)
cps := str.CodePoints(text)
defer cps.Free()
each cp in cps:
n := unicode.DecompLen(cp)
if n == 0:
ifnot unicode.IsMark(cp):
str.BufAppendCp(b, cp)
endcontinueendrepeat n in k:
part := unicode.DecompAt(cp, k)
ifnot unicode.IsMark(part):
str.BufAppendCp(b, part)
endendendretstr.BufStr(b)
endfuncmain:
printlnstrip_marks("café crème brûlée")
printlnstrip_marks("Ångström, naïve, São Paulo")
printlnstrip_marks("کِتابُ الْعَرَبِیَّة")
printlnstrip_marks("آموزشِ زبانِ برنامهنویسیِ سلام")
end
Output
cafe creme brulee
Angstrom, naive, Sao Paulo
کتاب العربیة
اموزش زبان برنامهنویسی سلام
The Arabic vowel marks go away, which is what a search usually wants. But look at the last line: آ decomposes into ا plus a madda, so the madda is removed too and آموزش becomes اموزش. That is wrong for Persian. For Persian and Arabic text use arabic, whose StripHarakat flag removes the vowel marks and leaves letters such as آ alone, and for whole strings use str.NFD and NFC rather than decomposing one level by hand.
Upper case in every script
str.Upper and str.Lower only change the ASCII letters, which is fast and enough for keywords and codes, but it leaves ü, Cyrillic and Greek alone. To change the case of any script, map each code point with ToUpper or ToLower and build the result again.
unicode-guide-upper.salam
import unicode
importstrfuncupper_all(text: str): str:
mut b := str.NewBuilder()
deferstr.BufFree(b)
cps := str.CodePoints(text)
defer cps.Free()
each cp in cps:
str.BufAppendCp(b, unicode.ToUpper(cp))
endretstr.BufStr(b)
endfuncmain:
each city in ["Zürich", "São Paulo", "Москва", "Αθήνα", "Straße"]:
printlnstr.Upper(city), "|", upper_all(city)
endend
Output
ZüRICH | ZÜRICH
SãO PAULO | SÃO PAULO
Москва | МОСКВА
Αθήνα | ΑΘΉΝΑ
STRAßE | STRAßE
The left column is str.Upper, the right one the per code point mapping. The last line shows the limit of a simple mapping: ß has no single upper case letter, so it stays as it is. Persian and Arabic have no letter case, and these functions return their letters unchanged.
The numbers needed to compose and decompose Korean (Hangul) syllables arithmetically. Each syllable is built from a leading consonant, a vowel and sometimes a trailing consonant, and its code point can be computed directly.
Every Unicode character has a "general category". Category returns one of these numbers and CategoryName returns its two-letter short name, such as Lo. Categories whose first letter is the same are numbered consecutively.
Name
Value
CatLu
0
CatLl
1
CatLt
2
CatLm
3
CatLo
4
CatMn
5
CatMc
6
CatMe
7
CatNd
8
CatNl
9
CatNo
10
CatPc
11
CatPd
12
CatPs
13
CatPe
14
CatPi
15
CatPf
16
CatPo
17
CatSm
18
CatSc
19
CatSk
20
CatSo
21
CatZs
22
CatZl
23
CatZp
24
CatCc
25
CatCf
26
CatCs
27
CatCo
28
CatCn
29
unicode-Category.salam
import unicode
funcmain:
each c in [65, 1588, 1776, 1614, 8204, 12288, 888]:
println c, unicode.CategoryName(c), unicode.Category(c), unicode.IsAssigned(c)
endprintln unicode.Category(1588) == unicode.CatLo
end
The classes GraphemeBreak returns. The str package uses them to find where one "visible letter" ends and the next begins; a letter with its marks, or a composed emoji, is one cluster.
The class BidiClass returns, which the bidirectional algorithm uses to decide where each character of mixed text appears. Arabic script letters are "Arabic", Latin letters are "left to right", and digits have classes of their own.
Every script Unicode knows has an identifier, and Script returns one of them. Persian and Arabic letters are both "Arabic script". "Common" characters such as digits and punctuation are used by several scripts, and "inherited" means the character takes the script of the character before it, as marks do.
Name
Value
ScriptUnknown
0
ScriptCommon
1
ScriptLatin
2
ScriptGreek
3
ScriptCyrillic
4
ScriptArmenian
5
ScriptHebrew
6
ScriptArabic
7
ScriptSyriac
8
ScriptThaana
9
ScriptDevanagari
10
ScriptBengali
11
ScriptGurmukhi
12
ScriptGujarati
13
ScriptOriya
14
ScriptTamil
15
ScriptTelugu
16
ScriptKannada
17
ScriptMalayalam
18
ScriptSinhala
19
ScriptThai
20
ScriptLao
21
ScriptTibetan
22
ScriptMyanmar
23
ScriptGeorgian
24
ScriptHangul
25
ScriptEthiopic
26
ScriptCherokee
27
ScriptCanadianAboriginal
28
ScriptOgham
29
ScriptRunic
30
ScriptKhmer
31
ScriptMongolian
32
ScriptHiragana
33
ScriptKatakana
34
ScriptBopomofo
35
ScriptHan
36
ScriptYi
37
ScriptOldItalic
38
ScriptGothic
39
ScriptDeseret
40
ScriptInherited
41
ScriptTagalog
42
ScriptHanunoo
43
ScriptBuhid
44
ScriptTagbanwa
45
ScriptLimbu
46
ScriptTaiLe
47
ScriptLinearB
48
ScriptUgaritic
49
ScriptShavian
50
ScriptOsmanya
51
ScriptCypriot
52
ScriptBraille
53
ScriptBuginese
54
ScriptCoptic
55
ScriptNewTaiLue
56
ScriptGlagolitic
57
ScriptTifinagh
58
ScriptSylotiNagri
59
ScriptOldPersian
60
ScriptKharoshthi
61
ScriptBalinese
62
ScriptCuneiform
63
ScriptPhoenician
64
ScriptPhagsPa
65
ScriptNko
66
ScriptSundanese
67
ScriptLepcha
68
ScriptOlChiki
69
ScriptVai
70
ScriptSaurashtra
71
ScriptKayahLi
72
ScriptRejang
73
ScriptLycian
74
ScriptCarian
75
ScriptLydian
76
ScriptCham
77
ScriptTaiTham
78
ScriptTaiViet
79
ScriptAvestan
80
ScriptEgyptianHieroglyphs
81
ScriptSamaritan
82
ScriptLisu
83
ScriptBamum
84
ScriptJavanese
85
ScriptMeeteiMayek
86
ScriptImperialAramaic
87
ScriptOldSouthArabian
88
ScriptInscriptionalParthian
89
ScriptInscriptionalPahlavi
90
ScriptOldTurkic
91
ScriptKaithi
92
ScriptBatak
93
ScriptBrahmi
94
ScriptMandaic
95
ScriptChakma
96
ScriptMeroiticCursive
97
ScriptMeroiticHieroglyphs
98
ScriptMiao
99
ScriptSharada
100
ScriptSoraSompeng
101
ScriptTakri
102
ScriptCaucasianAlbanian
103
ScriptBassaVah
104
ScriptDuployan
105
ScriptElbasan
106
ScriptGrantha
107
ScriptPahawhHmong
108
ScriptKhojki
109
ScriptLinearA
110
ScriptMahajani
111
ScriptManichaean
112
ScriptMendeKikakui
113
ScriptModi
114
ScriptMro
115
ScriptOldNorthArabian
116
ScriptNabataean
117
ScriptPalmyrene
118
ScriptPauCinHau
119
ScriptOldPermic
120
ScriptPsalterPahlavi
121
ScriptSiddham
122
ScriptKhudawadi
123
ScriptTirhuta
124
ScriptWarangCiti
125
ScriptAhom
126
ScriptAnatolianHieroglyphs
127
ScriptHatran
128
ScriptMultani
129
ScriptOldHungarian
130
ScriptSignWriting
131
ScriptAdlam
132
ScriptBhaiksuki
133
ScriptMarchen
134
ScriptNewa
135
ScriptOsage
136
ScriptTangut
137
ScriptMasaramGondi
138
ScriptNushu
139
ScriptSoyombo
140
ScriptZanabazarSquare
141
ScriptDogra
142
ScriptGunjalaGondi
143
ScriptMakasar
144
ScriptMedefaidrin
145
ScriptHanifiRohingya
146
ScriptSogdian
147
ScriptOldSogdian
148
ScriptElymaic
149
ScriptNandinagari
150
ScriptNyiakengPuachueHmong
151
ScriptWancho
152
ScriptChorasmian
153
ScriptDivesAkuru
154
ScriptKhitanSmallScript
155
ScriptYezidi
156
ScriptCyproMinoan
157
ScriptOldUyghur
158
ScriptTangsa
159
ScriptToto
160
ScriptVithkuqi
161
ScriptKawi
162
ScriptNagMundari
163
ScriptGaray
164
ScriptGurungKhema
165
ScriptKiratRai
166
ScriptOlOnal
167
ScriptSunuwar
168
ScriptTodhri
169
ScriptTuluTigalari
170
unicode-Script.salam
import unicode
funcmain:
each c in [65, 1588, 945, 20013, 12354, 1776, 1614]:
println c, unicode.ScriptName(c)
endprintln unicode.Script(1588) == unicode.ScriptArabic, unicode.Script(65) == unicode.ScriptLatin
println unicode.Script(66464) == unicode.ScriptOldPersian, unicode.ScriptName(66464)
end
Output
65 Latin
1588 Arabic
945 Greek
20013 Han
12354 Hiragana
1776 Arabic
1614 Inherited
true true
true Old_Persian
Returns the combining class of a mark. During normalisation, the marks following a letter are sorted by this number so that the order they were typed in does not matter. For ordinary characters it is zero.