1 module hunt.sql.parser.CharTypes;
2 
3 //import hunt.sql.parser.LayoutCharacters.EOI;
4 import hunt.sql.parser.LayoutCharacters;
5 
6 import std.conv;
7 import std.stdio;
8 
9 public class CharTypes {
10 
11     private  __gshared bool[] hexFlags;
12     private  __gshared bool[] firstIdentifierFlags;
13     private  __gshared string[] stringCache;
14     private  __gshared bool[] identifierFlags;
15     private  __gshared bool[] whitespaceFlags;
16 
17     shared static this() {
18         hexFlags = new bool[256];
19         firstIdentifierFlags = new bool[256];
20         stringCache = new string[256];
21         identifierFlags = new bool[256];
22         whitespaceFlags = new bool[256];
23 
24         for (dchar c = 0; c < CharTypes.hexFlags.length; ++c) {
25             if (c >= 'A' && c <= 'F') {
26                 CharTypes.hexFlags[c] = true;
27             } else if (c >= 'a' && c <= 'f') {
28                 CharTypes.hexFlags[c] = true;
29             } else if (c >= '0' && c <= '9') {
30                 CharTypes.hexFlags[c] = true;
31             }
32         }
33 
34         for (dchar c = 0; c < CharTypes.firstIdentifierFlags.length; ++c) {
35             if (c >= 'A' && c <= 'Z') {
36                 CharTypes.firstIdentifierFlags[c] = true;
37             } else if (c >= 'a' && c <= 'z') {
38                 CharTypes.firstIdentifierFlags[c] = true;
39             }
40         }
41         CharTypes.firstIdentifierFlags['`'] = true;
42         CharTypes.firstIdentifierFlags['_'] = true;
43         CharTypes.firstIdentifierFlags['$'] = true;
44     
45         for (dchar c = 0; c < CharTypes.identifierFlags.length; ++c) {
46             if (c >= 'A' && c <= 'Z') {
47                 CharTypes.identifierFlags[c] = true;
48             } else if (c >= 'a' && c <= 'z') {
49                 CharTypes.identifierFlags[c] = true;
50             } else if (c >= '0' && c <= '9') {
51                 CharTypes.identifierFlags[c] = true;
52             }
53         }
54         // identifierFlags['`'] = true;
55         CharTypes.identifierFlags['_'] = true;
56         CharTypes.identifierFlags['$'] = true;
57         CharTypes.identifierFlags['#'] = true;
58 
59         for (int i = 0; i < CharTypes.identifierFlags.length; i++) {
60             if (CharTypes.identifierFlags[i]) {
61                 char ch = cast(char) i;
62                 CharTypes.stringCache[i] = to!string(ch);
63             }
64         }
65 
66         for (int i = 0; i <= 32; ++i) {
67             CharTypes.whitespaceFlags[i] = true;
68         }
69         
70         CharTypes.whitespaceFlags[LayoutCharacters.EOI] = false;
71         for (int i = 0x7F; i <= 0xA0; ++i) {
72             CharTypes.whitespaceFlags[i] = true;
73         }
74    
75         CharTypes.whitespaceFlags[160] = true;    
76     }
77     
78 
79     public static bool isHex(char c) {
80         return c < 256 && hexFlags[c];
81     }
82 
83     public static bool isDigit(char c) {
84         return c >= '0' && c <= '9';
85     }
86 
87 
88     public static bool isFirstIdentifierChar(char c) {
89         if (c <= firstIdentifierFlags.length) {
90             return firstIdentifierFlags[c];
91         }
92         return c != ' ' && c != ',';
93     }
94 
95 
96 
97     public static bool isIdentifierChar(char c) {
98         if (c <= identifierFlags.length) {
99             return identifierFlags[c];
100         }
101         return c != ' ' && c != ',' && c != ')';
102     }
103 
104     public static string valueOf(char ch) {
105         if (ch < stringCache.length) {
106             return stringCache[ch];
107         }
108         return null;
109     }
110 
111 
112     /**
113      * @return false if {@link LayoutCharacters#EOI}
114      */
115     public static bool isWhitespace(char c) {
116         return (c <= whitespaceFlags.length && whitespaceFlags[c]) //
117                || c == ' '; // Chinese space
118     }
119 
120 }