33#pragma once
44
55#include < string>
6- #include < scratchcpp/veque.h>
76
87#include " value.h"
98#include " entity.h"
9+ #include " string_pool.h"
10+ #include " string_functions.h"
11+ #include " stringptr.h"
12+ #include " veque.h"
1013
1114namespace libscratchcpp
1215{
@@ -143,21 +146,21 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
143146 return m_dataPtr->operator [](index);
144147 }
145148
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
148151 {
149- dst. clear () ;
150- veque::veque<std::string> strings ;
152+ veque::veque<StringPtr *> strings ;
153+ size_t size = 0 ;
151154 strings.reserve (m_size);
152155 bool digits = true ;
153156 size_t i;
154157
155158 for (i = 0 ; i < m_size; i++) {
156159 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 ;
159162
160- if (value_isValidNumber (item) && !value_isBool (item) && ! strings.back (). empty () ) {
163+ if (value_isValidNumber (item) && !value_isBool (item) && strings.back ()-> size > 0 ) {
161164 double doubleNum = value_toDouble (item);
162165 long num = value_toLong (item);
163166
@@ -176,42 +179,58 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
176179 }
177180 }
178181
179- std::string s;
182+ StringPtr *ret = string_pool_new ();
183+ ret->size = 0 ;
180184
181185 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+ }
184193
185194 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);
188201 }
189202 } else {
203+ size += strings.size () - 1 ;
204+ string_alloc (ret, size);
205+
190206 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]);
192210
193211 if (i + 1 < m_size)
194- dst. push_back ( ' ' ) ;
212+ ret-> data [ret-> size ++] = u ' ' ;
195213 }
196214
197215 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);
200222
201223 if (i + 1 < m_size)
202- dst. push_back ( ' ' ) ;
224+ ret-> data [ret-> size ++] = u ' ' ;
203225 }
204226 }
205- }
206227
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 ' ;
212229 return ret;
213230 }
214231
232+ std::string toString () const ;
233+
215234 std::shared_ptr<List> clone ();
216235
217236 private:
0 commit comments