summaryrefslogtreecommitdiff
path: root/src/mt/charset.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mt/charset.h')
-rw-r--r--src/mt/charset.h131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/mt/charset.h b/src/mt/charset.h
new file mode 100644
index 0000000..84742b9
--- /dev/null
+++ b/src/mt/charset.h
@@ -0,0 +1,131 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef CHARSETH
21#define CHARSETH
22
23#include <string.h>
24
25struct charset {
26 explicit charset(const char s[]) {
27 memset(negmap,0,256); while(*s) map[*s++]=1;
28 }
29 charset(char beg,char end) {
30 memset(negmap,0,256); while(beg<=end) map[beg++]=1;
31 }
32 int test(char c) const { return map[c]; }
33 int operator[](char c) const { return map[c]; }
34 char negmap[128],map[128];
35};
36
37inline charset operator|(const charset& a,const charset& b) {
38 charset c(a); for(int i=-127;i<128;i++) c.map[i]|=b.map[i]; return c;
39}
40inline charset operator|(const charset& a,char b) {
41 charset c(a); c.map[b]=(b!=0); return c;
42}
43inline charset operator|(char a,const charset& b) {
44 charset c(b); c.map[a]=(a!=0); return c;
45}
46inline charset operator&(const charset& a,const charset& b) {
47 charset c(a); for(int i=-127;i<128;i++) c.map[i]&=b.map[i]; return c;
48}
49inline charset operator^(const charset& a,const charset& b) {
50 charset c(a); for(int i=-127;i<128;i++) c.map[i]^=b.map[i]; return c;
51}
52inline charset operator-(const charset& a,const charset& b) {
53 charset c(a); for(int i=-127;i<128;i++) c.map[i]&=~b.map[i]; return c;
54}
55inline charset operator-(const charset& a,char b) {
56 charset c(a); c.map[b]=0; return c;
57}
58inline charset operator~(const charset& a) {
59 charset c(a); for(int i=-127;i<128;i++) c.map[i]=!c.map[i]; c.map[0]=0;
60 return c;
61}
62
63//////////////////////////////////////////////////////////////////////////
64
65struct charmap {
66 charmap() { for(int i=-127;i<128;i++) map[i]=i; }
67 charmap(const charset& a,const charset& b) {
68 for(int i=-127,j=-127;i<128;i++) {
69 if(a[i]) { while(j<128&&!b[j]) j++; map[i]=j++; }
70 else map[i]=i;
71 }
72 }
73 charmap(const charset& a,const char* b) {
74 for(int i=-127;i<128;i++) if(a[i]&&*b) map[i]=*b++; else map[i]=i;
75 }
76 char operator[](char c) const { return map[c]; }
77 char negmap[128],map[128];
78};
79
80inline charmap operator<<(const charmap& a,const charmap& b) {
81 charmap c(a);
82 for(int i=-127;i<128;i++) if(b.map[i]!=i) c.map[i]=b.map[i];
83 for(int i=-127;i<128;i++) c.map[i]=c.map[c.map[i]];
84 return c;
85}
86
87//////////////////////////////////////////////////////////////////////////
88
89struct bracket {
90 explicit bracket(const char s[]) { strncpy(&left,s,sizeof(bracket)); }
91 int test(char c) const { return c==left||c==right; }
92 char left,right,esc,end;
93};
94
95//////////////////////////////////////////////////////////////////////////
96
97const charset cset_spaces(" \t");
98const charset cset_newl("\r\n");
99const charset cset_ws=cset_spaces|cset_newl;
100const charset cset_printable(' ','~');
101const charset cset_digit('0','9');
102const charset cset_real=cset_digit|'.';
103const charset cset_sign("+-");
104const charset cset_lalpha('a','z');
105const charset cset_ualpha('A','Z');
106const charset cset_alpha=cset_lalpha|cset_ualpha;
107const charset cset_alphanum=cset_alpha|cset_digit;
108const charset cset_alphareal=cset_alpha|cset_real;
109const charset cset_lext1(-32,-10),cset_lext2(-8,-2);
110const charset cset_uext1(-64,-42),cset_uext2(-40,-34);
111const charset cset_liso=cset_lalpha|cset_lext1|cset_lext2;
112const charset cset_uiso=cset_ualpha|cset_uext1|cset_uext2;
113
114const charmap cset_ident;
115const charmap cset_leqv1(cset_lext1,"aaaaaaaceeeeiiiidnooooo");
116const charmap cset_leqv2(cset_lext2,"ouuuuyp");
117const charmap cset_ueqv1(cset_uext1,"AAAAAAACEEEEIIIIDNOOOOO");
118const charmap cset_ueqv2(cset_uext2,"OUUUUYP");
119
120const charmap cset_lcase(cset_uiso,cset_liso);
121const charmap cset_ucase(cset_liso,cset_uiso);
122const charmap cset_sim=cset_lcase<<cset_leqv1<<cset_leqv2;
123
124const bracket cset_quotes("\"\"\\");
125const bracket cset_indices("[]\\");
126const bracket cset_braces("{}\\");
127const bracket cset_para("()\\");
128const bracket cset_dollars("$$\\");
129const bracket cset_hooks("<>\\");
130
131#endif