| 1 | #ifndef DOZERG_REGXSTRING_IMPL_H_20091012 | ||
| 2 | #define DOZERG_REGXSTRING_IMPL_H_20091012 | ||
| 3 | |||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | #include <iosfwd> | ||
| 7 | #include <utility> | ||
| 8 | #include <memory> | ||
| 9 | |||
| 10 | #define _DZ_DEBUG 0 | ||
| 11 | |||
| 12 | #define _MEM_LEAK 0 | ||
| 13 | |||
| 14 | //allocator choice | ||
| 15 | #ifndef __GNUC__ | ||
| 16 | # define __DZ_ALLOC std::allocator | ||
| 17 | #else | ||
| 18 | # ifndef NDEBUG | ||
| 19 | # define __DZ_ALLOC std::allocator | ||
| 20 | # else | ||
| 21 | # include <ext/pool_allocator.h> | ||
| 22 | # define __DZ_ALLOC __gnu_cxx::__pool_alloc | ||
| 23 | # endif | ||
| 24 | #endif | ||
| 25 | |||
| 26 | //stl containers redefine | ||
| 27 | //Sequence | ||
| 28 | #define __DZ_BASIC_STRING(C) std::basic_string< C,std::char_traits< C >,__DZ_ALLOC< C > > | ||
| 29 | #define __DZ_BASIC_STRING1(C,T) std::basic_string< C,T,__DZ_ALLOC< C > > | ||
| 30 | #define __DZ_STRING __DZ_BASIC_STRING(char) | ||
| 31 | #define __DZ_WSTRING __DZ_BASIC_STRING(wchar_t) | ||
| 32 | #define __DZ_DEQUE(T) std::deque< T,__DZ_ALLOC< T > > | ||
| 33 | #define __DZ_LIST(T) std::list< T,__DZ_ALLOC< T > > | ||
| 34 | #define __DZ_VECTOR(T) std::vector< T,__DZ_ALLOC< T > > | ||
| 35 | //Associative | ||
| 36 | #define __DZ_MAP(K,V) std::map< K,V,std::less< K >,__DZ_ALLOC<std::pair< K,V > > > | ||
| 37 | #define __DZ_MAP1(K,V,C) std::map< K,V,C,__DZ_ALLOC<std::pair< K,V > > > | ||
| 38 | #define __DZ_MULTIMAP(K,V) std::multimap< K,V,std::less< K >,__DZ_ALLOC<std::pair< K,V > > > | ||
| 39 | #define __DZ_MULTIMAP1(K,V,C) std::multimap< K,V,C,__DZ_ALLOC<std::pair< K,V > > > | ||
| 40 | #define __DZ_SET(K) std::set< K,std::less< K >,__DZ_ALLOC< K > > | ||
| 41 | #define __DZ_SET1(K,C) std::set< K,C,__DZ_ALLOC< K > > | ||
| 42 | #define __DZ_MULTISET(K) std::multiset< K,std::less< K >,__DZ_ALLOC< K > > | ||
| 43 | #define __DZ_MULTISET1(K,C) std::multiset< K,C,__DZ_ALLOC< K > > | ||
| 44 | //String Stream | ||
| 45 | #define __DZ_BASIC_ISTRINGSTREAM(C) std::basic_istringstream< C,std::char_traits< C >,__DZ_ALLOC< C > > | ||
| 46 | #define __DZ_BASIC_ISTRINGSTREAM1(C,T) std::basic_istringstream< C,T,__DZ_ALLOC< C > > | ||
| 47 | #define __DZ_BASIC_OSTRINGSTREAM(C) std::basic_ostringstream< C,std::char_traits< C >,__DZ_ALLOC< C > > | ||
| 48 | #define __DZ_BASIC_OSTRINGSTREAM1(C,T) std::basic_ostringstream< C,T,__DZ_ALLOC< C > > | ||
| 49 | #define __DZ_BASIC_STRINGSTREAM(C) std::basic_stringstream< C,std::char_traits< C >,__DZ_ALLOC< C > > | ||
| 50 | #define __DZ_BASIC_STRINGSTREAM1(C,T) std::basic_stringstream< C,T,__DZ_ALLOC< C > > | ||
| 51 | #define __DZ_ISTRINGSTREAM __DZ_BASIC_ISTRINGSTREAM(char) | ||
| 52 | #define __DZ_OSTRINGSTREAM __DZ_BASIC_OSTRINGSTREAM(char) | ||
| 53 | #define __DZ_STRINGSTREAM __DZ_BASIC_STRINGSTREAM(char) | ||
| 54 | #define __DZ_WISTRINGSTREAM __DZ_BASIC_ISTRINGSTREAM(wchar_t) | ||
| 55 | #define __DZ_WOSTRINGSTREAM __DZ_BASIC_OSTRINGSTREAM(wchar_t) | ||
| 56 | #define __DZ_WSTRINGSTREAM __DZ_BASIC_STRINGSTREAM(wchar_t) | ||
| 57 | //Stream Buf | ||
| 58 | #define __DZ_BASIC_STRINGBUF(C) std::basic_stringbuf< C,std::char_traits< C >,__DZ_ALLOC< C > > | ||
| 59 | #define __DZ_BASIC_STRINGBUF1(C,T) std::basic_stringbuf< C,T,__DZ_ALLOC< C > > | ||
| 60 | #define __DZ_STRINGBUF __DZ_BASIC_STRINGBUF(char) | ||
| 61 | #define __DZ_WSTRINGBUF __DZ_BASIC_STRINGBUF(wchar_t) | ||
| 62 | //Extension | ||
| 63 | #define __DZ_ROPE(T) __gnu_cxx::rope< T,__DZ_ALLOC< T > > | ||
| 64 | #define __DZ_SLIST(T) __gnu_cxx::slist< T,__DZ_ALLOC< T > > | ||
| 65 | |||
| 66 | #define REGXSTRING_NS __DZ_Regx_String | ||
| 67 | |||
| 68 | #define NAMESAPCE_BEGIN namespace __DZ_Regx_String{ | ||
| 69 | #define NAMESAPCE_END } | ||
| 70 | |||
| 71 | struct Config; | ||
| 72 | |||
| 73 | NAMESAPCE_BEGIN | ||
| 74 | |||
| 75 | typedef std::pair<size_t,size_t> __RefValue; | ||
| 76 | |||
| 77 | typedef __DZ_VECTOR(__RefValue) __Refs; | ||
| 78 | |||
| 79 | typedef __DZ_VECTOR(char) __Ends; | ||
| 80 | |||
| 81 | struct __ParseData{ | ||
| 82 | __Ends ends_; | ||
| 83 | const Config & config_; | ||
| 84 | size_t i_; | ||
| 85 | int ref_; | ||
| 86 | //functions: | ||
| 87 | 181 | explicit __ParseData(const Config & config) | |
| 88 | 362 | : config_(config) | |
| 89 | 181 | , i_(0) | |
| 90 | 181 | , ref_(0) | |
| 91 | 181 | {} | |
| 92 | int inEnds(int ch) const; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct __GenerateData | ||
| 96 | { | ||
| 97 | __Refs refs_; | ||
| 98 | __DZ_OSTRINGSTREAM & oss_; | ||
| 99 | 17607 | explicit __GenerateData(__DZ_OSTRINGSTREAM & oss) | |
| 100 | 17607 | : oss_(oss) | |
| 101 | 17607 | {} | |
| 102 | }; | ||
| 103 | |||
| 104 | struct __NodeBase | ||
| 105 | { | ||
| 106 | static __NodeBase * const REP_NULL; //replace with NULL(0) | ||
| 107 | #if _MEM_LEAK | ||
| 108 | static int ref; | ||
| 109 | __NodeBase(){++ref;} | ||
| 110 | #endif | ||
| 111 | virtual ~__NodeBase(); | ||
| 112 | virtual __NodeBase * Optimize(__ParseData & pdata) = 0; | ||
| 113 | virtual void RandString(__GenerateData & gdata) const = 0; | ||
| 114 | virtual void Debug(std::ostream & out,int lvl) const = 0; | ||
| 115 | virtual int Repeat(int ch); | ||
| 116 | virtual void AppendNode(__NodeBase * node); | ||
| 117 | }; | ||
| 118 | |||
| 119 | class __Edge : public __NodeBase | ||
| 120 | { | ||
| 121 | bool begin_; | ||
| 122 | public: | ||
| 123 | explicit __Edge(int ch); | ||
| 124 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 125 | void RandString(__GenerateData & gdata) const; | ||
| 126 | void Debug(std::ostream & out,int lvl) const; | ||
| 127 | }; | ||
| 128 | |||
| 129 | class __Text : public __NodeBase | ||
| 130 | { | ||
| 131 | __DZ_STRING str_; | ||
| 132 | public: | ||
| 133 | //functions | ||
| 134 | explicit __Text(int ch); | ||
| 135 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 136 | void RandString(__GenerateData & gdata) const; | ||
| 137 | void Debug(std::ostream & out,int lvl) const; | ||
| 138 | 922 | __Text & operator +=(const __Text & other){str_ += other.str_;return *this;} | |
| 139 | }; | ||
| 140 | |||
| 141 | class __Charset : public __NodeBase | ||
| 142 | { | ||
| 143 | __DZ_STRING str_; | ||
| 144 | size_t inc_; | ||
| 145 | public: | ||
| 146 | //functions | ||
| 147 | __Charset(); | ||
| 148 | __Charset(const __DZ_STRING & str,bool include); | ||
| 149 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 150 | void RandString(__GenerateData & gdata) const; | ||
| 151 | void Debug(std::ostream & out,int lvl) const; | ||
| 152 | void Exclude(); | ||
| 153 | void AddChar(int ch); | ||
| 154 | void AddRange(int from,int to); | ||
| 155 | void AddRange(__Charset * node); | ||
| 156 | void Unique(); | ||
| 157 | private: | ||
| 158 | void unite(__Charset & node); | ||
| 159 | void reverse(); | ||
| 160 | void unique(); | ||
| 161 | }; | ||
| 162 | |||
| 163 | struct __Repeat : public __NodeBase | ||
| 164 | { | ||
| 165 | static const int INFINITE = 1 << 16; | ||
| 166 | private: | ||
| 167 | static const int _REPEAT_MAX = __Repeat::INFINITE - 1; | ||
| 168 | static const int _NON_GREEDY = 1 << 17; | ||
| 169 | static const int _PROSSESSIVE = 1 << 18; | ||
| 170 | static const int _CLEAR_FLAGS = _NON_GREEDY - 1; | ||
| 171 | __NodeBase * node_; | ||
| 172 | int min_,max_; | ||
| 173 | public: | ||
| 174 | //functions | ||
| 175 | __Repeat(__NodeBase * node,int ch); | ||
| 176 | __Repeat(__NodeBase * node,int min,int max); | ||
| 177 | ~__Repeat(); | ||
| 178 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 179 | void RandString(__GenerateData & gdata) const; | ||
| 180 | void Debug(std::ostream & out,int lvl) const; | ||
| 181 | int Repeat(int ch); | ||
| 182 | private: | ||
| 183 | 212 | bool isInfinite() const{return (max_ & INFINITE) != 0;} | |
| 184 | bool isNonGreedy() const{return (min_ & _NON_GREEDY) != 0;} | ||
| 185 | bool isPossessive() const{return (min_ & _PROSSESSIVE) != 0;} | ||
| 186 | 0 | bool canRepeat() const{return !(min_ & (_NON_GREEDY | _PROSSESSIVE));} | |
| 187 | }; | ||
| 188 | |||
| 189 | class __Seq : public __NodeBase | ||
| 190 | { | ||
| 191 | typedef __DZ_VECTOR(__NodeBase *) __Con; | ||
| 192 | __Con seq_; | ||
| 193 | public: | ||
| 194 | //functions | ||
| 195 | explicit __Seq(__NodeBase * node); | ||
| 196 | ~__Seq(); | ||
| 197 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 198 | void RandString(__GenerateData & gdata) const; | ||
| 199 | void Debug(std::ostream & out,int lvl) const; | ||
| 200 | void AppendNode(__NodeBase * node); | ||
| 201 | }; | ||
| 202 | |||
| 203 | class __Group : public __NodeBase | ||
| 204 | { | ||
| 205 | static const int INDEX = 1 << 16; //group index flag | ||
| 206 | static const size_t MAX_GROUPS = 9; | ||
| 207 | __NodeBase * node_; | ||
| 208 | size_t mark_; | ||
| 209 | public: | ||
| 210 | //functions | ||
| 211 | __Group(__NodeBase * node,int mark); | ||
| 212 | ~__Group(); | ||
| 213 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 214 | void RandString(__GenerateData & gdata) const; | ||
| 215 | void Debug(std::ostream & out,int lvl) const; | ||
| 216 | }; | ||
| 217 | |||
| 218 | class __Select : public __NodeBase | ||
| 219 | { | ||
| 220 | typedef __DZ_VECTOR(__NodeBase *) __Con; | ||
| 221 | __Con sel_; | ||
| 222 | size_t sz_; | ||
| 223 | public: | ||
| 224 | //functions | ||
| 225 | explicit __Select(__NodeBase * node); | ||
| 226 | ~__Select(); | ||
| 227 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 228 | void RandString(__GenerateData & gdata) const; | ||
| 229 | void Debug(std::ostream & out,int lvl) const; | ||
| 230 | void AppendNode(__NodeBase * node); | ||
| 231 | }; | ||
| 232 | |||
| 233 | class __Ref : public __NodeBase | ||
| 234 | { | ||
| 235 | size_t index_; | ||
| 236 | public: | ||
| 237 | explicit __Ref(int index); | ||
| 238 | __NodeBase * Optimize(__ParseData & pdata); | ||
| 239 | void RandString(__GenerateData & gdata) const; | ||
| 240 | void Debug(std::ostream & out,int lvl) const; | ||
| 241 | }; | ||
| 242 | |||
| 243 | class __CRegxString | ||
| 244 | { | ||
| 245 | typedef std::pair<__NodeBase *,int> __Ret; | ||
| 246 | public: | ||
| 247 | __CRegxString(); | ||
| 248 | ~__CRegxString(){uninit();} | ||
| 249 | void ParseRegx(const __DZ_STRING & regx,const Config * config); | ||
| 250 | __DZ_STRING Regx() const{return regx_;} | ||
| 251 | const __DZ_STRING & RandString(); | ||
| 252 | const __DZ_STRING & LastString() const{return str_;} | ||
| 253 | void Debug(std::ostream & out) const; | ||
| 254 | private: | ||
| 255 | __CRegxString(const __CRegxString &); | ||
| 256 | __CRegxString & operator =(const __CRegxString &); | ||
| 257 | void uninit(); | ||
| 258 | __Ret processSeq(__ParseData & pdata); | ||
| 259 | __Ret processSlash(bool bNode,__ParseData & pdata); | ||
| 260 | __NodeBase * processSet(__ParseData & pdata); | ||
| 261 | __NodeBase * processGroup(__ParseData & pdata); | ||
| 262 | __Ret processSelect(__NodeBase * node,__ParseData & pdata); | ||
| 263 | __NodeBase * processRepeat(__NodeBase * node,__ParseData & pdata); | ||
| 264 | int processInt(int & result,__ParseData & pdata); | ||
| 265 | bool processRange(int & result,__ParseData & pdata); | ||
| 266 | int ignoreSubexpMarks(__ParseData & pdata); | ||
| 267 | //fields: | ||
| 268 | __DZ_STRING regx_; | ||
| 269 | __DZ_STRING str_; | ||
| 270 | __NodeBase * top_; //regx tree | ||
| 271 | }; | ||
| 272 | |||
| 273 | NAMESAPCE_END | ||
| 274 | |||
| 275 | #endif |