aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/regex/pcre/regexp.h
blob: 9862a9b85bd54c865ac8d2dc6fefb8b473ff0dd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include <sys/types.h>

#include <util/system/defaults.h>
#include <util/generic/string.h>
#include <util/generic/yexception.h>

#include <contrib/libs/pcre/pcre.h>
#include <contrib/libs/pcre/pcreposix.h>

//THIS CODE LOOKS LIKE A TRASH, BUT WORKS.

#define NMATCHES 100
#define REGEXP_GLOBAL 0x0080 // use this if you want to find all occurences

class TRegExBaseImpl;

class TRegExBase {
protected: 
    TSimpleIntrusivePtr<TRegExBaseImpl> Impl;

public: 
    TRegExBase(const char* regExpr = nullptr, int cflags = REG_EXTENDED);
    TRegExBase(const TString& regExpr, int cflags = REG_EXTENDED);

    virtual ~TRegExBase();
 
    int Exec(const char* str, regmatch_t pmatch[], int eflags, int nmatches = NMATCHES) const;
    void Compile(const TString& regExpr, int cflags = REG_EXTENDED);
    bool IsCompiled() const;
    int GetCompileOptions() const;
    TString GetRegExpr() const;
};

class TRegExMatch: public TRegExBase {
public: 
    TRegExMatch(const char* regExpr = nullptr, int cflags = REG_NOSUB | REG_EXTENDED);
    TRegExMatch(const TString& regExpr, int cflags = REG_NOSUB | REG_EXTENDED);

    bool Match(const char* str) const;
}; 

struct TBackReferences {
    int Beg; 
    int End; 
    int Refer; 
}; 

class TRegExSubst: public TRegExBase {
private: 
    const char* Replacement; 
    regmatch_t PMatch[NMATCHES];
 
    TBackReferences Brfs[NMATCHES]; 
    int BrfsCount; 
 
public: 
    TRegExSubst(const char* regExpr = nullptr, int cflags = REG_EXTENDED);
 
    TString Replace(const char* str, int eflags = 0);
    int ParseReplacement(const char* replacement);
};