2020-06-30 18:57:09 +03:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2020-06-30 18:57:09 +03:00
|
|
|
|
|
|
|
#include <optional>
|
2020-06-30 21:10:30 +03:00
|
|
|
#include <string_view>
|
2020-06-30 18:57:09 +03:00
|
|
|
|
2020-07-03 02:16:57 +03:00
|
|
|
#include "util.hh"
|
|
|
|
|
2020-06-30 18:57:09 +03:00
|
|
|
namespace nix {
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-07-03 02:16:57 +03:00
|
|
|
static inline std::optional<std::string_view> splitPrefixTo(std::string_view & string, char separator) {
|
2020-06-30 18:57:09 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-03 02:16:57 +03:00
|
|
|
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;
|
|
|
|
}
|
2020-07-02 18:29:33 +03:00
|
|
|
|
2020-06-30 18:57:09 +03:00
|
|
|
}
|