-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharUtils.cs
127 lines (95 loc) · 3.36 KB
/
CharUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Skopik
{
[Flags]
internal enum CharacterTypeFlags : int
{
Null = (1 << 0),
NewLine = (1 << 1),
Tab = (1 << 2),
Whitespace = (1 << 3),
TabOrWhitespace = (Tab | Whitespace),
Digit = (1 << 4),
Lowercase = (1 << 5),
Uppercase = (1 << 6),
Quote = (1 << 7),
Letter = (Lowercase | Uppercase),
Alphanumerical = (Letter | Digit),
EndOfToken = (Null | NewLine | TabOrWhitespace),
Control = (1 << 8), // 0x1 - 0x1F
Operator = (1 << 9),
Separator = (1 << 10),
OpenBrace = (1 << 11),
CloseBrace = (1 << 12),
Brace = (OpenBrace | CloseBrace),
ExtendedOperators = (Operator | Separator | Brace),
Unknown = (1 << 15),
}
internal sealed class CharUtils
{
// fast lookup
internal static bool IsLookupReady = false;
internal static int[] LookupTypes = new int[128];
internal static void MapLookupTypes()
{
// map once
if (IsLookupReady)
return;
for (int i = 0; i < LookupTypes.Length; i++)
{
int type = 0;
if (i >= 127)
type |= (int)CharacterTypeFlags.Unknown;
if (i >= 'a' && i <= 'z')
type |= (int)CharacterTypeFlags.Lowercase;
if (i >= 'A' && i <= 'Z')
type |= (int)CharacterTypeFlags.Uppercase;
if (i >= '0' && i <= '9')
type |= (int)CharacterTypeFlags.Digit;
if (i >= 1 && i <= 31)
type |= (int)CharacterTypeFlags.Control;
if (i == ' ')
type |= (int)CharacterTypeFlags.Whitespace;
if (i == '\t')
type |= (int)CharacterTypeFlags.Tab;
if (i == '\0')
type |= (int)CharacterTypeFlags.Null;
switch (i)
{
case '"':
type |= (int)CharacterTypeFlags.Quote;
break;
case '\r': case '\n':
type |= (int)CharacterTypeFlags.NewLine;
break;
case '$': case '@':
type |= (int)CharacterTypeFlags.Operator;
break;
case ',': case ';':
type |= (int)CharacterTypeFlags.Separator;
break;
case '(': case '{': case '[':
type |= (int)CharacterTypeFlags.OpenBrace;
break;
case ')': case '}': case ']':
type |= (int)CharacterTypeFlags.CloseBrace;
break;
}
LookupTypes[i] = type;
}
IsLookupReady = true;
}
internal static CharacterTypeFlags GetCharFlags(int value)
{
MapLookupTypes();
return ((value >= 0) && (value <= 127)) ? (CharacterTypeFlags)LookupTypes[value] : CharacterTypeFlags.Unknown;
}
internal static bool HasCharFlags(int value, CharacterTypeFlags charFlags)
{
return ((GetCharFlags(value) & charFlags) != 0);
}
}
}