@@ -16,8 +16,9 @@ extern "C"
1616 {
1717 if (v->type == ValueType::String) {
1818 assert (v->stringValue );
19- delete v->stringValue ;
19+ free ( v->stringValue ) ;
2020 v->stringValue = nullptr ;
21+ v->stringSize = 0 ;
2122 }
2223 }
2324
@@ -105,32 +106,29 @@ extern "C"
105106 /* ! Assigns string to the given value. */
106107 void value_assign_string (ValueData *v, const std::string &stringValue)
107108 {
108- if (stringValue == " Infinity" ) {
109+ value_assign_cstring (v, stringValue.c_str ());
110+ }
111+
112+ /* ! Assigns C string to the given value. */
113+ void value_assign_cstring (ValueData *v, const char *stringValue)
114+ {
115+ if (strcmp (stringValue, " Infinity" ) == 0 ) {
109116 value_free (v);
110117 v->type = ValueType::Infinity;
111- } else if (stringValue == " -Infinity" ) {
118+ } else if (strcmp ( stringValue, " -Infinity" ) == 0 ) {
112119 value_free (v);
113120 v->type = ValueType::NegativeInfinity;
114- } else if (stringValue == " NaN" ) {
121+ } else if (strcmp ( stringValue, " NaN" ) == 0 ) {
115122 value_free (v);
116123 v->type = ValueType::NaN;
124+ } else if (v->type == ValueType::String) {
125+ value_replaceStr (v, stringValue);
117126 } else {
118- if (v->type == ValueType::String)
119- v->stringValue ->assign (stringValue);
120- else {
121- value_free (v);
122- v->type = ValueType::String;
123- v->stringValue = new std::string (stringValue);
124- }
127+ value_free (v);
128+ value_initStr (v, stringValue);
125129 }
126130 }
127131
128- /* ! Assigns C string to the given value. */
129- void value_assign_cstring (ValueData *v, const char *stringValue)
130- {
131- value_assign_string (v, std::string (stringValue));
132- }
133-
134132 /* ! Assigns special value to the given value. */
135133 void value_assign_special (ValueData *v, SpecialValue specialValue)
136134 {
@@ -162,10 +160,10 @@ extern "C"
162160 v->boolValue = another->boolValue ;
163161 } else if (another->type == ValueType::String) {
164162 if (v->type == ValueType::String)
165- v-> stringValue -> assign (* another->stringValue );
163+ value_replaceStr (v, another->stringValue );
166164 else {
167165 value_free (v);
168- v-> stringValue = new std::string (* another->stringValue );
166+ value_initStr (v, another->stringValue );
169167 }
170168 }
171169
@@ -185,7 +183,7 @@ extern "C"
185183 case ValueType::Double:
186184 return value_isInf (v->doubleValue );
187185 case ValueType::String:
188- return * v->stringValue == " Infinity" ;
186+ return strcmp ( v->stringValue , " Infinity" ) == 0 ;
189187 default :
190188 return false ;
191189 }
@@ -202,7 +200,7 @@ extern "C"
202200 case ValueType::Double:
203201 return value_isNegativeInf (-v->doubleValue );
204202 case ValueType::String:
205- return * v->stringValue == " -Infinity" ;
203+ return strcmp ( v->stringValue , " -Infinity" ) == 0 ;
206204 default :
207205 return false ;
208206 }
@@ -218,7 +216,7 @@ extern "C"
218216 assert (!std::isnan (v->doubleValue ));
219217 return std::isnan (v->doubleValue );
220218 case ValueType::String:
221- return * v->stringValue == " NaN" ;
219+ return strcmp ( v->stringValue , " NaN" ) == 0 ;
222220 default :
223221 return false ;
224222 }
@@ -247,7 +245,7 @@ extern "C"
247245 case ValueType::Bool:
248246 return true ;
249247 case ValueType::String:
250- return v->stringValue -> empty () || value_checkString (* v->stringValue ) > 0 ;
248+ return strlen ( v->stringValue ) == 0 || value_checkString (v->stringValue ) > 0 ;
251249 default :
252250 return false ;
253251 }
@@ -270,7 +268,7 @@ extern "C"
270268 return v->doubleValue == intpart;
271269 }
272270 case ValueType::String:
273- return value_checkString (* v->stringValue ) == 1 ;
271+ return value_checkString (v->stringValue ) == 1 ;
274272 }
275273
276274 return false ;
@@ -300,7 +298,7 @@ extern "C"
300298 else if (v->type == ValueType::Bool)
301299 return v->boolValue ;
302300 else if (v->type == ValueType::String)
303- return value_stringToLong (* v->stringValue );
301+ return value_stringToLong (v->stringValue );
304302 else
305303 return 0 ;
306304 }
@@ -315,7 +313,7 @@ extern "C"
315313 else if (v->type == ValueType::Bool)
316314 return v->boolValue ;
317315 else if (v->type == ValueType::String)
318- return value_stringToLong (* v->stringValue );
316+ return value_stringToLong (v->stringValue );
319317 else
320318 return 0 ;
321319 }
@@ -330,7 +328,7 @@ extern "C"
330328 else if (v->type == ValueType::Bool)
331329 return v->boolValue ;
332330 else if (v->type == ValueType::String)
333- return value_stringToDouble (* v->stringValue );
331+ return value_stringToDouble (v->stringValue );
334332 else if (v->type == ValueType::Infinity)
335333 return std::numeric_limits<double >::infinity ();
336334 else if (v->type == ValueType::NegativeInfinity)
@@ -349,7 +347,7 @@ extern "C"
349347 } else if (v->type == ValueType::Double) {
350348 return v->doubleValue != 0 ;
351349 } else if (v->type == ValueType::String) {
352- return ! v->stringValue -> empty () && !value_stringsEqual (* v->stringValue , " false" ) && * v->stringValue != " 0 " ;
350+ return strlen ( v->stringValue ) != 0 && !value_stringsEqual (v->stringValue , " false" ) && strcmp ( v->stringValue , " 0 " ) != 0 ;
353351 } else if (v->type == ValueType::Infinity || v->type == ValueType::NegativeInfinity) {
354352 return true ;
355353 } else if (v->type == ValueType::NaN) {
@@ -363,7 +361,7 @@ extern "C"
363361 void value_toString (const libscratchcpp::ValueData *v, std::string *dst)
364362 {
365363 if (v->type == ValueType::String)
366- dst->assign (* v->stringValue );
364+ dst->assign (v->stringValue );
367365 else if (v->type == ValueType::Integer)
368366 dst->assign (std::to_string (v->intValue ));
369367 else if (v->type == ValueType::Double)
@@ -590,12 +588,12 @@ extern "C"
590588 double n1, n2;
591589
592590 if (v1->type == ValueType::String)
593- n1 = value_stringToDouble (* v1->stringValue );
591+ n1 = value_stringToDouble (v1->stringValue );
594592 else
595593 n1 = value_toDouble (v1);
596594
597595 if (v2->type == ValueType::String)
598- n2 = value_stringToDouble (* v2->stringValue );
596+ n2 = value_stringToDouble (v2->stringValue );
599597 else
600598 n2 = value_toDouble (v2);
601599
@@ -628,12 +626,12 @@ extern "C"
628626 double n1, n2;
629627
630628 if (v1->type == ValueType::String)
631- n1 = value_stringToDouble (* v1->stringValue );
629+ n1 = value_stringToDouble (v1->stringValue );
632630 else
633631 n1 = value_toDouble (v1);
634632
635633 if (v2->type == ValueType::String)
636- n2 = value_stringToDouble (* v2->stringValue );
634+ n2 = value_stringToDouble (v2->stringValue );
637635 else
638636 n2 = value_toDouble (v2);
639637
0 commit comments