Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
strings.h
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#pragma once
19
20#include <stddef.h>
21#include <stdint.h>
22
23// TODO(lxsameer): Implement the WTF8 support based on Rust's implementation
24
25typedef struct srn_context_t srn_context_t;
26typedef struct srn_error_t srn_error_t;
27typedef struct srn_value_t srn_value_t;
28typedef struct srn_metadata_t srn_metadata_t;
29
30// TODO(lxsameer): Decide how this type supports Unicode.
31//
32// Encoding. Strict UTF-8 keeps every live string valid Unicode by invariant
33// and IO needs no exit checks, ill formed input is rejected or lossily
34// converted at entry. WTF-8 is UTF-8 plus unpaired surrogates and exists to
35// round trip ill formed UTF-16 (Windows paths, JS strings), the cost is a
36// sanitize or reject step at every IO boundary. A third shape is two types,
37// strict UTF-8 for language strings plus a separate OS string type for FFI
38// with explicit conversions (the Rust String/OsString split).
39//
40// Validation. Validate at construction (srn_string_make and the reader)
41// returning error values, so validity is an invariant of the type and
42// nothing downstream revalidates. Trusting pre validated input instead is
43// what makes the vendored utf8.h dangerous.
44//
45// Length. Bytes stay the primitive. The user facing length can be bytes
46// only, or bytes plus a code point count cached during the validation pass
47// (O(1) length, iteration instead of integer indexing), or grapheme
48// clusters, which need Unicode segmentation tables and belong in a library
49// rather than the runtime.
50//
51// Implementation. A strict validator and decoder is roughly fifty lines as
52// a DFA (Hoehrmann's design is the standard reference), total and
53// allocation free. utf8proc only earns its place if normalization, case
54// folding, or grapheme segmentation move into the runtime.
55//
56// Symbol identity. Precomposed and combining spellings of the same text are
57// different byte sequences, so byte identity interning treats them as two
58// symbols. Normalizing identifiers at read time fixes that at the price of
59// normalization tables in the reader. Whichever wins belongs in the spec.
60typedef struct srn_string_t {
61 /// Size of the buffer
62 size_t size;
63 /// length of the WTF-8 sequence in bytes
64 size_t len;
65 /// The buffer that holds the WTF8 sequence.
66 uint8_t buffer[];
68
69/// Create a string from a null terminated C string. IT HAS TO BE NULL
70/// TERMINATED
71[[gnu::nonnull(1)]]
72srn_value_t *srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src);
73
74[[gnu::nonnull(1)]]
75size_t srn_string_size(const srn_string_t *s);
76[[gnu::nonnull(1)]]
77size_t srn_string_length(const srn_string_t *s);
78
79/// Allocate a copy of `src` through `ctx` and return it.
80[[gnu::nonnull(1, 2)]]
82
84bool srn_string_eq(const srn_string_t *a, const srn_string_t *b);
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
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
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