about summary refs log tree commit diff
path: root/src/libexpr/primops.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2016-10-19T14·32+0200
committerEelco Dolstra <edolstra@gmail.com>2016-10-19T14·32+0200
commit795d9b866881871419651081eb112f23b2f5bff5 (patch)
treee51077f7170bb04bb003fdadcd0e462bc52d9776 /src/libexpr/primops.cc
parent19c278de89fa60844fa1ea2b3c2abfa639f75b39 (diff)
parentc935e8eeaf3ef5f49f66468ba551fd016fd76aa0 (diff)
Merge branch 'regex' of https://github.com/groxxda/nix
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r--src/libexpr/primops.cc22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 377fb8c75eb4..5ba9830423b0 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -18,6 +18,7 @@
 
 #include <algorithm>
 #include <cstring>
+#include <regex>
 #include <dlfcn.h>
 
 
@@ -1618,25 +1619,26 @@ static void prim_hashString(EvalState & state, const Pos & pos, Value * * args,
    ‘null’ or a list containing substring matches. */
 static void prim_match(EvalState & state, const Pos & pos, Value * * args, Value & v)
 {
-    Regex regex(state.forceStringNoCtx(*args[0], pos), true);
+    std::regex regex(state.forceStringNoCtx(*args[0], pos), std::regex::extended);
 
     PathSet context;
-    string s = state.forceString(*args[1], context, pos);
+    const std::string str = state.forceString(*args[1], context, pos);
+
 
-    Regex::Subs subs;
-    if (!regex.matches(s, subs)) {
+    std::smatch match;
+    if (!std::regex_match(str, match, regex)) {
         mkNull(v);
         return;
     }
 
-    unsigned int len = subs.empty() ? 0 : subs.rbegin()->first + 1;
+    // the first match is the whole string
+    const size_t len = match.size() - 1;
     state.mkList(v, len);
-    for (unsigned int n = 0; n < len; ++n) {
-        auto i = subs.find(n);
-        if (i == subs.end())
-            mkNull(*(v.listElems()[n] = state.allocValue()));
+    for (size_t i = 0; i < len; ++i) {
+        if (!match[i+1].matched)
+            mkNull(*(v.listElems()[i] = state.allocValue()));
         else
-            mkString(*(v.listElems()[n] = state.allocValue()), i->second);
+            mkString(*(v.listElems()[i] = state.allocValue()), match[i + 1].str().c_str());
     }
 }