libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
dstring.c File Reference
#include <stdlib.h>
#include <string.h>
#include "dstring.h"
Include dependency graph for dstring.c:

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 free(string->data);
21 string->data = NULL;
22}
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 34 of file dstring.c.

35{
36 size_t source_length = strlen(source);
37
38 _dstring_ensure_size(string, string->length + source_length + 1);
39
40 memcpy(string->data + string->length, source, source_length + 1);
41
42 string->length += source_length;
43}
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 46 of file dstring.c.

47{
48 size_t source_length = strlen(source);
49
50 _dstring_ensure_size(string, source_length + 1);
51
52 memcpy(string->data, source, source_length + 1);
53
54 string->length = source_length;
55}

◆ 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.

Definition at line 10 of file dstring.c.

11{
12 string->length = 0;
13 string->_buffer_size = buffer_size;
14 string->data = 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 25 of file dstring.c.

26{
27 _dstring_ensure_size(string, string->length + 2);
28
29 string->data[string->length++] = character;
30 string->data[string->length] = '\0';
31}