Annotation of parser3/src/types/pa_vregex.C, revision 1.10

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.10    ! misha       8: static const char * const IDENT_VREGEX_C="$Date: 2009-09-17 02:17:25 $";
1.1       misha       9: 
                     10: #include "pa_vregex.h"
1.2       misha      11: #include "pa_vint.h"
1.8       misha      12: #include "pa_vstring.h"
                     13: 
                     14: 
                     15: // defines
                     16: 
                     17: #define REGEX_PATTERN_NAME "pattern"
                     18: #define REGEX_OPTIONS_NAME "options"
1.1       misha      19: 
                     20: 
                     21: char* get_pcre_exec_error_text(int exec_result){
                     22:        switch(exec_result){
                     23:                case PCRE_ERROR_BADUTF8:
                     24:                case PCRE_ERROR_BADUTF8_OFFSET:
                     25:                        return "UTF-8 validation failed during pcre_exec (%d).";
                     26:                        break;
                     27:                default:
                     28:                        return "execution error (%d)";
                     29:        }
                     30: }
                     31: 
                     32: 
                     33: Value& VRegex::as_expr_result(bool/*return_string_as_is=false*/) {
                     34:        return *new VInt(as_int());
                     35: }
                     36: 
                     37: void VRegex::regex_options(const String* options, int* result){
                     38:        struct Regex_option {
                     39:                const char* key;
                     40:                const char* keyAlt;
                     41:                int clear;
                     42:                int set;
                     43:                int *result;
                     44:        } regex_option[]={
                     45:                {"i", "I", 0, PCRE_CASELESS, result}, // a=A
                     46:                {"s", "S", 0, PCRE_DOTALL, result}, // ^\n\n$ [default]
                     47:                {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                     48:                {"x", 0, 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                     49:                {"U", 0, 0, PCRE_UNGREEDY, result}, // ungreedy patterns (greedy by default)
                     50:                {"g", "G", 0, MF_GLOBAL_SEARCH, result+1}, // many rows
                     51:                {"'", 0, 0, MF_NEED_PRE_POST_MATCH, result+1},
                     52:                {"n", 0, 0, MF_JUST_COUNT_MATCHES, result+1},
                     53:                {0, 0, 0, 0, 0}
                     54:        };
                     55:        result[0]=PCRE_EXTRA /* backslash+non-special char causes error */
                     56:                        | PCRE_DOTALL /* dot matches all chars including newline char */
                     57:                        | PCRE_DOLLAR_ENDONLY /* dollar matches only end of string, but not newline chars */;
                     58:        result[1]=0;
                     59: 
                     60:        if(options && !options->is_empty()) 
                     61:                for(Regex_option *o=regex_option; o->key; o++) 
                     62:                        if(
                     63:                                options->pos(o->key)!=STRING_NOT_FOUND
                     64:                                || (o->keyAlt && options->pos(o->keyAlt)!=STRING_NOT_FOUND)
                     65:                        ){
                     66:                                *o->result &= ~o->clear;
                     67:                                *o->result |= o->set;
                     68:                        }
                     69: }
                     70: 
                     71: 
                     72: void VRegex::set(Charset& acharset, const String* aregex, const String* aoptions){
                     73:        if(aregex->is_empty())
                     74:                throw Exception(PARSER_RUNTIME,
                     75:                        0,
                     76:                        "regexp is empty");
                     77: 
                     78:        fcharset=&acharset;
1.5       misha      79: 
1.7       misha      80:        fpattern=aregex->untaint_cstr(String::L_REGEX);
1.1       misha      81: 
1.9       misha      82:        foptions_cstr=aoptions?aoptions->cstr():0;
1.8       misha      83: 
1.1       misha      84:        regex_options(aoptions, foptions);
                     85: }
                     86: 
                     87: 
                     88: void VRegex::compile(){
                     89:        const char* err_ptr;
                     90:        int err_offset;
                     91:        int options=foptions[0];
                     92: 
                     93:        // @todo (for UTF-8): check string & pattern and use PCRE_NO_UTF8_CHECK option 
                     94:        if(fcharset->isUTF8())
                     95:                options|=PCRE_UTF8;
                     96: 
                     97:        fcode=pcre_compile(fpattern, options,
                     98:                &err_ptr, &err_offset,
                     99:                fcharset->pcre_tables);
                    100: 
                    101:        if(!fcode){
                    102:                throw Exception(PCRE_EXCEPTION_TYPE,
1.4       misha     103:                        new String(fpattern+err_offset, String::L_TAINTED),
1.1       misha     104:                        "regular expression syntax error - %s", err_ptr);
                    105:        }
                    106: 
                    107: }
                    108: 
                    109: 
                    110: size_t VRegex::full_info(int type){
                    111:        size_t result;
                    112:        int fullinfo_result=pcre_fullinfo(fcode, fextra, type, &result);
                    113:        if(fullinfo_result<0){
                    114:                throw Exception(PCRE_EXCEPTION_TYPE,
1.4       misha     115:                        new String(fpattern, String::L_TAINTED),
1.1       misha     116:                        "pcre_full_info error (%d)", fullinfo_result);
                    117:        }
                    118: 
                    119:        return result;
                    120: };
                    121: 
                    122: 
                    123: size_t VRegex::get_info_size(){
                    124:        return full_info(PCRE_INFO_SIZE);
                    125: }
                    126: 
                    127: 
                    128: size_t VRegex::get_study_size(){
                    129:        return full_info(PCRE_INFO_STUDYSIZE);
                    130: }
                    131: 
                    132: void VRegex::study(){
                    133:        if(fstudied)
                    134:                return;
                    135: 
                    136:        const char* err_ptr;
                    137:        fextra=pcre_study(fcode, 0/*options*/, &err_ptr);
                    138: 
                    139:        if(err_ptr){
                    140:                throw Exception(PCRE_EXCEPTION_TYPE,
1.4       misha     141:                        new String(fpattern, String::L_TAINTED),
1.1       misha     142:                        "pcre_study error: %s", err_ptr);
                    143:        }
                    144: 
                    145:        fstudied=true;
                    146: }
                    147: 
                    148: 
                    149: int VRegex::exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart){
                    150:        int result=pcre_exec(fcode, fextra, 
                    151:                string, string_len, prestart,
1.10    ! misha     152:                prestart>0 ? PCRE_NO_UTF8_CHECK : 0, ovector, ovector_size);
1.1       misha     153:                        
                    154:        if(result<0 && result!=PCRE_ERROR_NOMATCH){
                    155:                throw Exception(PCRE_EXCEPTION_TYPE, 
1.4       misha     156:                        new String(fpattern, String::L_TAINTED),
1.1       misha     157:                        get_pcre_exec_error_text(result), result);
                    158:        }
                    159: 
                    160:        return result;
                    161: }
                    162: 
                    163: 
1.8       misha     164: Value* VRegex::get_element(const String& aname) { 
                    165:        if(aname == REGEX_PATTERN_NAME)
                    166:                return new VString(*new String(fpattern, String::L_TAINTED));
                    167: 
                    168:        if(aname == REGEX_OPTIONS_NAME)
                    169:                return new VString(*new String(foptions_cstr, String::L_TAINTED));
                    170: 
                    171:        // .CLASS, .CLASS_NAME
                    172:        if(Value* result=VStateless_object::get_element(aname))
                    173:                return result;
                    174: 
                    175:        throw Exception(PARSER_RUNTIME,
                    176:                &aname,
                    177:                "reading of invalid field");
                    178: }

E-mail: