Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,88 @@
|
||||
import encoding.utf8.validate
|
||||
|
||||
fn test_validate_str() {
|
||||
assert validate.utf8_string('añçá') == true
|
||||
assert validate.utf8_string('\x61\xC3\xB1\xC3\xA7\xC3\xA1') == true
|
||||
|
||||
assert validate.utf8_string('\x01') == true
|
||||
assert validate.utf8_string('\x7e') == true
|
||||
assert validate.utf8_string('\x7f') == true
|
||||
assert validate.utf8_string('\xc2\x80') == true
|
||||
assert validate.utf8_string('\xc2\x81') == true
|
||||
assert validate.utf8_string('\xc2\xbf') == true
|
||||
assert validate.utf8_string('\xc3\x80') == true
|
||||
assert validate.utf8_string('\xc3\x81') == true
|
||||
assert validate.utf8_string('\xc3\x88') == true
|
||||
assert validate.utf8_string('\xc3\x90') == true
|
||||
assert validate.utf8_string('\xc3\xa0') == true
|
||||
assert validate.utf8_string('\xc3\xb0') == true
|
||||
assert validate.utf8_string('\xc3\xb8') == true
|
||||
assert validate.utf8_string('\xc3\xbf') == true
|
||||
assert validate.utf8_string('\xc4\x80') == true
|
||||
assert validate.utf8_string('\xdf\xbf') == true
|
||||
assert validate.utf8_string('\xd0\x80') == true
|
||||
assert validate.utf8_string('\xe0\xa0\x80') == true
|
||||
assert validate.utf8_string('\xe0\xa0\x81') == true
|
||||
assert validate.utf8_string('\xe1\x80\x80') == true
|
||||
assert validate.utf8_string('\xed\x80\x80') == true
|
||||
assert validate.utf8_string('\xed\x9f\xbf') == true
|
||||
assert validate.utf8_string('\xee\x80\x80') == true
|
||||
assert validate.utf8_string('\xef\xbf\xbe') == true
|
||||
assert validate.utf8_string('\xef\xbf\xbf') == true
|
||||
assert validate.utf8_string('\xf0\x90\x80\x80') == true
|
||||
assert validate.utf8_string('\xf0\x90\x80\x81') == true
|
||||
assert validate.utf8_string('\xf1\x80\x80\x80') == true
|
||||
assert validate.utf8_string('\xf4\x8f\xbf\xbe') == true
|
||||
assert validate.utf8_string('\xf4\x8f\xbf\xbf') == true
|
||||
assert validate.utf8_string('\xef\xbf\xbd') == true
|
||||
}
|
||||
|
||||
fn test_validate_invalid_str() {
|
||||
assert validate.utf8_string('\xC0\xC1') == false
|
||||
assert validate.utf8_string('\xF5\xFF') == false
|
||||
assert validate.utf8_string('\xE0\xEF') == false
|
||||
|
||||
// xx
|
||||
assert validate.utf8_string('\x91\x80\x80\x80') == false
|
||||
|
||||
// s1
|
||||
assert validate.utf8_string('\xC2\x7F\x80\x80') == false
|
||||
assert validate.utf8_string('\xC2\xC0\x80\x80') == false
|
||||
assert validate.utf8_string('\xDF\x7F\x80\x80') == false
|
||||
assert validate.utf8_string('\xDF\xC0\x80\x80') == false
|
||||
|
||||
// s2
|
||||
assert validate.utf8_string('\xE0\x9F\xBF\x80') == false
|
||||
assert validate.utf8_string('\xE0\xA0\x7F\x80') == false
|
||||
assert validate.utf8_string('\xE0\xBF\xC0\x80') == false
|
||||
assert validate.utf8_string('\xE0\xC0\x80\x80') == false
|
||||
|
||||
// s3
|
||||
assert validate.utf8_string('\xE1\x7F\xBF\x80') == false
|
||||
assert validate.utf8_string('\xE1\x80\x7F\x80') == false
|
||||
assert validate.utf8_string('\xE1\xBF\xC0\x80') == false
|
||||
assert validate.utf8_string('\xE1\xC0\x80\x80') == false
|
||||
|
||||
// s4
|
||||
assert validate.utf8_string('\xED\x7F\xBF\x80') == false
|
||||
assert validate.utf8_string('\xED\x80\x7F\x80') == false
|
||||
assert validate.utf8_string('\xED\x9F\xC0\x80') == false
|
||||
assert validate.utf8_string('\xED\xA0\x80\x80') == false
|
||||
|
||||
// s5
|
||||
assert validate.utf8_string('\xF0\x8F\xBF\xBF') == false
|
||||
assert validate.utf8_string('\xF0\x90\x7F\xBF') == false
|
||||
assert validate.utf8_string('\xF0\x90\x80\x7F') == false
|
||||
assert validate.utf8_string('\xF0\xBF\xBF\xC0') == false
|
||||
assert validate.utf8_string('\xF0\xBF\xC0\x80') == false
|
||||
assert validate.utf8_string('\xF0\xC0\x80\x80') == false
|
||||
|
||||
// s6
|
||||
assert validate.utf8_string('\xF1\x7F\xBF\xBF') == false
|
||||
assert validate.utf8_string('\xF1\x80\x7F\xBF') == false
|
||||
assert validate.utf8_string('\xF1\x80\x80\x7F') == false
|
||||
assert validate.utf8_string('\xF1\xBF\xBF\xC0') == false
|
||||
assert validate.utf8_string('\xF1\xBF\xC0\x80') == false
|
||||
assert validate.utf8_string('\xF1\xC0\x80\x80') == false
|
||||
assert validate.utf8_string('\xED\xEF\xBF\x89') == false
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
module validate
|
||||
|
||||
// http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
|
||||
|
||||
// vfmt off
|
||||
const utf8d = [
|
||||
u8(0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3, 3,
|
||||
3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11, 6, 6, 6, 5, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0, 12, 12, 12, 12, 12, 0, 12,
|
||||
0, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12,
|
||||
24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12,
|
||||
12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12 ]!
|
||||
// vfmt on
|
||||
|
||||
// utf8_string returns true, if the given string `s` consists only of valid UTF-8 runes
|
||||
pub fn utf8_string(s string) bool {
|
||||
return utf8_data(s.str, s.len)
|
||||
}
|
||||
|
||||
// utf8_data returns true, if the given `data` block, with length `len` bytes, consists only of valid UTF-8 runes
|
||||
@[direct_array_access]
|
||||
pub fn utf8_data(data &u8, len int) bool {
|
||||
mut state := 0
|
||||
|
||||
for i := 0; i < len; i++ {
|
||||
b := unsafe { data[i] }
|
||||
state = utf8d[256 + state + utf8d[b]]
|
||||
if state == 12 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return state == 0
|
||||
}
|
||||
Reference in new issue
Block a user