Skip to content

Commit

Permalink
Fix argument name splitting inside strings and chars.
Browse files Browse the repository at this point in the history
A comma inside a string or a char was being detected as an argument separator.
  • Loading branch information
renatoGarcia committed May 7, 2023
1 parent b1f01ec commit 92bf143
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions icecream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,23 @@ namespace icecream{ namespace detail
if (!this->arg_names.empty())
while (true)
{
// Don't split anything inside a string
if (*it == '"' && *(it+1) != '\\')
{
++it;
while (!(*it == '"' && *(it+1) != '\\')) ++it;
++it;
}

// Don't split a ','
if (*it == '\'' && *(it+1) != '\\')
{
++it;
while (!(*it == '\'' && *(it+1) != '\\')) ++it;
++it;
}


if (it == arg_names.crend() || (*it == ',' && par_count == 0 && angle_count == 0))
{
// Remove the leading spaces
Expand Down
33 changes: 33 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,39 @@ TEST_CASE("base")
str.clear();
}

{
auto const result =
"ic| 7: 7\n"
" \"same<int, \\\"int>\": [\n"
" 's',\n"
" 'a',\n"
" 'm',\n"
" 'e',\n"
" '<',\n"
" 'i',\n"
" 'n',\n"
" 't',\n"
" ',',\n"
" ' ',\n"
" '\"',\n"
" 'i',\n"
" 'n',\n"
" 't',\n"
" '>',\n"
" '\\0'\n"
" ]\n"
" 34: 34\n";
IC(7, "same<int, \"int>", 34);
REQUIRE(str == result);
str.clear();
}

{
IC(7, ',', 34);
REQUIRE(str == "ic| 7: 7, ',': ',', 34: 34\n");
str.clear();
}

{
IC(v0);
REQUIRE(str == "ic| v0: [1, 2, 3, 4, 5]\n");
Expand Down

0 comments on commit 92bf143

Please sign in to comment.