From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/mt/charset.h | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/mt/charset.h (limited to 'src/mt/charset.h') 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 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef CHARSETH +#define CHARSETH + +#include + +struct charset { + explicit charset(const char s[]) { + memset(negmap,0,256); while(*s) map[*s++]=1; + } + charset(char beg,char end) { + memset(negmap,0,256); while(beg<=end) map[beg++]=1; + } + int test(char c) const { return map[c]; } + int operator[](char c) const { return map[c]; } + char negmap[128],map[128]; +}; + +inline charset operator|(const charset& a,const charset& b) { + charset c(a); for(int i=-127;i<128;i++) c.map[i]|=b.map[i]; return c; +} +inline charset operator|(const charset& a,char b) { + charset c(a); c.map[b]=(b!=0); return c; +} +inline charset operator|(char a,const charset& b) { + charset c(b); c.map[a]=(a!=0); return c; +} +inline charset operator&(const charset& a,const charset& b) { + charset c(a); for(int i=-127;i<128;i++) c.map[i]&=b.map[i]; return c; +} +inline charset operator^(const charset& a,const charset& b) { + charset c(a); for(int i=-127;i<128;i++) c.map[i]^=b.map[i]; return c; +} +inline charset operator-(const charset& a,const charset& b) { + charset c(a); for(int i=-127;i<128;i++) c.map[i]&=~b.map[i]; return c; +} +inline charset operator-(const charset& a,char b) { + charset c(a); c.map[b]=0; return c; +} +inline charset operator~(const charset& a) { + charset c(a); for(int i=-127;i<128;i++) c.map[i]=!c.map[i]; c.map[0]=0; + return c; +} + +////////////////////////////////////////////////////////////////////////// + +struct charmap { + charmap() { for(int i=-127;i<128;i++) map[i]=i; } + charmap(const charset& a,const charset& b) { + for(int i=-127,j=-127;i<128;i++) { + if(a[i]) { while(j<128&&!b[j]) j++; map[i]=j++; } + else map[i]=i; + } + } + charmap(const charset& a,const char* b) { + for(int i=-127;i<128;i++) if(a[i]&&*b) map[i]=*b++; else map[i]=i; + } + char operator[](char c) const { return map[c]; } + char negmap[128],map[128]; +}; + +inline charmap operator<<(const charmap& a,const charmap& b) { + charmap c(a); + for(int i=-127;i<128;i++) if(b.map[i]!=i) c.map[i]=b.map[i]; + for(int i=-127;i<128;i++) c.map[i]=c.map[c.map[i]]; + return c; +} + +////////////////////////////////////////////////////////////////////////// + +struct bracket { + explicit bracket(const char s[]) { strncpy(&left,s,sizeof(bracket)); } + int test(char c) const { return c==left||c==right; } + char left,right,esc,end; +}; + +////////////////////////////////////////////////////////////////////////// + +const charset cset_spaces(" \t"); +const charset cset_newl("\r\n"); +const charset cset_ws=cset_spaces|cset_newl; +const charset cset_printable(' ','~'); +const charset cset_digit('0','9'); +const charset cset_real=cset_digit|'.'; +const charset cset_sign("+-"); +const charset cset_lalpha('a','z'); +const charset cset_ualpha('A','Z'); +const charset cset_alpha=cset_lalpha|cset_ualpha; +const charset cset_alphanum=cset_alpha|cset_digit; +const charset cset_alphareal=cset_alpha|cset_real; +const charset cset_lext1(-32,-10),cset_lext2(-8,-2); +const charset cset_uext1(-64,-42),cset_uext2(-40,-34); +const charset cset_liso=cset_lalpha|cset_lext1|cset_lext2; +const charset cset_uiso=cset_ualpha|cset_uext1|cset_uext2; + +const charmap cset_ident; +const charmap cset_leqv1(cset_lext1,"aaaaaaaceeeeiiiidnooooo"); +const charmap cset_leqv2(cset_lext2,"ouuuuyp"); +const charmap cset_ueqv1(cset_uext1,"AAAAAAACEEEEIIIIDNOOOOO"); +const charmap cset_ueqv2(cset_uext2,"OUUUUYP"); + +const charmap cset_lcase(cset_uiso,cset_liso); +const charmap cset_ucase(cset_liso,cset_uiso); +const charmap cset_sim=cset_lcase<\\"); + +#endif -- cgit v1.2.3