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
27 #include "mala_types.h"
29 #include "stringbucket.h"
32 struct mala_string_struct
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
;
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}
54 mala_string_new_cstr_attach (char* cstr
, MalaStringBucket bucket
);
57 mala_string_new (const char* cstr
, MalaStringBucket bucket
);
60 mala_string_new_n (const char* cstr
, const size_t n
, MalaStringBucket bucket
);
63 mala_string_new_cat2 (const char* cstr1
, const char* cstr2
, MalaStringBucket bucket
);
66 mala_string_find (const char* cstr
, MalaStringBucket bucket
);
69 mala_string_copy_n (MalaString src
, const size_t n
);
72 mala_string_new_prefix (const char* prefix
, MalaString str
);
75 //mala_string_cat_n (MalaString_ref dest, MalaString src, size_t n);
77 //mala_string_cat_nn (MalaString_ref dest, size_t n, MalaString src, size_t n);
80 mala_string_new_print (MalaStringBucket bucket
, const char* fmt
,...);
83 mala_string_scan (const_MalaString str
,const char* fmt
,...);
86 mala_string_free (MalaString s
);
89 mala_string_factory (MalaString_ref dest
, const char* src
, MalaStringBucket bucket
);
92 mala_string_data_free (MalaString s
);
95 mala_string_char_find (MalaString self
, const char c
);
98 mala_string_new_substr (MalaString self
, size_t start
, size_t end
);
103 mala_string_user_get (const_MalaString s
);
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
);
115 mala_string_length (MalaString s
);
118 mala_string_references (const_MalaString s
);
121 mala_string_same (const_MalaString a
, const_MalaString b
);
124 mala_string_check_prefix (const_MalaString self
, const char* prefix
);
127 mala_string_check_prefix_nocase (const_MalaString self
, const char* prefix
);
130 mala_string_compare (const_MalaString a
, const_MalaString b
);
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);
138 mala_string_compare_n (const_MalaString a
, const_MalaString b
, const size_t n
);
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
);
147 mala_string_at (MalaString self
, unsigned n
);
155 mala_string_user_get (const_MalaString s
)
157 return (s
&& s
->bucket
) ? s
->user
: NULL
;
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
)
175 static inline const char*
176 mala_string_cstr (const_MalaString s
)
183 mala_string_length (MalaString s
)
185 return s
->len
!= SIZE_MAX
? s
->len
: s
->len
= strlen (s
->str
);
190 mala_string_references (const_MalaString s
)
196 mala_string_same (const_MalaString a
, const_MalaString b
)
202 mala_string_check_prefix (const_MalaString self
, const char* prefix
)
204 return strncmp(self
->str
, prefix
, strlen(prefix
)) == 0;
208 mala_string_check_prefix_nocase (const_MalaString self
, const char* prefix
)
210 return strncasecmp(self
->str
, prefix
, strlen(prefix
)) == 0;
215 mala_string_compare (const_MalaString a
, const_MalaString b
)
217 return a
== b
? 0 : strcmp (mala_string_cstr (a
), mala_string_cstr (b
));
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
));
229 mala_string_compare_rev (const_MalaString a, const_MalaString b)
231 return mala_string_compare (b,a);
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
);
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
)
256 mala_string_at (MalaString self
, unsigned n
)
258 MALA_ASSERT (mala_string_length (self
) > n
);
263 #endif /* MALA_STRINGS */
268 // c-file-style: "gnu"
270 // arch-tag: b43b7350-ecb6-4aa8-94d3-930da822e3ea