simplifications
[mala.git] / engine / strings.h
blobe2f0774e461e49afa740b35d6b37c2cb9c742826
1 /*
2 strings.h - MaLa string handling
4 Copyright (C) 2004, 2005, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
20 #ifndef MALA_STRINGS_H
21 #define MALA_STRINGS_H
23 #include <string.h>
24 #include <stdint.h>
25 #include <search.h>
26 #include <stdlib.h>
27 #include "mala_types.h"
28 #include "strings.h"
29 #include "stringbucket.h"
32 struct mala_string_struct
34 const char * str;
35 size_t len; // MAX_SIZE means 'not yet counted'
36 unsigned long refcnt; // lowest bits: 0x1 descriptor is not on heap,
37 // 0x2 data is not on heap
38 // refcount is increased/decreased by 4 for each reference,
39 // because of the used lower bits
40 MalaStringBucket bucket;
41 void * user;
44 // static strings are initialized by "literals"
45 #define MALA_STRING_STATIC(literal) &(mala_string){literal,sizeof(literal)-1,0x3,NULL,NULL}
46 // automatic strings can be initialized by any const char*
47 #define MALA_STRING_AUTO(cstr) {cstr,SIZE_MAX,0x3,NULL,NULL}
51 * Declarations
53 MalaString
54 mala_string_new_cstr_attach (char* cstr, MalaStringBucket bucket);
56 MalaString
57 mala_string_new (const char* cstr, MalaStringBucket bucket);
59 MalaString
60 mala_string_new_n (const char* cstr, const size_t n, MalaStringBucket bucket);
62 MalaString
63 mala_string_new_cat2 (const char* cstr1, const char* cstr2, MalaStringBucket bucket);
65 MalaString
66 mala_string_find (const char* cstr, MalaStringBucket bucket);
68 MalaString
69 mala_string_copy_n (MalaString src, const size_t n);
71 MalaString
72 mala_string_new_prefix (const char* prefix, MalaString str);
74 //MalaString
75 //mala_string_cat_n (MalaString_ref dest, MalaString src, size_t n);
76 //MalaString
77 //mala_string_cat_nn (MalaString_ref dest, size_t n, MalaString src, size_t n);
79 MalaString
80 mala_string_new_print (MalaStringBucket bucket, const char* fmt,...);
82 int
83 mala_string_scan (const_MalaString str,const char* fmt,...);
85 void
86 mala_string_free (MalaString s);
88 MalaString
89 mala_string_factory (MalaString_ref dest, const char* src, MalaStringBucket bucket);
91 void
92 mala_string_data_free (MalaString s);
94 size_t
95 mala_string_char_find (MalaString self, const char c);
97 MalaString
98 mala_string_new_substr (MalaString self, size_t start, size_t end);
101 /*statics*/
102 static inline void*
103 mala_string_user_get (const_MalaString s);
105 static inline void*
106 mala_string_user_set (MalaString s, void* data);
108 static inline MalaStringBucket
109 mala_string_bucket_get (const_MalaString s);
111 static inline const char*
112 mala_string_cstr (const_MalaString s);
114 static inline size_t
115 mala_string_length (MalaString s);
117 static inline size_t
118 mala_string_references (const_MalaString s);
120 static inline int
121 mala_string_same (const_MalaString a, const_MalaString b);
123 static inline int
124 mala_string_check_prefix (const_MalaString self, const char* prefix);
126 static inline int
127 mala_string_check_prefix_nocase (const_MalaString self, const char* prefix);
129 static inline int
130 mala_string_compare (const_MalaString a, const_MalaString b);
132 static inline int
133 mala_string_compare_nocase (const_MalaString a, const_MalaString b);
135 //static inline int mala_string_compare_rev (const_MalaString a, const_MalaString b);
137 static inline int
138 mala_string_compare_n (const_MalaString a, const_MalaString b, const size_t n);
140 static inline int
141 mala_string_compare_n_nocase (const_MalaString a, const_MalaString b, size_t n);
143 static inline MalaString
144 mala_string_copy (MalaString src);
146 static inline int
147 mala_string_at (MalaString self, unsigned n);
151 * Inlines
154 static inline void*
155 mala_string_user_get (const_MalaString s)
157 return (s && s->bucket) ? s->user : NULL;
161 static inline void*
162 mala_string_user_set (MalaString s, void* data)
164 return s->user = s->bucket ? data : NULL;
168 static inline MalaStringBucket
169 mala_string_bucket_get (const_MalaString s)
171 return s->bucket;
175 static inline const char*
176 mala_string_cstr (const_MalaString s)
178 return s->str;
182 static inline size_t
183 mala_string_length (MalaString s)
185 return s->len != SIZE_MAX ? s->len : s->len = strlen (s->str);
189 static inline size_t
190 mala_string_references (const_MalaString s)
192 return s->refcnt>>2;
195 static inline int
196 mala_string_same (const_MalaString a, const_MalaString b)
198 return a == b;
201 static inline int
202 mala_string_check_prefix (const_MalaString self, const char* prefix)
204 return strncmp(self->str, prefix, strlen(prefix)) == 0;
207 static inline int
208 mala_string_check_prefix_nocase (const_MalaString self, const char* prefix)
210 return strncasecmp(self->str, prefix, strlen(prefix)) == 0;
214 static inline int
215 mala_string_compare (const_MalaString a, const_MalaString b)
217 return a == b ? 0 : strcmp (mala_string_cstr (a), mala_string_cstr (b));
221 static inline int
222 mala_string_compare_nocase (const_MalaString a, const_MalaString b)
224 return a == b ? 0 : strcasecmp (mala_string_cstr (a), mala_string_cstr (b));
228 static inline int
229 mala_string_compare_rev (const_MalaString a, const_MalaString b)
231 return mala_string_compare (b,a);
235 static inline int
236 mala_string_compare_n (const_MalaString a, const_MalaString b, const size_t n)
238 return a == b ? 0 : strncmp (mala_string_cstr (a), mala_string_cstr (b), n);
242 static inline int
243 mala_string_compare_n_nocase (const_MalaString a, const_MalaString b, const size_t n)
245 return a == b ? 0 : strncasecmp (mala_string_cstr (a), mala_string_cstr (b), n);
248 static inline MalaString
249 mala_string_copy (MalaString src)
251 src->refcnt += 4;
252 return src;
255 static inline int
256 mala_string_at (MalaString self, unsigned n)
258 MALA_ASSERT (mala_string_length (self) > n);
259 return self->str[n];
263 #endif /* MALA_STRINGS */
266 // Local Variables:
267 // mode: C
268 // c-file-style: "gnu"
269 // End:
270 // arch-tag: b43b7350-ecb6-4aa8-94d3-930da822e3ea
271 // end_of_file