diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index ec56a76e7b14b5..971005056120c4 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -239,7 +239,7 @@ fn (mut s Scanner) ident_name() string { s.pos++ for s.pos < s.text.len { c := s.text[s.pos] - if c.is_alnum() || c == `_` { + if util.func_char_table[c] { s.pos++ continue } @@ -537,7 +537,7 @@ fn (mut s Scanner) skip_whitespace() { s.pos++ continue } - if !(c == 32 || (c > 8 && c < 14) || c == 0x85 || c == 0xa0) { + if util.non_whitespace_table[c] { return } c_is_nl := c == scanner.b_cr || c == scanner.b_lf @@ -686,7 +686,7 @@ pub fn (mut s Scanner) text_scan() token.Token { c := s.text[s.pos] nextc := s.look_ahead(1) // name or keyword - if util.is_name_char(c) { + if util.name_char_table[c] { name := s.ident_name() // tmp hack to detect . in ${} // Check if not .eof to prevent panic @@ -1315,7 +1315,7 @@ pub fn (mut s Scanner) ident_string() string { break } // $var - if prevc == `$` && util.is_name_char(c) && !is_raw + if prevc == `$` && util.name_char_table[c] && !is_raw && s.count_symbol_before(s.pos - 2, scanner.backslash) & 1 == 0 { s.is_inside_string = true s.is_inter_start = true diff --git a/vlib/v/util/scanning.v b/vlib/v/util/scanning.v index e6eeac7c6896de..a407a18d2b3c47 100644 --- a/vlib/v/util/scanning.v +++ b/vlib/v/util/scanning.v @@ -1,13 +1,43 @@ module util -@[inline] +pub const name_char_table = get_name_char_table() + +pub const func_char_table = get_func_char_table() + +pub const non_whitespace_table = get_non_white_space_table() + +fn get_non_white_space_table() [256]bool { + mut bytes := [256]bool{} + for c in 0 .. 256 { + bytes[c] = !u8(c).is_space() + } + return bytes +} + +fn get_name_char_table() [256]bool { + mut res := [256]bool{} + for c in 0 .. 256 { + res[c] = u8(c).is_letter() || c == `_` + } + return res +} + +fn get_func_char_table() [256]bool { + mut res := [256]bool{} + for c in 0 .. 256 { + res[c] = u8(c).is_letter() || u8(c).is_digit() || c == `_` + } + return res +} + +@[direct_array_access; inline] pub fn is_name_char(c u8) bool { - return c.is_letter() || c == `_` + return util.name_char_table[c] } -@[inline] +@[direct_array_access; inline] pub fn is_func_char(c u8) bool { - return c.is_letter() || c == `_` || c.is_digit() + return util.func_char_table[c] } pub fn contains_capital(s string) bool {