13inline Result<std::string, std::string>
substitute_env(std::string_view in) {
15 out.reserve(in.size());
17 while (i < in.size()) {
18 if (in[i] ==
'$' && i + 1 < in.size() && in[i + 1] ==
'{') {
19 const std::size_t close = in.find(
'}', i + 2);
20 if (close == std::string_view::npos)
21 return fail(std::string(
"unterminated ${ in '") + std::string(in) +
"'");
22 const std::string name(in.substr(i + 2, close - i - 2));
23 const char* v = name.empty() ? nullptr : std::getenv(name.c_str());
24 if (v ==
nullptr)
return fail(name);
36 return s.find(
"${") != std::string_view::npos;
Minimal expected<T, E>: the hot path never throws, it returns Result.
Definition strategy.hpp:16
bool has_env_reference(std::string_view s) noexcept
Definition env_subst.hpp:37
constexpr Unexpected< std::decay_t< E > > fail(E &&e) noexcept
Definition result.hpp:25
Result< std::string, std::string > substitute_env(std::string_view in)
Returns the substituted string, or the name of the first missing variable.
Definition env_subst.hpp:15