diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2020-10-17 17:25:17 +0000 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2020-10-17 17:25:17 +0000 |
commit | 2546c63373149dea1511b892a9911768bd770cac (patch) | |
tree | 8766f3de7a9cd344e5b9067f83091ae7a28cfd0c /src/libutil/split.hh | |
parent | 7349f257da8278af9aae35544b15c9a204e2a57b (diff) | |
parent | f66bbd8c7bb1472facf8917e58e3cd4f6ddfa1b5 (diff) |
Merge commit 'f66bbd8c7bb1472facf8917e58e3cd4f6ddfa1b5' into auto-uid-allocation
Diffstat (limited to 'src/libutil/split.hh')
-rw-r--r-- | src/libutil/split.hh | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libutil/split.hh b/src/libutil/split.hh new file mode 100644 index 000000000..87a23b13e --- /dev/null +++ b/src/libutil/split.hh @@ -0,0 +1,33 @@ +#pragma once + +#include <optional> +#include <string_view> + +#include "util.hh" + +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 = hasPrefix(string, prefix); + if (res) + string.remove_prefix(prefix.length()); + return res; +} + +} |