vile / scheme / unicode_case.v
133 lines · 123 sloc · 3.62 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import encoding.utf8
4
5// This cursor borrows its input synchronously. The pinned Unicode17 C/F
6// mappings expand to at most three scalars, checked by data generation/tests.
7struct UnicodeFoldCursor {
8 text string
9mut:
10 offset int
11 pending0 i32
12 pending1 i32
13 pending2 i32
14 next_pending int
15 pending_len int
16}
17
18fn (mut cursor UnicodeFoldCursor) next() ?rune {
19 if cursor.next_pending < cursor.pending_len {
20 value := if cursor.next_pending == 1 { cursor.pending1 } else { cursor.pending2 }
21 cursor.next_pending++
22 return rune(value)
23 }
24 if cursor.offset >= cursor.text.len { return none }
25 byte := cursor.text[cursor.offset]
26 if byte < 0x80 {
27 cursor.offset++
28 return if byte >= `A` && byte <= `Z` { rune(byte + 32) } else { rune(byte) }
29 }
30 ch, width := utf8.decode_rune_at(cursor.text, cursor.offset)
31 cursor.offset += width
32 mut pending := [3]i32{}
33 count := C.vile_unicode17_fold(i32(ch), &pending[0])
34 cursor.pending0 = pending[0]
35 cursor.pending1 = pending[1]
36 cursor.pending2 = pending[2]
37 if count < 1 || count > unicode_fold_max { panic('Unicode17 fold table invariant violated') }
38 cursor.next_pending = 1
39 cursor.pending_len = int(count)
40 return rune(cursor.pending0)
41}
42
43fn unicode_simple_case(ch rune, mode int) rune {
44 if ch < 0x80 {
45 return if mode == 1 {
46 if ch >= `A` && ch <= `Z` { ch + 32 } else { ch }
47 } else {
48 if ch >= `a` && ch <= `z` { ch - 32 } else { ch }
49 }
50 }
51 return rune(C.vile_unicode17_scalar(i32(ch), mode))
52}
53
54fn unicode_simple_fold(ch rune) rune {
55 if ch < 0x80 { return if ch >= `A` && ch <= `Z` { ch + 32 } else { ch } }
56 mut low, mut high := 0, unicode_simple_folds.len / 2
57 for low < high {
58 middle := (low + high) / 2
59 key := unicode_simple_folds[middle * 2]
60 if u32(ch) == key { return rune(unicode_simple_folds[middle * 2 + 1]) }
61 if u32(ch) < key { high = middle } else { low = middle + 1 }
62 }
63 return ch
64}
65
66fn unicode_alphabetic(ch rune) bool {
67 if ch < 0x80 { return (ch >= `A` && ch <= `Z`) || (ch >= `a` && ch <= `z`) }
68 mut low, mut high := 0, unicode_alphabetic_ranges.len / 2
69 for low < high {
70 middle := (low + high) / 2
71 if u32(ch) < unicode_alphabetic_ranges[middle * 2] {
72 high = middle
73 } else if u32(ch) > unicode_alphabetic_ranges[middle * 2 + 1] {
74 low = middle + 1
75 } else {
76 return true
77 }
78 }
79 return false
80}
81
82fn unicode_white_space(ch rune) bool {
83 if ch < 0x80 { return ch == ` ` || (ch >= `\t` && ch <= `\r`) }
84 mut low, mut high := 0, unicode_white_space_ranges.len / 2
85 for low < high {
86 middle := (low + high) / 2
87 if u32(ch) < unicode_white_space_ranges[middle * 2] {
88 high = middle
89 } else if u32(ch) > unicode_white_space_ranges[middle * 2 + 1] {
90 low = middle + 1
91 } else {
92 return true
93 }
94 }
95 return false
96}
97
98fn unicode_decimal(ch rune) int {
99 if ch < 0x80 { return if ch >= `0` && ch <= `9` { int(ch - `0`) } else { -1 } }
100 mut low, mut high := 0, unicode_decimal_starts.len
101 for low < high {
102 middle := (low + high) / 2
103 start := unicode_decimal_starts[middle]
104 if u32(ch) < start {
105 high = middle
106 } else if u32(ch) >= start + 10 {
107 low = middle + 1
108 } else {
109 return int(u32(ch) - start)
110 }
111 }
112 return -1
113}
114
115fn unicode_case_property(ch rune, upper bool) bool {
116 if ch < 0x80 { return if upper { ch >= `A` && ch <= `Z` } else { ch >= `a` && ch <= `z` } }
117 return C.vile_unicode17_property(i32(ch), if upper { 0 } else { 1 }) != 0
118}
119
120fn compare_folded_text(a string, b string) int {
121 mut left := UnicodeFoldCursor{ text: a }
122 mut right := UnicodeFoldCursor{ text: b }
123 for {
124 x := left.next() or {
125 _ := right.next() or { return 0 }
126 return -1
127 }
128 y := right.next() or { return 1 }
129 if x < y { return -1 }
130 if x > y { return 1 }
131 }
132 return 0
133}
134