libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
dstring.h File Reference
#include "types/dstring.h"
Include dependency graph for dstring.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void dstring_init (dstring_t *string, unsigned int buffer_size)
 Initializes a dynamic string (dstring).
 
void dstring_close (dstring_t *string)
 Closes the dynamic string.
 
void dstring_putchar (dstring_t *string, int character)
 Concatenates a character on the end of the dstring.
 
void dstring_concat (dstring_t *string, const char *source)
 Concatenates a string on the end of the dstring.
 
void dstring_copy (dstring_t *string, const char *source)
 Copies the content of source to the dstring.
 

Function Documentation

◆ dstring_close()

void dstring_close ( dstring_t * string)

Closes the dynamic string.

Parameters
stringThe string to be closed.

Definition at line 18 of file dstring.c.

19{
20 if (! string->data) {
21 return;
22 }
23
24 free(string->data);
25 string->data = NULL;
26}
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12

◆ dstring_concat()

void dstring_concat ( dstring_t * string,
const char * source )

Concatenates a string on the end of the dstring.

Parameters
stringThe dstring where to concatenate the string.
sourceThe string to be concatenated.

Definition at line 38 of file dstring.c.

39{
40 size_t source_length = strlen(source);
41
42 _dstring_ensure_size(string, string->length + source_length + 1);
43
44 memcpy(string->data + string->length, source, source_length + 1);
45
46 string->length += source_length;
47}
unsigned int length
The length of the string.
Definition dstring.h:13

◆ dstring_copy()

void dstring_copy ( dstring_t * string,
const char * source )

Copies the content of source to the dstring.

Parameters
stringThe dstring where to copy.
sourceThe string to be copied.

Definition at line 50 of file dstring.c.

51{
52 size_t source_length = strlen(source);
53
54 _dstring_ensure_size(string, source_length + 1);
55
56 memcpy(string->data, source, source_length + 1);
57
58 string->length = source_length;
59}

◆ dstring_init()

void dstring_init ( dstring_t * string,
unsigned int buffer_size )

Initializes a dynamic string (dstring).

The dynamic string automatically realloc her buffer when needed. So you don't need to care about buffer sizes.

Parameters
stringPointer for the dstring to be initialized.
buffer_sizeThe initial buffer size for the string. If zero, doesn't allocate memory until required.

Definition at line 10 of file dstring.c.

11{
12 string->length = 0;
13 string->_buffer_size = buffer_size;
14 string->data = (buffer_size == 0) ? NULL : malloc(buffer_size);
15}

◆ dstring_putchar()

void dstring_putchar ( dstring_t * string,
int character )

Concatenates a character on the end of the dstring.

Parameters
stringThe dstring where to concatenate the character.
characterThe character to be concatenated.

Definition at line 29 of file dstring.c.

30{
31 _dstring_ensure_size(string, string->length + 2);
32
33 string->data[string->length++] = character;
34 string->data[string->length] = '\0';
35}