3
3
#pragma once
4
4
5
5
#include < string>
6
- #include < scratchcpp/veque.h>
7
6
8
7
#include " value.h"
9
8
#include " entity.h"
9
+ #include " string_pool.h"
10
+ #include " string_functions.h"
11
+ #include " stringptr.h"
12
+ #include " veque.h"
10
13
11
14
namespace libscratchcpp
12
15
{
@@ -143,21 +146,21 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
143
146
return m_dataPtr->operator [](index );
144
147
}
145
148
146
- /* ! Joins the list items with spaces or without any separator if there are only digits and stores the result in dst . */
147
- inline void toString (std::string &dst ) const
149
+ /* ! Joins the list items with spaces or without any separator if there are only digits and returns the result as StringPtr . */
150
+ inline StringPtr * toStringPtr ( ) const
148
151
{
149
- dst. clear () ;
150
- veque::veque<std::string> strings ;
152
+ veque::veque< StringPtr *> strings ;
153
+ size_t size = 0 ;
151
154
strings.reserve (m_size);
152
155
bool digits = true ;
153
156
size_t i;
154
157
155
158
for (i = 0 ; i < m_size; i++) {
156
159
const ValueData *item = &m_dataPtr->operator [](i);
157
- strings.push_back (std::string ( ));
158
- value_toString (item, & strings.back ()) ;
160
+ strings.push_back (value_toStringPtr (item ));
161
+ size += strings.back ()-> size ;
159
162
160
- if (value_isValidNumber (item) && !value_isBool (item) && ! strings.back (). empty () ) {
163
+ if (value_isValidNumber (item) && !value_isBool (item) && strings.back ()-> size > 0 ) {
161
164
double doubleNum = value_toDouble (item);
162
165
long num = value_toLong (item);
163
166
@@ -176,42 +179,58 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
176
179
}
177
180
}
178
181
179
- std::string s;
182
+ StringPtr *ret = string_pool_new ();
183
+ ret->size = 0 ;
180
184
181
185
if (digits) {
182
- for (i = 0 ; i < strings.size (); i++)
183
- dst.append (strings[i]);
186
+ string_alloc (ret, size);
187
+
188
+ for (i = 0 ; i < strings.size (); i++) {
189
+ memcpy (ret->data + ret->size , strings[i]->data , strings[i]->size * sizeof (char16_t ));
190
+ ret->size += strings[i]->size ;
191
+ string_pool_free (strings[i]);
192
+ }
184
193
185
194
for (; i < m_size; i++) {
186
- value_toString (&m_dataPtr->operator [](i), &s);
187
- dst.append (s);
195
+ StringPtr *item = value_toStringPtr (&m_dataPtr->operator [](i));
196
+ size += item->size + 1 ;
197
+ string_alloc (ret, size);
198
+ memcpy (ret->data + ret->size , item->data , item->size * sizeof (char16_t ));
199
+ ret->size += item->size ;
200
+ string_pool_free (item);
188
201
}
189
202
} else {
203
+ size += strings.size () - 1 ;
204
+ string_alloc (ret, size);
205
+
190
206
for (i = 0 ; i < strings.size (); i++) {
191
- dst.append (strings[i]);
207
+ memcpy (ret->data + ret->size , strings[i]->data , strings[i]->size * sizeof (char16_t ));
208
+ ret->size += strings[i]->size ;
209
+ string_pool_free (strings[i]);
192
210
193
211
if (i + 1 < m_size)
194
- dst. push_back ( ' ' ) ;
212
+ ret-> data [ret-> size ++] = u ' ' ;
195
213
}
196
214
197
215
for (; i < m_size; i++) {
198
- value_toString (&m_dataPtr->operator [](i), &s);
199
- dst.append (s);
216
+ StringPtr *item = value_toStringPtr (&m_dataPtr->operator [](i));
217
+ size += item->size + 1 ;
218
+ string_alloc (ret, size);
219
+ memcpy (ret->data + ret->size , item->data , item->size * sizeof (char16_t ));
220
+ ret->size += item->size ;
221
+ string_pool_free (item);
200
222
201
223
if (i + 1 < m_size)
202
- dst. push_back ( ' ' ) ;
224
+ ret-> data [ret-> size ++] = u ' ' ;
203
225
}
204
226
}
205
- }
206
227
207
- /* ! Same as the other method, but returns the result directly. */
208
- inline std::string toString () const
209
- {
210
- std::string ret;
211
- toString (ret);
228
+ ret->data [ret->size ] = u' \0 ' ;
212
229
return ret;
213
230
}
214
231
232
+ std::string toString () const ;
233
+
215
234
std::shared_ptr<List> clone ();
216
235
217
236
private:
0 commit comments