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