diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-07-24 02:31:58 +0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-07-24 02:38:09 +0200 |
commit | 2e8fd4c5cdeb59ae2bd403aa5069993303e8388e (patch) | |
tree | f9d51f5420dcd986b7f581324e068ea9a822c4a1 | |
parent | cb4320c1a0808fe88084d391341da86123b36e6a (diff) |
Add concatStringsSep as a primop
This fixes the quadratic behaviour of concatStrings/concatStringsSep
in Nixpkgs.
-rw-r--r-- | src/libexpr/primops.cc | 21 | ||||
-rw-r--r-- | tests/lang/eval-okay-concatstringssep.exp | 1 | ||||
-rw-r--r-- | tests/lang/eval-okay-concatstringssep.nix | 8 |
3 files changed, 30 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 7a4aad3cd..c320b4c39 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1514,6 +1514,26 @@ static void prim_match(EvalState & state, const Pos & pos, Value * * args, Value } +static void prim_concatStringSep(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + PathSet context; + + auto sep = state.forceString(*args[0], context, pos); + state.forceList(*args[1]); + + string res; + res.reserve((args[1]->listSize() + 32) * sep.size()); + bool first = true; + + for (unsigned int n = 0; n < args[1]->listSize(); ++n) { + if (first) first = false; else res += sep; + res += state.coerceToString(pos, *args[1]->listElems()[n], context); + } + + mkString(v, res, context); +} + + /************************************************************* * Versions *************************************************************/ @@ -1720,6 +1740,7 @@ void EvalState::createBaseEnv() addPrimOp("__unsafeDiscardOutputDependency", 1, prim_unsafeDiscardOutputDependency); addPrimOp("__hashString", 2, prim_hashString); addPrimOp("__match", 2, prim_match); + addPrimOp("__concatStringsSep", 2, prim_concatStringSep); // Versions addPrimOp("__parseDrvName", 1, prim_parseDrvName); diff --git a/tests/lang/eval-okay-concatstringssep.exp b/tests/lang/eval-okay-concatstringssep.exp new file mode 100644 index 000000000..93987647f --- /dev/null +++ b/tests/lang/eval-okay-concatstringssep.exp @@ -0,0 +1 @@ +[ "" "foobarxyzzy" "foo, bar, xyzzy" "foo" "" ] diff --git a/tests/lang/eval-okay-concatstringssep.nix b/tests/lang/eval-okay-concatstringssep.nix new file mode 100644 index 000000000..adc4c41bd --- /dev/null +++ b/tests/lang/eval-okay-concatstringssep.nix @@ -0,0 +1,8 @@ +with builtins; + +[ (concatStringsSep "" []) + (concatStringsSep "" ["foo" "bar" "xyzzy"]) + (concatStringsSep ", " ["foo" "bar" "xyzzy"]) + (concatStringsSep ", " ["foo"]) + (concatStringsSep ", " []) +] |