Skip to content

Commit

Permalink
escape control chars in keys
Browse files Browse the repository at this point in the history
  • Loading branch information
trobro committed Oct 15, 2023
1 parent 73c2da1 commit cdc06df
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/hjson_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,11 @@ static void _quote(Encoder *e, const std::string& value, const char *separator,
static void _quoteName(Encoder *e, const std::string& name) {
if (name.empty()) {
*e->os << "\"\"";
} else if (e->opt.quoteKeys || std::regex_search(name, e->needsEscapeName)) {
} else if (e->opt.quoteKeys || std::regex_search(name, e->needsEscapeName) ||
std::regex_search(name, e->needsEscape))
{
*e->os << '"';
if (std::regex_search(name, e->needsEscape)) {
_quoteReplace(e, name);
} else {
*e->os << name;
}

_quoteReplace(e, name);
*e->os << '"';
} else {
// without quotes
Expand Down
2 changes: 1 addition & 1 deletion test/assets/charset2_result.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
uescape: "\u0000,\u0001,\uffff"
umlaut: äöüßÄÖÜ
"um\u000blaut": äöüßÄÖÜ
hex: ģ䕧覫췯ꯍ
}
2 changes: 1 addition & 1 deletion test/assets/charset2_result.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"uescape": "\u0000,\u0001,\uffff",
"umlaut": "äöüßÄÖÜ",
"um\u000blaut": "äöüßÄÖÜ",
"hex": "ģ䕧覫췯ꯍ"
}
2 changes: 1 addition & 1 deletion test/assets/charset2_test.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
uescape: "\u0000,\u0001,\uffff"
umlaut: äöüßÄÖÜ
"um\u000blaut": äöüßÄÖÜ
hex: "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A"
}
2 changes: 1 addition & 1 deletion test/assets/comments/charset2_result.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
uescape: "\u0000,\u0001,\uffff"
umlaut: äöüßÄÖÜ
"um\u000blaut": äöüßÄÖÜ
hex: ģ䕧覫췯ꯍ
}
2 changes: 1 addition & 1 deletion test/assets/comments2/charset2_result.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
uescape: "\u0000,\u0001,\uffff"
umlaut: äöüßÄÖÜ
"um\u000blaut": äöüßÄÖÜ
hex: ģ䕧覫췯ꯍ
}
2 changes: 1 addition & 1 deletion test/assets/comments3/charset2_result.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
uescape: "\u0000,\u0001,\uffff"
umlaut: äöüßÄÖÜ
"um\u000blaut": äöüßÄÖÜ
hex: ģ䕧覫췯ꯍ
}
2 changes: 1 addition & 1 deletion test/assets/sorted/charset2_result.hjson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
hex: ģ䕧覫췯ꯍ
uescape: "\u0000,\u0001,\uffff"
umlaut: äöüßÄÖÜ
"um\u000blaut": äöüßÄÖÜ
}
2 changes: 1 addition & 1 deletion test/assets/sorted/charset2_result.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hex": "ģ䕧覫췯ꯍ",
"uescape": "\u0000,\u0001,\uffff",
"umlaut": "äöüßÄÖÜ"
"um\u000blaut": "äöüßÄÖÜ"
}
2 changes: 1 addition & 1 deletion test/assets/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pass1_test.json
pass2_test.json
pass3_test.json
pass4_test.json
pass5_test.json
pass5_test.hjson
pass6_test.hjson
passSingle_test.hjson
root_test.hjson
Expand Down

0 comments on commit cdc06df

Please sign in to comment.