Annotation of parser3/src/types/pa_vregex.C, revision 1.4
1.1 misha 1: /** @file
2: Parser: @b regex class.
3:
4: Copyright(c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.4 ! misha 8: static const char * const IDENT_VREGEX_C="$Date: 2009-05-13 08:36:22 $";
1.1 misha 9:
10: #include "pa_vregex.h"
1.2 misha 11: #include "pa_vint.h"
1.1 misha 12:
13:
14: char* get_pcre_exec_error_text(int exec_result){
15: switch(exec_result){
16: case PCRE_ERROR_BADUTF8:
17: case PCRE_ERROR_BADUTF8_OFFSET:
18: return "UTF-8 validation failed during pcre_exec (%d).";
19: break;
20: default:
21: return "execution error (%d)";
22: }
23: }
24:
25:
26: Value& VRegex::as_expr_result(bool/*return_string_as_is=false*/) {
27: return *new VInt(as_int());
28: }
29:
30: void VRegex::regex_options(const String* options, int* result){
31: struct Regex_option {
32: const char* key;
33: const char* keyAlt;
34: int clear;
35: int set;
36: int *result;
37: } regex_option[]={
38: {"i", "I", 0, PCRE_CASELESS, result}, // a=A
39: {"s", "S", 0, PCRE_DOTALL, result}, // ^\n\n$ [default]
40: {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
41: {"x", 0, 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
42: {"U", 0, 0, PCRE_UNGREEDY, result}, // ungreedy patterns (greedy by default)
43: {"g", "G", 0, MF_GLOBAL_SEARCH, result+1}, // many rows
44: {"'", 0, 0, MF_NEED_PRE_POST_MATCH, result+1},
45: {"n", 0, 0, MF_JUST_COUNT_MATCHES, result+1},
46: {0, 0, 0, 0, 0}
47: };
48: result[0]=PCRE_EXTRA /* backslash+non-special char causes error */
49: | PCRE_DOTALL /* dot matches all chars including newline char */
50: | PCRE_DOLLAR_ENDONLY /* dollar matches only end of string, but not newline chars */;
51: result[1]=0;
52:
53: if(options && !options->is_empty())
54: for(Regex_option *o=regex_option; o->key; o++)
55: if(
56: options->pos(o->key)!=STRING_NOT_FOUND
57: || (o->keyAlt && options->pos(o->keyAlt)!=STRING_NOT_FOUND)
58: ){
59: *o->result &= ~o->clear;
60: *o->result |= o->set;
61: }
62: }
63:
64:
65: void VRegex::set(Charset& acharset, const String* aregex, const String* aoptions){
66: if(aregex->is_empty())
67: throw Exception(PARSER_RUNTIME,
68: 0,
69: "regexp is empty");
70:
71: fcharset=&acharset;
72: fpattern=aregex->cstr(String::L_UNSPECIFIED); // fix any tainted with L_REGEX
73:
74: regex_options(aoptions, foptions);
75: }
76:
77:
78: void VRegex::compile(){
79: const char* err_ptr;
80: int err_offset;
81: int options=foptions[0];
82:
83: // @todo (for UTF-8): check string & pattern and use PCRE_NO_UTF8_CHECK option
84: if(fcharset->isUTF8())
85: options|=PCRE_UTF8;
86:
87: fcode=pcre_compile(fpattern, options,
88: &err_ptr, &err_offset,
89: fcharset->pcre_tables);
90:
91: if(!fcode){
92: throw Exception(PCRE_EXCEPTION_TYPE,
1.4 ! misha 93: new String(fpattern+err_offset, String::L_TAINTED),
1.1 misha 94: "regular expression syntax error - %s", err_ptr);
95: }
96:
97: }
98:
99:
100: size_t VRegex::full_info(int type){
101: size_t result;
102: int fullinfo_result=pcre_fullinfo(fcode, fextra, type, &result);
103: if(fullinfo_result<0){
104: throw Exception(PCRE_EXCEPTION_TYPE,
1.4 ! misha 105: new String(fpattern, String::L_TAINTED),
1.1 misha 106: "pcre_full_info error (%d)", fullinfo_result);
107: }
108:
109: return result;
110: };
111:
112:
113: size_t VRegex::get_info_size(){
114: return full_info(PCRE_INFO_SIZE);
115: }
116:
117:
118: size_t VRegex::get_study_size(){
119: return full_info(PCRE_INFO_STUDYSIZE);
120: }
121:
122: void VRegex::study(){
123: if(fstudied)
124: return;
125:
126: const char* err_ptr;
127: fextra=pcre_study(fcode, 0/*options*/, &err_ptr);
128:
129: if(err_ptr){
130: throw Exception(PCRE_EXCEPTION_TYPE,
1.4 ! misha 131: new String(fpattern, String::L_TAINTED),
1.1 misha 132: "pcre_study error: %s", err_ptr);
133: }
134:
135: fstudied=true;
136: }
137:
138:
139: int VRegex::exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart){
140: int result=pcre_exec(fcode, fextra,
141: string, string_len, prestart,
142: 0, ovector, ovector_size);
143:
144: if(result<0 && result!=PCRE_ERROR_NOMATCH){
145: throw Exception(PCRE_EXCEPTION_TYPE,
1.4 ! misha 146: new String(fpattern, String::L_TAINTED),
1.1 misha 147: get_pcre_exec_error_text(result), result);
148: }
149:
150: return result;
151: }
152:
153:
E-mail: