36 if (!s)
return strdup(
"");
37 int n = (int)strlen(s);
38 if (start < 0) start = 0;
39 if (start > n) start = n;
41 if (start + len > n) len = n - start;
42 char *out = (
char *)malloc((
size_t)len + 1);
43 if (!out)
return strdup(
"");
44 memcpy(out, s + start, (
size_t)len);
57 if (!hay || !needle)
return -1;
58 const char *p = strstr(hay, needle);
60 return (
int)(p - hay);
77 int seplen = (int)strlen(sep);
80 int n = (int)strlen(s);
84 for (
int i = 0; i < n; ++i) {
85 char ch[2] = {s[i], 0};
89 for (
int i = 0; i < n; ++i)
99 const char *pos = NULL;
100 while ((pos = strstr(cur, sep)) != NULL) {
101 int len = (int)(pos - cur);
102 char *piece = (
char *)malloc((
size_t)len + 1);
104 memcpy(piece, cur, (
size_t)len);
107 cap = cap == 0 ? 4 : cap * 2;
108 parts = (
Value *)realloc(parts,
sizeof(
Value) * cap);
115 char *tail = strdup(cur ? cur :
"");
117 cap = cap == 0 ? 1 : cap + 1;
118 parts = (
Value *)realloc(parts,
sizeof(
Value) * cap);
124 for (
int i = 0; i <
count; ++i)
146 if (n <= 0)
return strdup(
"");
148 char **parts = (
char **)malloc(
sizeof(
char *) * n);
149 if (!parts)
return strdup(
"");
152 for (
int i = 0; i < n; ++i) {
155 parts[i] = strdup(
"");
160 total += strlen(parts[i]);
161 if (i + 1 < n) total += strlen(sep);
164 char *out = (
char *)malloc(total + 1);
166 for (
int i = 0; i < n; ++i)
173 for (
int i = 0; i < n; ++i) {
174 size_t li = strlen(parts[i]);
175 memcpy(out + off, parts[i], li);
178 size_t ls = strlen(sep);
179 memcpy(out + off, sep, ls);
char * string_substr(const char *s, int start, int len)
Create a newly allocated substring of s.
char * array_join_with_sep(const Value *v, const char *sep)
Join the elements of a Value array into a single newly allocated C string.
int string_find(const char *hay, const char *needle)
Find first occurrence of needle in hay.
Value string_split_to_array(const char *s, const char *sep)
Split a C string by separator into a Value array of strings.
Tagged union representing a Fun value.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
int array_length(const Value *v)
Get the element count of an array Value.
void free_value(Value v)
Free dynamic storage owned by a Value.
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Defines the Value type and associated functions for the Fun VM.