|
1 /* |
|
2 * File: sstring.h |
|
3 * Author: olaf |
|
4 * |
|
5 * Created on 17. Juni 2010, 13:26 |
|
6 */ |
|
7 |
|
8 #ifndef _SSTRING_H |
|
9 #define _SSTRING_H |
|
10 |
|
11 #include "ucx.h" |
|
12 #include <stddef.h> |
|
13 |
|
14 /* use macros for literals only */ |
|
15 #define S(s) { (char*)s, sizeof(s)-1 } |
|
16 #define ST(s) sstrn((char*)s, sizeof(s)-1) |
|
17 |
|
18 #ifdef __cplusplus |
|
19 extern "C" { |
|
20 #endif |
|
21 |
|
22 typedef struct sstring { |
|
23 char *ptr; |
|
24 size_t length; |
|
25 } sstr_t; |
|
26 |
|
27 /* |
|
28 * creates a new sstr_t from a null terminated string |
|
29 * |
|
30 * s null terminated string |
|
31 */ |
|
32 sstr_t sstr(char *s); |
|
33 |
|
34 /* |
|
35 * creates a new sstr_t from a string and length |
|
36 * |
|
37 * s string |
|
38 * n length of string |
|
39 */ |
|
40 sstr_t sstrn(char *s, size_t n); |
|
41 |
|
42 |
|
43 /* |
|
44 * gets the length of n sstr_t strings |
|
45 * |
|
46 * n number of strings |
|
47 * s string |
|
48 * ... strings |
|
49 */ |
|
50 size_t sstrnlen(size_t n, sstr_t s, ...); |
|
51 |
|
52 |
|
53 /* |
|
54 * concatenates n strings |
|
55 * |
|
56 * n number of strings |
|
57 * s new string with enough memory allocated |
|
58 * ... strings |
|
59 */ |
|
60 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...); |
|
61 |
|
62 |
|
63 /* |
|
64 * |
|
65 */ |
|
66 sstr_t sstrsubs(sstr_t s, size_t start); |
|
67 |
|
68 /* |
|
69 * |
|
70 */ |
|
71 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length); |
|
72 |
|
73 /* |
|
74 * splits s into n parts |
|
75 * |
|
76 * s the string to split |
|
77 * d the delimiter string |
|
78 * n the maximum size of the resulting list |
|
79 * a size of 0 indicates an unbounded list size |
|
80 * the actual size of the list will be stored here |
|
81 * |
|
82 * Hint: use this value to avoid dynamic reallocation of the result list |
|
83 * |
|
84 * Returns a list of the split strings |
|
85 * NOTE: this list needs to be freed manually after usage |
|
86 * |
|
87 * Returns NULL on error |
|
88 */ |
|
89 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n); |
|
90 |
|
91 int sstrcmp(sstr_t s1, sstr_t s2); |
|
92 |
|
93 sstr_t sstrdup(sstr_t s); |
|
94 |
|
95 #ifdef __cplusplus |
|
96 } |
|
97 #endif |
|
98 |
|
99 #endif /* _SSTRING_H */ |