aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/split.hh
blob: 5455b6bffd33966f3c26b4c8790c82946d677fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
///@file

#include <optional>
#include <string_view>

namespace nix {

/**
 * If `separator` is found, we return the portion of the string before the
 * separator, and modify the string argument to contain only the part after the
 * separator. Otherwise, we return `std::nullopt`, and we leave the argument
 * string alone.
 */
static inline std::optional<std::string_view> splitPrefixTo(std::string_view & string, char separator) {
    auto sepInstance = string.find(separator);

    if (sepInstance != std::string_view::npos) {
        auto prefix = string.substr(0, sepInstance);
        string.remove_prefix(sepInstance+1);
        return prefix;
    }

    return std::nullopt;
}

static inline bool splitPrefix(std::string_view & string, std::string_view prefix) {
    bool res = string.starts_with(prefix);
    if (res)
        string.remove_prefix(prefix.length());
    return res;
}

}