encoding.utf8: fix is_punct func (fix #22646) (#22647)

This commit is contained in:
Felipe Pena 2024-10-26 04:33:31 -03:00 committed by GitHub
parent b5f022d804
commit 419645a045
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 4 deletions

View File

@ -0,0 +1,33 @@
module main
import encoding.utf8
fn test_true() {
assert utf8.is_punct('!', 0)
assert utf8.is_rune_punct(`!`)
assert utf8.is_punct('.', 0)
assert utf8.is_rune_punct(`.`)
assert utf8.is_punct(',', 0)
assert utf8.is_rune_punct(`,`)
assert utf8.is_punct(';', 0)
assert utf8.is_rune_punct(`;`)
assert utf8.is_punct("'", 0)
assert utf8.is_rune_punct(`'`)
assert utf8.is_punct(',', 0)
assert utf8.is_rune_punct(`,`)
assert utf8.is_punct('/', 0)
assert utf8.is_rune_punct(`/`)
assert utf8.is_punct('*', 0)
assert utf8.is_rune_punct(`*`)
}
fn test_false() {
assert !utf8.is_punct('a', 0)
assert !utf8.is_rune_punct(`a`)
assert !utf8.is_punct('ç', 0)
assert !utf8.is_rune_punct(`ç`)
assert !utf8.is_punct('á', 0)
assert !utf8.is_rune_punct(`á`)
assert !utf8.is_punct('-', 0)
assert !utf8.is_rune_punct(`-`)
}

View File

@ -190,7 +190,7 @@ pub fn is_uchar_punct(uchar int) bool {
// is_rune_punct return true if the input unicode is a western unicode punctuation // is_rune_punct return true if the input unicode is a western unicode punctuation
pub fn is_rune_punct(r rune) bool { pub fn is_rune_punct(r rune) bool {
return find_punct_in_table(r, unicode_punct_western) != 0 return find_punct_in_table(r, unicode_punct_western) != rune(-1)
} }
// Global // Global
@ -209,7 +209,7 @@ pub fn is_uchar_global_punct(uchar int) bool {
// is_rune_global_punct return true if the input unicode is a global unicode punctuation // is_rune_global_punct return true if the input unicode is a global unicode punctuation
pub fn is_rune_global_punct(r rune) bool { pub fn is_rune_global_punct(r rune) bool {
return find_punct_in_table(r, unicode_punct) != 0 return find_punct_in_table(r, unicode_punct) != rune(-1)
} }
// Private functions // Private functions
@ -553,7 +553,6 @@ fn find_punct_in_table(in_code rune, in_table []rune) rune {
mut x := rune(0) mut x := rune(0)
for { for {
index = (first_index + last_index) >> 1
x = in_table[index] x = in_table[index]
// C.printf("(%d..%d) index:%d base[%08x]==>[%08x]\n",first_index,last_index,index,in_code,x) // C.printf("(%d..%d) index:%d base[%08x]==>[%08x]\n",first_index,last_index,index,in_code,x)
@ -568,9 +567,10 @@ fn find_punct_in_table(in_code rune, in_table []rune) rune {
if (last_index - first_index) <= 1 { if (last_index - first_index) <= 1 {
break break
} }
index = (first_index + last_index) >> 1
} }
return 0 return -1
} }
// Unicode punctuation chars // Unicode punctuation chars