3
A brief file description
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
24
/****************************************************************************
28
C++ support for string manipulation.
31
****************************************************************************/
33
#if !defined (_ink_string_pp_h_)
34
#define _ink_string_pp_h_
38
//////////////////////////////////////////////////////////////////////////////
42
//////////////////////////////////////////////////////////////////////////////
45
_memcpy(char *dest, const char *src, int nbytes)
47
for (int i = 0; i < nbytes; i++)
52
//////////////////////////////////////////////////////////////////////////////
56
//////////////////////////////////////////////////////////////////////////////
59
_strlen(const char *src)
61
const char *old_src = src;
64
return (int) (src - old_src);
67
void *ink_memchr(const void *as, int ac, size_t an);
69
/***********************************************************************
71
* Str (string/length list cell) *
73
***********************************************************************/
77
const char *str; // string pointer
78
size_t len; // length of string (not counting NUL)
79
struct Str *next; // next in list
80
struct Str *prev; // prev in list
82
Str():str(NULL), len(0), next(NULL), prev(NULL)
108
void dump(FILE * fp = stderr) {
109
fprintf(fp, "Str [\"%.*s\", len %d]\n", (int) len, str, (int) len);
113
/***********************************************************************
115
* StrList (doubly-linked list of string/length list cells) *
117
***********************************************************************/
119
#define STRLIST_BASE_HEAP_SIZE 128
120
#define STRLIST_OVERFLOW_HEAP_SIZE 1024
121
#define STRLIST_BASE_CELLS 5
123
struct StrListOverflow;
133
StrList(bool do_copy_when_adding_string = true);
137
void append(Str * str);
138
void prepend(Str * str);
139
void add_after(Str * prev, Str * str);
140
void detach(Str * str);
142
Str *new_cell(const char *s, int len_not_counting_nul);
143
Str *append_string(const char *s, int len_not_counting_nul);
145
void dump(FILE * fp = stderr);
151
void *base_heap_alloc(int size);
152
void *alloc(int size);
153
Str *_new_cell(const char *s, int len_not_counting_nul);
154
void *overflow_heap_alloc(int size);
155
void overflow_heap_clean();
157
Str base_cells[STRLIST_BASE_CELLS];
158
char base_heap[STRLIST_BASE_HEAP_SIZE];
162
StrListOverflow *overflow_current;
163
StrListOverflow *overflow_first;
164
bool copy_when_adding_string;
167
struct StrListOverflow
169
StrListOverflow *next;
175
void *alloc(int size, StrListOverflow ** new_heap_ptr);
176
static StrListOverflow *create_heap(int user_size);
185
base_heap_size = STRLIST_BASE_HEAP_SIZE;
187
overflow_first = NULL;
188
overflow_current = NULL;
195
overflow_heap_clean();
200
StrList::StrList(bool do_copy_when_adding_string)
202
memset(base_heap, 0, sizeof(base_heap));
203
copy_when_adding_string = do_copy_when_adding_string;
215
StrList::base_heap_alloc(int size)
219
if (size <= (base_heap_size - base_heap_used)) {
220
p = &(base_heap[base_heap_used]);
221
base_heap_used += size;
228
StrList::alloc(int size)
230
void *p = base_heap_alloc(size);
232
p = overflow_heap_alloc(size);
237
StrList::new_cell(const char *s, int len_not_counting_nul)
240
int l = len_not_counting_nul;
242
// allocate a cell from the array or heap
243
if ((cells_allocated < STRLIST_BASE_CELLS) && (!copy_when_adding_string)) {
244
cell = &(base_cells[cells_allocated++]);
249
return (_new_cell(s, len_not_counting_nul));
254
StrList::get_idx(int i)
258
for (s = head; ((s != NULL) && i); s = s->next, i--);
259
return ((i == 0) ? s : NULL);
263
StrList::append(Str * str)
265
// do nothing if str is NULL to avoid pointer chasing below
281
StrList::prepend(Str * str)
298
StrList::add_after(Str * prev, Str * str)
300
if (str == NULL || prev == NULL)
303
str->next = prev->next;
311
StrList::detach(Str * str)
322
str->prev->next = str->next;
324
str->next->prev = str->prev;
328
StrList::append_string(const char *s, int len_not_counting_nul)
332
cell = new_cell(s, len_not_counting_nul);