diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/regex/pcre | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/regex/pcre')
-rw-r--r-- | library/cpp/regex/pcre/regexp.cpp | 122 | ||||
-rw-r--r-- | library/cpp/regex/pcre/regexp.h | 28 | ||||
-rw-r--r-- | library/cpp/regex/pcre/regexp_ut.cpp | 30 | ||||
-rw-r--r-- | library/cpp/regex/pcre/ya.make | 2 |
4 files changed, 91 insertions, 91 deletions
diff --git a/library/cpp/regex/pcre/regexp.cpp b/library/cpp/regex/pcre/regexp.cpp index e7108ae5e9..575c09cee4 100644 --- a/library/cpp/regex/pcre/regexp.cpp +++ b/library/cpp/regex/pcre/regexp.cpp @@ -1,21 +1,21 @@ -#include "regexp.h" - +#include "regexp.h" + #include <util/generic/string.h> #include <util/string/ascii.h> #include <util/system/defaults.h> - + #include <cstdlib> #include <util/generic/noncopyable.h> - + class TGlobalImpl : TNonCopyable { private: - const char* Str; - regmatch_t* Pmatch; + const char* Str; + regmatch_t* Pmatch; int Options; int StrLen; int StartOffset, NotEmptyOpts, MatchPos; int MatchBuf[NMATCHES * 3]; - pcre* PregComp; + pcre* PregComp; enum StateCode { TGI_EXIT, @@ -26,25 +26,25 @@ private: private: void CopyResults(int count) { for (int i = 0; i < count; i++) { - Pmatch[MatchPos].rm_so = MatchBuf[2 * i]; - Pmatch[MatchPos].rm_eo = MatchBuf[2 * i + 1]; + Pmatch[MatchPos].rm_so = MatchBuf[2 * i]; + Pmatch[MatchPos].rm_eo = MatchBuf[2 * i + 1]; MatchPos++; if (MatchPos >= NMATCHES) { ythrow yexception() << "TRegExBase::Exec(): Not enough space in internal buffer."; } - } + } } int DoPcreExec(int opts) { int rc = pcre_exec( - PregComp, /* the compiled pattern */ - nullptr, /* no extra data - we didn't study the pattern */ - Str, /* the subject string */ - StrLen, /* the length of the subject */ - StartOffset, /* start at offset 0 in the subject */ - opts, /* default options */ - MatchBuf, /* output vector for substring information */ - NMATCHES); /* number of elements in the output vector */ + PregComp, /* the compiled pattern */ + nullptr, /* no extra data - we didn't study the pattern */ + Str, /* the subject string */ + StrLen, /* the length of the subject */ + StartOffset, /* start at offset 0 in the subject */ + opts, /* default options */ + MatchBuf, /* output vector for substring information */ + NMATCHES); /* number of elements in the output vector */ if (rc == 0) { ythrow yexception() << "TRegExBase::Exec(): Not enough space in internal buffer."; @@ -55,7 +55,7 @@ private: StateCode CheckEmptyCase() { if (MatchBuf[0] == MatchBuf[1]) { // founded an empty string - if (MatchBuf[0] == StrLen) { // at the end + if (MatchBuf[0] == StrLen) { // at the end return TGI_EXIT; } NotEmptyOpts = PCRE_NOTEMPTY | PCRE_ANCHORED; // trying to find non empty string @@ -65,25 +65,25 @@ private: StateCode CheckNoMatch(int rc) { if (rc == PCRE_ERROR_NOMATCH) { - if (NotEmptyOpts == 0) { + if (NotEmptyOpts == 0) { return TGI_EXIT; } - - MatchBuf[1] = StartOffset + 1; // we have failed to find non-empty-string. trying to find again shifting "previous match offset" + + MatchBuf[1] = StartOffset + 1; // we have failed to find non-empty-string. trying to find again shifting "previous match offset" return TGI_CONTINUE; } return TGI_WALKTHROUGH; } public: - TGlobalImpl(const char* st, regmatch_t& pma, int opts, pcre* pc_re) - : Str(st) - , Pmatch(&pma) - , Options(opts) - , StartOffset(0) - , NotEmptyOpts(0) - , MatchPos(0) - , PregComp(pc_re) + TGlobalImpl(const char* st, regmatch_t& pma, int opts, pcre* pc_re) + : Str(st) + , Pmatch(&pma) + , Options(opts) + , StartOffset(0) + , NotEmptyOpts(0) + , MatchPos(0) + , PregComp(pc_re) { memset(Pmatch, -1, sizeof(regmatch_t) * NMATCHES); StrLen = strlen(Str); @@ -114,29 +114,29 @@ public: return 0; case TGI_WALKTHROUGH: default: - break; - } + break; + } if (rc < 0) { return rc; } CopyResults(rc); - } while (true); + } while (true); - return 0; + return 0; } - + private: }; -class TRegExBaseImpl: public TAtomicRefCount<TRegExBaseImpl> { +class TRegExBaseImpl: public TAtomicRefCount<TRegExBaseImpl> { friend class TRegExBase; protected: - int CompileOptions; + int CompileOptions; TString RegExpr; - regex_t Preg; + regex_t Preg; public: TRegExBaseImpl() @@ -159,7 +159,7 @@ public: } } - int Exec(const char* str, regmatch_t pmatch[], int eflags, int nmatches) const { + int Exec(const char* str, regmatch_t pmatch[], int eflags, int nmatches) const { if (!RegExpr) { ythrow yexception() << "Regular expression is not compiled"; } @@ -170,12 +170,12 @@ public: return regexec(&Preg, str, nmatches, pmatch, eflags); } else { int options = 0; - if ((eflags & REG_NOTBOL) != 0) - options |= PCRE_NOTBOL; - if ((eflags & REG_NOTEOL) != 0) - options |= PCRE_NOTEOL; + if ((eflags & REG_NOTBOL) != 0) + options |= PCRE_NOTBOL; + if ((eflags & REG_NOTEOL) != 0) + options |= PCRE_NOTEOL; - return TGlobalImpl(str, pmatch[0], options, (pcre*)Preg.re_pcre).ExecGlobal(); + return TGlobalImpl(str, pmatch[0], options, (pcre*)Preg.re_pcre).ExecGlobal(); } } @@ -195,12 +195,12 @@ bool TRegExBase::IsCompiled() const { return Impl && Impl->IsCompiled(); } -TRegExBase::TRegExBase(const char* re, int cflags) { +TRegExBase::TRegExBase(const char* re, int cflags) { if (re) { Compile(re, cflags); } } - + TRegExBase::TRegExBase(const TString& re, int cflags) { Compile(re, cflags); } @@ -211,8 +211,8 @@ TRegExBase::~TRegExBase() { void TRegExBase::Compile(const TString& re, int cflags) { Impl = new TRegExBaseImpl(re, cflags); } - -int TRegExBase::Exec(const char* str, regmatch_t pmatch[], int eflags, int nmatches) const { + +int TRegExBase::Exec(const char* str, regmatch_t pmatch[], int eflags, int nmatches) const { if (!Impl) ythrow yexception() << "!Regular expression is not compiled"; return Impl->Exec(str, pmatch, eflags, nmatches); @@ -230,22 +230,22 @@ TString TRegExBase::GetRegExpr() const { return Impl->RegExpr; } -TRegExMatch::TRegExMatch(const char* re, int cflags) - : TRegExBase(re, cflags) -{ -} +TRegExMatch::TRegExMatch(const char* re, int cflags) + : TRegExBase(re, cflags) +{ +} TRegExMatch::TRegExMatch(const TString& re, int cflags) : TRegExBase(re, cflags) { } -bool TRegExMatch::Match(const char* str) const { +bool TRegExMatch::Match(const char* str) const { return Exec(str, nullptr, 0, 0) == 0; } -TRegExSubst::TRegExSubst(const char* re, int cflags) - : TRegExBase(re, cflags) +TRegExSubst::TRegExSubst(const char* re, int cflags) + : TRegExBase(re, cflags) , Replacement(nullptr) { memset(Brfs, 0, sizeof(TBackReferences) * NMATCHES); @@ -256,7 +256,7 @@ TString TRegExSubst::Replace(const char* str, int eflags) { if (BrfsCount) { if (Exec(str, PMatch, eflags) == 0) { int i; - for (i = 0; i < BrfsCount; i++) { + for (i = 0; i < BrfsCount; i++) { s += TString(Replacement, Brfs[i].Beg, Brfs[i].End - Brfs[i].Beg); if (Brfs[i].Refer >= 0 && Brfs[i].Refer < NMATCHES) s += TString(str, PMatch[Brfs[i].Refer].rm_so, int(PMatch[Brfs[i].Refer].rm_eo - PMatch[Brfs[i].Refer].rm_so)); @@ -280,15 +280,15 @@ TString TRegExSubst::Replace(const char* str, int eflags) { // {beg = 22, end = 25, Refer = -1} => "ccc" // {beg = 0, end = 0, Refer = 0} //*** -int TRegExSubst::ParseReplacement(const char* repl) { +int TRegExSubst::ParseReplacement(const char* repl) { Replacement = repl; if (!Replacement || *Replacement == 0) return 0; - char* pos = (char*)Replacement; + char* pos = (char*)Replacement; char* pos1 = nullptr; char* pos2 = nullptr; int i = 0; - while (pos && *pos && i < NMATCHES) { + while (pos && *pos && i < NMATCHES) { pos1 = strchr(pos, '$'); Brfs[i].Refer = -1; pos2 = pos1; @@ -296,11 +296,11 @@ int TRegExSubst::ParseReplacement(const char* repl) { pos2 = pos1 + 1; while (IsAsciiDigit(*pos2)) pos2++; - if (pos2 > pos1 + 1) { + if (pos2 > pos1 + 1) { Brfs[i].Refer = atol(TString(Replacement, pos1 + 1 - Replacement, pos2 - (pos1 + 1)).data()); } else { pos1++; - if (*pos2 == '$') + if (*pos2 == '$') pos2++; Brfs[i].Refer = -1; } diff --git a/library/cpp/regex/pcre/regexp.h b/library/cpp/regex/pcre/regexp.h index c74d20b3ad..bc610bd2f3 100644 --- a/library/cpp/regex/pcre/regexp.h +++ b/library/cpp/regex/pcre/regexp.h @@ -1,16 +1,16 @@ #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> - + +#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 @@ -19,38 +19,38 @@ 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; + 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 { +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; + bool Match(const char* str) const; }; -struct TBackReferences { +struct TBackReferences { int Beg; int End; int Refer; }; -class TRegExSubst: public TRegExBase { +class TRegExSubst: public TRegExBase { private: const char* Replacement; - regmatch_t PMatch[NMATCHES]; + regmatch_t PMatch[NMATCHES]; TBackReferences Brfs[NMATCHES]; int BrfsCount; @@ -59,5 +59,5 @@ public: TRegExSubst(const char* regExpr = nullptr, int cflags = REG_EXTENDED); TString Replace(const char* str, int eflags = 0); - int ParseReplacement(const char* replacement); + int ParseReplacement(const char* replacement); }; diff --git a/library/cpp/regex/pcre/regexp_ut.cpp b/library/cpp/regex/pcre/regexp_ut.cpp index 6ace430a16..5184e801cc 100644 --- a/library/cpp/regex/pcre/regexp_ut.cpp +++ b/library/cpp/regex/pcre/regexp_ut.cpp @@ -11,17 +11,17 @@ struct TRegTest { int CompileOptions; int RunOptions; - TRegTest(const char* re, const char* text, const char* res, int copts = REG_EXTENDED, int ropts = 0) - : Regexp(re) - , Data(text) - , Result(res) - , CompileOptions(copts) - , RunOptions(ropts) - { - } + TRegTest(const char* re, const char* text, const char* res, int copts = REG_EXTENDED, int ropts = 0) + : Regexp(re) + , Data(text) + , Result(res) + , CompileOptions(copts) + , RunOptions(ropts) + { + } }; -struct TSubstTest: public TRegTest { +struct TSubstTest: public TRegTest { const char* Replacement; const char* Replacement2; @@ -29,15 +29,15 @@ struct TSubstTest: public TRegTest { : TRegTest(re, text, res, REG_EXTENDED, REGEXP_GLOBAL) , Replacement(repl) , Replacement2(repl2) - { - } + { + } }; -const TRegTest REGTEST_DATA[] = { - TRegTest("test", "its a test and test string.", "6 10", REG_EXTENDED, 0), +const TRegTest REGTEST_DATA[] = { + TRegTest("test", "its a test and test string.", "6 10", REG_EXTENDED, 0), TRegTest("test", "its a test and test string.", "6 10 15 19", REG_EXTENDED, REGEXP_GLOBAL), TRegTest("test|[an]{0,0}", "test and test an test string tes", "0 4 4 4 5 5 6 6 7 7 8 8 9 13 13 13 14 14 15 15 16 16 17 21 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32", REG_EXTENDED, REGEXP_GLOBAL), - TRegTest("test[an]{1,}", "test and test an test string tes", "NM", REG_EXTENDED, REGEXP_GLOBAL)}; + TRegTest("test[an]{1,}", "test and test an test string tes", "NM", REG_EXTENDED, REGEXP_GLOBAL)}; const TSubstTest SUBSTTEST_DATA[] = { TSubstTest("([a-zA-Z]*[0-9]+) (_[a-z]+)", "Xxx123 534 ___124 bsd _A ZXC _L 141 _sd dsfg QWE123 _bbb", "141 XXX/_sd", "$1 XXX/$2", "$2$2$2 YY$1Y/$2")}; @@ -48,7 +48,7 @@ private: private: UNIT_TEST_SUITE(TRegexpTest); - UNIT_TEST(TestRe) + UNIT_TEST(TestRe) UNIT_TEST(TestSubst) UNIT_TEST(TestOffEndOfBuffer); UNIT_TEST_SUITE_END(); diff --git a/library/cpp/regex/pcre/ya.make b/library/cpp/regex/pcre/ya.make index 4971c6f35a..d34911f103 100644 --- a/library/cpp/regex/pcre/ya.make +++ b/library/cpp/regex/pcre/ya.make @@ -1,4 +1,4 @@ -LIBRARY() +LIBRARY() OWNER(g:util) |