ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
cstr.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef DS_CSTR_H
4#define DS_CSTR_H 1
5
6//! @file ds/cstr.h
7//! @author Avinash Maddikonda (svasssakavi@gmail.com)
8//! @brief Declaration of the C-string types, related constants and functions.
9//! @since 0.5.0
10//! @date 2023-07-31
11
12#include "ds/char.h"
13#include "ds/result.h"
14#include "ds/usize.h"
15
16#ifdef __cplusplus
17extern "C"
18{
19#endif /* __cplusplus */
20
21 /// @brief An immutable C-string type.
22 ///
23 /// The @ref ds_cstr_t "immutable C-string" type represents a pointer to an
24 /// immutable @ref ds_char_t "char" array.
25 ///
26 /// @see https://en.wikipedia.org/wiki/Immutable_object
27 /// @see https://en.wikipedia.org/wiki/C_string_handling
28 typedef ds_char_t const *ds_cstr_t;
29
30 /// @brief A mutable C-string type.
31 ///
32 /// The @ref ds_cstr_mut_t "mutable C-string" type represents a pointer to a
33 /// mutable @ref ds_char_t "char" array.
34 ///
35 /// @see https://en.wikipedia.org/wiki/Immutable_object
36 /// @see https://en.wikipedia.org/wiki/C_string_handling
38
39 /// @brief Attempts to allocate a C-string.
40 ///
41 /// @warning Always pair a @ref ds_cstr_allocate call with a @ref
42 /// ds_cstr_reallocate or @ref ds_cstr_deallocate call (passing the same
43 /// pointer). Failing to do so ***will*** lead to memory leaks.
44 ///
45 /// @param[out] self The allocated C-string on success.
46 /// @param size The size of the C-string to allocate.
47 ///
48 /// @return @ref DS_RESULT_OK "Ok" on success.
49 /// @return @ref DS_RESULT_ERR_PTR_IS_NULL "Err(PtrIsNull)" if @p self is
50 /// `NULL`.
51 /// @return @ref DS_RESULT_ERR_MEM_ALLOC_FAILED "Err(MemAllocFailed)" if a
52 /// memory allocation fails.
54
55 /// @brief Attempts to reallocate (extend, shrink) the C-string.
56 ///
57 /// @warning Always pair a @ref ds_cstr_reallocate call with a @ref
58 /// ds_cstr_reallocate or @ref ds_cstr_deallocate call (passing the same
59 /// pointer). Failing to do so ***will** lead to memory leaks.
60 ///
61 /// @param src_cstr_ptr A reference to the C-string to reallocate.
62 /// @param src_size The old size of the C-string to reallocate.
63 /// @param[out] dst_cstr_ptr A reference to the reallocated C-string.
64 /// @param dst_size The new size of the reallocated C-string.
65 ///
66 /// @return @ref DS_RESULT_OK "Ok" on success.
67 /// @return @ref DS_RESULT_ERR_PTR_IS_NULL "Err(PtrIsNull)" if @p
68 /// src_cstr_ptr or @p dst_cstr_ptr is `NULL`.
69 /// @return @ref DS_RESULT_ERR_MEM_ALLOC_FAILED "Err(MemAllocFailed)" if a
70 /// memory allocation fails.
71 extern ds_result_t ds_cstr_reallocate (ds_cstr_mut_t *src_cstr_ptr,
72 ds_usize_t src_size,
73 ds_cstr_mut_t *dst_cstr_ptr,
74 ds_usize_t dst_size);
75
76 /// @brief Deallocates the C-string referenced by @p self.
77 ///
78 /// @warning Only pass pointers allocated using @ref ds_cstr_allocate or @ref
79 /// ds_cstr_reallocate. Passing other pointers may lead to undefined
80 /// behavior.
81 ///
82 /// @param[out] self A reference to the C-string to deallocate.
83 /// @return @ref DS_RESULT_OK "Ok" on success.
84 /// @return @ref DS_RESULT_ERR_PTR_IS_NULL "Err(PtrIsNull)" if @p self is
85 /// `NULL`.
87
88/// @brief The size of C-strings in bytes.
89///
90/// ### Examples
91///
92/// Basic usage:
93///
94/// ```c
95/// assert (DS_CSTR_BYTES == sizeof (ds_char_t *));
96/// ```
97#define DS_CSTR_BYTES ((ds_usize_t)sizeof (ds_cstr_t))
98
99/// @internal
100/// @brief Internal implementation of @ref DS_SCN_CSTR.
101#define DS_SCN_CSTR_INTERNAL_IMPL(len) #len "[^\n]%*" DS_SCN_CHAR
102
103/// @brief C-string notation. Can be used in `scanf` to parse and read
104/// `len + 1` (@p len = `size - 1`) sized C-string values.
105///
106/// @param len The length of the C-string (excluding the null-terminator).
107/// @warning @p len must be (a macro constant containing) an unsigned integer
108/// literal (with no suffixes or preceding zeroes). Any other kind of value,
109/// expression, or statement is strictly disallowed.
110///
111/// @return C-string notation for the given string length @p len.
112///
113/// @warning It is strongly advised to use
114/// [`fgets`](https://en.cppreference.com/w/c/io/fgets "fgets -
115/// cppreference.com") instead of `scanf` to read input.
116///
117/// ### Examples
118///
119/// Basic usage:
120///
121/// ```c
122/// #define SIZE 15
123/// char x[SIZE + 1];
124/// scanf("%" DS_SCN_CSTR(SIZE), x);
125/// ```
126#define DS_SCN_CSTR(len) DS_SCN_CSTR_INTERNAL_IMPL (len)
127
128/// @brief C-string notation. Can be used in `printf` to format and write
129/// C-string values.
130///
131/// ### Examples
132///
133/// Basic usage:
134///
135/// ```c
136/// ds_cstr_t const x = "abc";
137/// printf ("%" DS_PRI_CSTR, x);
138/// ```
139#define DS_PRI_CSTR "s"
140
141#ifdef __cplusplus
142}
143#endif /* __cplusplus */
144
145#endif /* DS_CSTR_H */
Declaration of the char type, related constants and functions.
char ds_char_t
A character type.
Definition: char.h:30
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.
Definition: cstr.c:41
ds_char_t const * ds_cstr_t
An immutable C-string type.
Definition: cstr.h:28
ds_result_t ds_cstr_allocate(ds_cstr_mut_t *self, ds_usize_t size)
Attempts to allocate a C-string.
Definition: cstr.c:18
ds_char_t * ds_cstr_mut_t
A mutable C-string type.
Definition: cstr.h:37
ds_result_t ds_cstr_deallocate(ds_cstr_mut_t *self)
Deallocates the C-string referenced by self.
Definition: cstr.c:84
Declaration of the Result enumeration type, related constants and functions.
ds_result_t
Result is a type that represents either success (Ok) or failure (a DS_RESULT_ERR_* variant).
Definition: result.h:26
Declaration of the usize type, related constants and functions.
size_t ds_usize_t
The pointer-sized unsigned integer type.
Definition: usize.h:26