File: ink_string%2B%2B.h

package info (click to toggle)
trafficserver 6.2.0-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 45,456 kB
  • sloc: cpp: 271,894; ansic: 80,740; sh: 6,032; makefile: 3,364; python: 2,135; perl: 2,040; java: 277; lex: 128; sql: 94; yacc: 68; sed: 8
file content (302 lines) | stat: -rw-r--r-- 6,482 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

/****************************************************************************

  ink_string++.h

  C++ support for string manipulation.


 ****************************************************************************/

#if !defined(_ink_string_pp_h_)
#define _ink_string_pp_h_
#include <stdio.h>
#include <strings.h>

/***********************************************************************
 *                                                                     *
 *                     Str (string/length list cell)                   *
 *                                                                     *
 ***********************************************************************/

struct Str {
  const char *str;  // string pointer
  size_t len;       // length of string (not counting NUL)
  struct Str *next; // next in list
  struct Str *prev; // prev in list

  Str() : str(NULL), len(0), next(NULL), prev(NULL) {}
  Str(char *s)
  {
    str  = s;
    len  = strlen(s);
    next = NULL;
    prev = NULL;
  }
  Str(char *s, int l)
  {
    str  = s;
    len  = l;
    next = NULL;
    prev = NULL;
  }

  void
  clean()
  {
    str  = NULL;
    len  = 0;
    next = NULL;
    prev = NULL;
  }

  void
  dump(FILE *fp = stderr)
  {
    fprintf(fp, "Str [\"%.*s\", len %d]\n", (int)len, str, (int)len);
  }
};

/***********************************************************************
 *                                                                     *
 *       StrList (doubly-linked list of string/length list cells)      *
 *                                                                     *
 ***********************************************************************/

#define STRLIST_BASE_HEAP_SIZE 128
#define STRLIST_OVERFLOW_HEAP_SIZE 1024
#define STRLIST_BASE_CELLS 5

struct StrListOverflow;

struct StrList {
public:
  int count;
  Str *head;
  Str *tail;

public:
  StrList(bool do_copy_when_adding_string = true);
  ~StrList();

  Str *get_idx(int i);
  void append(Str *str);
  void prepend(Str *str);
  void add_after(Str *prev, Str *str);
  void detach(Str *str);

  Str *new_cell(const char *s, int len_not_counting_nul);
  Str *append_string(const char *s, int len_not_counting_nul);

  void dump(FILE *fp = stderr);

private:
  void init();
  void clean();

  void *base_heap_alloc(int size);
  void *alloc(int size);
  Str *_new_cell(const char *s, int len_not_counting_nul);
  void *overflow_heap_alloc(int size);
  void overflow_heap_clean();

  Str base_cells[STRLIST_BASE_CELLS];
  char base_heap[STRLIST_BASE_HEAP_SIZE];
  int cells_allocated;
  int base_heap_size;
  int base_heap_used;
  StrListOverflow *overflow_current;
  StrListOverflow *overflow_first;
  bool copy_when_adding_string;
};

struct StrListOverflow {
  StrListOverflow *next;
  int heap_size;
  int heap_used;

  void init();
  void clean();
  void *alloc(int size, StrListOverflow **new_heap_ptr);
  static StrListOverflow *create_heap(int user_size);
};

inline void
StrList::init()
{
  count           = 0;
  cells_allocated = 0;
  head = tail      = NULL;
  base_heap_size   = STRLIST_BASE_HEAP_SIZE;
  base_heap_used   = 0;
  overflow_first   = NULL;
  overflow_current = NULL;
}

inline void
StrList::clean()
{
  if (overflow_first)
    overflow_heap_clean();
  init();
}

inline StrList::StrList(bool do_copy_when_adding_string)
{
  memset(base_heap, 0, sizeof(base_heap));
  copy_when_adding_string = do_copy_when_adding_string;
  init();
}

inline StrList::~StrList()
{
  clean();
}

inline void *
StrList::base_heap_alloc(int size)
{
  char *p;

  if (size <= (base_heap_size - base_heap_used)) {
    p = &(base_heap[base_heap_used]);
    base_heap_used += size;
    return ((void *)p);
  } else
    return (NULL);
}

inline void *
StrList::alloc(int size)
{
  void *p = base_heap_alloc(size);
  if (p == NULL)
    p = overflow_heap_alloc(size);
  return (p);
}

inline Str *
StrList::new_cell(const char *s, int len_not_counting_nul)
{
  Str *cell;
  int l = len_not_counting_nul;

  // allocate a cell from the array or heap
  if ((cells_allocated < STRLIST_BASE_CELLS) && (!copy_when_adding_string)) {
    cell      = &(base_cells[cells_allocated++]);
    cell->str = s;
    cell->len = l;
    return (cell);
  } else {
    return (_new_cell(s, len_not_counting_nul));
  }
}

inline Str *
StrList::get_idx(int i)
{
  Str *s;

  for (s = head; ((s != NULL) && i); s = s->next, i--)
    ;
  return ((i == 0) ? s : NULL);
}

inline void
StrList::append(Str *str)
{
  // do nothing if str is NULL to avoid pointer chasing below
  if (str == NULL)
    return;
  ++count;
  str->next = NULL;
  str->prev = tail;

  if (tail == NULL) {
    head = tail = str;
  } else {
    tail->next = str;
    tail       = str;
  }
}

inline void
StrList::prepend(Str *str)
{
  if (str == NULL)
    return;
  ++count;
  str->next = head;
  str->prev = NULL;

  if (tail == NULL) {
    head = tail = str;
  } else {
    head->prev = str;
    head       = str;
  }
}

inline void
StrList::add_after(Str *prev, Str *str)
{
  if (str == NULL || prev == NULL)
    return;
  ++count;
  str->next  = prev->next;
  str->prev  = prev;
  prev->next = str;
  if (tail == prev)
    tail = str;
}

inline void
StrList::detach(Str *str)
{
  if (str == NULL)
    return;
  --count;

  if (head == str)
    head = str->next;
  if (tail == str)
    tail = str->prev;
  if (str->prev)
    str->prev->next = str->next;
  if (str->next)
    str->next->prev = str->prev;
}

inline Str *
StrList::append_string(const char *s, int len_not_counting_nul)
{
  Str *cell;

  cell = new_cell(s, len_not_counting_nul);
  append(cell);
  return (cell);
}

#endif