mirror of
https://github.com/textmate/textmate.git
synced 2026-04-28 03:00:34 -04:00
Add format string API that accepts lambda (callback) for variables
This commit is contained in:
@@ -386,13 +386,8 @@ namespace format_string
|
||||
nodes = std::make_shared<parser::nodes_t>(n);
|
||||
}
|
||||
|
||||
std::string format_string_t::expand (std::map<std::string, std::string> const& variables) const
|
||||
std::string format_string_t::expand (std::function<std::string(std::string const&, std::string const&)> const& getVariable) const
|
||||
{
|
||||
auto getVariable = [&variables](std::string const& name, std::string const& fallback) -> std::string {
|
||||
auto it = variables.find(name);
|
||||
return it != variables.end() ? it->second : fallback;
|
||||
};
|
||||
|
||||
expand_visitor v(getVariable, nullptr);
|
||||
v.traverse(*nodes);
|
||||
v.handle_case_changes();
|
||||
@@ -418,11 +413,20 @@ namespace format_string
|
||||
return v.res;
|
||||
}
|
||||
|
||||
std::string expand (std::string const& format, std::map<std::string, std::string> const& variables)
|
||||
std::string expand (std::string const& format, std::function<std::string(std::string const&, std::string const&)> const& getVariable)
|
||||
{
|
||||
if(format.find_first_of("$(\\") == std::string::npos)
|
||||
return format;
|
||||
return format_string_t(format).expand(variables);
|
||||
return format_string_t(format).expand(getVariable);
|
||||
}
|
||||
|
||||
std::string expand (std::string const& format, std::map<std::string, std::string> const& variables)
|
||||
{
|
||||
auto getVariable = [&variables](std::string const& name, std::string const& fallback) -> std::string {
|
||||
auto it = variables.find(name);
|
||||
return it != variables.end() ? it->second : fallback;
|
||||
};
|
||||
return expand(format, getVariable);
|
||||
}
|
||||
|
||||
std::string escape (std::string const& format)
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace format_string
|
||||
format_string_t (std::string const& str, char const* stopChars = "") { init(str, stopChars); }
|
||||
|
||||
format_string_t (parser::nodes_t const& nodes);
|
||||
std::string expand (std::map<std::string, std::string> const& variables) const;
|
||||
std::string expand (std::function<std::string(std::string const&, std::string const&)> const& getVariable) const;
|
||||
|
||||
size_t length () const { return _length; }
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace format_string
|
||||
};
|
||||
|
||||
PUBLIC std::string replace (std::string const& src, regexp::pattern_t const& ptrn, format_string_t const& format, bool repeat = true, std::map<std::string, std::string> const& variables = std::map<std::string, std::string>());
|
||||
PUBLIC std::string expand (std::string const& format, std::function<std::string(std::string const&, std::string const&)> const& getVariable);
|
||||
PUBLIC std::string expand (std::string const& format, std::map<std::string, std::string> const& variables = std::map<std::string, std::string>());
|
||||
PUBLIC std::string escape (std::string const& format);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user