Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
strings.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include "serene/rt/strings.h"
20
21#include "serene/rt/context.h"
22#include "serene/rt/core.h"
23#include "serene/rt/errors.h"
24#include "serene/utils.h"
25
26#include <string.h>
27
28size_t srn_string_size(const srn_string_t *s) {
30 return s->size;
31}
32
35 return s->len;
36}
37
39 PANIC_IF_NULL(ctx);
40 size_t alloc_size = sizeof(srn_string_t) + src->size;
41 srn_string_t *copy = srn_allocate(ctx, alloc_size, alignof(srn_string_t));
42 memcpy(copy->buffer, src->buffer, src->size);
43 copy->size = src->size;
44 copy->len = src->len;
45 return copy;
46}
47
48bool srn_string_is_empty(const srn_string_t *s) { return s->len == 0; }
49
50srn_value_t *srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src) {
51 PANIC_IF_NULL(ctx);
52 size_t size = strnlen(src, SRN_STRING_MAX_LEN);
53 if (size == SRN_STRING_MAX_LEN) {
54 return srn_errors_make_error(
55 ctx, metadata, STRING_LENGTH_LIMIT_EXCEEDED,
56 "Check your string to be null terminated and smaller than (1 << 20)");
57 }
58
59 // The one is there for '\0'. We include the null terminator to have easier
60 // time dealing with C strings
61 size_t alloc_size = sizeof(srn_string_t) + size + 1;
62 srn_string_t *string = srn_allocate(ctx, alloc_size, alignof(srn_string_t));
63 // For now we only handle ASCII, but when we implement WTF8 we should
64 // handel len here
65
66 string->len = size;
67 string->size = size + 1; // Again null terminator
68 memcpy(&string->buffer, src, size);
69 // Inject the null terminator at the end of the buffer to make it C friendly
70 string->buffer[string->len] = '\0';
71
72 srn_value_t *string_value = srn_value_make(ctx, VString, metadata, (void *)string);
73 return string_value;
74}
75
76bool srn_string_eq(const srn_string_t *a, const srn_string_t *b) {
77 if (a == nullptr || b == nullptr || (a->size != b->size)) {
78 return false;
79 }
80
81 return memcmp(&a->buffer, &b->buffer, a->size) == 0;
82}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:72
srn_value_t * srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
Creates a new serene value.
Definition core.c:24
@ VString
Definition core.h:123
@ STRING_LENGTH_LIMIT_EXCEEDED
Definition errors.h:52
size_t srn_string_length(const srn_string_t *s)
Definition strings.c:33
size_t srn_string_size(const srn_string_t *s)
Definition strings.c:28
srn_value_t * srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src)
Create a string from a null terminated C string.
Definition strings.c:50
bool srn_string_is_empty(const srn_string_t *s)
Definition strings.c:48
bool srn_string_eq(const srn_string_t *a, const srn_string_t *b)
Definition strings.c:76
srn_string_t * srn_string_copy(srn_context_t *ctx, const srn_string_t *src)
Copy the src string to dst string.
Definition strings.c:38
#define SRN_STRING_MAX_LEN
On top of my head, no other reason.
Definition strings.h:31
size_t size
Size of the buffer.
Definition strings.h:35
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:39
size_t len
length of the WTF-8 sequence in bytes
Definition strings.h:37
#define PANIC_IF_NULL(ptr)
Definition utils.h:64