2 Copyright (c) 2008-2017, Troy D. Hanson http://troydhanson.github.com/uthash/
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
15 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 /* a dynamic string implementation using macros
29 #define UTSTRING_VERSION 2.0.2
32 #define _UNUSED_ __attribute__ ((__unused__))
43 #define oom() exit(-1)
48 size_t n
; /* allocd size */
49 size_t i
; /* index of first unused byte */
52 #define utstring_reserve(s,amt) \
54 if (((s)->n - (s)->i) < (size_t)(amt)) { \
55 char *utstring_tmp = (char*)realloc( \
56 (s)->d, (s)->n + (amt)); \
57 if (utstring_tmp == NULL) oom(); \
58 (s)->d = utstring_tmp; \
63 #define utstring_init(s) \
65 (s)->n = 0; (s)->i = 0; (s)->d = NULL; \
66 utstring_reserve(s,100); \
70 #define utstring_done(s) \
72 if ((s)->d != NULL) free((s)->d); \
76 #define utstring_free(s) \
82 #define utstring_new(s) \
84 s = (UT_string*)calloc(sizeof(UT_string),1); \
89 #define utstring_renew(s) \
98 #define utstring_clear(s) \
104 #define utstring_bincpy(s,b,l) \
106 utstring_reserve((s),(l)+1); \
107 if (l) memcpy(&(s)->d[(s)->i], b, l); \
109 (s)->d[(s)->i]='\0'; \
112 #define utstring_concat(dst,src) \
114 utstring_reserve((dst),((src)->i)+1); \
115 if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \
116 (dst)->i += (src)->i; \
117 (dst)->d[(dst)->i]='\0'; \
120 #define utstring_len(s) ((s)->i)
122 #define utstring_body(s) ((s)->d)
124 _UNUSED_
static void utstring_printf_va(UT_string
*s
, const char *fmt
, va_list ap
) {
133 n
= vsnprintf (&s
->d
[s
->i
], s
->n
-s
->i
, fmt
, cp
);
136 if ((n
> -1) && ((size_t) n
< (s
->n
-s
->i
))) {
141 /* Else try again with more space. */
142 if (n
> -1) utstring_reserve(s
,n
+1); /* exact */
143 else utstring_reserve(s
,(s
->n
)*2); /* 2x */
147 /* support printf format checking (2=the format string, 3=start of varargs) */
148 static void utstring_printf(UT_string
*s
, const char *fmt
, ...)
149 __attribute__ (( format( printf
, 2, 3) ));
151 _UNUSED_
static void utstring_printf(UT_string
*s
, const char *fmt
, ...) {
154 utstring_printf_va(s
,fmt
,ap
);
158 /*******************************************************************************
159 * begin substring search functions *
160 ******************************************************************************/
161 /* Build KMP table from left to right. */
162 _UNUSED_
static void _utstring_BuildTable(
163 const char *P_Needle
,
172 while (i
< (long) P_NeedleLen
)
174 while ( (j
> -1) && (P_Needle
[i
] != P_Needle
[j
]) )
180 if (i
< (long) P_NeedleLen
)
182 if (P_Needle
[i
] == P_Needle
[j
])
184 P_KMP_Table
[i
] = P_KMP_Table
[j
];
201 /* Build KMP table from right to left. */
202 _UNUSED_
static void _utstring_BuildTableR(
203 const char *P_Needle
,
211 P_KMP_Table
[i
+ 1] = j
;
214 while ( (j
< (long) P_NeedleLen
) && (P_Needle
[i
] != P_Needle
[j
]) )
216 j
= P_KMP_Table
[j
+ 1];
222 if (P_Needle
[i
] == P_Needle
[j
])
224 P_KMP_Table
[i
+ 1] = P_KMP_Table
[j
+ 1];
228 P_KMP_Table
[i
+ 1] = j
;
233 P_KMP_Table
[i
+ 1] = j
;
241 /* Search data from left to right. ( Multiple search mode. ) */
242 _UNUSED_
static long _utstring_find(
243 const char *P_Haystack
,
244 size_t P_HaystackLen
,
245 const char *P_Needle
,
250 long V_FindPosition
= -1;
252 /* Search from left to right. */
254 while ( (j
< (int)P_HaystackLen
) && (((P_HaystackLen
- j
) + i
) >= P_NeedleLen
) )
256 while ( (i
> -1) && (P_Needle
[i
] != P_Haystack
[j
]) )
262 if (i
>= (int)P_NeedleLen
)
265 V_FindPosition
= j
- i
;
270 return V_FindPosition
;
274 /* Search data from right to left. ( Multiple search mode. ) */
275 _UNUSED_
static long _utstring_findR(
276 const char *P_Haystack
,
277 size_t P_HaystackLen
,
278 const char *P_Needle
,
283 long V_FindPosition
= -1;
285 /* Search from right to left. */
286 j
= (P_HaystackLen
- 1);
287 i
= (P_NeedleLen
- 1);
288 while ( (j
>= 0) && (j
>= i
) )
290 while ( (i
< (int)P_NeedleLen
) && (P_Needle
[i
] != P_Haystack
[j
]) )
292 i
= P_KMP_Table
[i
+ 1];
299 V_FindPosition
= j
+ 1;
304 return V_FindPosition
;
308 /* Search data from left to right. ( One time search mode. ) */
309 _UNUSED_
static long utstring_find(
311 long P_StartPosition
, /* Start from 0. -1 means last position. */
312 const char *P_Needle
,
315 long V_StartPosition
;
318 long V_FindPosition
= -1;
320 if (P_StartPosition
< 0)
322 V_StartPosition
= s
->i
+ P_StartPosition
;
326 V_StartPosition
= P_StartPosition
;
328 V_HaystackLen
= s
->i
- V_StartPosition
;
329 if ( (V_HaystackLen
>= (long) P_NeedleLen
) && (P_NeedleLen
> 0) )
331 V_KMP_Table
= (long *)malloc(sizeof(long) * (P_NeedleLen
+ 1));
332 if (V_KMP_Table
!= NULL
)
334 _utstring_BuildTable(P_Needle
, P_NeedleLen
, V_KMP_Table
);
336 V_FindPosition
= _utstring_find(s
->d
+ V_StartPosition
,
341 if (V_FindPosition
>= 0)
343 V_FindPosition
+= V_StartPosition
;
350 return V_FindPosition
;
354 /* Search data from right to left. ( One time search mode. ) */
355 _UNUSED_
static long utstring_findR(
357 long P_StartPosition
, /* Start from 0. -1 means last position. */
358 const char *P_Needle
,
361 long V_StartPosition
;
364 long V_FindPosition
= -1;
366 if (P_StartPosition
< 0)
368 V_StartPosition
= s
->i
+ P_StartPosition
;
372 V_StartPosition
= P_StartPosition
;
374 V_HaystackLen
= V_StartPosition
+ 1;
375 if ( (V_HaystackLen
>= (long) P_NeedleLen
) && (P_NeedleLen
> 0) )
377 V_KMP_Table
= (long *)malloc(sizeof(long) * (P_NeedleLen
+ 1));
378 if (V_KMP_Table
!= NULL
)
380 _utstring_BuildTableR(P_Needle
, P_NeedleLen
, V_KMP_Table
);
382 V_FindPosition
= _utstring_findR(s
->d
,
392 return V_FindPosition
;
394 /*******************************************************************************
395 * end substring search functions *
396 ******************************************************************************/
398 #endif /* UTSTRING_H */