OGLplus  (0.59.0) a C++ wrapper for rendering APIs

str_var_subst.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_STR_VAR_SUBST_HPP
10 #define EAGINE_STR_VAR_SUBST_HPP
11 
12 #include "callable_ref.hpp"
13 #include "config/basic.hpp"
14 #include "string_span.hpp"
15 #include <map>
16 #include <string>
17 
18 namespace eagine {
19 //------------------------------------------------------------------------------
24  bool keep_untranslated{false};
25 
27  char leading_sign{'$'};
28 
30  char opening_bracket{'{'};
31 
33  char closing_bracket{'}'};
34 };
35 //------------------------------------------------------------------------------
44  std::string& dst,
45  string_view src,
46  const callable_ref<optionally_valid<string_view>(string_view)>& translate,
47  variable_substitution_options = {}) -> std::string&;
48 //------------------------------------------------------------------------------
57  string_view src,
58  const callable_ref<optionally_valid<string_view>(string_view)>& translate,
59  variable_substitution_options = {}) -> std::string;
60 //------------------------------------------------------------------------------
69  const std::string& str,
70  span<const std::string> strings,
71  variable_substitution_options = {}) -> std::string;
72 //------------------------------------------------------------------------------
81  const std::string& str,
82  const std::map<std::string, std::string, str_view_less>& dictionary,
83  variable_substitution_options = {}) -> std::string;
84 //------------------------------------------------------------------------------
88 
89 public:
91  auto set(std::string name, std::string value) -> string_variable_map& {
92  _dict.emplace(std::move(name), std::move(value));
93  return *this;
94  }
95 
97  auto subst_variables(const std::string& str) const -> std::string {
98  return substitute_variables(str, _dict);
99  }
100 
102  auto operator()(const std::string& str) const -> std::string {
103  return substitute_variables(str, _dict);
104  }
105 
106 private:
107  std::map<std::string, std::string, str_view_less> _dict{};
108 };
109 //------------------------------------------------------------------------------
110 } // namespace eagine
111 
112 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
113 #include <eagine/str_var_subst.inl>
114 #endif
115 
116 #endif // EAGINE_STR_VAR_SUBST_HPP
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
static auto translate(std::shared_ptr< generator > gen, std::array< float, 3 > d) noexcept
Constructs instances of translated_gen modifier.
Definition: translated.hpp:41
Common code is placed in this namespace.
Definition: eagine.hpp:21
char leading_sign
The variable reference leading sign.
Definition: str_var_subst.hpp:27
auto subst_variables(const std::string &str) const -> std::string
Uses the stored variables to do substitution in the given string.
Definition: str_var_subst.hpp:97
basic_callable_ref< Sig, is_noexcept_function_v< Sig > > callable_ref
Alias for callable object references.
Definition: callable_ref.hpp:191
auto substitute_variables_into(std::string &dst, string_view src, const callable_ref< optionally_valid< string_view >(string_view)> &translate, variable_substitution_options={}) -> std::string &
Substitutes variable values by using translate, from src into dst.
Class storing a map of variable names to values, doing string substitution.
Definition: str_var_subst.hpp:87
char closing_bracket
The variable reference closing (right) bracket.
Definition: str_var_subst.hpp:33
bool keep_untranslated
Keep the untranslated variable references in the format string.
Definition: str_var_subst.hpp:24
char opening_bracket
The variable reference opening (left) bracket.
Definition: str_var_subst.hpp:30
auto set(std::string name, std::string value) -> string_variable_map &
Set the value of a variable with name.
Definition: str_var_subst.hpp:91
auto substitute_variables(string_view src, const callable_ref< optionally_valid< string_view >(string_view)> &translate, variable_substitution_options={}) -> std::string
Substitutes variable values by using translate, from src.
Options for string variable substitution customization.
Definition: str_var_subst.hpp:22
auto operator()(const std::string &str) const -> std::string
Uses the stored variables to do substitution in the given string.
Definition: str_var_subst.hpp:102

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).