Annotation of parser3/src/types/pa_vregex.h, revision 1.16

1.1       misha       1: /** @file
                      2:        Parser: @b regex class decls.
                      3: 
1.15      moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       misha       6: */
                      7: 
                      8: #ifndef PA_VREGEX_H
                      9: #define PA_VREGEX_H
                     10: 
1.16    ! moko       11: #define IDENT_PA_VREGEX_H "$Id: pa_vregex.h,v 1.15 2023/09/26 20:49:13 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: 
                     20: // defines
                     21: 
                     22: #define VREGEX_TYPE "regex"
                     23: 
                     24: enum Match_feature {
                     25:        MF_GLOBAL_SEARCH = 0x01,
                     26:        MF_NEED_PRE_POST_MATCH = 0x02,
                     27:        MF_JUST_COUNT_MATCHES = 0x04
                     28: };
                     29: 
                     30: extern Methoded* regex_class;
                     31: 
                     32: // VRegex
                     33: class VRegex: public VStateless_object {
                     34: 
                     35: public: // Value
                     36: 
                     37:        override const char* type() const { return VREGEX_TYPE; }
                     38:        override VStateless_class *get_class() { return regex_class; }
                     39: 
1.2       misha      40:        /// VRegex: count
1.13      moko       41:        override int as_int() { return (int)get_info_size(); }
1.2       misha      42: 
                     43:        /// VRegex: count
1.8       misha      44:        override double as_double() { return get_info_size(); }
1.1       misha      45: 
                     46:        /// VRegex: scalar
1.7       moko       47:        override Value& as_expr_result();
1.1       misha      48: 
                     49:        /// VRegex: true
1.8       misha      50:        virtual bool is_defined() const { return true; }
1.1       misha      51: 
                     52:        /// VRegex: true
                     53:        override bool as_bool() const { return true; }
                     54: 
1.5       misha      55:        override Value* get_element(const String& aname);
                     56: 
1.1       misha      57: public: // usage
                     58: 
1.16    ! moko       59: #ifdef HAVE_PCRE2
        !            60:        static pcre2_general_context* fgen_ctxt;
        !            61: #endif
        !            62: 
1.1       misha      63:        VRegex():
1.3       misha      64:                fcharset(0),
                     65:                fpattern(0),
1.5       misha      66:                foptions_cstr(0),
1.3       misha      67:                fcode(0),
1.16    ! moko       68: #ifdef HAVE_PCRE2
        !            69:                fcmp_ctxt(0),
        !            70:                fmatch_ctxt(0),
        !            71:                fmatch_data(0)
        !            72: #else
1.3       misha      73:                fextra(0),
                     74:                fstudied(false)
1.16    ! moko       75: #endif
1.3       misha      76:        {
                     77:                foptions[0]=0;
                     78:                foptions[1]=0;
                     79:        }
1.1       misha      80: 
                     81:        VRegex(Charset& acharset, const String* aregex, const String* aoptions):
1.16    ! moko       82: #ifdef HAVE_PCRE2
        !            83:                fcmp_ctxt(0),
        !            84:                fmatch_ctxt(0),
        !            85:                fmatch_data(0)
        !            86: #else
1.3       misha      87:                fextra(0),
1.1       misha      88:                fstudied(false)
1.16    ! moko       89: #endif
1.1       misha      90:        {
                     91:                set(acharset, aregex, aoptions);
                     92:                compile();
                     93:        }
                     94: 
                     95:        ~VRegex(){
1.16    ! moko       96: #ifdef HAVE_PCRE2
        !            97:                pcre2_match_data_free(fmatch_data);
        !            98:                pcre2_match_context_free(fmatch_ctxt);
        !            99:                pcre2_compile_context_free(fcmp_ctxt);
        !           100:                pcre2_code_free(fcode);
        !           101: #else
1.1       misha     102:                if(fextra)
                    103:                        pcre_free(fextra);
                    104:                if(fcode)
                    105:                        pcre_free(fcode);
1.16    ! moko      106: #endif
1.1       misha     107:        }
                    108: 
                    109:        void set(Charset& acharset, const String* aregex, const String* aoptions);
1.12      moko      110:        void set(VRegex& avregex);
1.1       misha     111: 
                    112:        void compile();
                    113: 
                    114:        void study();
                    115: 
                    116:        int exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart=0);
                    117: 
                    118:        // size_t info();
                    119: 
                    120:        size_t full_info(int type);
                    121: 
                    122:        size_t get_info_size();
                    123: 
                    124:        size_t get_study_size();
                    125: 
                    126:        size_t get_options();
                    127: 
                    128:        bool is_pre_post_match_needed(){
                    129:                return (foptions[1] & MF_NEED_PRE_POST_MATCH)!=0;
                    130:        }
                    131: 
                    132:        bool is_just_count(){
                    133:                return (foptions[1] & MF_JUST_COUNT_MATCHES)!=0;
                    134:        }
                    135: 
                    136:        bool is_global_search(){
                    137:                return (foptions[1] & MF_GLOBAL_SEARCH)!=0;
                    138:        }
                    139: 
                    140: private:
                    141:        static void regex_options(const String* options, int* result);
                    142: 
                    143: private:
                    144:        Charset* fcharset;
                    145:        const char* fpattern;
1.5       misha     146:        const char* foptions_cstr;
1.1       misha     147:        int foptions[2];
                    148: 
1.16    ! moko      149: #ifdef HAVE_PCRE2
        !           150:        pcre2_code* fcode;
        !           151:        pcre2_compile_context* fcmp_ctxt;
        !           152:        pcre2_match_context* fmatch_ctxt;
        !           153:        pcre2_match_data* fmatch_data;
        !           154: #else
1.1       misha     155:        pcre* fcode;
                    156:        pcre_extra* fextra;
                    157:        bool fstudied;
1.16    ! moko      158: #endif
1.1       misha     159: };
                    160: 
1.2       misha     161: 
                    162: class VRegexCleaner {
                    163: public:
                    164:        VRegex *vregex;
                    165: 
                    166:        VRegexCleaner(): vregex(0) { }
                    167: 
                    168:        ~VRegexCleaner(){
                    169:                if(vregex) delete vregex;
                    170:        }
                    171: 
                    172: };
                    173: 
1.1       misha     174: 
                    175: #endif

E-mail: