v2 / vlib / v / util / scanning.v
78 lines · 66 sloc · 1.54 KB · d97ed7724d7ebdc6297d7ddbc352db652d7afe3d
Raw
1module util
2
3pub const name_char_table = get_name_char_table()
4
5pub const func_char_table = get_func_char_table()
6
7pub const non_whitespace_table = get_non_white_space_table()
8
9@[direct_array_access]
10fn get_non_white_space_table() [256]bool {
11 mut bytes := [256]bool{}
12 for c in 0 .. 256 {
13 bytes[c] = !u8(c).is_space()
14 }
15 return bytes
16}
17
18@[direct_array_access]
19fn get_name_char_table() [256]bool {
20 mut res := [256]bool{}
21 for c in 0 .. 256 {
22 res[c] = u8(c).is_letter() || c == `_`
23 }
24 return res
25}
26
27@[direct_array_access]
28fn get_func_char_table() [256]bool {
29 mut res := [256]bool{}
30 for c in 0 .. 256 {
31 res[c] = u8(c).is_letter() || u8(c).is_digit() || c == `_`
32 }
33 return res
34}
35
36@[direct_array_access; inline]
37pub fn is_name_char(c u8) bool {
38 return name_char_table[c]
39}
40
41@[direct_array_access; inline]
42pub fn is_func_char(c u8) bool {
43 return func_char_table[c]
44}
45
46pub fn contains_capital(s string) bool {
47 for c in s {
48 if c.is_capital() {
49 return true
50 }
51 }
52 return false
53}
54
55// HTTPRequest bad
56// HttpRequest good
57@[direct_array_access]
58pub fn good_type_name(s string) bool {
59 if s.len < 4 {
60 return true
61 }
62 for i in 2 .. s.len {
63 if s[i].is_capital() && s[i - 1].is_capital() && s[i - 2].is_capital() {
64 return false
65 }
66 }
67 return true
68}
69
70// is_generic_type_name returns true if the current token is a generic type name.
71@[direct_array_access; inline]
72pub fn is_generic_type_name(name string) bool {
73 return name.len == 1 && name[0] != `C` && (name[0] >= `A` && name[0] <= `Z`)
74}
75
76pub fn cescaped_path(s string) string {
77 return s.replace('\\', '\\\\')
78}
79