Wapcaplet
Loading...
Searching...
No Matches
libwapcaplet.h
Go to the documentation of this file.
1/* libwapcaplet.h
2 *
3 * String internment and management tools.
4 *
5 * Copyright 2009 The NetSurf Browser Project.
6 * Daniel Silverstone <dsilvers@netsurf-browser.org>
7 */
8
9#ifndef libwapcaplet_h_
10#define libwapcaplet_h_
11
12#ifdef __cplusplus
13extern "C"
14{
15#endif
16
17#include <sys/types.h>
18#include <stdbool.h>
19#include <stddef.h>
20#include <stdint.h>
21#include <assert.h>
22
26typedef uint32_t lwc_refcounter;
27
31typedef uint32_t lwc_hash;
32
49
56typedef void (*lwc_iteration_callback_fn)(lwc_string *str, void *pw);
57
66
90extern lwc_error lwc_intern_string(const char *s, size_t slen,
91 lwc_string **ret);
92
106 size_t ssoffset, size_t sslen,
107 lwc_string **ret);
108
118
136#if defined(STMTEXPR)
137#define lwc_string_ref(str) ({lwc_string *__lwc_s = (str); assert(__lwc_s != NULL); __lwc_s->refcnt++; __lwc_s;})
138#else
139static inline lwc_string *
140lwc_string_ref(lwc_string *str)
141{
142 assert(str != NULL);
143 str->refcnt++;
144 return str;
145}
146#endif
147
159#define lwc_string_unref(str) { \
160 lwc_string *__lwc_s = (str); \
161 if (__lwc_s != NULL) { \
162 __lwc_s->refcnt--; \
163 if ((__lwc_s->refcnt == 0) || \
164 ((__lwc_s->refcnt == 1) && \
165 (__lwc_s->insensitive == __lwc_s))) \
166 lwc_string_destroy(__lwc_s); \
167 } \
168 }
169
177extern void lwc_string_destroy(lwc_string *str);
178
188#define lwc_string_isequal(str1, str2, ret) \
189 ((*(ret) = ((str1) == (str2))), lwc_error_ok)
190
202extern lwc_error
204
205#if defined(STMTEXPR)
215#define lwc_string_caseless_isequal(_str1,_str2,_ret) ({ \
216 lwc_error __lwc_err = lwc_error_ok; \
217 lwc_string *__lwc_str1 = (_str1); \
218 lwc_string *__lwc_str2 = (_str2); \
219 bool *__lwc_ret = (_ret); \
220 \
221 if (__lwc_str1->insensitive == NULL) { \
222 __lwc_err = lwc__intern_caseless_string(__lwc_str1); \
223 } \
224 if (__lwc_err == lwc_error_ok && __lwc_str2->insensitive == NULL) { \
225 __lwc_err = lwc__intern_caseless_string(__lwc_str2); \
226 } \
227 if (__lwc_err == lwc_error_ok) \
228 *__lwc_ret = (__lwc_str1->insensitive == __lwc_str2->insensitive); \
229 __lwc_err; \
230 })
231
232#else
242static inline lwc_error
243lwc_string_caseless_isequal(lwc_string *str1, lwc_string *str2, bool *ret)
244{
246 if (str1->insensitive == NULL) {
247 err = lwc__intern_caseless_string(str1);
248 }
249 if (err == lwc_error_ok && str2->insensitive == NULL) {
250 err = lwc__intern_caseless_string(str2);
251 }
252 if (err == lwc_error_ok)
253 *ret = (str1->insensitive == str2->insensitive);
254 return err;
255}
256#endif
257
258#if defined(STMTEXPR)
259#define lwc__assert_and_expr(str, expr) ({assert(str != NULL); expr;})
260#else
261#define lwc__assert_and_expr(str, expr) (expr)
262#endif
263
276#define lwc_string_data(str) lwc__assert_and_expr(str, (const char *)((str)+1))
277
284#define lwc_string_length(str) lwc__assert_and_expr(str, (str)->len)
285
298#define lwc_string_hash_value(str) lwc__assert_and_expr(str, (str)->hash)
299
308static inline lwc_error lwc_string_caseless_hash_value(
309 lwc_string *str, lwc_hash *hash)
310{
311 if (str->insensitive == NULL) {
313 if (err != lwc_error_ok) {
314 return err;
315 }
316 }
317
318 *hash = str->insensitive->hash;
319 return lwc_error_ok;
320}
321
322
333extern void lwc_iterate_strings(lwc_iteration_callback_fn cb, void *pw);
334
335#ifdef __cplusplus
336}
337#endif
338
339#endif /* libwapcaplet_h_ */
lwc_error lwc_string_tolower(lwc_string *str, lwc_string **ret)
lwc_error lwc_intern_substring(lwc_string *str, size_t ssoffset, size_t sslen, lwc_string **ret)
lwc_error lwc_intern_string(const char *s, size_t slen, lwc_string **ret)
uint32_t lwc_hash
struct lwc_string_s lwc_string
enum lwc_error_e lwc_error
void(* lwc_iteration_callback_fn)(lwc_string *str, void *pw)
void lwc_string_destroy(lwc_string *str)
void lwc_iterate_strings(lwc_iteration_callback_fn cb, void *pw)
lwc_error_e
@ lwc_error_ok
@ lwc_error_oom
@ lwc_error_range
uint32_t lwc_refcounter
lwc_error lwc__intern_caseless_string(lwc_string *str)
struct lwc_string_s * next
lwc_refcounter refcnt
struct lwc_string_s * insensitive
lwc_hash hash
struct lwc_string_s ** prevptr