@@ -973,3 +973,61 @@ pub const RESERVED_FOR_IDENTIFIER: &[Keyword] = &[
973
973
Keyword :: STRUCT ,
974
974
Keyword :: TRIM ,
975
975
] ;
976
+
977
+ pub const NA : usize = usize:: MAX ;
978
+
979
+ #[ rustfmt:: skip]
980
+ pub const KEYWORD_LOOKUP_INDEX_ROOT : & [ usize ; 26 ] = & [
981
+ 0 , 42 , 67 , 148 , 198 , 241 , 281 , 294 , 305 , 350 , 357 , 360 , 390 ,
982
+ 430 , 465 , 497 , 539 , 543 , 605 , 683 , 728 , 761 , 780 , 793 , 795 , 796 ,
983
+ ] ;
984
+
985
+ pub fn lookup ( word : & str ) -> Keyword {
986
+ if word. len ( ) < 2 {
987
+ return Keyword :: NoKeyword ;
988
+ }
989
+
990
+ let word = word. to_uppercase ( ) ;
991
+ let byte1 = word. as_bytes ( ) [ 0 ] ;
992
+ if !byte1. is_ascii_uppercase ( ) {
993
+ return Keyword :: NoKeyword ;
994
+ }
995
+
996
+ let start = KEYWORD_LOOKUP_INDEX_ROOT [ ( byte1 - b'A' ) as usize ] ;
997
+
998
+ let end = if ( byte1 + 1 ) <= b'Z' {
999
+ KEYWORD_LOOKUP_INDEX_ROOT [ ( byte1 - b'A' + 1 ) as usize ]
1000
+ } else {
1001
+ ALL_KEYWORDS . len ( )
1002
+ } ;
1003
+
1004
+ let keyword = ALL_KEYWORDS [ start..end] . binary_search ( & word. as_str ( ) ) ;
1005
+ keyword. map_or ( Keyword :: NoKeyword , |x| ALL_KEYWORDS_INDEX [ x + start] )
1006
+ }
1007
+
1008
+ #[ cfg( test) ]
1009
+ mod tests {
1010
+ use super :: * ;
1011
+
1012
+ #[ test]
1013
+ fn check_keyword_index_roots ( ) {
1014
+ let mut root_index = Vec :: with_capacity ( 26 ) ;
1015
+ root_index. push ( 0 ) ;
1016
+ for idx in 1 ..ALL_KEYWORDS . len ( ) {
1017
+ assert ! ( ALL_KEYWORDS [ idx - 1 ] < ALL_KEYWORDS [ idx] ) ;
1018
+ let prev = ALL_KEYWORDS [ idx - 1 ] . as_bytes ( ) [ 0 ] ;
1019
+ let curr = ALL_KEYWORDS [ idx] . as_bytes ( ) [ 0 ] ;
1020
+ if curr != prev {
1021
+ root_index. push ( idx) ;
1022
+ }
1023
+ }
1024
+ assert_eq ! ( & root_index, KEYWORD_LOOKUP_INDEX_ROOT ) ;
1025
+ }
1026
+
1027
+ #[ test]
1028
+ fn check_keyword_lookup ( ) {
1029
+ for idx in 0 ..ALL_KEYWORDS . len ( ) {
1030
+ assert_eq ! ( lookup( ALL_KEYWORDS [ idx] ) , ALL_KEYWORDS_INDEX [ idx] ) ;
1031
+ }
1032
+ }
1033
+ }
0 commit comments