mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Enhancement - Nibbler
- Added Nibbler::getQuoted with support for unescaping escaped quotes and for including the original quotes.
This commit is contained in:
parent
80f9af08e3
commit
91d5448a5a
3 changed files with 85 additions and 12 deletions
|
@ -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 (mCursor < mLength)
|
||||||
|
|
||||||
if (skip (c) &&
|
|
||||||
getUntil (c, result) &&
|
|
||||||
skip (c))
|
|
||||||
{
|
{
|
||||||
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ public:
|
||||||
bool getUntilEOL (std::string&);
|
bool getUntilEOL (std::string&);
|
||||||
bool getUntilEOS (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 getInt (int&);
|
||||||
bool getUnsignedInt (int&);
|
bool getUnsignedInt (int&);
|
||||||
bool getLiteral (const std::string&);
|
bool getLiteral (const std::string&);
|
||||||
|
|
|
@ -33,7 +33,7 @@ Context context;
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
UnitTest t (140);
|
UnitTest t (149);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -176,11 +176,11 @@ int main (int argc, char** argv)
|
||||||
|
|
||||||
n = Nibbler ("'\"'");
|
n = Nibbler ("'\"'");
|
||||||
t.ok (n.getQuoted ('\'', s), " ''\"'' : getQuoted (''') -> true");
|
t.ok (n.getQuoted ('\'', s), " ''\"'' : getQuoted (''') -> true");
|
||||||
t.is (s, "\"", " ''\"'' : getQuoted (''') -> '\"'");
|
t.is (s, "\"", " ''\"'' : getQuoted (''') -> '\"'"); // 81
|
||||||
|
|
||||||
n = Nibbler ("'x'");
|
n = Nibbler ("'x'");
|
||||||
t.ok (n.getQuoted ('\'', s), " ''x'' : getQuoted (''') -> true");
|
t.ok (n.getQuoted ('\'', s), " ''x'' : getQuoted (''') -> true");
|
||||||
t.is (s, "x", " ''x'' : getQuoted (''') -> ''");
|
t.is (s, "x", " ''x'' : getQuoted (''') -> ''"); // 83
|
||||||
|
|
||||||
n = Nibbler ("'x");
|
n = Nibbler ("'x");
|
||||||
t.notok (n.getQuoted ('\'', s), " ''x' : getQuoted (''') -> false");
|
t.notok (n.getQuoted ('\'', s), " ''x' : getQuoted (''') -> false");
|
||||||
|
@ -188,6 +188,26 @@ int main (int argc, char** argv)
|
||||||
n = Nibbler ("x");
|
n = Nibbler ("x");
|
||||||
t.notok (n.getQuoted ('\'', s), " 'x' : getQuoted (''') -> false");
|
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&);
|
// bool getInt (int&);
|
||||||
t.diag ("Nibbler::getInt");
|
t.diag ("Nibbler::getInt");
|
||||||
n = Nibbler ("123 -4");
|
n = Nibbler ("123 -4");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue