src/cx/string.h

changeset 1674
8b0f162ac88e
parent 1673
0c338b80e7dd
child 1675
36c0fb2b60b2
equal deleted inserted replaced
1673:0c338b80e7dd 1674:8b0f162ac88e
393 */ 393 */
394 #define cx_strcast(str) cx_strcast_(cx_strcast_m(str)) 394 #define cx_strcast(str) cx_strcast_(cx_strcast_m(str))
395 #endif 395 #endif
396 396
397 /** 397 /**
398 * Casts away constness and converts a cxstring to a cxmutstr.
399 * For internal use only!
400 * @param str
401 * @return
402 */
403 CX_INLINE cxmutstr cx_mutstrcast(cxstring str) {
404 cxmutstr s;
405 s.ptr = (char*)str.ptr;
406 s.length = str.length;
407 return s;
408 }
409
410 /**
398 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function. 411 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function.
399 * 412 *
400 * The pointer in the struct is set to @c NULL, and the length is set to zero, 413 * The pointer in the struct is set to @c NULL, and the length is set to zero,
401 * which means that this function protects you against double-free. 414 * which means that this function protects you against double-free.
402 * 415 *
528 */ 541 */
529 #define cx_strcat(str, count, ...) \ 542 #define cx_strcat(str, count, ...) \
530 cx_strcat_a(cxDefaultAllocator, str, count, __VA_ARGS__) 543 cx_strcat_a(cxDefaultAllocator, str, count, __VA_ARGS__)
531 544
532 /** 545 /**
533 * Returns a substring starting at the specified location. 546 * Returns a substring.
534 * 547 *
535 * @attention the new string references the same memory area as the 548 * Internal function - do not use.
536 * input string and is usually @em not zero-terminated.
537 * Use cx_strdup() to get a copy.
538 *
539 * @param string input string
540 * @param start start location of the substring
541 * @return a substring of @p string starting at @p start
542 *
543 * @see cx_strsubsl()
544 * @see cx_strsubs_m()
545 * @see cx_strsubsl_m()
546 */
547 cx_attr_nodiscard
548 CX_EXPORT cxstring cx_strsubs(cxstring string, size_t start);
549
550 /**
551 * Returns a substring starting at the specified location.
552 *
553 * The returned string will be limited to @p length bytes or the number
554 * of bytes available in @p string, whichever is smaller.
555 *
556 * @attention the new string references the same memory area as the
557 * input string and is usually @em not zero-terminated.
558 * Use cx_strdup() to get a copy.
559 * 549 *
560 * @param string input string 550 * @param string input string
561 * @param start start location of the substring 551 * @param start start location of the substring
562 * @param length the maximum length of the returned string 552 * @param length the maximum length of the returned string
563 * @return a substring of @p string starting at @p start 553 * @return a substring of @p string starting at @p start
564 * 554 * @see cx_strsubsl()
565 * @see cx_strsubs() 555 */
566 * @see cx_strsubs_m() 556 cx_attr_nodiscard
567 * @see cx_strsubsl_m() 557 CX_EXPORT cxstring cx_strsubsl_(cxstring string, size_t start, size_t length);
568 */ 558
569 cx_attr_nodiscard 559 /**
570 CX_EXPORT cxstring cx_strsubsl(cxstring string, size_t start, size_t length); 560 * Returns a substring.
571 561 *
572 /** 562 * Internal function - do not use.
573 * Returns a substring starting at the specified location.
574 *
575 * @attention the new string references the same memory area as the
576 * input string and is usually @em not zero-terminated.
577 * Use cx_strdup() to get a copy.
578 * 563 *
579 * @param string input string 564 * @param string input string
580 * @param start start location of the substring 565 * @param start start location of the substring
581 * @return a substring of @p string starting at @p start 566 * @return a substring of @p string starting at @p start
582 *
583 * @see cx_strsubsl_m()
584 * @see cx_strsubs() 567 * @see cx_strsubs()
568 */
569 cx_attr_nodiscard
570 CX_EXPORT cxstring cx_strsubs_(cxstring string, size_t start);
571
572 CX_INLINE cxmutstr cx_strsubs_m_(cxmutstr string, size_t start) {
573 return cx_mutstrcast(cx_strsubs_(cx_strcast(string), start));
574 }
575
576 CX_INLINE cxmutstr cx_strsubsl_m_(cxmutstr string, size_t start, size_t length) {
577 return cx_mutstrcast(cx_strsubsl_(cx_strcast(string), start, length));
578 }
579
580 #ifdef __cplusplus
581 } // extern "C"
582 CX_CPPDECL cxstring cx_strsubs_cpp_(cxstring string, size_t start) {
583 return cx_strsubs_(string, start);
584 }
585 CX_CPPDECL cxstring cx_strsubsl_cpp_(cxstring string, size_t start, size_t length) {
586 return cx_strsubsl_(string, start, length);
587 }
588 CX_CPPDECL cxmutstr cx_strsubs_cpp_(cxmutstr string, size_t start) {
589 return cx_strsubs_m_(string, start);
590 }
591 CX_CPPDECL cxmutstr cx_strsubsl_cpp_(cxmutstr string, size_t start, size_t length) {
592 return cx_strsubsl_m_(string, start, length);
593 }
594 #define cx_strsubs(string, start) cx_strsubs_cpp_(cx_strcast_m(string), start)
595 #define cx_strsubsl(string, start, length) cx_strsubsl_cpp_(cx_strcast_m(string), start, length)
596 extern "C" {
597 #else
598 /**
599 * Returns a substring starting at the specified location.
600 *
601 * @attention the new string references the same memory area as the
602 * input string and is @em not zero-terminated.
603 * Use cx_strdup() to get a copy.
604 *
605 * @param string input string
606 * @param start (@c size_t) start location of the substring
607 * @return (@c cxstring or @c cxmutstr) a substring of @p string starting at @p start
608 *
585 * @see cx_strsubsl() 609 * @see cx_strsubsl()
586 */ 610 */
587 cx_attr_nodiscard 611 #define cx_strsubs(string, start) _Generic(cx_strcast_m(string), \
588 CX_EXPORT cxmutstr cx_strsubs_m(cxmutstr string, size_t start); 612 cxstring: cx_strsubs_, \
613 cxmutstr: cx_strsubs_m_)(cx_strcast_m(string), start)
589 614
590 /** 615 /**
591 * Returns a substring starting at the specified location. 616 * Returns a substring starting at the specified location.
592 * 617 *
593 * The returned string will be limited to @p length bytes or the number 618 * The returned string will be limited to @p length bytes or the number
600 * @param string input string 625 * @param string input string
601 * @param start start location of the substring 626 * @param start start location of the substring
602 * @param length the maximum length of the returned string 627 * @param length the maximum length of the returned string
603 * @return a substring of @p string starting at @p start 628 * @return a substring of @p string starting at @p start
604 * 629 *
605 * @see cx_strsubs_m()
606 * @see cx_strsubs() 630 * @see cx_strsubs()
607 * @see cx_strsubsl() 631 */
608 */ 632 #define cx_strsubsl(string, start, length) _Generic(cx_strcast_m(string), \
609 cx_attr_nodiscard 633 cxstring: cx_strsubsl_, \
610 CX_EXPORT cxmutstr cx_strsubsl_m(cxmutstr string, size_t start, size_t length); 634 cxmutstr: cx_strsubsl_m_)(cx_strcast_m(string), start, length)
635 #endif
611 636
612 /** 637 /**
613 * Returns the character at the specified index offset. 638 * Returns the character at the specified index offset.
614 * 639 *
615 * Internal function - do not use. 640 * Internal function - do not use.

mercurial