macrodelete_parser fix
[mala.git] / engine / strings.h
blob8ad01752dd30235bd5d22361f514fdfc5afd383d
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,3,NULL,NULL}
46 // automatic strings can be initialized by any const char*
47 #define MALA_STRING_AUTO(cstr) {cstr,SIZE_MAX,3,NULL,NULL}
51 * Declarations
53 MalaString
54 mala_string_new (const char* cstr, MalaStringBucket bucket);
56 MalaString
57 mala_string_new_n (const char* cstr, const size_t n, MalaStringBucket bucket);
59 MalaString
60 mala_string_new_cat2 (const char* cstr1, const char* cstr2, MalaStringBucket bucket);
62 MalaString
63 mala_string_find (const char* cstr, MalaStringBucket bucket);
65 MalaString
66 mala_string_copy_n (MalaString src, const size_t n);
68 MalaString
69 mala_string_new_prefix (const char* prefix, MalaString str);
71 //MalaString
72 //mala_string_cat_n (MalaString_ref dest, MalaString src, size_t n);
73 //MalaString
74 //mala_string_cat_nn (MalaString_ref dest, size_t n, MalaString src, size_t n);
76 MalaString
77 mala_string_new_print (MalaStringBucket bucket, const char* fmt,...);
79 int
80 mala_string_scan (const_MalaString str,const char* fmt,...);
82 void
83 mala_string_free (MalaString s);
85 MalaString
86 mala_string_factory (MalaString_ref dest, const char* src, MalaStringBucket bucket);
88 void
89 mala_string_data_free (MalaString s);
91 size_t
92 mala_string_char_find (MalaString self, const char c);
94 MalaString
95 mala_string_new_substr (MalaString self, size_t start, size_t end);
98 /*statics*/
99 static inline void*
100 mala_string_user_get (const_MalaString s);
102 static inline void*
103 mala_string_user_set (MalaString s, void* data);
105 static inline MalaStringBucket
106 mala_string_bucket_get (const_MalaString s);
108 static inline const char*
109 mala_string_cstr (const_MalaString s);
111 static inline size_t
112 mala_string_length (MalaString s);
114 static inline size_t
115 mala_string_references (const_MalaString s);
117 static inline int
118 mala_string_same (const_MalaString a, const_MalaString b);
120 static inline int
121 mala_string_check_prefix (const_MalaString self, const char* prefix);
123 static inline int
124 mala_string_check_prefix_nocase (const_MalaString self, const char* prefix);
126 static inline int
127 mala_string_compare (const_MalaString a, const_MalaString b);
129 static inline int
130 mala_string_compare_nocase (const_MalaString a, const_MalaString b);
132 //static inline int mala_string_compare_rev (const_MalaString a, const_MalaString b);
134 static inline int
135 mala_string_compare_n (const_MalaString a, const_MalaString b, const size_t n);
137 static inline int
138 mala_string_compare_n_nocase (const_MalaString a, const_MalaString b, size_t n);
140 static inline MalaString
141 mala_string_copy (MalaString src);
143 static inline int
144 mala_string_at (MalaString self, unsigned n);
148 * Inlines
151 static inline void*
152 mala_string_user_get (const_MalaString s)
154 return (s && s->bucket) ? s->user : NULL;
158 static inline void*
159 mala_string_user_set (MalaString s, void* data)
161 return s->user = s->bucket ? data : NULL;
165 static inline MalaStringBucket
166 mala_string_bucket_get (const_MalaString s)
168 return s->bucket;
172 static inline const char*
173 mala_string_cstr (const_MalaString s)
175 return s->str;
179 static inline size_t
180 mala_string_length (MalaString s)
182 return s->len != SIZE_MAX ? s->len : s->len = strlen (s->str);
186 static inline size_t
187 mala_string_references (const_MalaString s)
189 return s->refcnt>>2;
192 static inline int
193 mala_string_same (const_MalaString a, const_MalaString b)
195 return a == b;
198 static inline int
199 mala_string_check_prefix (const_MalaString self, const char* prefix)
201 return strncmp(self->str, prefix, strlen(prefix)) == 0;
204 static inline int
205 mala_string_check_prefix_nocase (const_MalaString self, const char* prefix)
207 return strncasecmp(self->str, prefix, strlen(prefix)) == 0;
211 static inline int
212 mala_string_compare (const_MalaString a, const_MalaString b)
214 return a == b ? 0 : strcmp (mala_string_cstr (a), mala_string_cstr (b));
218 static inline int
219 mala_string_compare_nocase (const_MalaString a, const_MalaString b)
221 return a == b ? 0 : strcasecmp (mala_string_cstr (a), mala_string_cstr (b));
225 static inline int
226 mala_string_compare_rev (const_MalaString a, const_MalaString b)
228 return mala_string_compare (b,a);
232 static inline int
233 mala_string_compare_n (const_MalaString a, const_MalaString b, const size_t n)
235 return a == b ? 0 : strncmp (mala_string_cstr (a), mala_string_cstr (b), n);
239 static inline int
240 mala_string_compare_n_nocase (const_MalaString a, const_MalaString b, const size_t n)
242 return a == b ? 0 : strncasecmp (mala_string_cstr (a), mala_string_cstr (b), n);
245 static inline MalaString
246 mala_string_copy (MalaString src)
248 src->refcnt += 4;
249 return src;
252 static inline int
253 mala_string_at (MalaString self, unsigned n)
255 MALA_ASSERT (mala_string_length (self) > n);
256 return self->str[n];
260 #endif /* MALA_STRINGS */
263 // Local Variables:
264 // mode: C
265 // c-file-style: "gnu"
266 // End:
267 // arch-tag: b43b7350-ecb6-4aa8-94d3-930da822e3ea
268 // end_of_file