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_free_forced (MalaString s
);
92 mala_string_factory (MalaString_ref dest
, const char* src
, MalaStringBucket bucket
);
95 mala_string_data_free (MalaString s
);
98 mala_string_char_find (MalaString self
, const char c
);
101 mala_string_new_substr (MalaString self
, size_t start
, size_t end
);
106 mala_string_user_get (const_MalaString s
);
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
);
118 mala_string_length (MalaString s
);
121 mala_string_references (const_MalaString s
);
124 mala_string_same (const_MalaString a
, const_MalaString b
);
127 mala_string_check_prefix (const_MalaString self
, const char* prefix
);
130 mala_string_check_prefix_nocase (const_MalaString self
, const char* prefix
);
133 mala_string_compare (const_MalaString a
, const_MalaString b
);
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);
141 mala_string_compare_n (const_MalaString a
, const_MalaString b
, const size_t n
);
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
);
150 mala_string_at (MalaString self
, unsigned n
);
158 mala_string_user_get (const_MalaString s
)
160 return (s
&& s
->bucket
) ? s
->user
: NULL
;
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
)
178 static inline const char*
179 mala_string_cstr (const_MalaString s
)
186 mala_string_length (MalaString s
)
188 return s
->len
!= SIZE_MAX
? s
->len
: (s
->len
= strlen (s
->str
));
193 mala_string_references (const_MalaString s
)
199 mala_string_same (const_MalaString a
, const_MalaString b
)
205 mala_string_check_prefix (const_MalaString self
, const char* prefix
)
207 return strncmp(self
->str
, prefix
, strlen(prefix
)) == 0;
211 mala_string_check_prefix_nocase (const_MalaString self
, const char* prefix
)
213 return strncasecmp(self
->str
, prefix
, strlen(prefix
)) == 0;
218 mala_string_compare (const_MalaString a
, const_MalaString b
)
220 return a
== b
? 0 : strcmp (mala_string_cstr (a
), mala_string_cstr (b
));
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
));
232 mala_string_compare_rev (const_MalaString a, const_MalaString b)
234 return mala_string_compare (b,a);
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
);
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
)
259 mala_string_at (MalaString self
, unsigned n
)
261 MALA_ASSERT (mala_string_length (self
) > n
);
266 #endif /* MALA_STRINGS */
271 // c-file-style: "gnu"
273 // arch-tag: b43b7350-ecb6-4aa8-94d3-930da822e3ea