23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
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 |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
|
28 /** |
|
29 * Bounded string implementation. |
|
30 * |
|
31 * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings. |
|
32 * The main difference to C strings is, that <code>sstr_t</code> does <b>not |
|
33 * need to be <code>NULL</code>-terminated</b>. Instead the length is stored |
|
34 * within the structure. |
|
35 * |
|
36 * When using <code>sstr_t</code>, developers must be full aware of what type |
|
37 * of string (<code>NULL</code>-terminated) or not) they are using, when |
|
38 * accessing the <code>char* ptr</code> directly. |
|
39 * |
|
40 * The UCX string module provides some common string functions, known from |
|
41 * standard libc, working with <code>sstr_t</code>. |
|
42 * |
|
43 * @file string.h |
|
44 * @author Mike Becker |
|
45 * @author Olaf Wintermann |
|
46 */ |
28 |
47 |
29 #ifndef _SSTRING_H |
48 #ifndef UCX_STRING_H |
30 #define _SSTRING_H |
49 #define UCX_STRING_H |
31 |
50 |
32 #include "ucx.h" |
51 #include "ucx.h" |
33 #include "allocator.h" |
52 #include "allocator.h" |
34 #include <stddef.h> |
53 #include <stddef.h> |
35 |
54 |
36 /* use macros for literals only */ |
55 /** Shortcut for a <code>sstr_t struct</code> literal. */ |
37 #define S(s) { (char*)s, sizeof(s)-1 } |
56 #define ST(s) { (char*)s, sizeof(s)-1 } |
38 #define ST(s) sstrn((char*)s, sizeof(s)-1) |
57 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */ |
|
58 #define S(s) sstrn((char*)s, sizeof(s)-1) |
39 |
59 |
40 #ifdef __cplusplus |
60 #ifdef __cplusplus |
41 extern "C" { |
61 extern "C" { |
42 #endif |
62 #endif |
43 |
63 |
44 typedef struct sstring { |
64 /** |
|
65 * The UCX string structure. |
|
66 */ |
|
67 typedef struct { |
|
68 /** A reference to the string (<b>not necessarily <code>NULL</code> |
|
69 * -terminated</b>) */ |
45 char *ptr; |
70 char *ptr; |
|
71 /** The length of the string */ |
46 size_t length; |
72 size_t length; |
47 } sstr_t; |
73 } sstr_t; |
48 |
74 |
49 /* |
75 /** |
50 * creates a new sstr_t from a null terminated string |
76 * Creates a new sstr_t based on a C string. |
|
77 * |
|
78 * The length is implicitly inferred by using a call to <code>strlen()</code>. |
51 * |
79 * |
52 * s null terminated string |
80 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you |
|
81 * do want a copy, use sstrdup() on the return value of this function. |
|
82 * |
|
83 * @param cstring the C string to wrap |
|
84 * @return a new sstr_t containing the C string |
|
85 * |
|
86 * @see sstrn() |
53 */ |
87 */ |
54 sstr_t sstr(char *s); |
88 sstr_t sstr(char *cstring); |
55 |
89 |
56 /* |
90 /** |
57 * creates a new sstr_t from a string and length |
91 * Creates a new sstr_t of the specified length based on a C string. |
58 * |
92 * |
59 * s string |
93 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you |
60 * n length of string |
94 * do want a copy, use sstrdup() on the return value of this function. |
|
95 * |
|
96 * @param cstring the C string to wrap |
|
97 * @param length the length of the string |
|
98 * @return a new sstr_t containing the C string |
|
99 * |
|
100 * @see sstr() |
|
101 * @see S() |
61 */ |
102 */ |
62 sstr_t sstrn(char *s, size_t n); |
103 sstr_t sstrn(char *cstring, size_t length); |
63 |
104 |
64 |
105 |
65 /* |
106 /** |
66 * gets the length of n sstr_t strings |
107 * Returns the cumulated length of all specified strings. |
67 * |
108 * |
68 * n number of strings |
109 * At least one string must be specified. |
69 * s string |
110 * |
70 * ... strings |
111 * <b>Attention:</b> if the count argument does not match the count of the |
|
112 * specified strings, the behavior is undefined. |
|
113 * |
|
114 * @param count the total number of specified strings (so at least 1) |
|
115 * @param string the first string |
|
116 * @param ... all other strings |
|
117 * @return the cumulated length of all strings |
71 */ |
118 */ |
72 size_t sstrnlen(size_t n, sstr_t s, ...); |
119 size_t sstrnlen(size_t count, sstr_t string, ...); |
73 |
120 |
74 |
121 |
75 /* |
122 /* |
76 * concatenates n strings |
123 * concatenates n strings |
77 * |
124 * |
113 * |
160 * |
114 * Returns NULL on error |
161 * Returns NULL on error |
115 */ |
162 */ |
116 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n); |
163 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n); |
117 |
164 |
|
165 /** |
|
166 * Compares two UCX strings with standard <code>memcmp()</code>. |
|
167 * |
|
168 * At first it compares the sstr_t.length attribute of the two strings. The |
|
169 * <code>memcmp()</code> function is called, if and only if the lengths match. |
|
170 * |
|
171 * @param s1 the first string |
|
172 * @param s2 the second string |
|
173 * @return -1, if the length of s1 is less than the length of s2 or 1, if the |
|
174 * length of s1 is greater than the length of s2 or the result of |
|
175 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match) |
|
176 */ |
118 int sstrcmp(sstr_t s1, sstr_t s2); |
177 int sstrcmp(sstr_t s1, sstr_t s2); |
119 |
178 |
120 sstr_t sstrdup(sstr_t s); |
179 /** |
121 sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s); |
180 * Creates a duplicate of the specified string. |
|
181 * |
|
182 * The new sstr_t will contain a copy allocated by standard |
|
183 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to |
|
184 * <code>free()</code>. |
|
185 * |
|
186 * @param string the string to duplicate |
|
187 * @return a duplicate of the argument |
|
188 */ |
|
189 sstr_t sstrdup(sstr_t string); |
|
190 sstr_t sstrdupa(UcxAllocator *allocator, sstr_t s); |
122 |
191 |
123 sstr_t sstrtrim(sstr_t string); |
192 sstr_t sstrtrim(sstr_t string); |
124 |
193 |
125 #ifdef __cplusplus |
194 #ifdef __cplusplus |
126 } |
195 } |
127 #endif |
196 #endif |
128 |
197 |
129 #endif /* _SSTRING_H */ |
198 #endif /* UCX_STRING_H */ |