242 cxstring cx_strn( |
242 cxstring cx_strn( |
243 const char *cstring, |
243 const char *cstring, |
244 size_t length |
244 size_t length |
245 ); |
245 ); |
246 |
246 |
|
247 #ifdef __cplusplus |
|
248 } // extern "C" |
|
249 cx_attr_nodiscard |
|
250 static inline cxstring cx_strcast(cxmutstr str) { |
|
251 return cx_strn(str.ptr, str.length); |
|
252 } |
|
253 cx_attr_nodiscard |
|
254 static inline cxstring cx_strcast(cxstring str) { |
|
255 return str; |
|
256 } |
|
257 extern "C" { |
|
258 #else |
|
259 /** |
|
260 * Internal function, do not use. |
|
261 * @param str |
|
262 * @return |
|
263 * @see cx_strcast() |
|
264 */ |
|
265 cx_attr_nodiscard |
|
266 static inline cxstring cx_strcast_m(cxmutstr str) { |
|
267 return (cxstring) {str.ptr, str.length}; |
|
268 } |
|
269 /** |
|
270 * Internal function, do not use. |
|
271 * @param str |
|
272 * @return |
|
273 * @see cx_strcast() |
|
274 */ |
|
275 cx_attr_nodiscard |
|
276 static inline cxstring cx_strcast_c(cxstring str) { |
|
277 return str; |
|
278 } |
|
279 |
247 /** |
280 /** |
248 * Casts a mutable string to an immutable string. |
281 * Casts a mutable string to an immutable string. |
249 * |
282 * |
250 * \note This is not seriously a cast. Instead you get a copy |
283 * Does nothing for already immutable strings. |
|
284 * |
|
285 * \note This is not seriously a cast. Instead, you get a copy |
251 * of the struct with the desired pointer type. Both structs still |
286 * of the struct with the desired pointer type. Both structs still |
252 * point to the same location, though! |
287 * point to the same location, though! |
253 * |
288 * |
254 * @param str the mutable string to cast |
289 * @param str the mutable string to cast |
255 * @return an immutable copy of the string pointer |
290 * @return an immutable copy of the string pointer |
256 */ |
291 */ |
257 cx_attr_nodiscard |
292 #define cx_strcast(str) _Generic((str), \ |
258 cxstring cx_strcast(cxmutstr str); |
293 cxmutstr: cx_strcast_m, \ |
|
294 cxstring: cx_strcast_c) \ |
|
295 (str) |
|
296 #endif |
259 |
297 |
260 /** |
298 /** |
261 * Passes the pointer in this string to \c free(). |
299 * Passes the pointer in this string to \c free(). |
262 * |
300 * |
263 * The pointer in the struct is set to \c NULL and the length is set to zero. |
301 * The pointer in the struct is set to \c NULL and the length is set to zero. |