diff options
Diffstat (limited to 'src/mt/charset.h')
| -rw-r--r-- | src/mt/charset.h | 131 |
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 | |||
| 25 | struct 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 | |||
| 37 | inline 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 | } | ||
| 40 | inline charset operator|(const charset& a,char b) { | ||
| 41 | charset c(a); c.map[b]=(b!=0); return c; | ||
| 42 | } | ||
| 43 | inline charset operator|(char a,const charset& b) { | ||
| 44 | charset c(b); c.map[a]=(a!=0); return c; | ||
| 45 | } | ||
| 46 | inline 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 | } | ||
| 49 | inline 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 | } | ||
| 52 | inline 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 | } | ||
| 55 | inline charset operator-(const charset& a,char b) { | ||
| 56 | charset c(a); c.map[b]=0; return c; | ||
| 57 | } | ||
| 58 | inline 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 | |||
| 65 | struct 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 | |||
| 80 | inline 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 | |||
| 89 | struct 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 | |||
| 97 | const charset cset_spaces(" \t"); | ||
| 98 | const charset cset_newl("\r\n"); | ||
| 99 | const charset cset_ws=cset_spaces|cset_newl; | ||
| 100 | const charset cset_printable(' ','~'); | ||
| 101 | const charset cset_digit('0','9'); | ||
| 102 | const charset cset_real=cset_digit|'.'; | ||
| 103 | const charset cset_sign("+-"); | ||
| 104 | const charset cset_lalpha('a','z'); | ||
| 105 | const charset cset_ualpha('A','Z'); | ||
| 106 | const charset cset_alpha=cset_lalpha|cset_ualpha; | ||
| 107 | const charset cset_alphanum=cset_alpha|cset_digit; | ||
| 108 | const charset cset_alphareal=cset_alpha|cset_real; | ||
| 109 | const charset cset_lext1(-32,-10),cset_lext2(-8,-2); | ||
| 110 | const charset cset_uext1(-64,-42),cset_uext2(-40,-34); | ||
| 111 | const charset cset_liso=cset_lalpha|cset_lext1|cset_lext2; | ||
| 112 | const charset cset_uiso=cset_ualpha|cset_uext1|cset_uext2; | ||
| 113 | |||
| 114 | const charmap cset_ident; | ||
| 115 | const charmap cset_leqv1(cset_lext1,"aaaaaaaceeeeiiiidnooooo"); | ||
| 116 | const charmap cset_leqv2(cset_lext2,"ouuuuyp"); | ||
| 117 | const charmap cset_ueqv1(cset_uext1,"AAAAAAACEEEEIIIIDNOOOOO"); | ||
| 118 | const charmap cset_ueqv2(cset_uext2,"OUUUUYP"); | ||
| 119 | |||
| 120 | const charmap cset_lcase(cset_uiso,cset_liso); | ||
| 121 | const charmap cset_ucase(cset_liso,cset_uiso); | ||
| 122 | const charmap cset_sim=cset_lcase<<cset_leqv1<<cset_leqv2; | ||
| 123 | |||
| 124 | const bracket cset_quotes("\"\"\\"); | ||
| 125 | const bracket cset_indices("[]\\"); | ||
| 126 | const bracket cset_braces("{}\\"); | ||
| 127 | const bracket cset_para("()\\"); | ||
| 128 | const bracket cset_dollars("$$\\"); | ||
| 129 | const bracket cset_hooks("<>\\"); | ||
| 130 | |||
| 131 | #endif | ||
