Skip to content

Fix #188: Join digits without spaces in List::toString() #229

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 2 commits into from
Sep 24, 2023
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
32 changes: 27 additions & 5 deletions src/scratch/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,36 @@ bool List::contains(const Value &value) const
return (indexOf(value) != -1);
}

/*! Joins the list items with spaces. */
/*! Joins the list items with spaces or without any separator if there are only digits. */
std::string List::toString() const
{
std::string ret;
for (int i = 0; i < size(); i++) {
ret.append(at(i).toString());
if (i + 1 < size())
ret.push_back(' ');
bool digits = true;

for (const auto &item : *this) {
if (item.type() == Value::Type::Integer) {
long num = item.toLong();

if (num < 0 || num >= 10) {
digits = false;
break;
}
} else {
digits = false;
break;
}
}

if (digits) {
for (const auto &item : *this)
ret.append(item.toString());
} else {
for (int i = 0; i < size(); i++) {
ret.append(at(i).toString());
if (i + 1 < size())
ret.push_back(' ');
}
}

return ret;
}
2 changes: 1 addition & 1 deletion test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ TEST(EngineTest, Clones)
if (i < 10)
ASSERT_EQ((*list)[i].toInt(), 1);
else
ASSERT_EQ((*list)[i].toString(), "1 2"); // TODO: Change this to "12" after #188 is fixed
ASSERT_EQ((*list)[i].toString(), "12");
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/scratch_classes/list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,22 @@ TEST(ListTest, ToString)
list.push_back("áä");
list.push_back("ľ š");
ASSERT_EQ(list.toString(), "áä ľ š");

list.clear();
list.push_back(-2);
list.push_back(5);
list.push_back(8);
ASSERT_EQ(list.toString(), "-2 5 8");

list.clear();
list.push_back(2);
list.push_back(10);
list.push_back(8);
ASSERT_EQ(list.toString(), "2 10 8");

list.clear();
list.push_back(0);
list.push_back(9);
list.push_back(8);
ASSERT_EQ(list.toString(), "098");
}