module scheme import encoding.utf8 // This cursor borrows its input synchronously. The pinned Unicode17 C/F // mappings expand to at most three scalars, checked by data generation/tests. struct UnicodeFoldCursor { text string mut: offset int pending0 i32 pending1 i32 pending2 i32 next_pending int pending_len int } fn (mut cursor UnicodeFoldCursor) next() ?rune { if cursor.next_pending < cursor.pending_len { value := if cursor.next_pending == 1 { cursor.pending1 } else { cursor.pending2 } cursor.next_pending++ return rune(value) } if cursor.offset >= cursor.text.len { return none } byte := cursor.text[cursor.offset] if byte < 0x80 { cursor.offset++ return if byte >= `A` && byte <= `Z` { rune(byte + 32) } else { rune(byte) } } ch, width := utf8.decode_rune_at(cursor.text, cursor.offset) cursor.offset += width mut pending := [3]i32{} count := C.vile_unicode17_fold(i32(ch), &pending[0]) cursor.pending0 = pending[0] cursor.pending1 = pending[1] cursor.pending2 = pending[2] if count < 1 || count > unicode_fold_max { panic('Unicode17 fold table invariant violated') } cursor.next_pending = 1 cursor.pending_len = int(count) return rune(cursor.pending0) } fn unicode_simple_case(ch rune, mode int) rune { if ch < 0x80 { return if mode == 1 { if ch >= `A` && ch <= `Z` { ch + 32 } else { ch } } else { if ch >= `a` && ch <= `z` { ch - 32 } else { ch } } } return rune(C.vile_unicode17_scalar(i32(ch), mode)) } fn unicode_simple_fold(ch rune) rune { if ch < 0x80 { return if ch >= `A` && ch <= `Z` { ch + 32 } else { ch } } mut low, mut high := 0, unicode_simple_folds.len / 2 for low < high { middle := (low + high) / 2 key := unicode_simple_folds[middle * 2] if u32(ch) == key { return rune(unicode_simple_folds[middle * 2 + 1]) } if u32(ch) < key { high = middle } else { low = middle + 1 } } return ch } fn unicode_alphabetic(ch rune) bool { if ch < 0x80 { return (ch >= `A` && ch <= `Z`) || (ch >= `a` && ch <= `z`) } mut low, mut high := 0, unicode_alphabetic_ranges.len / 2 for low < high { middle := (low + high) / 2 if u32(ch) < unicode_alphabetic_ranges[middle * 2] { high = middle } else if u32(ch) > unicode_alphabetic_ranges[middle * 2 + 1] { low = middle + 1 } else { return true } } return false } fn unicode_white_space(ch rune) bool { if ch < 0x80 { return ch == ` ` || (ch >= `\t` && ch <= `\r`) } mut low, mut high := 0, unicode_white_space_ranges.len / 2 for low < high { middle := (low + high) / 2 if u32(ch) < unicode_white_space_ranges[middle * 2] { high = middle } else if u32(ch) > unicode_white_space_ranges[middle * 2 + 1] { low = middle + 1 } else { return true } } return false } fn unicode_decimal(ch rune) int { if ch < 0x80 { return if ch >= `0` && ch <= `9` { int(ch - `0`) } else { -1 } } mut low, mut high := 0, unicode_decimal_starts.len for low < high { middle := (low + high) / 2 start := unicode_decimal_starts[middle] if u32(ch) < start { high = middle } else if u32(ch) >= start + 10 { low = middle + 1 } else { return int(u32(ch) - start) } } return -1 } fn unicode_case_property(ch rune, upper bool) bool { if ch < 0x80 { return if upper { ch >= `A` && ch <= `Z` } else { ch >= `a` && ch <= `z` } } return C.vile_unicode17_property(i32(ch), if upper { 0 } else { 1 }) != 0 } fn compare_folded_text(a string, b string) int { mut left := UnicodeFoldCursor{ text: a } mut right := UnicodeFoldCursor{ text: b } for { x := left.next() or { _ := right.next() or { return 0 } return -1 } y := right.next() or { return 1 } if x < y { return -1 } if x > y { return 1 } } return 0 }