Skip to content

replaced recursive calls with loops #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 222 additions & 160 deletions src/hjson_decode.cpp

Large diffs are not rendered by default.

401 changes: 244 additions & 157 deletions src/hjson_encode.cpp

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/hjson_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Value::ValueImpl {
ValueImpl(const std::string&);
ValueImpl(Type);
~ValueImpl();
static void DeepClear(Value &val);
};


Expand Down Expand Up @@ -112,16 +113,47 @@ Value::ValueImpl::ValueImpl(Type _type)
}


// Bottom-up destruction in order to avoid stack overflow due to recursive destructor calls.
void Value::ValueImpl::DeepClear(Value &val) {
// The map/vector will only be destroyed if use_count == 1
if (val.size() && val.prv.use_count() == 1) {
std::vector<std::pair<Value, int> > v;

v.emplace_back(val, 0);

while (!v.empty()) {
if (v.back().second >= v.back().first.size()) {
v.back().first.clear();
v.pop_back();
} else {
Value &n = v.back().first[v.back().second];
v.back().second++;
// The map/vector will only be destroyed if use_count == 1
if (n.size() && n.prv.use_count() == 1) {
v.emplace_back(v.back().first[v.back().second - 1], 0);
}
}
}
}
}


Value::ValueImpl::~ValueImpl() {
switch (type)
{
case Type::String:
delete s;
break;
case Type::Vector:
for (auto e = v->begin(); e != v->end(); ++e) {
DeepClear(*e);
}
delete v;
break;
case Type::Map:
for (auto e = m->m.begin(); e != m->m.end(); ++e) {
DeepClear(e->second);
}
delete m;
break;
default:
Expand Down
1 change: 1 addition & 0 deletions test/assets/comments/strings4_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
1 change: 1 addition & 0 deletions test/assets/comments2/strings4_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
1 change: 1 addition & 0 deletions test/assets/comments3/strings4_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
4 changes: 4 additions & 0 deletions test/assets/failObj4_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a: 1
b: 2
# trailing bracket in bracketless root
}
4 changes: 4 additions & 0 deletions test/assets/failStr9a_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
[
=
[[''''''
1 change: 1 addition & 0 deletions test/assets/sorted/strings4_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
1 change: 1 addition & 0 deletions test/assets/sorted/strings4_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
1 change: 1 addition & 0 deletions test/assets/strings4_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
1 change: 1 addition & 0 deletions test/assets/strings4_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
1 change: 1 addition & 0 deletions test/assets/strings4_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
''''''
3 changes: 3 additions & 0 deletions test/assets/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ failMLStr1_test.hjson
failObj1_test.hjson
failObj2_test.hjson
failObj3_test.hjson
failObj4_test.hjson
failStr1a_test.hjson
failStr1b_test.hjson
failStr1c_test.hjson
Expand All @@ -70,6 +71,7 @@ failStr6c_test.hjson
failStr6d_test.hjson
failStr7a_test.hjson
failStr8a_test.hjson
failStr9a_test.hjson
int64_test.hjson
kan_test.hjson
keys_test.hjson
Expand All @@ -86,6 +88,7 @@ root_test.hjson
stringify1_test.hjson
strings2_test.hjson
strings3_test.hjson
strings4_test.hjson
strings_test.hjson
trail_test.hjson
windowseol_test.hjson
80 changes: 78 additions & 2 deletions test/test_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,82 @@ void test_value() {
assert(root["key1"]["key2"]["key3"]["B"] == 5);
}

{
int a = 0;
char *szBrackets = new char[19];
for (; a < 10; a++) {
szBrackets[a] = '[';
szBrackets[++a] = '\n';
}
--a;
for (; a < 18; a++) {
szBrackets[a] = ']';
szBrackets[++a] = '\n';
}
szBrackets[18] = 0;
auto root = Hjson::Unmarshal(szBrackets);

Hjson::EncoderOptions opt;
opt.indentBy = "";
auto res = Hjson::Marshal(root, opt);

assert(!std::strcmp(res.c_str(), szBrackets));

delete[] szBrackets;
}

{
std::ostringstream oss;
for (int a = 0; a < 10; ++a) {
oss << "a: {\n";
}
oss << "a: {}\n";
for (int a = 0; a < 10; ++a) {
oss << "}\n";
}
const std::string in = oss.str();
Hjson::DecoderOptions decOpt;
decOpt.comments = true;
decOpt.whitespaceAsComments = true;
Hjson::Value root = Hjson::Unmarshal(in);

Hjson::EncoderOptions opt;
opt.comments = true;
opt.omitRootBraces = true;
opt.indentBy = "";
const std::string out = Hjson::Marshal(root, opt) + "\n";

assert(out == in);
}

{
Hjson::Value node;
node["a"] = 1;
{
Hjson::Value root;
root["n"] = node;
}
assert(node.size() == 1);
}

{
Hjson::Value node;
node["a"] = 1;
node["a2"] = 2;
{
Hjson::Value node2;
node2["b"] = node;
node2["c"] = "alfa";
node2["d"] = Hjson::Value(Hjson::Type::Undefined);
{
Hjson::Value root;
root["n"] = node2;
}
assert(node2.size() == 3);
}
assert(node.size() == 2);
}

{
Hjson::Value val;
try {
Expand Down Expand Up @@ -683,11 +759,11 @@ void test_value() {
// Assert that explicit assignment creates an element.
assert(val.size() == 2);
std::string generatedHjson = Hjson::Marshal(val);
assert(generatedHjson == "{\n}");
assert(generatedHjson == "{}");
Hjson::EncoderOptions options;
options.preserveInsertionOrder = false;
generatedHjson = Hjson::Marshal(val, options);
assert(generatedHjson == "{\n}");
assert(generatedHjson == "{}");
sub1["sub1"] = "abc";
sub2["sub2"] = "åäö";
generatedHjson = Hjson::Marshal(val);
Expand Down