1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-13 22:12:43 +01:00

Have splitPrefix and splitPrefixTo parser helpers

This commit is contained in:
John Ericson 2020-07-02 23:16:57 +00:00
parent a7cd7425d9
commit 13796be78d
3 changed files with 15 additions and 9 deletions

View file

@ -3,13 +3,15 @@
#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, wer return `std::nullopt`, and we leave the argument
// string alone.
static inline std::optional<std::string_view> splitPrefix(std::string_view & string, char separator) {
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) {
@ -21,5 +23,11 @@ static inline std::optional<std::string_view> splitPrefix(std::string_view & str
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;
}
}