Fix up CommandParser operation and add unit tests
This commit is contained in:
1 parent
0da4108c64
commit
210a7b82c5
4 files changed
+116
-14
No files matched your search
@@ -18,7 +18,7 @@ bool CommandParser::parse(const EncodedString & command)
|
||||
auto collect_arg = [this, ¤t_arg, &arg_content] {
|
||||
if (arg_content)
|
||||
{
|
||||
m_args.push_back(std::make_shared<EncodedString>(¤t_arg[0], current_arg.size()));
|
||||
m_args.push_back(EncodedString(¤t_arg[0], current_arg.size()));
|
||||
current_arg.clear();
|
||||
arg_content = false;
|
||||
}
|
||||
@@ -30,16 +30,13 @@ bool CommandParser::parse(const EncodedString & command)
|
||||
if ((quote != 0u) && (cp == quote))
|
||||
{
|
||||
quote = 0u;
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
if ((quote == 0u) && ((cp == '\'') || (cp == '"')))
|
||||
else if ((quote == 0u) && ((cp == '\'') || (cp == '"')))
|
||||
{
|
||||
quote = cp;
|
||||
++it;
|
||||
continue;
|
||||
arg_content = true;
|
||||
}
|
||||
if (cp == '\\')
|
||||
else if (cp == '\\')
|
||||
{
|
||||
if (++it == end)
|
||||
{
|
||||
@@ -47,19 +44,16 @@ bool CommandParser::parse(const EncodedString & command)
|
||||
return false;
|
||||
}
|
||||
append_char();
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
else if (cp == ' ')
|
||||
else if ((cp == ' ') && (quote == 0u))
|
||||
{
|
||||
collect_arg();
|
||||
}
|
||||
else
|
||||
{
|
||||
append_char();
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
if (quote != 0u)
|
||||
|
||||
@@ -9,7 +9,7 @@ class CommandParser
|
||||
{
|
||||
public:
|
||||
bool parse(const EncodedString & command);
|
||||
std::shared_ptr<EncodedString> operator[](size_t index) const
|
||||
const EncodedString & operator[](size_t index) const
|
||||
{
|
||||
return m_args[index];
|
||||
}
|
||||
@@ -21,9 +21,13 @@ public:
|
||||
{
|
||||
return m_parse_error;
|
||||
}
|
||||
const std::vector<EncodedString> args() const
|
||||
{
|
||||
return m_args;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<EncodedString>> m_args;
|
||||
std::vector<EncodedString> m_args;
|
||||
std::string m_parse_error;
|
||||
};
|
||||
|
||||
|
||||
@@ -46,6 +46,10 @@ public:
|
||||
};
|
||||
|
||||
EncodedString(const uint8_t * data, size_t size, Encoding::Type encoding = Encoding::UTF_8);
|
||||
EncodedString(const std::string & s)
|
||||
: EncodedString((const uint8_t *)&s[0], s.size())
|
||||
{
|
||||
}
|
||||
|
||||
iterator begin() const;
|
||||
iterator end() const;
|
||||
@@ -61,6 +65,16 @@ public:
|
||||
{
|
||||
return (*m_data)[index];
|
||||
}
|
||||
bool operator==(const EncodedString & other) const
|
||||
{
|
||||
return (m_encoding == other.m_encoding) &&
|
||||
(m_size == other.m_size) &&
|
||||
(memcmp(&(*m_data)[0], &(*other.m_data)[0], m_size) == 0);
|
||||
}
|
||||
bool operator!=(const EncodedString & other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
bool operator==(const std::string & s) const
|
||||
{
|
||||
return (m_size == s.size()) && (memcmp(&(*m_data)[0], &s[0], m_size) == 0);
|
||||
|
||||
Reference in new issue
Block a user