@@ -212,6 +212,86 @@ int KAOS_EXPORT Kaos_search()
212212 return 0 ;
213213}
214214
215+ // list array.replace(list haystack, any needle, any replacement)
216+
217+ char * replace_params_name [] = {
218+ "haystack" ,
219+ "needle" ,
220+ "replacement"
221+ };
222+ unsigned replace_params_type [] = {
223+ K_LIST ,
224+ K_ANY ,
225+ K_ANY
226+ };
227+ unsigned short replace_params_length = (unsigned short ) sizeof (replace_params_type ) / sizeof (unsigned );
228+ int KAOS_EXPORT Kaos_replace ()
229+ {
230+ unsigned long list_length = kaos .getListLength (replace_params_name [0 ]);
231+ enum Type list_type = kaos .getListType (replace_params_name [0 ]);
232+
233+ kaos .startBuildingList ();
234+
235+ for (unsigned long i = 0 ; i < list_length ; i ++ ) {
236+ enum ValueType value_type = kaos .getListElementValueType (replace_params_name [0 ], i );
237+
238+ bool x_b , y_b ;
239+ long long x_i , y_i ;
240+ long double x_f , y_f ;
241+ char * x_s , * y_s ;
242+ char * replacement ;
243+
244+ switch (value_type )
245+ {
246+ case V_BOOL :
247+ x_b = kaos .getVariableBool (replace_params_name [1 ]);
248+ y_b = kaos .getListElementBool (replace_params_name [0 ], i );
249+ if (x_b == y_b ) {
250+ kaos .createVariableBool (NULL , kaos .getVariableBool (replace_params_name [2 ]));
251+ } else {
252+ kaos .createVariableBool (NULL , y_b );
253+ }
254+ break ;
255+ case V_INT :
256+ x_i = kaos .getVariableInt (replace_params_name [1 ]);
257+ y_i = kaos .getListElementInt (replace_params_name [0 ], i );
258+ if (x_i == y_i ) {
259+ kaos .createVariableInt (NULL , kaos .getVariableInt (replace_params_name [2 ]));
260+ } else {
261+ kaos .createVariableInt (NULL , y_i );
262+ }
263+ break ;
264+ case V_FLOAT :
265+ x_f = kaos .getVariableFloat (replace_params_name [1 ]);
266+ y_f = kaos .getListElementFloat (replace_params_name [0 ], i );
267+ if (x_f == y_f ) {
268+ kaos .createVariableFloat (NULL , kaos .getVariableFloat (replace_params_name [2 ]));
269+ } else {
270+ kaos .createVariableFloat (NULL , y_f );
271+ }
272+ break ;
273+ case V_STRING :
274+ x_s = kaos .getVariableString (replace_params_name [1 ]);
275+ y_s = kaos .getListElementString (replace_params_name [0 ], i );
276+ if (strcmp (x_s , y_s ) == 0 ) {
277+ replacement = kaos .getVariableString (replace_params_name [2 ]);
278+ kaos .createVariableString (NULL , replacement );
279+ free (replacement );
280+ } else {
281+ kaos .createVariableString (NULL , y_s );
282+ }
283+ free (x_s );
284+ free (y_s );
285+ break ;
286+ default :
287+ break ;
288+ }
289+ }
290+
291+ kaos .returnList (list_type );
292+ return 0 ;
293+ }
294+
215295
216296// Information Functions
217297
@@ -243,6 +323,7 @@ int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
243323
244324 // Searching & Replacing
245325 kaos .defineFunction ("search" , K_LIST , K_NUMBER , search_params_name , search_params_type , search_params_length );
326+ kaos .defineFunction ("replace" , K_LIST , K_ANY , replace_params_name , replace_params_type , replace_params_length );
246327
247328 // Information Functions
248329 kaos .defineFunction ("length" , K_NUMBER , K_ANY , length_params_name , length_params_type , length_params_length );
0 commit comments