aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/meson.build2
-rw-r--r--src/libutil/regex.cc16
-rw-r--r--src/libutil/regex.hh11
3 files changed, 29 insertions, 0 deletions
diff --git a/src/libutil/meson.build b/src/libutil/meson.build
index 11bf97ee7..069798a6f 100644
--- a/src/libutil/meson.build
+++ b/src/libutil/meson.build
@@ -22,6 +22,7 @@ libutil_sources = files(
'position.cc',
'print-elided.cc',
'references.cc',
+ 'regex.cc',
'serialise.cc',
'shlex.cc',
'signals.cc',
@@ -77,6 +78,7 @@ libutil_headers = files(
'ref.hh',
'references.hh',
'regex-combinators.hh',
+ 'regex.hh',
'repair-flag.hh',
'serialise.hh',
'shlex.hh',
diff --git a/src/libutil/regex.cc b/src/libutil/regex.cc
new file mode 100644
index 000000000..a9e6c6bee
--- /dev/null
+++ b/src/libutil/regex.cc
@@ -0,0 +1,16 @@
+#include <string>
+#include <regex>
+
+namespace nix::regex {
+std::string quoteRegexChars(const std::string & raw)
+{
+ static auto specialRegex = std::regex(R"([.^$\\*+?()\[\]{}|])");
+ return std::regex_replace(raw, specialRegex, R"(\$&)");
+}
+
+std::regex storePathRegex(const std::string & storeDir)
+{
+ return std::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)");
+}
+
+}
diff --git a/src/libutil/regex.hh b/src/libutil/regex.hh
new file mode 100644
index 000000000..744a7d54a
--- /dev/null
+++ b/src/libutil/regex.hh
@@ -0,0 +1,11 @@
+#pragma once
+///@file
+
+#include <string>
+#include <regex>
+
+namespace nix::regex {
+std::string quoteRegexChars(const std::string & raw);
+
+std::regex storePathRegex(const std::string & storeDir);
+}