Serene Runtime 1.0.0
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
 

Macros

#define SRN_STRING_MAX_LEN   (1U << 20U)
 On top of my head, no other reason.
 

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)
 Copy the src string to dst string.
 
bool srn_string_is_empty (const srn_string_t *s)
 
bool srn_string_eq (const srn_string_t *a, const srn_string_t *b)
 

Macro Definition Documentation

◆ SRN_STRING_MAX_LEN

#define SRN_STRING_MAX_LEN   (1U << 20U)

On top of my head, no other reason.

Definition at line 31 of file strings.h.

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 )

Copy the src string to dst string.

It's important to note that dst has to be allocated already

Definition at line 38 of file strings.c.

38 {
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}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:72
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
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 76 of file strings.c.

76 {
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}
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 48 of file strings.c.

48{ 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 33 of file strings.c.

33 {
35 return s->len;
36}

◆ 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 50 of file strings.c.

50 {
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}
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
#define SRN_STRING_MAX_LEN
On top of my head, no other reason.
Definition strings.h:31
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 28 of file strings.c.

28 {
30 return s->size;
31}