Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
str_utils.c File Reference

Helpers for manipulating C strings and bridging with Value arrays. More...

#include "value.h"
#include <stdlib.h>
#include <string.h>
Include dependency graph for str_utils.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * string_substr (const char *s, int start, int len)
 Create a newly allocated substring of s.
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.
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.

Detailed Description

Helpers for manipulating C strings and bridging with Value arrays.

Functions here return newly allocated C strings or construct Value arrays from strings. Callers own returned allocations and must free them using free()/free_value() as appropriate.

Definition in file str_utils.c.

Function Documentation

◆ array_join_with_sep()

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.

Each array element is converted to a string via value_to_string_alloc. NULL/invalid inputs yield an empty string. The caller owns the returned buffer and must free() it.

Parameters
vPointer to Value (expected VAL_ARRAY).
sepSeparator C string inserted between items (may be NULL).
Returns
Newly allocated joined string; never NULL.

Definition at line 141 of file str_utils.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_find()

int string_find(const char *hay,
const char *needle )

Find first occurrence of needle in hay.

Parameters
hayHaystack C string (may be NULL).
needleNeedle C string (may be NULL).
Returns
Zero-based index or -1 if not found/invalid input.

Definition at line 56 of file str_utils.c.

Here is the caller graph for this function:

◆ string_split_to_array()

Value string_split_to_array(const char *s,
const char *sep )

Split a C string by separator into a Value array of strings.

When sep is empty, splits into individual UTF-8 bytes (characters). Uses make_string/make_array_from_values; the returned Value owns internal memory per Value semantics. NULL inputs are treated as empty strings.

Parameters
sSource C string (may be NULL).
sepSeparator C string (may be NULL). Empty means split into chars.
Returns
Value of type VAL_ARRAY with string elements.

Definition at line 74 of file str_utils.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_substr()

char * string_substr(const char *s,
intstart,
intlen )

Create a newly allocated substring of s.

Indices are clamped into valid range. If s is NULL, an empty string is returned. The caller owns the returned buffer and must free() it.

Parameters
sSource C string (may be NULL).
startZero-based start index; clamped to [0, strlen(s)].
lenMaximum number of characters to copy; negative treated as 0.
Returns
Newly allocated NUL-terminated substring; never NULL.

Definition at line 35 of file str_utils.c.

Here is the caller graph for this function: