Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
strings.h File Reference
#include <stddef.h>
#include <stdint.h>
Include dependency graph for strings.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  srn_string_t

Typedefs

typedef struct srn_string_t srn_string_t

Functions

srn_value_tsrn_string_make (srn_context_t *ctx, srn_metadata_t *metadata, const char *src)
 Create a string from a null terminated C string.
size_t srn_string_size (const srn_string_t *s)
size_t srn_string_length (const srn_string_t *s)
srn_string_tsrn_string_copy (srn_context_t *ctx, const srn_string_t *src)
 Allocate a copy of src through ctx and return it.
bool srn_string_is_empty (const srn_string_t *s)
bool srn_string_eq (const srn_string_t *a, const srn_string_t *b)

Typedef Documentation

◆ srn_string_t

typedef struct srn_string_t srn_string_t

Function Documentation

◆ srn_string_copy()

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 at line 39 of file strings.c.

39 {
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}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_string_eq()

bool srn_string_eq ( const srn_string_t * a,
const srn_string_t * b )

Definition at line 79 of file strings.c.

79 {
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}
Here is the caller graph for this function:

◆ srn_string_is_empty()

bool srn_string_is_empty ( const srn_string_t * s)

Definition at line 49 of file strings.c.

49{ return s->len == 0; }
Here is the caller graph for this function:

◆ srn_string_length()

size_t srn_string_length ( const srn_string_t * s)

Definition at line 34 of file strings.c.

34 {
36 return s->len;
37}
Here is the caller graph for this function:

◆ srn_string_make()

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.

IT HAS TO BE NULL TERMINATED

Definition at line 51 of file strings.c.

51 {
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}
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
@ STRING_LENGTH_LIMIT_EXCEEDED
Definition errors.h:83
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_string_size()

size_t srn_string_size ( const srn_string_t * s)

Definition at line 29 of file strings.c.

29 {
31 return s->size;
32}