Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
17
18#include "value.h"
19#include <stdlib.h>
20
21/* String built-ins wrappers used by VM opcodes */
22
37Value bi_split(const Value *str, const Value *sep) {
38 const char *s = (str && str->type == VAL_STRING && str->s) ? str->s : "";
39 const char *p = (sep && sep->type == VAL_STRING && sep->s) ? sep->s : "";
41}
42
56Value bi_join(const Value *arr, const Value *sep) {
57 const char *p = (sep && sep->type == VAL_STRING && sep->s) ? sep->s : "";
58 char *s = array_join_with_sep(arr, p);
59 Value out = make_string(s ? s : "");
60 if (s) free(s);
61 return out;
62}
63
77Value bi_substr(const Value *str, int start, int len) {
78 const char *s = (str && str->type == VAL_STRING && str->s) ? str->s : "";
79 char *sub = string_substr(s, start, len);
80 Value out = make_string(sub ? sub : "");
81 if (sub) free(sub);
82 return out;
83}
84
96int bi_find(const Value *hay, const Value *needle) {
97 const char *h = (hay && hay->type == VAL_STRING && hay->s) ? hay->s : "";
98 const char *n = (needle && needle->type == VAL_STRING && needle->s) ? needle->s : "";
99 return string_find(h, n);
100}
Value out
Definition apop.c:38
array_clear & arr
Definition clear.c:38
Value hay
Definition find.c:37
size_t len
Definition input_line.c:102
int n
Definition insert.c:41
free(vals)
const char * p
Definition read_file.c:37
Value str
Definition regex_match.c:41
uint32_t s
Definition rol.c:31
Value start
Definition slice.c:34
char * string_substr(const char *s, int start, int len)
Create a newly allocated substring of s.
Definition str_utils.c:35
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.
Definition str_utils.c:141
int string_find(const char *hay, const char *needle)
Find first occurrence of needle in hay.
Definition str_utils.c:56
Value string_split_to_array(const char *s, const char *sep)
Split a C string by separator into a Value array of strings.
Definition str_utils.c:74
int bi_find(const Value *hay, const Value *needle)
Find the first occurrence of a needle inside a haystack string.
Definition string.c:96
Value bi_split(const Value *str, const Value *sep)
Split a string by a separator into an array Value.
Definition string.c:37
Value bi_join(const Value *arr, const Value *sep)
Join an array of strings with a separator into a single string Value.
Definition string.c:56
Value bi_substr(const Value *str, int start, int len)
Extract a substring from a string Value.
Definition string.c:77
Tagged union representing a Fun value.
Definition value.h:68
ValueType type
Definition value.h:69
char * s
Definition value.h:73
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
Defines the Value type and associated functions for the Fun VM.
@ VAL_STRING
Definition value.h:53