Allow “stop chars” when parsing a format string

This is useful when we have a format that embed format strings and we wish to use the format string parsers (to avoid an extra layer of escapes).
This commit is contained in:
Allan Odgaard
2013-08-28 15:23:52 +02:00
parent 8feaba809e
commit 27c054484e
6 changed files with 18 additions and 10 deletions

View File

@@ -266,10 +266,10 @@ namespace format_string
nodes.reset(new parser::nodes_t(n));
}
void format_string_t::init (std::string const& str)
void format_string_t::init (std::string const& str, char const* stopChars)
{
D(DBF_FormatString, bug("%s\n", str.c_str()););
parser::nodes_t const& n = parser::parse_format_string(str);
parser::nodes_t const& n = parser::parse_format_string(str, stopChars, &_length);
nodes.reset(new parser::nodes_t(n));
}

View File

@@ -11,18 +11,21 @@ namespace format_string
{
WATCH_LEAKS(format_string::format_string_t);
format_string_t (char const* str = "") { init(str); }
format_string_t (std::string const& str) { init(str); }
format_string_t (char const* str = "", char const* stopChars = "") { init(str, stopChars); }
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;
size_t length () const { return _length; }
private:
friend std::string replace (std::string const& src, regexp::pattern_t const& ptrn, format_string_t const& format, bool repeat, std::map<std::string, std::string> const& variables);
friend std::set<std::string> get_variables (format_string_t const&);
void init (std::string const& str);
void init (std::string const& str, char const* stopChars);
std::shared_ptr<parser::nodes_t> nodes;
size_t _length = 0;
};
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>());

View File

@@ -387,11 +387,13 @@ bool parse_context_t::parse_snippet (char const* stopChars, nodes_t& nodes)
// = API Functions =
// =================
nodes_t parse_format_string (std::string const& str)
nodes_t parse_format_string (std::string const& str, char const* stopChars, size_t* length)
{
parse_context_t context(str);
nodes_t nodes;
context.parse_format_string("", nodes);
context.parse_format_string(stopChars, nodes);
if(length)
*length = context.bytes_parsed();
return nodes;
}

View File

@@ -28,7 +28,7 @@ namespace parser
OnigOptionType convert (regexp_options::type const& options);
nodes_t parse_format_string (std::string const& str);
nodes_t parse_format_string (std::string const& str, char const* stopChars = "", size_t* length = nullptr);
nodes_t parse_snippet (std::string const& str);
} /* parser */

View File

@@ -2,8 +2,8 @@
parser_base_t::parser_base_t (std::string const& str)
{
it = str.data();
last = str.data() + str.size();
first = it = str.data();
last = first + str.size();
}
bool parser_base_t::parse_char (char const* ch)

View File

@@ -10,7 +10,10 @@ struct parser_base_t
bool parse_int (size_t& res);
bool parse_until (char const* stopChars, std::string& res);
size_t bytes_parsed () const { return it - first; }
protected:
char const* first;
char const* it;
char const* last;
};