| 135 /** |
137 /** |
| 136 * A string tokenizing context. |
138 * A string tokenizing context. |
| 137 */ |
139 */ |
| 138 typedef struct cx_strtok_ctx_s CxStrtokCtx; |
140 typedef struct cx_strtok_ctx_s CxStrtokCtx; |
| 139 |
141 |
| 140 #ifdef __cplusplus |
|
| 141 extern "C" { |
|
| 142 |
|
| 143 /** |
|
| 144 * A literal initializer for an UCX string structure. |
|
| 145 * |
|
| 146 * @param literal the string literal |
|
| 147 */ |
|
| 148 #define CX_STR(literal) cxstring{literal, sizeof(literal) - 1} |
|
| 149 |
|
| 150 #else // __cplusplus |
|
| 151 |
|
| 152 /** |
|
| 153 * A literal initializer for an UCX string structure. |
|
| 154 * |
|
| 155 * The argument MUST be a string (const char*) @em literal. |
|
| 156 * |
|
| 157 * @param literal the string literal |
|
| 158 */ |
|
| 159 #define CX_STR(literal) ((cxstring){literal, sizeof(literal) - 1}) |
|
| 160 |
|
| 161 #endif |
|
| 162 |
|
| 163 |
|
| 164 /** |
142 /** |
| 165 * Wraps a mutable string that must be zero-terminated. |
143 * Wraps a mutable string that must be zero-terminated. |
| 166 * |
144 * |
| 167 * The length is implicitly inferred by using a call to @c strlen(). |
145 * The length is implicitly inferred by using a call to @c strlen(). |
| 168 * |
146 * |
| 177 * @return the wrapped string |
155 * @return the wrapped string |
| 178 * |
156 * |
| 179 * @see cx_mutstrn() |
157 * @see cx_mutstrn() |
| 180 */ |
158 */ |
| 181 cx_attr_nodiscard cx_attr_cstr_arg(1) |
159 cx_attr_nodiscard cx_attr_cstr_arg(1) |
| 182 CX_EXPORT cxmutstr cx_mutstr(char *cstring); |
160 CX_INLINE cxmutstr cx_mutstr(char *cstring) { |
| |
161 cxmutstr str; |
| |
162 str.ptr = cstring; |
| |
163 str.length = cstring == NULL ? 0 : strlen(cstring); |
| |
164 return str; |
| |
165 } |
| 183 |
166 |
| 184 /** |
167 /** |
| 185 * Wraps a string that does not need to be zero-terminated. |
168 * Wraps a string that does not need to be zero-terminated. |
| 186 * |
169 * |
| 187 * The argument may be @c NULL if the length is zero. |
170 * The argument may be @c NULL if the length is zero. |
| 196 * @return the wrapped string |
179 * @return the wrapped string |
| 197 * |
180 * |
| 198 * @see cx_mutstr() |
181 * @see cx_mutstr() |
| 199 */ |
182 */ |
| 200 cx_attr_nodiscard cx_attr_access_rw(1, 2) |
183 cx_attr_nodiscard cx_attr_access_rw(1, 2) |
| 201 CX_EXPORT cxmutstr cx_mutstrn(char *cstring, size_t length); |
184 CX_INLINE cxmutstr cx_mutstrn(char *cstring, size_t length) { |
| |
185 cxmutstr str; |
| |
186 str.ptr = cstring; |
| |
187 str.length = length; |
| |
188 return str; |
| |
189 } |
| 202 |
190 |
| 203 /** |
191 /** |
| 204 * Wraps a string that must be zero-terminated. |
192 * Wraps a string that must be zero-terminated. |
| 205 * |
193 * |
| 206 * The length is implicitly inferred by using a call to @c strlen(). |
194 * The length is implicitly inferred by using a call to @c strlen(). |
| 236 * @return the wrapped string |
229 * @return the wrapped string |
| 237 * |
230 * |
| 238 * @see cx_str() |
231 * @see cx_str() |
| 239 */ |
232 */ |
| 240 cx_attr_nodiscard cx_attr_access_r(1, 2) |
233 cx_attr_nodiscard cx_attr_access_r(1, 2) |
| 241 CX_EXPORT cxstring cx_strn(const char *cstring, size_t length); |
234 CX_INLINE cxstring cx_strn(const char *cstring, size_t length) { |
| |
235 cxstring str; |
| |
236 str.ptr = cstring; |
| |
237 str.length = length; |
| |
238 return str; |
| |
239 } |
| 242 |
240 |
| 243 #ifdef __cplusplus |
241 #ifdef __cplusplus |
| 244 } // extern "C" |
|
| 245 cx_attr_nodiscard |
242 cx_attr_nodiscard |
| 246 CX_CPPDECL cxstring cx_strcast(cxmutstr str) { |
243 CX_CPPDECL cxstring cx_strcast(cxmutstr str) { |
| 247 return cx_strn(str.ptr, str.length); |
244 return cx_strn(str.ptr, str.length); |
| 248 } |
245 } |
| 249 cx_attr_nodiscard |
246 cx_attr_nodiscard |