Debian Codegraph @

internal/tables/assigned/assigned.go

raw
1 package assigned
2
3 import (
4 "sync"
5 "unicode"
6 )
7
8 // Assigned returns a RangeTable with all assigned code points for a given
9 // Unicode version. This includes graphic, format, control, and private-use
10 // characters. It returns nil if the data for the given version is not
11 // available.
12 func Assigned(version string) *unicode.RangeTable {
13 return assigned[version]
14 }
15
16 var runes sync.Map
17
18 // Assigned returns a slice of runes with all assigned code points for a given
19 // Unicode version. This includes graphic, format, control, and private-use
20 // characters. An empty slice is returned if the data for the given version is
21 // not available.
22 func AssignedRunes(version string) []rune {
23 if v, ok := runes.Load(version); ok {
24 return v.(func() []rune)()
25 }
26 rt := Assigned(version)
27 if rt == nil {
28 return nil
29 }
30 var all []rune
31 var once sync.Once
32 fn := func() []rune {
33 once.Do(func() {
34 n := 0
35 visit(rt, func(_ rune) {
36 n++
37 })
38 all = make([]rune, 0, n)
39 visit(rt, func(r rune) {
40 all = append(all, r)
41 })
42 })
43 return all
44 }
45 if v, loaded := runes.LoadOrStore(version, fn); loaded {
46 return v.(func() []rune)()
47 }
48 return fn()
49 }
50
51 // visit visits all runes in the given RangeTable in order, calling fn for each.
52 func visit(rt *unicode.RangeTable, fn func(rune)) {
53 for _, r16 := range rt.R16 {
54 for r := rune(r16.Lo); r <= rune(r16.Hi); r += rune(r16.Stride) {
55 fn(r)
56 }
57 }
58 for _, r32 := range rt.R32 {
59 for r := rune(r32.Lo); r <= rune(r32.Hi); r += rune(r32.Stride) {
60 fn(r)
61 }
62 }
63 }