Skip to content

Commit

Permalink
Fixes #30 - prevent numbers from being quoted when emitting
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Feb 2, 2020
1 parent dc6033d commit bbc8488
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 22 deletions.
21 changes: 14 additions & 7 deletions src/c4/yml/emit.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,17 @@ void Emitter<Writer>::_write_json(NodeScalar const& sc, NodeType flags)
template<class Writer>
void Emitter<Writer>::_write_scalar(csubstr s)
{
const bool no_dquotes = s.first_of( '"') == npos;
const bool no_squotes = s.first_of('\'') == npos;
// force use quotes when any of these characters is present
const bool no_special = s.first_of("#:-,\n{}[]") == npos;
const bool needs_quotes = (
!s.is_number() // is not a number
&&
(
(s != s.trim(" \t\n\r")) // has leading or trailing whitespace
||
s.first_of("#:-,\n{}[]'\"") != npos // has special chars
)
);

if(no_dquotes && no_squotes && no_special)
if(!needs_quotes)
{
if( ! s.empty())
{
Expand All @@ -287,13 +292,15 @@ void Emitter<Writer>::_write_scalar(csubstr s)
}
else
{
if(no_squotes && !no_dquotes)
const bool has_dquotes = s.first_of( '"') != npos;
const bool has_squotes = s.first_of('\'') != npos;
if(!has_squotes && has_dquotes)
{
this->Writer::_do_write('\'');
this->Writer::_do_write(s);
this->Writer::_do_write('\'');
}
else if(no_dquotes && !no_squotes)
else if(has_squotes && !has_dquotes)
{
this->Writer::_do_write('"');
this->Writer::_do_write(s);
Expand Down
13 changes: 13 additions & 0 deletions test/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,19 @@ TEST(general, print_tree)
print_tree(t); // to make sure this is covered too
}

TEST(general, numbers)
{
const char yaml[] = R"(- -1
- -1.0
- +1.0
- 1e-2
- 1e+2
)";
Tree t = parse(yaml);
auto s = emitrs<std::string>(t);
EXPECT_EQ(s, std::string(yaml));
}

TEST(general, lookup_path)
{
const char yaml[] = R"(
Expand Down
61 changes: 46 additions & 15 deletions test/double_quoted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

namespace c4 {
namespace yml {
#define DOUBLE_QUOTED_CASES \
"dquoted, only text", \
"dquoted, with single quotes", \
"dquoted, with double quotes", \
"dquoted, with single and double quotes", \
"dquoted, with escapes", \
"dquoted, with newline", \
"dquoted, all", \
"dquoted, empty", \
"dquoted, 1 dquote", \
"dquoted, 2 dquotes", \
"dquoted, 3 dquotes", \
"dquoted, 4 dquotes", \
"dquoted, example 2", \
"dquoted, example 2.1"

#define DOUBLE_QUOTED_CASES \
"dquoted, only text", \
"dquoted, with single quotes", \
"dquoted, with double quotes", \
"dquoted, with single and double quotes", \
"dquoted, with escapes", \
"dquoted, with newline", \
"dquoted, all", \
"dquoted, empty", \
"dquoted, numbers", \
"dquoted, trailing space", \
"dquoted, leading space", \
"dquoted, trailing and leading space", \
"dquoted, 1 dquote", \
"dquoted, 2 dquotes", \
"dquoted, 3 dquotes", \
"dquoted, 4 dquotes", \
"dquoted, example 2", \
"dquoted, example 2.1"

CASE_GROUP(DOUBLE_QUOTED)
{
Expand Down Expand Up @@ -71,6 +76,32 @@ R"("")",
L{N("")}
),

C("dquoted, numbers", // these should not be quoted when emitting
R"(
- -1
- -1.0
- +1.0
- 1e-2
- 1e+2
)",
L{N("-1"), N("-1.0"), N("+1.0"), N("1e-2"), N("1e+2")}
),

C("dquoted, trailing space",
R"('a aaaa ')",
L{N("a aaaa ")}
),

C("dquoted, leading space",
R"(' a aaaa')",
L{N(" a aaaa")}
),

C("dquoted, trailing and leading space",
R"(' 012345 ')",
L{N(" 012345 ")}
),

C("dquoted, 1 dquote",
R"("\"")",
L{N("\"")}
Expand Down
30 changes: 30 additions & 0 deletions test/single_quoted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace yml {
"squoted, with escapes", \
"squoted, all", \
"squoted, empty", \
"squoted, numbers", \
"squoted, trailing space", \
"squoted, leading space", \
"squoted, trailing and leading space", \
"squoted, 1 squote", \
"squoted, 2 squotes", \
"squoted, 3 squotes", \
Expand Down Expand Up @@ -62,6 +66,32 @@ R"('')",
L{N("")}
),

C("squoted, numbers", // these should not be quoted when emitting
R"(
- -1
- -1.0
- +1.0
- 1e-2
- 1e+2
)",
L{N("-1"), N("-1.0"), N("+1.0"), N("1e-2"), N("1e+2")}
),

C("squoted, trailing space",
R"('a aaaa ')",
L{N("a aaaa ")}
),

C("squoted, leading space",
R"(' a aaaa')",
L{N(" a aaaa")}
),

C("squoted, trailing and leading space",
R"(' 012345 ')",
L{N(" 012345 ")}
),

C("squoted, 1 squote",
R"('''')",
L{N("'")}
Expand Down

0 comments on commit bbc8488

Please sign in to comment.