ucx
UAP Common Extensions
string.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 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  */
48 #ifndef UCX_STRING_H
49 #define UCX_STRING_H
50 
51 #include "ucx.h"
52 #include "allocator.h"
53 #include <stddef.h>
54 
56 #define ST(s) { (char*)s, sizeof(s)-1 }
57 
59 #define S(s) sstrn((char*)s, sizeof(s)-1)
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
68 typedef struct {
71  char *ptr;
73  size_t length;
74 } sstr_t;
75 
89 sstr_t sstr(char *cstring);
90 
104 sstr_t sstrn(char *cstring, size_t length);
105 
106 
120 size_t sstrnlen(size_t count, sstr_t string, ...);
121 
137 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...);
138 
151 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...);
152 
153 
168 sstr_t sstrsubs(sstr_t string, size_t start);
169 
186 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
187 
200 sstr_t sstrchr(sstr_t string, int chr);
201 
214 sstr_t sstrrchr(sstr_t string, int chr);
215 
231 sstr_t sstrstr(sstr_t string, sstr_t match);
232 
280 sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count);
281 
304 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
305  ssize_t *count);
306 
319 int sstrcmp(sstr_t s1, sstr_t s2);
320 
335 int sstrcasecmp(sstr_t s1, sstr_t s2);
336 
351 sstr_t sstrdup(sstr_t string);
352 
369 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
370 
388 sstr_t sstrtrim(sstr_t string);
389 
396 int sstrprefix(sstr_t string, sstr_t prefix);
397 
404 int sstrsuffix(sstr_t string, sstr_t suffix);
405 
416 sstr_t sstrlower(sstr_t string);
417 
429 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
430 
441 sstr_t sstrupper(sstr_t string);
442 
454 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
455 
456 #ifdef __cplusplus
457 }
458 #endif
459 
460 #endif /* UCX_STRING_H */
sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string)
Returns a upper case version of a string.
Definition: string.c:457
sstr_t sstr(char *cstring)
Creates a new sstr_t based on a C string.
Definition: string.c:39
The UCX string structure.
Definition: string.h:68
sstr_t sstrsubsl(sstr_t string, size_t start, size_t length)
Returns a substring with a maximum length starting at the specified location.
Definition: string.c:139
sstr_t * sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, ssize_t *count)
Performing sstrsplit() using a UcxAllocator.
Definition: string.c:257
sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2,...)
Concatenates two or more strings using a UcxAllocator.
Definition: string.c:127
Main UCX Header providing most common definitions.
sstr_t sstrrchr(sstr_t string, int chr)
Returns a substring starting at the location of the last occurrence of the specified character...
Definition: string.c:166
sstr_t sstrtrim(sstr_t string)
Omits leading and trailing spaces.
Definition: string.c:388
sstr_t sstrsubs(sstr_t string, size_t start)
Returns a substring starting at the specified location.
Definition: string.c:135
sstr_t * sstrsplit(sstr_t string, sstr_t delim, ssize_t *count)
Splits a string into parts by using a delimiter string.
Definition: string.c:253
char * ptr
A reference to the string (not necessarily NULL -terminated)
Definition: string.h:71
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
sstr_t sstrstr(sstr_t string, sstr_t match)
Returns a substring starting at the location of the first occurrence of the specified string...
Definition: string.c:188
size_t length
The length of the string.
Definition: string.h:73
sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string)
Creates a duplicate of the specified string using a UcxAllocator.
Definition: string.c:373
sstr_t sstrchr(sstr_t string, int chr)
Returns a substring starting at the location of the first occurrence of the specified character...
Definition: string.c:154
int sstrcmp(sstr_t s1, sstr_t s2)
Compares two UCX strings with standard memcmp().
Definition: string.c:345
size_t sstrnlen(size_t count, sstr_t string,...)
Returns the cumulated length of all specified strings.
Definition: string.c:53
sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2,...)
Concatenates two or more strings.
Definition: string.c:119
sstr_t sstrlower(sstr_t string)
Returns a lower case version of a string.
Definition: string.c:433
int sstrcasecmp(sstr_t s1, sstr_t s2)
Compares two UCX strings ignoring the case.
Definition: string.c:355
Allocator for custom memory management.
sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string)
Returns a lower case version of a string.
Definition: string.c:441
int sstrprefix(sstr_t string, sstr_t prefix)
Checks, if a string has a specific prefix.
Definition: string.c:402
sstr_t sstrdup(sstr_t string)
Creates a duplicate of the specified string.
Definition: string.c:369
sstr_t sstrn(char *cstring, size_t length)
Creates a new sstr_t of the specified length based on a C string.
Definition: string.c:46
int sstrsuffix(sstr_t string, sstr_t suffix)
Checks, if a string has a specific suffix.
Definition: string.c:417
sstr_t sstrupper(sstr_t string)
Returns a upper case version of a string.
Definition: string.c:449