Debian Codegraph @

Xpjproject/pjlib/include/pj++/string.hpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #ifndef __PJPP_STRING_HPP__
20 #define __PJPP_STRING_HPP__
21
22 #include <pj/string.h>
23 #include <pj++/pool.hpp>
24 #include <pj/assert.h>
25
26 //
27 // String wrapper class for pj_str_t.
28 //
29 class Pj_String : public pj_str_t
30 {
31 public:
32 //
33 // Default constructor.
34 //
35 Pj_String()
36 {
37 pj_assert(sizeof(Pj_String) == sizeof(pj_str_t));
38 ptr=NULL;
39 slen=0;
40 }
41
42 //
43 // Construct the buffer from a char* (use with care)
44 //
45 Pj_String(char *str)
46 {
47 set(str);
48 }
49
50 //
51 // Construct from a const char*.
52 //
53 Pj_String(Pj_Pool &pool, const char *src)
54 {
55 set(pool, src);
56 }
57
58 //
59 // Construct from pj_str_t&.
60 //
61 explicit Pj_String(pj_str_t &s)
62 {
63 ptr = s.ptr;
64 slen = s.slen;
65 }
66
67 //
68 // Construct from const pj_str_t& (use with care!).
69 //
70 explicit Pj_String(const pj_str_t &s)
71 {
72 ptr = (char*)s.ptr;
73 slen = s.slen;
74 }
75
76 //
77 // Construct by copying from const pj_str_t*.
78 //
79 Pj_String(Pj_Pool &pool, const pj_str_t *s)
80 {
81 set(pool, s);
82 }
83
84 //
85 // Construct by copying from Pj_String
86 //
87 Pj_String(Pj_Pool &pool, const Pj_String &rhs)
88 {
89 set(pool, rhs);
90 }
91
92 //
93 // Construct from another Pj_String, use with care!
94 //
95 explicit Pj_String(const Pj_String &rhs)
96 {
97 ptr = rhs.ptr;
98 slen = rhs.slen;
99 }
100
101 //
102 // Construct from a char* and a length.
103 //
104 Pj_String(char *str, pj_size_t len)
105 {
106 set(str, len);
107 }
108
109 //
110 // Construct from pair of pointer.
111 //
112 Pj_String(char *begin, char *end)
113 {
114 pj_strset3(this, begin, end);
115 }
116
117 //
118 // You can cast Pj_String to pj_str_t*
119 //
120 operator pj_str_t*()
121 {
122 return this;
123 }
124
125 //
126 // You can cast const Pj_String to const pj_str_t*
127 //
128 operator const pj_str_t*() const
129 {
130 return this;
131 }
132
133 //
134 // Get the length of the string.
135 //
136 pj_size_t length() const
137 {
138 return pj_strlen(this);
139 }
140
141 //
142 // Get the length of the string.
143 //
144 pj_size_t size() const
145 {
146 return length();
147 }
148
149 //
150 // Get the string buffer.
151 //
152 const char *buf() const
153 {
154 return ptr;
155 }
156
157 //
158 // Initialize buffer from char*.
159 //
160 void set(char *str)
161 {
162 pj_strset2(this, str);
163 }
164
165 //
166 // Initialize by copying from a const char*.
167 //
168 void set(Pj_Pool &pool, const char *s)
169 {
170 pj_strdup2(pool, this, s);
171 }
172
173 //
174 // Initialize from pj_str_t*.
175 //
176 void set(pj_str_t *s)
177 {
178 pj_strassign(this, s);
179 }
180
181 //
182 // Initialize by copying from const pj_str_t*.
183 //
184 void set(Pj_Pool &pool, const pj_str_t *s)
185 {
186 pj_strdup(pool, this, s);
187 }
188
189 //
190 // Initialize from char* and length.
191 //
192 void set(char *str, pj_size_t len)
193 {
194 pj_strset(this, str, len);
195 }
196
197 //
198 // Initialize from pair of pointers.
199 //
200 void set(char *begin, char *end)
201 {
202 pj_strset3(this, begin, end);
203 }
204
205 //
206 // Initialize from other Pj_String.
207 //
208 void set(Pj_String &rhs)
209 {
210 pj_strassign(this, &rhs);
211 }
212
213 //
214 // Initialize by copying from a Pj_String*.
215 //
216 void set(Pj_Pool &pool, const Pj_String *s)
217 {
218 pj_strdup(pool, this, s);
219 }
220
221 //
222 // Initialize by copying from other Pj_String.
223 //
224 void set(Pj_Pool &pool, const Pj_String &s)
225 {
226 pj_strdup(pool, this, &s);
227 }
228
229 //
230 // Copy the contents of other string.
231 //
232 void strcpy(const pj_str_t *s)
233 {
234 pj_strcpy(this, s);
235 }
236
237 //
238 // Copy the contents of other string.
239 //
240 void strcpy(const Pj_String &rhs)
241 {
242 pj_strcpy(this, &rhs);
243 }
244
245 //
246 // Copy the contents of other string.
247 //
248 void strcpy(const char *s)
249 {
250 pj_strcpy2(this, s);
251 }
252
253 //
254 // Compare string.
255 //
256 int strcmp(const char *s) const
257 {
258 return pj_strcmp2(this, s);
259 }
260
261 //
262 // Compare string.
263 //
264 int strcmp(const pj_str_t *s) const
265 {
266 return pj_strcmp(this, s);
267 }
268
269 //
270 // Compare string.
271 //
272 int strcmp(const Pj_String &rhs) const
273 {
274 return pj_strcmp(this, &rhs);
275 }
276
277 //
278 // Compare string.
279 //
280 int strncmp(const char *s, pj_size_t len) const
281 {
282 return pj_strncmp2(this, s, len);
283 }
284
285 //
286 // Compare string.
287 //
288 int strncmp(const pj_str_t *s, pj_size_t len) const
289 {
290 return pj_strncmp(this, s, len);
291 }
292
293 //
294 // Compare string.
295 //
296 int strncmp(const Pj_String &rhs, pj_size_t len) const
297 {
298 return pj_strncmp(this, &rhs, len);
299 }
300
301 //
302 // Compare string.
303 //
304 int stricmp(const char *s) const
305 {
306 return pj_stricmp2(this, s);
307 }
308
309 //
310 // Compare string.
311 //
312 int stricmp(const pj_str_t *s) const
313 {
314 return pj_stricmp(this, s);
315 }
316
317 //
318 // Compare string.
319 //
320 int stricmp(const Pj_String &rhs) const
321 {
322 return stricmp(&rhs);
323 }
324
325 //
326 // Compare string.
327 //
328 int strnicmp(const char *s, pj_size_t len) const
329 {
330 return pj_strnicmp2(this, s, len);
331 }
332
333 //
334 // Compare string.
335 //
336 int strnicmp(const pj_str_t *s, pj_size_t len) const
337 {
338 return pj_strnicmp(this, s, len);
339 }
340
341 //
342 // Compare string.
343 //
344 int strnicmp(const Pj_String &rhs, pj_size_t len) const
345 {
346 return strnicmp(&rhs, len);
347 }
348
349 //
350 // Compare contents for equality.
351 //
352 bool operator==(const char *s) const
353 {
354 return strcmp(s) == 0;
355 }
356
357 //
358 // Compare contents for equality.
359 //
360 bool operator==(const pj_str_t *s) const
361 {
362 return strcmp(s) == 0;
363 }
364
365 //
366 // Compare contents for equality.
367 //
368 bool operator==(const Pj_String &rhs) const
369 {
370 return pj_strcmp(this, &rhs) == 0;
371 }
372
373 //
374 // Assign from char*
375 //
376 Pj_String& operator=(char *s)
377 {
378 set(s);
379 return *this;
380 }
381
382 ///
383 // Assign from another Pj_String, use with care!
384 //
385 Pj_String& operator=(const Pj_String &rhs)
386 {
387 ptr = rhs.ptr;
388 slen = rhs.slen;
389 return *this;
390 }
391
392 //
393 // Find a character in the string.
394 //
395 char *strchr(int chr)
396 {
397 return pj_strchr(this, chr);
398 }
399
400 //
401 // Find a character in the string.
402 //
403 char *find(int chr)
404 {
405 return strchr(chr);
406 }
407
408 //
409 // Concatenate string.
410 //
411 void strcat(const Pj_String &rhs)
412 {
413 pj_strcat(this, &rhs);
414 }
415
416 //
417 // Left trim.
418 //
419 void ltrim()
420 {
421 pj_strltrim(this);
422 }
423
424 //
425 // Right trim.
426 //
427 void rtrim()
428 {
429 pj_strrtrim(this);
430 }
431
432 //
433 // Left and right trim.
434 //
435 void trim()
436 {
437 pj_strtrim(this);
438 }
439
440 //
441 // Convert to unsigned long.
442 //
443 unsigned long to_ulong() const
444 {
445 return pj_strtoul(this);
446 }
447
448 //
449 // Convert from unsigned long.
450 //
451 void from_ulong(unsigned long value)
452 {
453 slen = pj_utoa(value, ptr);
454 }
455
456 //
457 // Convert from unsigned long with padding.
458 //
459 void from_ulong_with_pad(unsigned long value, int min_dig=0, int pad=' ')
460 {
461 slen = pj_utoa_pad(value, ptr, min_dig, pad);
462 }
463
464 };
465
466 #endif /* __PJPP_STRING_HPP__ */
467