1
0
Fork 0
forked from fun/fun

Tons of refactoring and some fixes and enhancements... ;)

This commit is contained in:
Johannes Findeisen 2025-09-15 06:31:32 +02:00
commit 13919ad87d
82 changed files with 1775 additions and 1382 deletions

32
src/builtins_string.c Normal file
View file

@ -0,0 +1,32 @@
#include "value.h"
#include <stdlib.h>
/* String built-ins wrappers used by VM opcodes */
Value bi_split(const Value *str, const Value *sep) {
const char *s = (str && str->type == VAL_STRING && str->s) ? str->s : "";
const char *p = (sep && sep->type == VAL_STRING && sep->s) ? sep->s : "";
return string_split_to_array(s, p);
}
Value bi_join(const Value *arr, const Value *sep) {
const char *p = (sep && sep->type == VAL_STRING && sep->s) ? sep->s : "";
char *s = array_join_with_sep(arr, p);
Value out = make_string(s ? s : "");
if (s) free(s);
return out;
}
Value bi_substr(const Value *str, int start, int len) {
const char *s = (str && str->type == VAL_STRING && str->s) ? str->s : "";
char *sub = string_substr(s, start, len);
Value out = make_string(sub ? sub : "");
if (sub) free(sub);
return out;
}
int bi_find(const Value *hay, const Value *needle) {
const char *h = (hay && hay->type == VAL_STRING && hay->s) ? hay->s : "";
const char *n = (needle && needle->type == VAL_STRING && needle->s) ? needle->s : "";
return string_find(h, n);
}