query
On this page

concatStringsSep

lib.strings.concatStringsSep

Primop
Docs pulled from | This Revision | 12 minutes ago


Nixpkgs manual

Concatenate a list of strings with a separator between each element

Inputs

sep
Separator to add between elements
list
List of input strings

Type

concatStringsSep :: String -> [String] -> String

Examples

lib.strings.concatStringsSep usage example

concatStringsSep "/" ["usr" "local" "bin"]
=> "usr/local/bin"

Nix manual

Takes 2 arguments

separator, list

Concatenate a list of strings with a separator between each element, e.g. concatStringsSep "/" ["usr" "local" "bin"] == "usr/local/bin".

Time Complexity

O(n + m) (amortized) where:

n = number of list elements m = total length of output string

Noogle detected

Aliases

Implementation

This function is implemented in c++ and is part of the native nix runtime.

src/libexpr/primops.cc:5132

static void prim_concatStringsSep(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    NixStringContext context;

    auto sep = state.forceString(
        *args[0],
        context,
        pos,
        "while evaluating the first argument (the separator string) passed to builtins.concatStringsSep");
    state.forceList(
        *args[1],
        pos,
        "while evaluating the second argument (the list of strings to concat) passed to builtins.concatStringsSep");

    std::string res;
    res.reserve((args[1]->listSize() + 32) * sep.size());
    bool first = true;

    for (auto elem : args[1]->listView()) {
        if (first)
            first = false;
        else
            res += sep;
        res += *state.coerceToString(
            pos,
            *elem,
            context,
            "while evaluating one element of the list of strings to concat passed to builtins.concatStringsSep");
    }

    v.mkString(res, context, state.mem);
}

Implementation

The following is the current implementation of this function.

concatStringsSep = builtins.concatStringsSep;