aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/regex/hyperscan/hyperscan.cpp
diff options
context:
space:
mode:
authorartyasen <artyasen@yandex-team.com>2025-01-10 10:43:42 +0300
committerartyasen <artyasen@yandex-team.com>2025-01-10 11:54:42 +0300
commit215178650c519a89f29a47f942fe2be607a91e58 (patch)
tree58313a1490bf7d70dde00647915e468ae55e710d /library/cpp/regex/hyperscan/hyperscan.cpp
parent2677f7fd48473bd66e77dbf330dc9065db086e9f (diff)
downloadydb-215178650c519a89f29a47f942fe2be607a91e58.tar.gz
HyperScan literal compilation
add literal compilation commit_hash:29f6f2d5c4ec11ceb61add67bc4e697194a4efff
Diffstat (limited to 'library/cpp/regex/hyperscan/hyperscan.cpp')
-rw-r--r--library/cpp/regex/hyperscan/hyperscan.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/library/cpp/regex/hyperscan/hyperscan.cpp b/library/cpp/regex/hyperscan/hyperscan.cpp
index 0a4bfcb9ec..bfb9d7b5ff 100644
--- a/library/cpp/regex/hyperscan/hyperscan.cpp
+++ b/library/cpp/regex/hyperscan/hyperscan.cpp
@@ -96,6 +96,27 @@ namespace NHyperscan {
return db;
}
+ TDatabase CompileLiteral(const TStringBuf& literal, unsigned int flags, hs_platform_info_t* platform) {
+ hs_database_t* rawDb = nullptr;
+ hs_compile_error_t* rawCompileErr = nullptr;
+ hs_error_t status = hs_compile_lit(
+ literal.data(),
+ flags,
+ literal.size(),
+ HS_MODE_BLOCK,
+ platform,
+ &rawDb,
+ &rawCompileErr);
+ TDatabase db(rawDb);
+ NHyperscan::TCompileError compileError(rawCompileErr);
+ if (status != HS_SUCCESS) {
+ ythrow TCompileException()
+ << "Failed to compile literal: " << literal << ". "
+ << "Error message (hyperscan): " << compileError->message;
+ }
+ return db;
+ }
+
TDatabase CompileMulti(
const TVector<const char*>& regexs,
const TVector<unsigned int>& flags,
@@ -150,6 +171,61 @@ namespace NHyperscan {
return db;
}
+ TDatabase CompileMultiLiteral(
+ const TVector<const char*>& literals,
+ const TVector<unsigned int>& flags,
+ const TVector<unsigned int>& ids,
+ const TVector<size_t>& lens,
+ hs_platform_info_t* platform)
+ {
+ unsigned int count = literals.size();
+ if (flags.size() != count) {
+ ythrow yexception()
+ << "Mismatch of sizes vectors passed to CompileMultiLiteral. "
+ << "size(literals) = " << literals.size() << ". "
+ << "size(flags) = " << flags.size() << ".";
+ }
+ if (ids.size() != count) {
+ ythrow yexception()
+ << "Mismatch of sizes vectors passed to CompileMultiLiteral. "
+ << "size(literals) = " << literals.size() << ". "
+ << "size(ids) = " << ids.size() << ".";
+ }
+ if (lens.size() != count) {
+ ythrow yexception()
+ << "Mismatch of sizes vectors passed to CompileMultiLiteral. "
+ << "size(literals) = " << literals.size() << ". "
+ << "size(lens) = " << lens.size() << ".";
+ }
+ hs_database_t* rawDb = nullptr;
+ hs_compile_error_t* rawCompileErr = nullptr;
+ hs_error_t status = hs_compile_lit_multi(
+ literals.data(),
+ flags.data(),
+ ids.data(),
+ lens.data(),
+ count,
+ HS_MODE_BLOCK,
+ platform,
+ &rawDb,
+ &rawCompileErr);
+ TDatabase db(rawDb);
+ NHyperscan::TCompileError compileError(rawCompileErr);
+ if (status != HS_SUCCESS) {
+ if (compileError->expression >= 0) {
+ const char* literal = literals[compileError->expression];
+ ythrow TCompileException()
+ << "Failed to compile literal: " << literal << ". "
+ << "Error message (hyperscan): " << compileError->message;
+ } else {
+ ythrow TCompileException()
+ << "Failed to compile multiple literals. "
+ << "Error message (hyperscan): " << compileError->message;
+ }
+ }
+ return db;
+ }
+
bool Matches(
const TDatabase& db,
const TScratch& scratch,
@@ -180,6 +256,16 @@ namespace NHyperscan {
return NPrivate::Compile(regex, flags, &platformInfo);
}
+ TDatabase CompileLiteral(const TStringBuf& literal, unsigned int flags) {
+ auto platformInfo = NPrivate::MakeCurrentPlatformInfo();
+ return NPrivate::CompileLiteral(literal, flags, &platformInfo);
+ }
+
+ TDatabase CompileLiteral(const TStringBuf& literal, unsigned int flags, TCPUFeatures cpuFeatures) {
+ auto platformInfo = NPrivate::MakePlatformInfo(cpuFeatures);
+ return NPrivate::CompileLiteral(literal, flags, &platformInfo);
+ }
+
TDatabase CompileMulti(
const TVector<const char*>& regexs,
const TVector<unsigned int>& flags,
@@ -201,6 +287,27 @@ namespace NHyperscan {
return NPrivate::CompileMulti(regexs, flags, ids, &platformInfo, extendedParameters);
}
+ TDatabase CompileMultiLiteral(
+ const TVector<const char*>& literals,
+ const TVector<unsigned int>& flags,
+ const TVector<unsigned int>& ids,
+ const TVector<size_t>& lens)
+ {
+ auto platformInfo = NPrivate::MakeCurrentPlatformInfo();
+ return NPrivate::CompileMultiLiteral(literals, flags, ids, lens, &platformInfo);
+ }
+
+ TDatabase CompileMultiLiteral(
+ const TVector<const char*>& literals,
+ const TVector<unsigned int>& flags,
+ const TVector<unsigned int>& ids,
+ const TVector<size_t>& lens,
+ TCPUFeatures cpuFeatures)
+ {
+ auto platformInfo = NPrivate::MakePlatformInfo(cpuFeatures);
+ return NPrivate::CompileMultiLiteral(literals, flags, ids, lens, &platformInfo);
+ }
+
TScratch MakeScratch(const TDatabase& db) {
hs_scratch_t* rawScratch = nullptr;
hs_error_t status = Singleton<NPrivate::TImpl>()->AllocScratch(db.Get(), &rawScratch);