ucx
UAP Common Extensions
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
37#ifndef UCX_STRING_H
38#define UCX_STRING_H
39
40#include "common.h"
41#include "allocator.h"
42
52 char *ptr;
54 size_t length;
55};
56
60typedef struct cx_mutstr_s cxmutstr;
61
71 char const *ptr;
73 size_t length;
74};
75
79typedef struct cx_string_s cxstring;
80
104 size_t pos;
112 size_t delim_pos;
116 size_t next_pos;
120 size_t found;
124 size_t limit;
125};
126
131
132#ifdef __cplusplus
133extern "C" {
134
140#define CX_STR(literal) cxstring{literal, sizeof(literal) - 1}
141
142#else // __cplusplus
143
151#define CX_STR(literal) (cxstring){literal, sizeof(literal) - 1}
152
153#endif
154
155
171__attribute__((__warn_unused_result__, __nonnull__))
172cxmutstr cx_mutstr(char *cstring);
173
190__attribute__((__warn_unused_result__))
192 char *cstring,
193 size_t length
194);
195
211__attribute__((__warn_unused_result__, __nonnull__))
212cxstring cx_str(char const *cstring);
213
214
231__attribute__((__warn_unused_result__))
233 char const *cstring,
234 size_t length
235);
236
247__attribute__((__warn_unused_result__))
249
261__attribute__((__nonnull__))
263
276__attribute__((__nonnull__))
278 CxAllocator const *alloc,
280);
281
292__attribute__((__warn_unused_result__))
294 size_t count,
295 ...
296);
297
316__attribute__((__warn_unused_result__, __nonnull__))
318 CxAllocator const *alloc,
320 size_t count,
321 ...
322);
323
338#define cx_strcat_a(alloc, count, ...) \
339cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
340
354#define cx_strcat(count, ...) \
355cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
356
374#define cx_strcat_m(str, count, ...) \
375cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__)
376
392__attribute__((__warn_unused_result__))
394 cxstring string,
395 size_t start
396);
397
417__attribute__((__warn_unused_result__))
419 cxstring string,
420 size_t start,
421 size_t length
422);
423
439__attribute__((__warn_unused_result__))
441 cxmutstr string,
442 size_t start
443);
444
464__attribute__((__warn_unused_result__))
466 cxmutstr string,
467 size_t start,
468 size_t length
469);
470
483__attribute__((__warn_unused_result__))
485 cxstring string,
486 int chr
487);
488
501__attribute__((__warn_unused_result__))
503 cxmutstr string,
504 int chr
505);
506
519__attribute__((__warn_unused_result__))
521 cxstring string,
522 int chr
523);
524
537__attribute__((__warn_unused_result__))
539 cxmutstr string,
540 int chr
541);
542
559__attribute__((__warn_unused_result__))
561 cxstring haystack,
562 cxstring needle
563);
564
581__attribute__((__warn_unused_result__))
583 cxmutstr haystack,
584 cxstring needle
585);
586
599__attribute__((__warn_unused_result__, __nonnull__))
601 cxstring string,
603 size_t limit,
604 cxstring *output
605);
606
626__attribute__((__warn_unused_result__, __nonnull__))
628 CxAllocator const *allocator,
629 cxstring string,
631 size_t limit,
632 cxstring **output
633);
634
635
648__attribute__((__warn_unused_result__, __nonnull__))
650 cxmutstr string,
652 size_t limit,
653 cxmutstr *output
654);
655
675__attribute__((__warn_unused_result__, __nonnull__))
677 CxAllocator const *allocator,
678 cxmutstr string,
680 size_t limit,
681 cxmutstr **output
682);
683
692__attribute__((__warn_unused_result__))
694 cxstring s1,
695 cxstring s2
696);
697
706__attribute__((__warn_unused_result__))
708 cxstring s1,
709 cxstring s2
710);
711
722__attribute__((__warn_unused_result__, __nonnull__))
724 void const *s1,
725 void const *s2
726);
727
738__attribute__((__warn_unused_result__, __nonnull__))
740 void const *s1,
741 void const *s2
742);
743
744
757__attribute__((__warn_unused_result__, __nonnull__))
759 CxAllocator const *allocator,
760 cxstring string
761);
762
775#define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
776
777
790#define cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string))
791
804#define cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string))
805
815__attribute__((__warn_unused_result__))
817
827__attribute__((__warn_unused_result__))
829
838__attribute__((__warn_unused_result__))
840 cxstring string,
841 cxstring prefix
842);
843
852__attribute__((__warn_unused_result__))
854 cxstring string,
855 cxstring suffix
856);
857
866__attribute__((__warn_unused_result__))
868 cxstring string,
869 cxstring prefix
870);
871
880__attribute__((__warn_unused_result__))
882 cxstring string,
883 cxstring suffix
884);
885
895
905
925__attribute__((__warn_unused_result__, __nonnull__))
927 CxAllocator const *allocator,
929 cxstring pattern,
930 cxstring replacement,
931 size_t replmax
932);
933
952#define cx_strreplacen(str, pattern, replacement, replmax) \
953cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, replmax)
954
972#define cx_strreplace_a(allocator, str, pattern, replacement) \
973cx_strreplacen_a(allocator, str, pattern, replacement, SIZE_MAX)
974
992#define cx_strreplace(str, pattern, replacement) \
993cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX)
994
1003__attribute__((__warn_unused_result__))
1005 cxstring str,
1007 size_t limit
1008);
1009
1018__attribute__((__warn_unused_result__))
1020 cxmutstr str,
1022 size_t limit
1023);
1024
1035__attribute__((__warn_unused_result__, __nonnull__))
1037 CxStrtokCtx *ctx,
1038 cxstring *token
1039);
1040
1053__attribute__((__warn_unused_result__, __nonnull__))
1055 CxStrtokCtx *ctx,
1056 cxmutstr *token
1057);
1058
1066__attribute__((__nonnull__))
1068 CxStrtokCtx *ctx,
1069 cxstring const *delim,
1070 size_t count
1071);
1072
1073
1074#ifdef __cplusplus
1075} // extern "C"
1076#endif
1077
1078#endif //UCX_STRING_H
Interface for custom allocators.
Common definitions and feature checks.
#define __attribute__(x)
Removes GNU C attributes where they are not supported.
Definition: common.h:127
cxmutstr cx_strreplacen_a(CxAllocator const *allocator, cxstring str, cxstring pattern, cxstring replacement, size_t replmax)
Replaces a pattern in a string with another string.
cxmutstr cx_strcat_ma(CxAllocator const *alloc, cxmutstr str, size_t count,...)
Concatenates strings.
cxstring cx_str(char const *cstring)
Wraps a string that must be zero-terminated.
cxstring cx_strsubsl(cxstring string, size_t start, size_t length)
Returns a substring starting at the specified location.
void cx_strfree(cxmutstr *str)
Passes the pointer in this string to free().
cxmutstr cx_strsubsl_m(cxmutstr string, size_t start, size_t length)
Returns a substring starting at the specified location.
cxstring cx_strrchr(cxstring string, int chr)
Returns a substring starting at the location of the last occurrence of the specified character.
CxStrtokCtx cx_strtok_m(cxmutstr str, cxstring delim, size_t limit)
Creates a string tokenization context for a mutable string.
cxstring cx_strn(char const *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
bool cx_strtok_next(CxStrtokCtx *ctx, cxstring *token)
Returns the next token.
size_t cx_strsplit_a(CxAllocator const *allocator, cxstring string, cxstring delim, size_t limit, cxstring **output)
Splits a given string using a delimiter string.
CxStrtokCtx cx_strtok(cxstring str, cxstring delim, size_t limit)
Creates a string tokenization context.
cxstring cx_strcast(cxmutstr str)
Casts a mutable string to an immutable string.
size_t cx_strsplit(cxstring string, cxstring delim, size_t limit, cxstring *output)
Splits a given string using a delimiter string.
int cx_strcasecmp(cxstring s1, cxstring s2)
Compares two strings ignoring case.
cxmutstr cx_strchr_m(cxmutstr string, int chr)
Returns a substring starting at the location of the first occurrence of the specified character.
cxmutstr cx_strdup_a(CxAllocator const *allocator, cxstring string)
Creates a duplicate of the specified string.
cxmutstr cx_mutstr(char *cstring)
Wraps a mutable string that must be zero-terminated.
cxstring cx_strchr(cxstring string, int chr)
Returns a substring starting at the location of the first occurrence of the specified character.
size_t cx_strlen(size_t count,...)
Returns the accumulated length of all specified strings.
cxmutstr cx_strtrim_m(cxmutstr string)
Omits leading and trailing spaces.
int cx_strcasecmp_p(void const *s1, void const *s2)
Compares two strings ignoring case.
bool cx_strtok_next_m(CxStrtokCtx *ctx, cxmutstr *token)
Returns the next token of a mutable string.
void cx_strfree_a(CxAllocator const *alloc, cxmutstr *str)
Passes the pointer in this string to the allocators free function.
size_t cx_strsplit_ma(CxAllocator const *allocator, cxmutstr string, cxstring delim, size_t limit, cxmutstr **output)
Splits a given string using a delimiter string.
void cx_strlower(cxmutstr string)
Converts the string to lower case.
bool cx_strprefix(cxstring string, cxstring prefix)
Checks, if a string has a specific prefix.
bool cx_strcaseprefix(cxstring string, cxstring prefix)
Checks, if a string has a specific prefix, ignoring the case.
cxstring cx_strtrim(cxstring string)
Omits leading and trailing spaces.
cxstring cx_strstr(cxstring haystack, cxstring needle)
Returns a substring starting at the location of the first occurrence of the specified string.
void cx_strupper(cxmutstr string)
Converts the string to upper case.
cxstring cx_strsubs(cxstring string, size_t start)
Returns a substring starting at the specified location.
void cx_strtok_delim(CxStrtokCtx *ctx, cxstring const *delim, size_t count)
Defines an array of more delimiters for the specified tokenization context.
cxmutstr cx_strsubs_m(cxmutstr string, size_t start)
Returns a substring starting at the specified location.
cxmutstr cx_strrchr_m(cxmutstr string, int chr)
Returns a substring starting at the location of the last occurrence of the specified character.
size_t cx_strsplit_m(cxmutstr string, cxstring delim, size_t limit, cxmutstr *output)
Splits a given string using a delimiter string.
int cx_strcmp(cxstring s1, cxstring s2)
Compares two strings.
bool cx_strsuffix(cxstring string, cxstring suffix)
Checks, if a string has a specific suffix.
cxmutstr cx_strstr_m(cxmutstr haystack, cxstring needle)
Returns a substring starting at the location of the first occurrence of the specified string.
bool cx_strcasesuffix(cxstring string, cxstring suffix)
Checks, if a string has a specific suffix, ignoring the case.
cxmutstr cx_mutstrn(char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
int cx_strcmp_p(void const *s1, void const *s2)
Compares two strings.
Structure holding the data for an allocator.
Definition: allocator.h:86
The UCX string structure.
Definition: string.h:46
char * ptr
A pointer to the string.
Definition: string.h:52
size_t length
The length of the string.
Definition: string.h:54
The UCX string structure for immutable (constant) strings.
Definition: string.h:65
size_t length
The length of the string.
Definition: string.h:73
char const * ptr
A pointer to the immutable string.
Definition: string.h:71
Context for string tokenizing.
Definition: string.h:84
cxstring const * delim_more
Optional array of more delimiters.
Definition: string.h:96
size_t delim_pos
Position of next delimiter in the source string.
Definition: string.h:112
cxstring str
The string to tokenize.
Definition: string.h:88
cxstring delim
The primary delimiter.
Definition: string.h:92
size_t delim_more_count
Length of the array containing more delimiters.
Definition: string.h:100
size_t pos
Position of the currently active token in the source string.
Definition: string.h:104
size_t next_pos
The position of the next token in the source string.
Definition: string.h:116
size_t found
The number of already found tokens.
Definition: string.h:120
size_t limit
The maximum number of tokens that shall be returned.
Definition: string.h:124