77 * An immutable string. |
77 * An immutable string. |
78 */ |
78 */ |
79 typedef struct cx_string_s cxstring; |
79 typedef struct cx_string_s cxstring; |
80 |
80 |
81 /** |
81 /** |
|
82 * Context for string tokenizing. |
|
83 */ |
|
84 struct cx_strtok_ctx_s { |
|
85 /** |
|
86 * The string to tokenize. |
|
87 */ |
|
88 cxstring str; |
|
89 /** |
|
90 * The primary delimiter. |
|
91 */ |
|
92 cxstring delim; |
|
93 /** |
|
94 * Optional array of more delimiters. |
|
95 */ |
|
96 cxstring const *delim_more; |
|
97 /** |
|
98 * Length of the array containing more delimiters. |
|
99 */ |
|
100 size_t delim_more_count; |
|
101 /** |
|
102 * Position of the currently active token in the source string. |
|
103 */ |
|
104 size_t pos; |
|
105 /** |
|
106 * Position of next delimiter in the source string. |
|
107 * |
|
108 * If the tokenizer has not yet returned a token, the content of this field |
|
109 * is undefined. If the tokenizer reached the end of the string, this field |
|
110 * contains the length of the source string. |
|
111 */ |
|
112 size_t delim_pos; |
|
113 /** |
|
114 * The position of the next token in the source string. |
|
115 */ |
|
116 size_t next_pos; |
|
117 /** |
|
118 * The number of already found tokens. |
|
119 */ |
|
120 size_t found; |
|
121 /** |
|
122 * The maximum number of tokens that shall be returned. |
|
123 */ |
|
124 size_t limit; |
|
125 }; |
|
126 |
|
127 /** |
|
128 * A string tokenizing context. |
|
129 */ |
|
130 typedef struct cx_strtok_ctx_s CxStrtokCtx; |
|
131 |
|
132 /** |
82 * A literal initializer for an UCX string structure. |
133 * A literal initializer for an UCX string structure. |
83 * |
134 * |
84 * The argument MUST be a string (const char*) \em literal. |
135 * The argument MUST be a string (const char*) \em literal. |
85 * |
136 * |
86 * @param literal the string literal |
137 * @param literal the string literal |
826 * @return the resulting string after applying the replacements |
877 * @return the resulting string after applying the replacements |
827 */ |
878 */ |
828 #define cx_strreplace(str, pattern, replacement) \ |
879 #define cx_strreplace(str, pattern, replacement) \ |
829 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX) |
880 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX) |
830 |
881 |
|
882 /** |
|
883 * Creates a string tokenization context. |
|
884 * |
|
885 * @param str the string to tokenize |
|
886 * @param delim the delimiter (must not be empty) |
|
887 * @param limit the maximum number of tokens that shall be returned |
|
888 * @return a new string tokenization context |
|
889 */ |
|
890 __attribute__((__warn_unused_result__)) |
|
891 CxStrtokCtx cx_strtok( |
|
892 cxstring str, |
|
893 cxstring delim, |
|
894 size_t limit |
|
895 ); |
|
896 |
|
897 /** |
|
898 * Creates a string tokenization context for a mutable string. |
|
899 * |
|
900 * @param str the string to tokenize |
|
901 * @param delim the delimiter (must not be empty) |
|
902 * @param limit the maximum number of tokens that shall be returned |
|
903 * @return a new string tokenization context |
|
904 */ |
|
905 __attribute__((__warn_unused_result__)) |
|
906 CxStrtokCtx cx_strtok_m( |
|
907 cxmutstr str, |
|
908 cxstring delim, |
|
909 size_t limit |
|
910 ); |
|
911 |
|
912 /** |
|
913 * Returns the next token. |
|
914 * |
|
915 * The token will point to the source string. |
|
916 * |
|
917 * @param ctx the tokenization context |
|
918 * @param token a pointer to memory where the next token shall be stored |
|
919 * @return true if successful, false if the limit or the end of the string |
|
920 * has been reached |
|
921 */ |
|
922 __attribute__((__warn_unused_result__, __nonnull__)) |
|
923 bool cx_strtok_next( |
|
924 CxStrtokCtx *ctx, |
|
925 cxstring *token |
|
926 ); |
|
927 |
|
928 /** |
|
929 * Returns the next token of a mutable string. |
|
930 * |
|
931 * The token will point to the source string. |
|
932 * If the context was not initialized over a mutable string, modifying |
|
933 * the data of the returned token is undefined behavior. |
|
934 * |
|
935 * @param ctx the tokenization context |
|
936 * @param token a pointer to memory where the next token shall be stored |
|
937 * @return true if successful, false if the limit or the end of the string |
|
938 * has been reached |
|
939 */ |
|
940 __attribute__((__warn_unused_result__, __nonnull__)) |
|
941 bool cx_strtok_next_m( |
|
942 CxStrtokCtx *ctx, |
|
943 cxmutstr *token |
|
944 ); |
|
945 |
|
946 /** |
|
947 * Defines an array of more delimiters for the specified tokenization context. |
|
948 * |
|
949 * @param ctx the tokenization context |
|
950 * @param delim array of more delimiters |
|
951 * @param count number of elements in the array |
|
952 */ |
|
953 __attribute__((__nonnull__)) |
|
954 void cx_strtok_delim( |
|
955 CxStrtokCtx *ctx, |
|
956 cxstring const *delim, |
|
957 size_t count |
|
958 ); |
|
959 |
|
960 |
831 #ifdef __cplusplus |
961 #ifdef __cplusplus |
832 } // extern "C" |
962 } // extern "C" |
833 #endif |
963 #endif |
834 |
964 |
835 #endif //UCX_STRING_H |
965 #endif //UCX_STRING_H |