|
|
1.1 misha 1: /** @file
2: Parser: @b regex class decls.
3:
1.11 moko 4: Copyright (c) 2001-2017 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.12 ! moko 11: #define IDENT_PA_VREGEX_H "$Id: pa_vregex.h,v 1.11 2017/02/07 22:00:50 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);
1.12 ! moko 88: void set(VRegex& avregex);
1.1 misha 89:
90: void compile();
91:
92: void study();
93:
94: int exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart=0);
95:
96: // size_t info();
97:
98: size_t full_info(int type);
99:
100: size_t get_info_size();
101:
102: size_t get_study_size();
103:
104: size_t get_options();
105:
106: bool is_pre_post_match_needed(){
107: return (foptions[1] & MF_NEED_PRE_POST_MATCH)!=0;
108: }
109:
110: bool is_just_count(){
111: return (foptions[1] & MF_JUST_COUNT_MATCHES)!=0;
112: }
113:
114: bool is_global_search(){
115: return (foptions[1] & MF_GLOBAL_SEARCH)!=0;
116: }
117:
118: private:
119: static void regex_options(const String* options, int* result);
120:
121: private:
122: Charset* fcharset;
123: const char* fpattern;
1.5 misha 124: const char* foptions_cstr;
1.1 misha 125: int foptions[2];
126:
127: pcre* fcode;
128: pcre_extra* fextra;
129: bool fstudied;
130: };
131:
1.2 misha 132:
133: class VRegexCleaner {
134: public:
135: VRegex *vregex;
136:
137: VRegexCleaner(): vregex(0) { }
138:
139: ~VRegexCleaner(){
140: if(vregex) delete vregex;
141: }
142:
143: };
144:
1.1 misha 145:
146: #endif