Skip to content

Commit 188c803

Browse files
authored
Merge pull request #9508 from infinisil/revert-7762-boost-regex
Revert "Switch from std::regex to boost::regex"
2 parents 4781e7f + 333ea68 commit 188c803

File tree

4 files changed

+28
-39
lines changed

4 files changed

+28
-39
lines changed

flake.nix

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
configureFlags =
159159
lib.optionals stdenv.isLinux [
160-
"--with-boost=${boost-nix}/lib"
160+
"--with-boost=${boost}/lib"
161161
"--with-sandbox-shell=${sh}/bin/busybox"
162162
]
163163
++ lib.optionals (stdenv.isLinux && !(isStatic && stdenv.system == "aarch64-linux")) [
@@ -210,7 +210,7 @@
210210
version = libgit2.lastModifiedDate;
211211
cmakeFlags = (attrs.cmakeFlags or []) ++ ["-DUSE_SSH=exec"];
212212
}))
213-
boost-nix
213+
boost
214214
lowdown-nix
215215
libsodium
216216
]
@@ -434,14 +434,14 @@
434434

435435
propagatedBuildInputs = propagatedDeps;
436436

437-
disallowedReferences = [ boost-nix ];
437+
disallowedReferences = [ boost ];
438438

439439
preConfigure = lib.optionalString (! currentStdenv.hostPlatform.isStatic)
440440
''
441441
# Copy libboost_context so we don't get all of Boost in our closure.
442442
# https://github.com/NixOS/nixpkgs/issues/45462
443443
mkdir -p $out/lib
444-
cp -pd ${boost-nix}/lib/{libboost_context*,libboost_thread*,libboost_system*,libboost_regex*} $out/lib
444+
cp -pd ${boost}/lib/{libboost_context*,libboost_thread*,libboost_system*} $out/lib
445445
rm -f $out/lib/*.a
446446
${lib.optionalString currentStdenv.hostPlatform.isLinux ''
447447
chmod u+w $out/lib/*.so.*
@@ -451,9 +451,9 @@
451451
for LIB in $out/lib/*.dylib; do
452452
chmod u+w $LIB
453453
install_name_tool -id $LIB $LIB
454-
install_name_tool -delete_rpath ${boost-nix}/lib/ $LIB || true
454+
install_name_tool -delete_rpath ${boost}/lib/ $LIB || true
455455
done
456-
install_name_tool -change ${boost-nix}/lib/libboost_system.dylib $out/lib/libboost_system.dylib $out/lib/libboost_thread.dylib
456+
install_name_tool -change ${boost}/lib/libboost_system.dylib $out/lib/libboost_system.dylib $out/lib/libboost_thread.dylib
457457
''}
458458
'';
459459

@@ -481,13 +481,9 @@
481481
''}
482482
${lib.optionalString currentStdenv.isDarwin ''
483483
install_name_tool \
484-
-change ${boost-nix}/lib/libboost_context.dylib \
484+
-change ${boost}/lib/libboost_context.dylib \
485485
$out/lib/libboost_context.dylib \
486486
$out/lib/libnixutil.dylib
487-
install_name_tool \
488-
-change ${boost-nix}/lib/libboost_regex.dylib \
489-
$out/lib/libboost_regex.dylib \
490-
$out/lib/libnixexpr.dylib
491487
''}
492488
'';
493489

@@ -510,10 +506,6 @@
510506
meta.mainProgram = "nix";
511507
});
512508

513-
boost-nix = final.boost.override {
514-
enableIcu = false;
515-
};
516-
517509
lowdown-nix = with final; currentStdenv.mkDerivation rec {
518510
name = "lowdown-0.9.0";
519511

src/libexpr/local.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ libexpr_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/lib
1616

1717
libexpr_LIBS = libutil libstore libfetchers
1818

19-
libexpr_LDFLAGS += -lboost_context -lboost_regex -pthread
19+
libexpr_LDFLAGS += -lboost_context -pthread
2020
ifdef HOST_LINUX
2121
libexpr_LDFLAGS += -ldl
2222
endif

src/libexpr/primops.cc

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "primops.hh"
1818

1919
#include <boost/container/small_vector.hpp>
20-
#include <boost/regex.hpp>
2120
#include <nlohmann/json.hpp>
2221

2322
#include <sys/types.h>
@@ -26,6 +25,7 @@
2625

2726
#include <algorithm>
2827
#include <cstring>
28+
#include <regex>
2929
#include <dlfcn.h>
3030

3131
#include <cmath>
@@ -3886,30 +3886,19 @@ static RegisterPrimOp primop_convertHash({
38863886
.fun = prim_convertHash,
38873887
});
38883888

3889-
// regex aliases, switch between boost and std
3890-
using regex = boost::regex;
3891-
using regex_error = boost::regex_error;
3892-
using cmatch = boost::cmatch;
3893-
using cregex_iterator = boost::cregex_iterator;
3894-
namespace regex_constants = boost::regex_constants;
3895-
// overloaded function alias
3896-
constexpr auto regex_match = [] (auto &&...args) {
3897-
return boost::regex_match(std::forward<decltype(args)>(args)...);
3898-
};
3899-
39003889
struct RegexCache
39013890
{
39023891
// TODO use C++20 transparent comparison when available
3903-
std::unordered_map<std::string_view, regex> cache;
3892+
std::unordered_map<std::string_view, std::regex> cache;
39043893
std::list<std::string> keys;
39053894

3906-
regex get(std::string_view re)
3895+
std::regex get(std::string_view re)
39073896
{
39083897
auto it = cache.find(re);
39093898
if (it != cache.end())
39103899
return it->second;
39113900
keys.emplace_back(re);
3912-
return cache.emplace(keys.back(), regex(keys.back(), regex::extended)).first->second;
3901+
return cache.emplace(keys.back(), std::regex(keys.back(), std::regex::extended)).first->second;
39133902
}
39143903
};
39153904

@@ -3929,8 +3918,8 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
39293918
NixStringContext context;
39303919
const auto str = state.forceString(*args[1], context, pos, "while evaluating the second argument passed to builtins.match");
39313920

3932-
cmatch match;
3933-
if (!regex_match(str.begin(), str.end(), match, regex)) {
3921+
std::cmatch match;
3922+
if (!std::regex_match(str.begin(), str.end(), match, regex)) {
39343923
v.mkNull();
39353924
return;
39363925
}
@@ -3945,8 +3934,8 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
39453934
(v.listElems()[i] = state.allocValue())->mkString(match[i + 1].str());
39463935
}
39473936

3948-
} catch (regex_error & e) {
3949-
if (e.code() == regex_constants::error_space) {
3937+
} catch (std::regex_error & e) {
3938+
if (e.code() == std::regex_constants::error_space) {
39503939
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
39513940
state.debugThrowLastTrace(EvalError({
39523941
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
@@ -4009,8 +3998,8 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
40093998
NixStringContext context;
40103999
const auto str = state.forceString(*args[1], context, pos, "while evaluating the second argument passed to builtins.split");
40114000

4012-
auto begin = cregex_iterator(str.begin(), str.end(), regex);
4013-
auto end = cregex_iterator();
4001+
auto begin = std::cregex_iterator(str.begin(), str.end(), regex);
4002+
auto end = std::cregex_iterator();
40144003

40154004
// Any matches results are surrounded by non-matching results.
40164005
const size_t len = std::distance(begin, end);
@@ -4049,8 +4038,8 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
40494038

40504039
assert(idx == 2 * len + 1);
40514040

4052-
} catch (regex_error & e) {
4053-
if (e.code() == regex_constants::error_space) {
4041+
} catch (std::regex_error & e) {
4042+
if (e.code() == std::regex_constants::error_space) {
40544043
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
40554044
state.debugThrowLastTrace(EvalError({
40564045
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re),

src/libexpr/tests/primops.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,14 @@ namespace nix {
814814
ASSERT_THAT(*v.listElems()[0], IsStringEq("FOO"));
815815
}
816816

817+
TEST_F(PrimOpTest, match5) {
818+
// The regex "\\{}" is valid and matches the string "{}".
819+
// Caused a regression before when trying to switch from std::regex to boost::regex.
820+
// See https://github.com/NixOS/nix/pull/7762#issuecomment-1834303659
821+
auto v = eval("builtins.match \"\\\\{}\" \"{}\"");
822+
ASSERT_THAT(v, IsListOfSize(0));
823+
}
824+
817825
TEST_F(PrimOpTest, attrNames) {
818826
auto v = eval("builtins.attrNames { x = 1; y = 2; z = 3; a = 2; }");
819827
ASSERT_THAT(v, IsListOfSize(4));

0 commit comments

Comments
 (0)