src/ucx/cx/string.h

changeset 613
b649d66c433e
parent 582
82b60a8dd55c
child 621
956c03c25edd
equal deleted inserted replaced
612:d647ba7f0db6 613:b649d66c433e
165 /** 165 /**
166 * Wraps a mutable string that must be zero-terminated. 166 * Wraps a mutable string that must be zero-terminated.
167 * 167 *
168 * The length is implicitly inferred by using a call to @c strlen(). 168 * The length is implicitly inferred by using a call to @c strlen().
169 * 169 *
170 * When @c NULL is passed, the length will be set to zero.
171 *
170 * @note the wrapped string will share the specified pointer to the string. 172 * @note the wrapped string will share the specified pointer to the string.
171 * If you do want a copy, use cx_strdup() on the return value of this function. 173 * If you do want a copy, use cx_strdup() on the return value of this function.
172 * 174 *
173 * If you need to wrap a constant string, use cx_str(). 175 * If you need to wrap a constant string, use cx_str().
174 * 176 *
175 * @param cstring the string to wrap, must be zero-terminated 177 * @param cstring the string to wrap, must be zero-terminated
176 * @return the wrapped string 178 * @return the wrapped string
177 * 179 *
178 * @see cx_mutstrn() 180 * @see cx_mutstrn()
179 */ 181 */
180 cx_attr_nonnull
181 cx_attr_nodiscard 182 cx_attr_nodiscard
182 cx_attr_cstr_arg(1) 183 cx_attr_cstr_arg(1)
183 cx_attr_export 184 cx_attr_export
184 cxmutstr cx_mutstr(char *cstring); 185 cxmutstr cx_mutstr(char *cstring);
185 186
210 /** 211 /**
211 * Wraps a string that must be zero-terminated. 212 * Wraps a string that must be zero-terminated.
212 * 213 *
213 * The length is implicitly inferred by using a call to @c strlen(). 214 * The length is implicitly inferred by using a call to @c strlen().
214 * 215 *
216 * When @c NULL is passed, the length will be set to zero.
217 *
215 * @note the wrapped string will share the specified pointer to the string. 218 * @note the wrapped string will share the specified pointer to the string.
216 * If you do want a copy, use cx_strdup() on the return value of this function. 219 * If you do want a copy, use cx_strdup() on the return value of this function.
217 * 220 *
218 * If you need to wrap a non-constant string, use cx_mutstr(). 221 * If you need to wrap a non-constant string, use cx_mutstr().
219 * 222 *
220 * @param cstring the string to wrap, must be zero-terminated 223 * @param cstring the string to wrap, must be zero-terminated
221 * @return the wrapped string 224 * @return the wrapped string
222 * 225 *
223 * @see cx_strn() 226 * @see cx_strn()
224 */ 227 */
225 cx_attr_nonnull
226 cx_attr_nodiscard 228 cx_attr_nodiscard
227 cx_attr_cstr_arg(1) 229 cx_attr_cstr_arg(1)
228 cx_attr_export 230 cx_attr_export
229 cxstring cx_str(const char *cstring); 231 cxstring cx_str(const char *cstring);
230 232
260 return cx_strn(str.ptr, str.length); 262 return cx_strn(str.ptr, str.length);
261 } 263 }
262 cx_attr_nodiscard 264 cx_attr_nodiscard
263 static inline cxstring cx_strcast(cxstring str) { 265 static inline cxstring cx_strcast(cxstring str) {
264 return str; 266 return str;
267 }
268 cx_attr_nodiscard
269 static inline cxstring cx_strcast(const char *str) {
270 return cx_str(str);
265 } 271 }
266 extern "C" { 272 extern "C" {
267 #else 273 #else
268 /** 274 /**
269 * Internal function, do not use. 275 * Internal function, do not use.
282 * @see cx_strcast() 288 * @see cx_strcast()
283 */ 289 */
284 cx_attr_nodiscard 290 cx_attr_nodiscard
285 static inline cxstring cx_strcast_c(cxstring str) { 291 static inline cxstring cx_strcast_c(cxstring str) {
286 return str; 292 return str;
293 }
294
295 /**
296 * Internal function, do not use.
297 * @param str
298 * @return
299 * @see cx_strcast()
300 */
301 cx_attr_nodiscard
302 static inline cxstring cx_strcast_z(const char *str) {
303 return cx_str(str);
287 } 304 }
288 305
289 /** 306 /**
290 * Casts a mutable string to an immutable string. 307 * Casts a mutable string to an immutable string.
291 * 308 *
298 * @param str (@c cxstring or @c cxmutstr) the string to cast 315 * @param str (@c cxstring or @c cxmutstr) the string to cast
299 * @return (@c cxstring) an immutable copy of the string pointer 316 * @return (@c cxstring) an immutable copy of the string pointer
300 */ 317 */
301 #define cx_strcast(str) _Generic((str), \ 318 #define cx_strcast(str) _Generic((str), \
302 cxmutstr: cx_strcast_m, \ 319 cxmutstr: cx_strcast_m, \
303 cxstring: cx_strcast_c) \ 320 cxstring: cx_strcast_c, \
304 (str) 321 const char*: cx_strcast_z, \
322 char *: cx_strcast_z) (str)
305 #endif 323 #endif
306 324
307 /** 325 /**
308 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function. 326 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function.
309 * 327 *

mercurial