Skip to content

Commit e47fa27

Browse files
committed
lib/pcre/UniqueRegex: add Compile() overload with std::string_view
1 parent 9522f1b commit e47fa27

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/lib/pcre/UniqueRegex.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ UniqueRegex::Compile(const char *pattern, const int options)
2727
pcre2_pattern_info_8(re, PCRE2_INFO_CAPTURECOUNT, &n) == 0)
2828
n_capture = n;
2929
}
30+
31+
void
32+
UniqueRegex::Compile(std::string_view pattern, const int options)
33+
{
34+
int error_number;
35+
PCRE2_SIZE error_offset;
36+
re = pcre2_compile_8(PCRE2_SPTR8(pattern.data()), pattern.size(),
37+
options,
38+
&error_number, &error_offset,
39+
nullptr);
40+
if (re == nullptr) {
41+
const auto msg = FmtBuffer<256>("Error in regex at offset {}",
42+
error_offset);
43+
throw Pcre::MakeError(error_number, msg);
44+
}
45+
46+
pcre2_jit_compile_8(re, PCRE2_JIT_COMPLETE);
47+
48+
if (int n; (options & PCRE2_NO_AUTO_CAPTURE) == 0 &&
49+
pcre2_pattern_info_8(re, PCRE2_INFO_CAPTURECOUNT, &n) == 0)
50+
n_capture = n;
51+
}

src/lib/pcre/UniqueRegex.hxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Options.hxx"
88
#include "RegexPointer.hxx"
99

10+
#include <string_view>
1011
#include <utility>
1112

1213
class UniqueRegex : public RegexPointer {
@@ -17,6 +18,10 @@ public:
1718
Compile(pattern, options);
1819
}
1920

21+
UniqueRegex(std::string_view pattern, Pcre::CompileOptions options) {
22+
Compile(pattern, options);
23+
}
24+
2025
UniqueRegex(UniqueRegex &&src) noexcept:RegexPointer(src) {
2126
src.re = nullptr;
2227
}
@@ -36,8 +41,13 @@ public:
3641
* Throws Pcre::Error on error.
3742
*/
3843
void Compile(const char *pattern, int options);
44+
void Compile(std::string_view pattern, int options);
3945

4046
void Compile(const char *pattern, Pcre::CompileOptions options={}) {
4147
Compile(pattern, (int)options);
4248
}
49+
50+
void Compile(std::string_view pattern, Pcre::CompileOptions options={}) {
51+
Compile(pattern, (int)options);
52+
}
4353
};

0 commit comments

Comments
 (0)