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 
62 #define SFMT(s) (int) (s).length, (s).ptr
63 
65 #define PRIsstr ".*s"
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
74 typedef struct {
77  char *ptr;
79  size_t length;
80 } sstr_t;
81 
95 sstr_t sstr(char *cstring);
96 
110 sstr_t sstrn(char *cstring, size_t length);
111 
112 
126 size_t sstrnlen(size_t count, sstr_t string, ...);
127 
143 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...);
144 
157 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...);
158 
159 
174 sstr_t sstrsubs(sstr_t string, size_t start);
175 
192 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
193 
206 sstr_t sstrchr(sstr_t string, int chr);
207 
220 sstr_t sstrrchr(sstr_t string, int chr);
221 
237 sstr_t sstrstr(sstr_t string, sstr_t match);
238 
286 sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count);
287 
310 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
311  ssize_t *count);
312 
325 int sstrcmp(sstr_t s1, sstr_t s2);
326 
341 int sstrcasecmp(sstr_t s1, sstr_t s2);
342 
357 sstr_t sstrdup(sstr_t string);
358 
375 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
376 
394 sstr_t sstrtrim(sstr_t string);
395 
402 int sstrprefix(sstr_t string, sstr_t prefix);
403 
410 int sstrsuffix(sstr_t string, sstr_t suffix);
411 
422 sstr_t sstrlower(sstr_t string);
423 
435 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string);
436 
447 sstr_t sstrupper(sstr_t string);
448 
460 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string);
461 
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif /* UCX_STRING_H */
sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string)
Returns a upper case version of a string.
Definition: string.c:482
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:74
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:156
sstr_t * sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, ssize_t *count)
Performing sstrsplit() using a UcxAllocator.
Definition: string.c:274
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:144
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:183
sstr_t sstrtrim(sstr_t string)
Omits leading and trailing spaces.
Definition: string.c:413
sstr_t sstrsubs(sstr_t string, size_t start)
Returns a substring starting at the specified location.
Definition: string.c:152
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:270
char * ptr
A reference to the string (not necessarily NULL -terminated)
Definition: string.h:77
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:205
size_t length
The length of the string.
Definition: string.h:79
sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string)
Creates a duplicate of the specified string using a UcxAllocator.
Definition: string.c:398
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:171
int sstrcmp(sstr_t s1, sstr_t s2)
Compares two UCX strings with standard memcmp().
Definition: string.c:370
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:136
sstr_t sstrlower(sstr_t string)
Returns a lower case version of a string.
Definition: string.c:458
int sstrcasecmp(sstr_t s1, sstr_t s2)
Compares two UCX strings ignoring the case.
Definition: string.c:380
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:466
int sstrprefix(sstr_t string, sstr_t prefix)
Checks, if a string has a specific prefix.
Definition: string.c:427
sstr_t sstrdup(sstr_t string)
Creates a duplicate of the specified string.
Definition: string.c:394
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:442
sstr_t sstrupper(sstr_t string)
Returns a upper case version of a string.
Definition: string.c:474