Serene Runtime 1.0.0-dev
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 <string.h>
22
23#include "serene/rt/context.h"
24#include "serene/rt/core.h"
25#include "serene/rt/engine.h"
26#include "serene/rt/errors.h"
27#include "serene/utils.h"
28
29size_t srn_string_size(const srn_string_t *s) {
31 return s->size;
32}
33
36 return s->len;
37}
38
40 PANIC_IF_NULL(ctx);
41 size_t alloc_size = sizeof(srn_string_t) + src->size;
42 srn_string_t *copy = srn_allocate(ctx, alloc_size, alignof(srn_string_t));
43 memcpy(copy->buffer, src->buffer, src->size);
44 copy->size = src->size;
45 copy->len = src->len;
46 return copy;
47}
48
49bool srn_string_is_empty(const srn_string_t *s) { return s->len == 0; }
50
51srn_value_t *srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src) {
52 PANIC_IF_NULL(ctx);
53 const size_t max_len = ctx->engine->config.limits.string_max_len;
54 size_t size = strnlen(src, max_len);
55 if (size == max_len) {
56 return srn_errors_make_error(
57 ctx, metadata, STRING_LENGTH_LIMIT_EXCEEDED,
58 "Check your string to be null terminated and within the string length limit"
59 );
60 }
61
62 // The one is there for '\0'. We include the null terminator to have easier
63 // time dealing with C strings
64 size_t alloc_size = sizeof(srn_string_t) + size + 1;
65 srn_string_t *string = srn_allocate(ctx, alloc_size, alignof(srn_string_t));
66 // For now we only handle ASCII, but when we implement WTF8 we should
67 // handel len here
68
69 string->len = size;
70 string->size = size + 1; // Again null terminator
71 memcpy(&string->buffer, src, size);
72 // Inject the null terminator at the end of the buffer to make it C friendly
73 string->buffer[string->len] = '\0';
74
75 srn_value_t *string_value = srn_value_make(ctx, VString, metadata, (void *)string);
76 return string_value;
77}
78
79bool srn_string_eq(const srn_string_t *a, const srn_string_t *b) {
80 if (a == nullptr || b == nullptr || (a->size != b->size)) {
81 return false;
82 }
83
84 return memcmp(&a->buffer, &b->buffer, a->size) == 0;
85}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
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:36
@ VString
Definition core.h:123
Error handling for the runtime.
@ STRING_LENGTH_LIMIT_EXCEEDED
Definition errors.h:83
size_t srn_string_length(const srn_string_t *s)
Definition strings.c:34
size_t srn_string_size(const srn_string_t *s)
Definition strings.c:29
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:51
bool srn_string_is_empty(const srn_string_t *s)
Definition strings.c:49
bool srn_string_eq(const srn_string_t *a, const srn_string_t *b)
Definition strings.c:79
srn_string_t * srn_string_copy(srn_context_t *ctx, const srn_string_t *src)
Allocate a copy of src through ctx and return it.
Definition strings.c:39
srn_limits_config_t limits
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
srn_configuration_t config
The runtime's tunable knobs, the single source for every configurable value (see configuration....
Definition engine.h:62
size_t string_max_len
Largest string accepted, in bytes.
size_t size
Size of the buffer.
Definition strings.h:62
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:66
size_t len
length of the WTF-8 sequence in bytes
Definition strings.h:64
#define PANIC_IF_NULL(ptr)
Definition utils.h:66