Skip to content

Commit 395e14a

Browse files
committed
Optimize List::toString()
1 parent ed0c342 commit 395e14a

File tree

4 files changed

+89
-64
lines changed

4 files changed

+89
-64
lines changed

include/scratchcpp/list.h

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,73 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
123123
return m_dataPtr->operator[](index);
124124
}
125125

126-
std::string toString() const;
126+
/*! Joins the list items with spaces or without any separator if there are only digits and stores the result in dst. */
127+
inline void toString(std::string &dst) const
128+
{
129+
dst.clear();
130+
veque::veque<std::string> strings;
131+
strings.reserve(m_dataPtr->size());
132+
bool digits = true;
133+
134+
for (const auto &item : *m_dataPtr) {
135+
strings.push_back(std::string());
136+
value_toString(&item, &strings.back());
137+
138+
if (value_isValidNumber(&item) && !strings.back().empty()) {
139+
double doubleNum = value_toDouble(&item);
140+
long num = value_toLong(&item);
141+
142+
if (doubleNum != num) {
143+
digits = false;
144+
break;
145+
}
146+
147+
if (num < 0 || num >= 10) {
148+
digits = false;
149+
break;
150+
}
151+
} else {
152+
digits = false;
153+
break;
154+
}
155+
}
156+
157+
size_t i;
158+
std::string s;
159+
160+
if (digits) {
161+
for (i = 0; i < strings.size(); i++)
162+
dst.append(strings[i]);
163+
164+
for (; i < m_dataPtr->size(); i++) {
165+
value_toString(&m_dataPtr->operator[](i), &s);
166+
dst.append(s);
167+
}
168+
} else {
169+
for (i = 0; i < strings.size(); i++) {
170+
dst.append(strings[i]);
171+
172+
if (i + 1 < m_dataPtr->size())
173+
dst.push_back(' ');
174+
}
175+
176+
for (; i < m_dataPtr->size(); i++) {
177+
value_toString(&m_dataPtr->operator[](i), &s);
178+
dst.append(s);
179+
180+
if (i + 1 < m_dataPtr->size())
181+
dst.push_back(' ');
182+
}
183+
}
184+
}
185+
186+
/*! Same as the other method, but returns the result directly. */
187+
inline std::string toString() const
188+
{
189+
std::string ret;
190+
toString(ret);
191+
return ret;
192+
}
127193

128194
std::shared_ptr<List> clone();
129195

src/engine/virtualmachine_p.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,11 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
609609
DISPATCH();
610610

611611
OP(READ_LIST) :
612-
ADD_RET_VALUE(lists[*++pos]->toString());
612+
{
613+
std::string s;
614+
lists[*++pos]->toString(s);
615+
ADD_RET_VALUE(s);
616+
}
613617
DISPATCH();
614618

615619
OP(LIST_APPEND) :

src/scratch/list.cpp

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -57,68 +57,6 @@ void List::setMonitor(Monitor *monitor)
5757
impl->monitor = monitor;
5858
}
5959

60-
/*! Joins the list items with spaces or without any separator if there are only digits. */
61-
std::string List::toString() const
62-
{
63-
std::string ret;
64-
veque::veque<std::string> strings;
65-
strings.reserve(m_dataPtr->size());
66-
bool digits = true;
67-
68-
for (const auto &item : *m_dataPtr) {
69-
strings.push_back(std::string());
70-
value_toString(&item, &strings.back());
71-
72-
if (value_isValidNumber(&item) && !strings.back().empty()) {
73-
double doubleNum = value_toDouble(&item);
74-
long num = value_toLong(&item);
75-
76-
if (doubleNum != num) {
77-
digits = false;
78-
break;
79-
}
80-
81-
if (num < 0 || num >= 10) {
82-
digits = false;
83-
break;
84-
}
85-
} else {
86-
digits = false;
87-
break;
88-
}
89-
}
90-
91-
size_t i;
92-
std::string s;
93-
94-
if (digits) {
95-
for (i = 0; i < strings.size(); i++)
96-
ret.append(strings[i]);
97-
98-
for (; i < m_dataPtr->size(); i++) {
99-
value_toString(&m_dataPtr->operator[](i), &s);
100-
ret.append(s);
101-
}
102-
} else {
103-
for (i = 0; i < strings.size(); i++) {
104-
ret.append(strings[i]);
105-
106-
if (i + 1 < m_dataPtr->size())
107-
ret.push_back(' ');
108-
}
109-
110-
for (; i < m_dataPtr->size(); i++) {
111-
value_toString(&m_dataPtr->operator[](i), &s);
112-
ret.append(s);
113-
114-
if (i + 1 < m_dataPtr->size())
115-
ret.push_back(' ');
116-
}
117-
}
118-
119-
return ret;
120-
}
121-
12260
/*! Creates a copy of the list. */
12361
std::shared_ptr<List> List::clone()
12462
{

test/scratch_classes/list_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,14 @@ TEST(ListTest, ArrayIndexOperator)
217217
TEST(ListTest, ToString)
218218
{
219219
List list("", "test list");
220+
std::string s;
221+
list.toString(s);
222+
ASSERT_EQ(s, "");
220223
ASSERT_EQ(list.toString(), "");
221224

222225
list.append("");
226+
list.toString(s);
227+
ASSERT_EQ(s, "");
223228
ASSERT_EQ(list.toString(), "");
224229

225230
list.append("");
@@ -230,36 +235,48 @@ TEST(ListTest, ToString)
230235
list.append("item1");
231236
list.append("i t e m 2");
232237
list.append("item 3");
238+
list.toString(s);
239+
ASSERT_EQ(s, "item1 i t e m 2 item 3");
233240
ASSERT_EQ(list.toString(), "item1 i t e m 2 item 3");
234241

235242
list.clear();
236243
list.append(" ");
237244
list.append("a ");
238245
list.append(" b");
239246
list.append(" c ");
247+
list.toString(s);
248+
ASSERT_EQ(s, " a b c ");
240249
ASSERT_EQ(list.toString(), " a b c ");
241250

242251
list.clear();
243252
list.append("áä");
244253
list.append("ľ š");
254+
list.toString(s);
255+
ASSERT_EQ(s, "áä ľ š");
245256
ASSERT_EQ(list.toString(), "áä ľ š");
246257

247258
list.clear();
248259
list.append(-2);
249260
list.append(5);
250261
list.append(8);
262+
list.toString(s);
263+
ASSERT_EQ(s, "-2 5 8");
251264
ASSERT_EQ(list.toString(), "-2 5 8");
252265

253266
list.clear();
254267
list.append(2);
255268
list.append(10);
256269
list.append(8);
270+
list.toString(s);
271+
ASSERT_EQ(s, "2 10 8");
257272
ASSERT_EQ(list.toString(), "2 10 8");
258273

259274
list.clear();
260275
list.append(0);
261276
list.append(9);
262277
list.append(8);
278+
list.toString(s);
279+
ASSERT_EQ(s, "098");
263280
ASSERT_EQ(list.toString(), "098");
264281
}
265282

0 commit comments

Comments
 (0)