src/cx/string.h

changeset 1673
0c338b80e7dd
parent 1672
94360453bce4
child 1674
8b0f162ac88e
equal deleted inserted replaced
1672:94360453bce4 1673:0c338b80e7dd
241 return str; 241 return str;
242 } 242 }
243 243
244 #ifdef __cplusplus 244 #ifdef __cplusplus
245 cx_attr_nodiscard 245 cx_attr_nodiscard
246 CX_CPPDECL cxstring cx_strcast(cxmutstr str) { 246 CX_CPPDECL cxmutstr cx_strcast_m(cxmutstr str) {
247 return str;
248 }
249 cx_attr_nodiscard
250 CX_CPPDECL cxstring cx_strcast_m(cxstring str) {
251 return str;
252 }
253 cx_attr_nodiscard
254 CX_CPPDECL cxmutstr cx_strcast_m(char *str) {
255 return cx_mutstr(str);
256 }
257 cx_attr_nodiscard
258 CX_CPPDECL cxmutstr cx_strcast_m(unsigned char *str) {
259 return cx_mutstr(reinterpret_cast<char*>(str));
260 }
261 cx_attr_nodiscard
262 CX_CPPDECL cxstring cx_strcast_m(const char *str) {
263 return cx_str(str);
264 }
265 cx_attr_nodiscard
266 CX_CPPDECL cxstring cx_strcast_m(const unsigned char *str) {
267 return cx_str(reinterpret_cast<const char*>(str));
268 }
269 cx_attr_nodiscard
270 CX_CPPDECL cxstring cx_strcast_(cxmutstr str) {
247 return cx_strn(str.ptr, str.length); 271 return cx_strn(str.ptr, str.length);
248 } 272 }
249 cx_attr_nodiscard 273 cx_attr_nodiscard
250 CX_CPPDECL cxstring cx_strcast(cxstring str) { 274 CX_CPPDECL cxstring cx_strcast_(cxstring str) {
251 return str; 275 return str;
252 } 276 }
253 cx_attr_nodiscard 277 #define cx_strcast(s) cx_strcast_(cx_strcast_m(s))
254 CX_CPPDECL cxstring cx_strcast(const char *str) {
255 return cx_str(str);
256 }
257 cx_attr_nodiscard
258 CX_CPPDECL cxstring cx_strcast(const unsigned char *str) {
259 return cx_str(reinterpret_cast<const char*>(str));
260 }
261 extern "C" { 278 extern "C" {
262 #else 279 #else
263 /** 280 /**
264 * Internal function, do not use. 281 * Internal function, do not use.
265 * @param str 282 * @param str
266 * @return 283 * @return
284 * @see cx_strcast_m()
267 * @see cx_strcast() 285 * @see cx_strcast()
268 */ 286 */
269 cx_attr_nodiscard 287 cx_attr_nodiscard
270 CX_INLINE cxstring cx_strcast_m(cxmutstr str) { 288 CX_INLINE cxmutstr cx_strcast_cxms(cxmutstr str) {
271 return (cxstring) {str.ptr, str.length}; 289 return str;
272 } 290 }
273 /** 291 /**
274 * Internal function, do not use. 292 * Internal function, do not use.
275 * @param str 293 * @param str
276 * @return 294 * @return
295 * @see cx_strcast_m()
277 * @see cx_strcast() 296 * @see cx_strcast()
278 */ 297 */
279 cx_attr_nodiscard 298 cx_attr_nodiscard
280 CX_INLINE cxstring cx_strcast_c(cxstring str) { 299 CX_INLINE cxstring cx_strcast_cxs(cxstring str) {
281 return str; 300 return str;
282 } 301 }
283 302
284 /** 303 /**
285 * Internal function, do not use. 304 * Internal function, do not use.
286 * @param str 305 * @param str
287 * @return 306 * @return
307 * @see cx_strcast_m()
288 * @see cx_strcast() 308 * @see cx_strcast()
289 */ 309 */
290 cx_attr_nodiscard 310 cx_attr_nodiscard
291 CX_INLINE cxstring cx_strcast_u(const unsigned char *str) { 311 CX_INLINE cxmutstr cx_strcast_uc(unsigned char *str) {
292 return cx_str((const char*)str); 312 return cx_mutstr((char*)str);
293 } 313 }
294 314
295 /** 315 /**
296 * Internal function, do not use. 316 * Internal function, do not use.
297 * @param str 317 * @param str
298 * @return 318 * @return
319 * @see cx_strcast_m()
299 * @see cx_strcast() 320 * @see cx_strcast()
300 */ 321 */
301 cx_attr_nodiscard 322 cx_attr_nodiscard
302 CX_INLINE cxstring cx_strcast_z(const char *str) { 323 CX_INLINE cxmutstr cx_strcast_c(char *str) {
324 return cx_mutstr(str);
325 }
326
327 /**
328 * Internal function, do not use.
329 * @param str
330 * @return
331 * @see cx_strcast_m()
332 * @see cx_strcast()
333 */
334 cx_attr_nodiscard
335 CX_INLINE cxstring cx_strcast_ucc(const unsigned char *str) {
336 return cx_str((const char*)str);
337 }
338
339 /**
340 * Internal function, do not use.
341 * @param str
342 * @return
343 * @see cx_strcast_m()
344 * @see cx_strcast()
345 */
346 cx_attr_nodiscard
347 CX_INLINE cxstring cx_strcast_cc(const char *str) {
303 return cx_str(str); 348 return cx_str(str);
304 } 349 }
305 350
306 /** 351 /**
307 * Wraps any string into an UCX string. 352 * Wraps any string into an UCX string.
308 * 353 *
309 * @param str (any supported string type) the string to cast 354 * @param str (any supported string type) the string to cast
310 * @return (@c cxstring) the string wrapped as UCX string 355 * @return (@c cxstring) or (@c cxmutstr) the string wrapped as UCX string
311 */ 356 */
312 #define cx_strcast(str) _Generic((str), \ 357 #define cx_strcast_m(str) _Generic((str), \
313 cxmutstr: cx_strcast_m, \ 358 cxmutstr: cx_strcast_cxms, \
314 cxstring: cx_strcast_c, \ 359 cxstring: cx_strcast_cxs, \
315 const unsigned char*: cx_strcast_u, \ 360 const unsigned char*: cx_strcast_ucc, \
316 unsigned char *: cx_strcast_u, \ 361 unsigned char *: cx_strcast_uc, \
317 const char*: cx_strcast_z, \ 362 const char*: cx_strcast_cc, \
318 char *: cx_strcast_z) (str) 363 char *: cx_strcast_c) (str)
364
365 /**
366 * Internal function, do not use.
367 * @param str
368 * @return
369 */
370 CX_INLINE cxstring cx_strcast_1(cxmutstr str) {
371 return (cxstring){str.ptr, str.length};
372 }
373
374 /**
375 * Internal function, do not use.
376 * @param str
377 * @return
378 */
379 CX_INLINE cxstring cx_strcast_2(cxstring str) {
380 return str;
381 }
382
383 /** internal conversion macro */
384 #define cx_strcast_(str) _Generic((str), \
385 cxmutstr: cx_strcast_1, \
386 cxstring: cx_strcast_2)(str)
387
388 /**
389 * Converts any string to a cxstring.
390 *
391 * @param str (any supported string type) the string to cast
392 * @return he string converted to a (@c cxstring)
393 */
394 #define cx_strcast(str) cx_strcast_(cx_strcast_m(str))
319 #endif 395 #endif
320 396
321 /** 397 /**
322 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function. 398 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function.
323 * 399 *

mercurial