use --WORLD as root of all actions (GC just deletes --WORLD)
[mala.git] / engine / strings.h
blob37cdf98c348c488f437b6988c05e56168eda625e
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 void
89 mala_string_free_forced (MalaString s);
91 MalaString
92 mala_string_factory (MalaString_ref dest, const char* src, MalaStringBucket bucket);
94 void
95 mala_string_data_free (MalaString s);
97 size_t
98 mala_string_char_find (MalaString self, const char c);
100 MalaString
101 mala_string_new_substr (MalaString self, size_t start, size_t end);
104 /*statics*/
105 static inline void*
106 mala_string_user_get (const_MalaString s);
108 static inline void*
109 mala_string_user_set (MalaString s, void* data);
111 static inline MalaStringBucket
112 mala_string_bucket_get (const_MalaString s);
114 static inline const char*
115 mala_string_cstr (const_MalaString s);
117 static inline size_t
118 mala_string_length (MalaString s);
120 static inline size_t
121 mala_string_references (const_MalaString s);
123 static inline int
124 mala_string_same (const_MalaString a, const_MalaString b);
126 static inline int
127 mala_string_check_prefix (const_MalaString self, const char* prefix);
129 static inline int
130 mala_string_check_prefix_nocase (const_MalaString self, const char* prefix);
132 static inline int
133 mala_string_compare (const_MalaString a, const_MalaString b);
135 static inline int
136 mala_string_compare_nocase (const_MalaString a, const_MalaString b);
138 //static inline int mala_string_compare_rev (const_MalaString a, const_MalaString b);
140 static inline int
141 mala_string_compare_n (const_MalaString a, const_MalaString b, const size_t n);
143 static inline int
144 mala_string_compare_n_nocase (const_MalaString a, const_MalaString b, size_t n);
146 static inline MalaString
147 mala_string_copy (MalaString src);
149 static inline int
150 mala_string_at (MalaString self, unsigned n);
154 * Inlines
157 static inline void*
158 mala_string_user_get (const_MalaString s)
160 return (s && s->bucket) ? s->user : NULL;
164 static inline void*
165 mala_string_user_set (MalaString s, void* data)
167 return s->user = s->bucket ? data : NULL;
171 static inline MalaStringBucket
172 mala_string_bucket_get (const_MalaString s)
174 return s->bucket;
178 static inline const char*
179 mala_string_cstr (const_MalaString s)
181 return s->str;
185 static inline size_t
186 mala_string_length (MalaString s)
188 return s->len != SIZE_MAX ? s->len : (s->len = strlen (s->str));
192 static inline size_t
193 mala_string_references (const_MalaString s)
195 return s->refcnt>>2;
198 static inline int
199 mala_string_same (const_MalaString a, const_MalaString b)
201 return a == b;
204 static inline int
205 mala_string_check_prefix (const_MalaString self, const char* prefix)
207 return strncmp(self->str, prefix, strlen(prefix)) == 0;
210 static inline int
211 mala_string_check_prefix_nocase (const_MalaString self, const char* prefix)
213 return strncasecmp(self->str, prefix, strlen(prefix)) == 0;
217 static inline int
218 mala_string_compare (const_MalaString a, const_MalaString b)
220 return a == b ? 0 : strcmp (mala_string_cstr (a), mala_string_cstr (b));
224 static inline int
225 mala_string_compare_nocase (const_MalaString a, const_MalaString b)
227 return a == b ? 0 : strcasecmp (mala_string_cstr (a), mala_string_cstr (b));
231 static inline int
232 mala_string_compare_rev (const_MalaString a, const_MalaString b)
234 return mala_string_compare (b,a);
238 static inline int
239 mala_string_compare_n (const_MalaString a, const_MalaString b, const size_t n)
241 return a == b ? 0 : strncmp (mala_string_cstr (a), mala_string_cstr (b), n);
245 static inline int
246 mala_string_compare_n_nocase (const_MalaString a, const_MalaString b, const size_t n)
248 return a == b ? 0 : strncasecmp (mala_string_cstr (a), mala_string_cstr (b), n);
251 static inline MalaString
252 mala_string_copy (MalaString src)
254 src->refcnt += 4;
255 return src;
258 static inline int
259 mala_string_at (MalaString self, unsigned n)
261 MALA_ASSERT (mala_string_length (self) > n);
262 return self->str[n];
266 #endif /* MALA_STRINGS */
269 // Local Variables:
270 // mode: C
271 // c-file-style: "gnu"
272 // End:
273 // arch-tag: b43b7350-ecb6-4aa8-94d3-930da822e3ea
274 // end_of_file