ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
cstr.c File Reference

Implementation of the C-string types, related constants and functions. More...

#include <stddef.h>
#include "ds/alloc.h"
#include "ds/char.h"
#include "ds/cstr.h"
#include "ds/helpers.h"
#include "ds/result.h"
#include "ds/usize.h"
Include dependency graph for cstr.c:

Go to the source code of this file.

Functions

ds_result_t ds_cstr_allocate (ds_cstr_mut_t *const self, ds_usize_t const size)
 Attempts to allocate a C-string. More...
 
ds_result_t ds_cstr_reallocate (ds_cstr_mut_t *const src_cstr_ptr, ds_usize_t const src_size, ds_cstr_mut_t *const dst_cstr_ptr, ds_usize_t const dst_size)
 Attempts to reallocate (extend, shrink) the C-string. More...
 
ds_result_t ds_cstr_deallocate (ds_cstr_mut_t *const self)
 Deallocates the C-string referenced by self. More...
 

Detailed Description

Implementation of the C-string types, related constants and functions.

Author
Avinash Maddikonda (svass.nosp@m.saka.nosp@m.vi@gm.nosp@m.ail..nosp@m.com)
Since
0.5.0
Date
2023-07-31

Definition in file cstr.c.

Function Documentation

◆ ds_cstr_allocate()

ds_result_t ds_cstr_allocate ( ds_cstr_mut_t self,
ds_usize_t  size 
)

Attempts to allocate a C-string.

Warning
Always pair a ds_cstr_allocate call with a ds_cstr_reallocate or ds_cstr_deallocate call (passing the same pointer). Failing to do so will lead to memory leaks.
Parameters
[out]selfThe allocated C-string on success.
sizeThe size of the C-string to allocate.
Returns
Ok on success.
Err(PtrIsNull) if self is NULL.
Err(MemAllocFailed) if a memory allocation fails.

Definition at line 18 of file cstr.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ds_cstr_deallocate()

ds_result_t ds_cstr_deallocate ( ds_cstr_mut_t self)

Deallocates the C-string referenced by self.

Warning
Only pass pointers allocated using ds_cstr_allocate or ds_cstr_reallocate. Passing other pointers may lead to undefined behavior.
Parameters
[out]selfA reference to the C-string to deallocate.
Returns
Ok on success.
Err(PtrIsNull) if self is NULL.

Definition at line 84 of file cstr.c.

Here is the caller graph for this function:

◆ ds_cstr_reallocate()

ds_result_t ds_cstr_reallocate ( ds_cstr_mut_t src_cstr_ptr,
ds_usize_t  src_size,
ds_cstr_mut_t dst_cstr_ptr,
ds_usize_t  dst_size 
)

Attempts to reallocate (extend, shrink) the C-string.

Warning
Always pair a ds_cstr_reallocate call with a ds_cstr_reallocate or ds_cstr_deallocate call (passing the same pointer). Failing to do so ***will** lead to memory leaks.
Parameters
src_cstr_ptrA reference to the C-string to reallocate.
src_sizeThe old size of the C-string to reallocate.
[out]dst_cstr_ptrA reference to the reallocated C-string.
dst_sizeThe new size of the reallocated C-string.
Returns
Ok on success.
Err(PtrIsNull) if src_cstr_ptr or dst_cstr_ptr is NULL.
Err(MemAllocFailed) if a memory allocation fails.

Definition at line 41 of file cstr.c.

Here is the call graph for this function:
Here is the caller graph for this function: