|
|
1.1 misha 1: /** @file
2: Parser: @b regex class decls.
3:
1.9 moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 misha 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
8: #ifndef PA_VREGEX_H
9: #define PA_VREGEX_H
10:
1.10 ! moko 11: #define IDENT_PA_VREGEX_H "$Id: pa_vregex.h,v 1.9 2015/10/26 01:22:02 moko Exp $"
1.1 misha 12:
13: // include
14:
15: #include "classes.h"
16: #include "pa_common.h"
17: #include "pa_vstateless_object.h"
18: #include "pa_charset.h"
19: #include "pcre.h"
20:
21: // defines
22:
23: #define VREGEX_TYPE "regex"
24:
25: enum Match_feature {
26: MF_GLOBAL_SEARCH = 0x01,
27: MF_NEED_PRE_POST_MATCH = 0x02,
28: MF_JUST_COUNT_MATCHES = 0x04
29: };
30:
31: extern Methoded* regex_class;
32:
33: // VRegex
34: class VRegex: public VStateless_object {
35:
36: public: // Value
37:
38: override const char* type() const { return VREGEX_TYPE; }
39: override VStateless_class *get_class() { return regex_class; }
40:
1.2 misha 41: /// VRegex: count
42: override int as_int() { return get_info_size(); }
43:
44: /// VRegex: count
1.8 misha 45: override double as_double() { return get_info_size(); }
1.1 misha 46:
47: /// VRegex: scalar
1.7 moko 48: override Value& as_expr_result();
1.1 misha 49:
50: /// VRegex: true
1.8 misha 51: virtual bool is_defined() const { return true; }
1.1 misha 52:
53: /// VRegex: true
54: override bool as_bool() const { return true; }
55:
1.5 misha 56: override Value* get_element(const String& aname);
57:
1.1 misha 58: public: // usage
59:
60: VRegex():
1.3 misha 61: fcharset(0),
62: fpattern(0),
1.5 misha 63: foptions_cstr(0),
1.3 misha 64: fcode(0),
65: fextra(0),
66: fstudied(false)
67: {
68: foptions[0]=0;
69: foptions[1]=0;
70: }
1.1 misha 71:
72: VRegex(Charset& acharset, const String* aregex, const String* aoptions):
1.3 misha 73: fextra(0),
1.1 misha 74: fstudied(false)
75: {
76: set(acharset, aregex, aoptions);
77: compile();
78: }
79:
80: ~VRegex(){
81: if(fextra)
82: pcre_free(fextra);
83: if(fcode)
84: pcre_free(fcode);
85: }
86:
87: void set(Charset& acharset, const String* aregex, const String* aoptions);
88:
89: void compile();
90:
91: void study();
92:
93: int exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart=0);
94:
95: // size_t info();
96:
97: size_t full_info(int type);
98:
99: size_t get_info_size();
100:
101: size_t get_study_size();
102:
103: size_t get_options();
104:
105: bool is_pre_post_match_needed(){
106: return (foptions[1] & MF_NEED_PRE_POST_MATCH)!=0;
107: }
108:
109: bool is_just_count(){
110: return (foptions[1] & MF_JUST_COUNT_MATCHES)!=0;
111: }
112:
113: bool is_global_search(){
114: return (foptions[1] & MF_GLOBAL_SEARCH)!=0;
115: }
116:
117: private:
118: static void regex_options(const String* options, int* result);
119:
120: private:
121: Charset* fcharset;
122: const char* fpattern;
1.5 misha 123: const char* foptions_cstr;
1.1 misha 124: int foptions[2];
125:
126: pcre* fcode;
127: pcre_extra* fextra;
128: bool fstudied;
129: };
130:
1.2 misha 131:
132: class VRegexCleaner {
133: public:
134: VRegex *vregex;
135:
136: VRegexCleaner(): vregex(0) { }
137:
138: ~VRegexCleaner(){
139: if(vregex) delete vregex;
140: }
141:
142: };
143:
1.1 misha 144:
145: #endif