src/cx/string.h

changeset 1582
32b82c424252
parent 1488
946895d19dde
equal deleted inserted replaced
1581:814049fb62ee 1582:32b82c424252
36 #ifndef UCX_STRING_H 36 #ifndef UCX_STRING_H
37 #define UCX_STRING_H 37 #define UCX_STRING_H
38 38
39 #include "common.h" 39 #include "common.h"
40 #include "allocator.h" 40 #include "allocator.h"
41
42 #include <string.h>
41 43
42 /** Expands a UCX string as printf arguments. */ 44 /** Expands a UCX string as printf arguments. */
43 #define CX_SFMT(s) (int) (s).length, (s).ptr 45 #define CX_SFMT(s) (int) (s).length, (s).ptr
44 46
45 /** Format specifier for a UCX string */ 47 /** Format specifier for a UCX string */
135 /** 137 /**
136 * A string tokenizing context. 138 * A string tokenizing context.
137 */ 139 */
138 typedef struct cx_strtok_ctx_s CxStrtokCtx; 140 typedef struct cx_strtok_ctx_s CxStrtokCtx;
139 141
140 #ifdef __cplusplus
141 extern "C" {
142
143 /**
144 * A literal initializer for an UCX string structure.
145 *
146 * @param literal the string literal
147 */
148 #define CX_STR(literal) cxstring{literal, sizeof(literal) - 1}
149
150 #else // __cplusplus
151
152 /**
153 * A literal initializer for an UCX string structure.
154 *
155 * The argument MUST be a string (const char*) @em literal.
156 *
157 * @param literal the string literal
158 */
159 #define CX_STR(literal) ((cxstring){literal, sizeof(literal) - 1})
160
161 #endif
162
163
164 /** 142 /**
165 * Wraps a mutable string that must be zero-terminated. 143 * Wraps a mutable string that must be zero-terminated.
166 * 144 *
167 * The length is implicitly inferred by using a call to @c strlen(). 145 * The length is implicitly inferred by using a call to @c strlen().
168 * 146 *
177 * @return the wrapped string 155 * @return the wrapped string
178 * 156 *
179 * @see cx_mutstrn() 157 * @see cx_mutstrn()
180 */ 158 */
181 cx_attr_nodiscard cx_attr_cstr_arg(1) 159 cx_attr_nodiscard cx_attr_cstr_arg(1)
182 CX_EXPORT cxmutstr cx_mutstr(char *cstring); 160 CX_INLINE cxmutstr cx_mutstr(char *cstring) {
161 cxmutstr str;
162 str.ptr = cstring;
163 str.length = cstring == NULL ? 0 : strlen(cstring);
164 return str;
165 }
183 166
184 /** 167 /**
185 * Wraps a string that does not need to be zero-terminated. 168 * Wraps a string that does not need to be zero-terminated.
186 * 169 *
187 * The argument may be @c NULL if the length is zero. 170 * The argument may be @c NULL if the length is zero.
196 * @return the wrapped string 179 * @return the wrapped string
197 * 180 *
198 * @see cx_mutstr() 181 * @see cx_mutstr()
199 */ 182 */
200 cx_attr_nodiscard cx_attr_access_rw(1, 2) 183 cx_attr_nodiscard cx_attr_access_rw(1, 2)
201 CX_EXPORT cxmutstr cx_mutstrn(char *cstring, size_t length); 184 CX_INLINE cxmutstr cx_mutstrn(char *cstring, size_t length) {
185 cxmutstr str;
186 str.ptr = cstring;
187 str.length = length;
188 return str;
189 }
202 190
203 /** 191 /**
204 * Wraps a string that must be zero-terminated. 192 * Wraps a string that must be zero-terminated.
205 * 193 *
206 * The length is implicitly inferred by using a call to @c strlen(). 194 * The length is implicitly inferred by using a call to @c strlen().
216 * @return the wrapped string 204 * @return the wrapped string
217 * 205 *
218 * @see cx_strn() 206 * @see cx_strn()
219 */ 207 */
220 cx_attr_nodiscard cx_attr_cstr_arg(1) 208 cx_attr_nodiscard cx_attr_cstr_arg(1)
221 CX_EXPORT cxstring cx_str(const char *cstring); 209 CX_INLINE cxstring cx_str(const char *cstring) {
210 cxstring str;
211 str.ptr = cstring;
212 str.length = cstring == NULL ? 0 : strlen(cstring);
213 return str;
214 }
222 215
223 216
224 /** 217 /**
225 * Wraps a string that does not need to be zero-terminated. 218 * Wraps a string that does not need to be zero-terminated.
226 * 219 *
236 * @return the wrapped string 229 * @return the wrapped string
237 * 230 *
238 * @see cx_str() 231 * @see cx_str()
239 */ 232 */
240 cx_attr_nodiscard cx_attr_access_r(1, 2) 233 cx_attr_nodiscard cx_attr_access_r(1, 2)
241 CX_EXPORT cxstring cx_strn(const char *cstring, size_t length); 234 CX_INLINE cxstring cx_strn(const char *cstring, size_t length) {
235 cxstring str;
236 str.ptr = cstring;
237 str.length = length;
238 return str;
239 }
242 240
243 #ifdef __cplusplus 241 #ifdef __cplusplus
244 } // extern "C"
245 cx_attr_nodiscard 242 cx_attr_nodiscard
246 CX_CPPDECL cxstring cx_strcast(cxmutstr str) { 243 CX_CPPDECL cxstring cx_strcast(cxmutstr str) {
247 return cx_strn(str.ptr, str.length); 244 return cx_strn(str.ptr, str.length);
248 } 245 }
249 cx_attr_nodiscard 246 cx_attr_nodiscard

mercurial