Enhancement - Nibbler

- Added Nibbler::getQuoted with support for unescaping escaped quotes
  and for including the original quotes.
This commit is contained in:
Paul Beckingham 2010-08-13 00:48:05 -04:00
parent 80f9af08e3
commit 91d5448a5a
3 changed files with 85 additions and 12 deletions

View file

@ -210,18 +210,71 @@ bool Nibbler::getUntilEOS (std::string& result)
}
////////////////////////////////////////////////////////////////////////////////
bool Nibbler::getQuoted (char c, std::string& result)
bool Nibbler::getQuoted (
char c,
std::string& result,
bool unescape /* = true */,
bool quote /* = false */)
{
std::string::size_type backup = mCursor;
if (skip (c) &&
getUntil (c, result) &&
skip (c))
if (mCursor < mLength)
{
return true;
if (mInput[mCursor] != c)
return false;
result = "";
bool inquote = false;
char current = 0;
char previous = 0;
// '"'
// p c
// - -
// '
// ' "
// " '
for (std::string::size_type i = mCursor; i < mLength; ++i)
{
previous = current;
current = mInput[i];
if (current == c)
{
if (previous == '\\')
{
if (!unescape)
result += previous;
result += current;
}
else
{
if (!inquote)
{
inquote = true;
if (quote)
result += current;
}
else
{
if (quote)
result += current;
mCursor = i + 1;
return true;
}
}
}
else if (current == '\\')
{
// NOP
}
else
{
result += current;
}
}
}
mCursor = backup;
return false;
}

View file

@ -47,7 +47,7 @@ public:
bool getUntilEOL (std::string&);
bool getUntilEOS (std::string&);
bool getQuoted (char, std::string&);
bool getQuoted (char, std::string&, bool unescape = true, bool quote = false);
bool getInt (int&);
bool getUnsignedInt (int&);
bool getLiteral (const std::string&);

View file

@ -33,7 +33,7 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (140);
UnitTest t (149);
try
{
@ -176,11 +176,11 @@ int main (int argc, char** argv)
n = Nibbler ("'\"'");
t.ok (n.getQuoted ('\'', s), " ''\"'' : getQuoted (''') -> true");
t.is (s, "\"", " ''\"'' : getQuoted (''') -> '\"'");
t.is (s, "\"", " ''\"'' : getQuoted (''') -> '\"'"); // 81
n = Nibbler ("'x'");
t.ok (n.getQuoted ('\'', s), " ''x'' : getQuoted (''') -> true");
t.is (s, "x", " ''x'' : getQuoted (''') -> ''");
t.is (s, "x", " ''x'' : getQuoted (''') -> ''"); // 83
n = Nibbler ("'x");
t.notok (n.getQuoted ('\'', s), " ''x' : getQuoted (''') -> false");
@ -188,6 +188,26 @@ int main (int argc, char** argv)
n = Nibbler ("x");
t.notok (n.getQuoted ('\'', s), " 'x' : getQuoted (''') -> false");
n = Nibbler ("\"one\\\"two\"");
t.notok (n.getQuoted ('\'', s), "\"one\\\"two\" : getQuoted (''') -> false"); // 86
n = Nibbler ("\"one\\\"two\"");
t.ok (n.getQuoted ('"', s, false, false), "\"one\\\"two\" : getQuoted ('\"', false, false) -> true"); // 87
t.is (s, "one\\\"two", "getQuoted ('\"', false, false) -> one\\\"two"); // 88
n = Nibbler ("\"one\\\"two\"");
t.ok (n.getQuoted ('"', s, false, true), "\"one\\\"two\" : getQuoted ('\"', false, true) -> true"); // 89
t.is (s, "\"one\\\"two\"", "getQuoted ('\"', false, true) -> \"one\\\"two\""); // 90
n = Nibbler ("\"one\\\"two\"");
t.ok (n.getQuoted ('"', s, true, false), "\"one\\\"two\" : getQuoted ('\"', true, false) -> true"); // 91
t.is (s, "one\"two", "getQuoted ('\"', true, false) -> one\"two"); // 92
n = Nibbler ("\"one\\\"two\"");
t.ok (n.getQuoted ('"', s, true, true), "\"one\\\"two\" : getQuoted ('\"', true, true) -> true"); // 93
t.is (s, "\"one\"two\"", "getQuoted ('\"', true, true) -> \"one\"two\""); // 94
// bool getInt (int&);
t.diag ("Nibbler::getInt");
n = Nibbler ("123 -4");