aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2024-11-01 11:58:42 +0200
committerKaren Arutyunov <karen@codesynthesis.com>2024-11-04 10:41:08 +0200
commitd061c88ae81eb5e1354526f07f9f8d90d0732656 (patch)
tree0e639efdc35d8fd89804dce728e6fc110c0dc9bb /tests
parent0befab300849be7ac0f77bc4228f8de50a108191 (diff)
Add comments parameter to string_parser functions
Diffstat (limited to 'tests')
-rw-r--r--tests/string-parser/driver.cxx24
-rw-r--r--tests/string-parser/testscript30
2 files changed, 49 insertions, 5 deletions
diff --git a/tests/string-parser/driver.cxx b/tests/string-parser/driver.cxx
index 8cba912..93d2088 100644
--- a/tests/string-parser/driver.cxx
+++ b/tests/string-parser/driver.cxx
@@ -14,13 +14,14 @@
using namespace std;
using namespace butl::string_parser;
-// Usage: argv[0] [-l] [-u] [-p]
+// Usage: argv[0] [-l] [-u] [-p] [-c]
//
// Read and parse lines into strings from STDIN and print them to STDOUT.
//
// -l output each string on a separate line
// -u unquote strings
// -p output positions
+// -c comments
//
int
main (int argc, char* argv[])
@@ -29,6 +30,7 @@ try
bool spl (false); // Print string per line.
bool unquote (false);
bool pos (false);
+ bool comments (false);
for (int i (1); i != argc; ++i)
{
@@ -40,6 +42,8 @@ try
unquote = true;
else if (o == "-p")
pos = true;
+ else if (o == "-c")
+ comments = true;
else
assert (false);
}
@@ -51,11 +55,8 @@ try
cout.exceptions (ios::failbit | ios::badbit);
- string l;
- while (getline (cin, l))
+ auto print = [spl, pos] (const vector<pair<string, size_t>>& v)
{
- vector<pair<string, size_t>> v (parse_quoted_position (l, unquote));
-
if (!spl)
{
for (auto b (v.cbegin ()), i (b), e (v.cend ()); i != e; ++i)
@@ -81,6 +82,19 @@ try
cout << s.first << endl;
}
}
+ };
+
+ if (!comments)
+ {
+ string l;
+ while (getline (cin, l))
+ print (parse_quoted_position (l, unquote));
+ }
+ else
+ {
+ string s;
+ getline (cin, s, '\0');
+ print (parse_quoted_position (s, unquote, true /* comments */));
}
return 0;
diff --git a/tests/string-parser/testscript b/tests/string-parser/testscript
index d484e01..05c2807 100644
--- a/tests/string-parser/testscript
+++ b/tests/string-parser/testscript
@@ -30,6 +30,36 @@
x "y z
EOO
}
+
+ : comments
+ :
+ {
+ $* -c <<EOI >>EOO
+ # Comment 1
+ #
+ abc #xyz
+
+ # Comment 2
+ #
+ abc#
+
+ "# not a comment 3" #not-a-comment4
+
+ "abc
+ # not a comment 5
+ "
+ # Comment 6
+ EOI
+ abc
+ #xyz
+ abc#
+ "# not a comment 3"
+ #not-a-comment4
+ "abc
+ # not a comment 5
+ "
+ EOO
+ }
}
: invalid