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

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

E-mail: