FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
env_subst.hpp
Go to the documentation of this file.
1#pragma once
2// ${VAR} environment substitution for [venues.*] string values (8.7). Only exact
3// `${NAME}` tokens are replaced; a missing variable is an error (never silently empty).
5
6#include <cstdlib>
7#include <string>
8#include <string_view>
9
10namespace fastmm {
11
12// Returns the substituted string, or the name of the first missing variable.
13inline Result<std::string, std::string> substitute_env(std::string_view in) {
14 std::string out;
15 out.reserve(in.size());
16 std::size_t i = 0;
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);
25 out += v;
26 i = close + 1;
27 continue;
28 }
29 out.push_back(in[i]);
30 ++i;
31 }
32 return out;
33}
34
35[[nodiscard]] inline bool has_env_reference(std::string_view s) noexcept {
36 return s.find("${") != std::string_view::npos;
38
39} // namespace fastmm
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