Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ 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 Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | /** |
| 19 | * A common string class and character string support functions. |
| 20 | * Ucommon offers a simple string class that operates through copy-on-write |
| 21 | * when needing to expand buffer size. Derived classes and templates allows |
| 22 | * one to create strings which live entirely in the stack frame rather |
| 23 | * than using the heap. This offers the benefit of the string class |
| 24 | * manipulative members without compromising performance or locking issues |
| 25 | * in threaded applications. Other things found here include better and |
| 26 | * safer char array manipulation functions. |
| 27 | * @file ucommon/string.h |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * An example of the string class. |
| 32 | * @example string.cpp |
| 33 | */ |
| 34 | |
| 35 | #ifndef _UCOMMON_STRING_H_ |
| 36 | #define _UCOMMON_STRING_H_ |
| 37 | |
| 38 | #ifndef _UCOMMON_CPR_H_ |
| 39 | #include <ucommon/cpr.h> |
| 40 | #endif |
| 41 | |
| 42 | #ifndef _UCOMMON_GENERICS_H_ |
| 43 | #include <ucommon/generics.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifndef _UCOMMON_PROTOCOLS_H_ |
| 47 | #include <ucommon/protocols.h> |
| 48 | #endif |
| 49 | |
| 50 | #ifndef _UCOMMON_OBJECT_H_ |
| 51 | #include <ucommon/object.h> |
| 52 | #endif |
| 53 | |
| 54 | #include <stdio.h> |
| 55 | #include <string.h> |
| 56 | #include <stdarg.h> |
| 57 | |
| 58 | #ifdef HAVE_DIRENT_H |
| 59 | #include <dirent.h> |
| 60 | #endif |
| 61 | |
| 62 | #define PGP_B64_WIDTH 64 |
| 63 | #define MIME_B64_WIDTH 76 |
| 64 | |
| 65 | NAMESPACE_UCOMMON |
| 66 | |
| 67 | /** |
| 68 | * A convenience class for size of strings. |
| 69 | */ |
| 70 | typedef unsigned short strsize_t; |
| 71 | |
| 72 | /** |
| 73 | * A copy-on-write string class that operates by reference count. This string |
| 74 | * class anchors a counted object that is managed as a copy-on-write |
| 75 | * instance of the string data. This means that multiple instances of the |
| 76 | * string class can refer to the same string in memory if it has not been |
| 77 | * modifed, which reduces heap allocation. The string class offers functions |
| 78 | * to manipulate both the string object, and generic safe string functions to |
| 79 | * manipulate ordinary null terminated character arrays directly in memory. |
| 80 | * @author David Sugar <dyfet@gnutelephony.org> |
| 81 | */ |
| 82 | class __EXPORT String : public ObjectProtocol |
| 83 | { |
| 84 | protected: |
| 85 | /** |
| 86 | * This is an internal class which contains the actual string data |
| 87 | * along with some control fields. The string can be either NULL |
| 88 | * terminated or filled like a Pascal-style string, but with a user |
| 89 | * selected fill character. The cstring object is an overdraft |
| 90 | * object, as the actual string text which is of unknown size follows |
| 91 | * immediately after the class control data. This class is primarily |
| 92 | * for internal use. |
| 93 | * @author David Sugar <dyfet@gnutelephony.org> |
| 94 | */ |
| 95 | |
| 96 | public: |
| 97 | enum { |
| 98 | SENSITIVE = 0x00, |
| 99 | INSENSITIVE = 0x01 |
| 100 | }; |
| 101 | |
| 102 | class __EXPORT regex |
| 103 | { |
| 104 | private: |
| 105 | void *object; |
| 106 | void *results; |
| 107 | size_t count; |
| 108 | |
| 109 | public: |
| 110 | regex(const char *pattern, size_t slots = 1); |
| 111 | regex(size_t slots = 1); |
| 112 | ~regex(); |
| 113 | |
| 114 | size_t offset(unsigned member); |
| 115 | size_t size(unsigned member); |
| 116 | |
| 117 | inline size_t members(void) |
| 118 | {return count;}; |
| 119 | |
| 120 | bool match(const char *text, unsigned flags = 0); |
| 121 | |
| 122 | regex& operator=(const char *string); |
| 123 | |
| 124 | bool operator*=(const char *string); |
| 125 | |
| 126 | operator bool() |
| 127 | {return object != NULL;} |
| 128 | |
| 129 | bool operator!() |
| 130 | {return object == NULL;} |
| 131 | }; |
| 132 | |
| 133 | class __EXPORT cstring : public CountedObject |
| 134 | { |
| 135 | public: |
| 136 | #pragma pack(1) |
| 137 | strsize_t max; /**< Allocated size of cstring text */ |
| 138 | strsize_t len; /**< Current length of cstring text */ |
| 139 | char fill; /**< Filler character or 0 for none */ |
| 140 | char text[1]; /**< Null terminated text, in overdraft space */ |
| 141 | #pragma pack() |
| 142 | |
| 143 | /** |
| 144 | * Create a cstring node allocated for specified string size. The |
| 145 | * new operator would also need the size as an overdraft value. |
| 146 | * @param size of string. |
| 147 | */ |
| 148 | cstring(strsize_t size); |
| 149 | |
| 150 | /** |
| 151 | * Create a filled cstring node allocated for specified string size. |
| 152 | * The new operator would also need the size as an overdraft value. |
| 153 | * The newly allocated string is filled with the fill value. |
| 154 | * @param size of string. |
| 155 | * @param fill character value to fill string with. |
| 156 | */ |
| 157 | cstring(strsize_t size, char fill); |
| 158 | |
| 159 | /** |
| 160 | * Used to clear a string. If null terminated, then the string ends |
| 161 | * at the offset, otherwise it is simply filled with fill data up to |
| 162 | * the specified size. |
| 163 | * @param offset to clear from. |
| 164 | * @param size of field to clear. |
| 165 | */ |
| 166 | void clear(strsize_t offset, strsize_t size); |
| 167 | |
| 168 | /** |
| 169 | * Set part or all of a string with new text. |
| 170 | * @param offset to set from. |
| 171 | * @param text to insert from null terminated string. |
| 172 | * @param size of field to modify. This is filled for fill mode. |
| 173 | */ |
| 174 | void set(strsize_t offset, const char *text, strsize_t size); |
| 175 | |
| 176 | /** |
| 177 | * Set our string from null terminated text up to our allocated size. |
| 178 | * @param text to set from. |
| 179 | */ |
| 180 | void set(const char *text); |
| 181 | |
| 182 | /** |
| 183 | * Append null terminated text to our string buffer. |
| 184 | * @param text to append. |
| 185 | */ |
| 186 | void add(const char *text); |
| 187 | |
| 188 | /** |
| 189 | * Append a single character to our string buffer. |
| 190 | * @param character to append. |
| 191 | */ |
| 192 | void add(char character); |
| 193 | |
| 194 | /** |
| 195 | * Fill our string buffer to end if fill mode. |
| 196 | */ |
| 197 | void fix(void); |
| 198 | |
| 199 | /** |
| 200 | * Trim filler at end to reduce filled string to null terminated |
| 201 | * string for further processing. |
| 202 | */ |
| 203 | void unfix(void); |
| 204 | |
| 205 | /** |
| 206 | * Adjust size of our string buffer by deleting characters from |
| 207 | * start of buffer. |
| 208 | * @param number of characters to delete. |
| 209 | */ |
| 210 | void inc(strsize_t number); |
| 211 | |
| 212 | /** |
| 213 | * Adjust size of our string buffer by deleting characters from |
| 214 | * end of buffer. |
| 215 | * @param number of characters to delete. |
| 216 | */ |
| 217 | void dec(strsize_t number); |
| 218 | }; |
| 219 | |
| 220 | protected: |
| 221 | cstring *str; /**< cstring instance our object references. */ |
| 222 | |
| 223 | /** |
| 224 | * Factory create a cstring object of specified size. |
| 225 | * @param size of allocated space for string buffer. |
| 226 | * @param fill character to use or 0 if null. |
| 227 | * @return new cstring object. |
| 228 | */ |
| 229 | cstring *create(strsize_t size, char fill = 0) const; |
| 230 | |
| 231 | public: |
| 232 | /** |
| 233 | * Compare the values of two string. This is a virtual so that it |
| 234 | * can be overridden for example if we want to create strings which |
| 235 | * ignore case, or which have special ordering rules. |
| 236 | * @param string to compare with. |
| 237 | * @return 0 if equal, <0 if less than, 0> if greater than. |
| 238 | */ |
| 239 | virtual int compare(const char *string) const; |
| 240 | |
| 241 | inline int collate(const char *string) const |
| 242 | {return compare(string);}; |
| 243 | |
| 244 | protected: |
| 245 | /** |
| 246 | * Test if two string values are equal. |
| 247 | * @param string to compare with. |
| 248 | * @return true if equal. |
| 249 | */ |
| 250 | bool equal(const char *string) const; |
| 251 | |
| 252 | /** |
| 253 | * Increase retention of our reference counted cstring. May be overridden |
| 254 | * for memstring which has fixed cstring object. |
| 255 | */ |
| 256 | virtual void retain(void); |
| 257 | |
| 258 | /** |
| 259 | * Decrease retention of our reference counted cstring. May be overridden |
| 260 | * for memstring which has fixed cstring object. |
| 261 | */ |
| 262 | virtual void release(void); |
| 263 | |
| 264 | /** |
| 265 | * Return cstring to use in copy constructors. Is virtual for memstring. |
| 266 | * @return cstring for copy constructor. |
| 267 | */ |
| 268 | virtual cstring *c_copy(void) const; |
| 269 | |
| 270 | /** |
| 271 | * Copy on write operation for cstring. This always creates a new |
| 272 | * unique copy for write/modify operations and is a virtual for memstring |
| 273 | * to disable. |
| 274 | * @param size to add to allocated space when creating new cstring. |
| 275 | */ |
| 276 | virtual void cow(strsize_t size = 0); |
| 277 | |
| 278 | strsize_t getStringSize(void); |
| 279 | |
| 280 | public: |
| 281 | /** |
| 282 | * A constant for an invalid position value. |
| 283 | */ |
| 284 | #if _MSC_VER > 1400 // windows broken dll linkage issue... |
| 285 | const static strsize_t npos = ((strsize_t)-1); |
| 286 | #else |
| 287 | static const strsize_t npos; |
| 288 | #endif |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * Create a new empty string object. |
| 293 | */ |
| 294 | String(); |
| 295 | |
| 296 | /** |
| 297 | * Create a string from a long integer. |
| 298 | * @param value to convert to string. |
| 299 | */ |
| 300 | String(long value); |
| 301 | |
| 302 | /** |
| 303 | * Create a string from a floating point. |
| 304 | * @param value to convert to string. |
| 305 | */ |
| 306 | String(double value); |
| 307 | |
| 308 | /** |
| 309 | * Create an empty string with a buffer pre-allocated to a specified size. |
| 310 | * @param size of buffer to allocate. |
| 311 | */ |
| 312 | String(strsize_t size); |
| 313 | |
| 314 | /** |
| 315 | * Create a filled string with a buffer pre-allocated to a specified size. |
| 316 | * @param size of buffer to allocate. |
| 317 | * @param fill character to use. |
| 318 | */ |
| 319 | String(strsize_t size, char fill); |
| 320 | |
| 321 | /** |
| 322 | * Create a string by printf-like formating into a pre-allocated space |
| 323 | * of a specified size. A typical use might be in a concat function |
| 324 | * like String x = (String)something + (String){10, "%ud", var}. |
| 325 | * @param size of buffer to allocate. |
| 326 | * @param format control for string. |
| 327 | */ |
| 328 | String(strsize_t size, const char *format, ...) __PRINTF(3, 4); |
| 329 | |
| 330 | |
| 331 | /** |
| 332 | * Create a string from null terminated text. |
| 333 | * @param text to use for string. |
| 334 | */ |
| 335 | String(const char *text); |
| 336 | |
| 337 | /** |
| 338 | * Create a string from null terminated text up to a maximum specified |
| 339 | * size. |
| 340 | * @param text to use for string. |
| 341 | * @param size limit of new string. |
| 342 | */ |
| 343 | String(const char *text, strsize_t size); |
| 344 | |
| 345 | /** |
| 346 | * Create a string for a substring. The end of the substring is a |
| 347 | * pointer within the substring itself. |
| 348 | * @param text to use for string. |
| 349 | * @param end of text in substring. |
| 350 | */ |
| 351 | String(const char *text, const char *end); |
| 352 | |
| 353 | /** |
| 354 | * Construct a copy of a string object. Our copy inherets the same |
| 355 | * reference counted instance of cstring as in the original. |
| 356 | * @param existing string to copy from. |
| 357 | */ |
| 358 | String(const String& existing); |
| 359 | |
| 360 | /** |
| 361 | * Destroy string. De-reference cstring. If last reference to cstring, |
| 362 | * then also remove cstring from heap. |
| 363 | */ |
| 364 | virtual ~String(); |
| 365 | |
| 366 | /** |
| 367 | * Get a new string object as a substring of the current object. |
| 368 | * @param offset of substring. |
| 369 | * @param size of substring or 0 if to end. |
| 370 | * @return string object holding substring. |
| 371 | */ |
| 372 | String get(strsize_t offset, strsize_t size = 0) const; |
| 373 | |
| 374 | /** |
| 375 | * Scan input items from a string object. |
| 376 | * @param format string of input to scan. |
| 377 | * @return number of items scanned. |
| 378 | */ |
| 379 | int scanf(const char *format, ...) __SCANF(2, 3); |
| 380 | |
| 381 | /** |
| 382 | * Scan input items from a string object. |
| 383 | * @param format string of input to scan. |
| 384 | * @param args list to scan into. |
| 385 | * @return number of items scanned. |
| 386 | */ |
| 387 | int vscanf(const char *format, va_list args) __SCANF(2, 0); |
| 388 | |
| 389 | /** |
| 390 | * Print items into a string object. |
| 391 | * @param format string of print format. |
| 392 | * @return number of bytes written to string. |
| 393 | */ |
| 394 | strsize_t printf(const char *format, ...) __PRINTF(2, 3); |
| 395 | |
| 396 | /** |
| 397 | * Print items into a string object. |
| 398 | * @param format string of print format. |
| 399 | * @param args list to print. |
| 400 | * @return number of bytes written to string. |
| 401 | */ |
| 402 | strsize_t vprintf(const char *format, va_list args) __PRINTF(2, 0); |
| 403 | |
| 404 | /** |
| 405 | * Get memory text buffer of string object. |
| 406 | * @return writable string buffer. |
| 407 | */ |
| 408 | char *c_mem(void) const; |
| 409 | |
| 410 | /** |
| 411 | * Get character text buffer of string object. |
| 412 | * @return character text buffer. |
| 413 | */ |
| 414 | const char *c_str(void) const; |
| 415 | |
| 416 | /** |
| 417 | * Resize and re-allocate string memory. |
| 418 | * @param size to allocate for string. |
| 419 | * @return true if re-allocated. False in derived memstring. |
| 420 | */ |
| 421 | virtual bool resize(strsize_t size); |
| 422 | |
| 423 | /** |
| 424 | * Set string object to text of a null terminated string. |
| 425 | * @param text string to set. |
| 426 | */ |
| 427 | void set(const char *text); |
| 428 | |
| 429 | /** |
| 430 | * Set a portion of the string object at a specified offset to a text |
| 431 | * string. |
| 432 | * @param offset in object string buffer. |
| 433 | * @param text to set at offset. |
| 434 | * @param size of text area to set or 0 until end of text. |
| 435 | */ |
| 436 | void set(strsize_t offset, const char *text, strsize_t size = 0); |
| 437 | |
| 438 | /** |
| 439 | * Set a text field within our string object. |
| 440 | * @param text to set. |
| 441 | * @param overflow character to use as filler if text is too short. |
| 442 | * @param offset in object string buffer to set text at. |
| 443 | * @param size of part of buffer to set with text and overflow. |
| 444 | */ |
| 445 | void set(const char *text, char overflow, strsize_t offset, strsize_t size = 0); |
| 446 | |
| 447 | /** |
| 448 | * Set a text field within our string object offset from the end of buffer. |
| 449 | * @param text to set. |
| 450 | * @param overflow character to use as filler if text is too short. |
| 451 | * @param offset from end of object string buffer to set text at. |
| 452 | * @param size of part of buffer to set with text and overflow. |
| 453 | */ |
| 454 | void rset(const char *text, char overflow, strsize_t offset, strsize_t size = 0); |
| 455 | |
| 456 | /** |
| 457 | * Append null terminated text to our string buffer. |
| 458 | * @param text to append. |
| 459 | */ |
| 460 | void add(const char *text); |
| 461 | |
| 462 | /** |
| 463 | * Append a single character to our string buffer. |
| 464 | * @param character to append. |
| 465 | */ |
| 466 | void add(char character); |
| 467 | |
| 468 | /** |
| 469 | * Trim lead characters from the string. |
| 470 | * @param list of characters to remove. |
| 471 | */ |
| 472 | void trim(const char *list); |
| 473 | |
| 474 | /** |
| 475 | * Trim lead characters from text. |
| 476 | * @param count of characters to remove. |
| 477 | */ |
| 478 | inline void trim(strsize_t count = 1) |
| 479 | {operator+=(count);}; |
| 480 | |
| 481 | /** |
| 482 | * Chop trailing characters from the string. |
| 483 | * @param list of characters to remove. |
| 484 | */ |
| 485 | void chop(const char *list); |
| 486 | |
| 487 | /** |
| 488 | * Chop trailing characters from text. |
| 489 | * @param count of characters to remove. |
| 490 | */ |
| 491 | inline void chop(strsize_t count = 1) |
| 492 | {operator-=(count);}; |
| 493 | |
| 494 | /** |
| 495 | * Strip lead and trailing characters from the string. |
| 496 | * @param list of characters to remove. |
| 497 | */ |
| 498 | void strip(const char *list); |
| 499 | |
| 500 | /** |
| 501 | * Unquote a quoted string. Removes lead and trailing quote marks. |
| 502 | * @param quote pairs of characters for open and close quote. |
| 503 | * @return true if string was quoted. |
| 504 | */ |
| 505 | bool unquote(const char *quote); |
| 506 | |
| 507 | /** |
| 508 | * Cut (remove) text from string. |
| 509 | * @param offset to start of text field to remove. |
| 510 | * @param size of text field to remove or 0 to remove to end of string. |
| 511 | */ |
| 512 | void cut(strsize_t offset, strsize_t size = 0); |
| 513 | |
| 514 | /** |
| 515 | * Insert (paste) text into string. |
| 516 | * @param offset to start paste. |
| 517 | * @param text to paste. |
| 518 | * @param size of text to paste. |
| 519 | */ |
| 520 | void paste(strsize_t offset, const char *text, strsize_t size = 0); |
| 521 | |
| 522 | /** |
| 523 | * Clear a field of a filled string with filler. |
| 524 | * @param offset to start of field to clear. |
| 525 | * @param size of field to fill or 0 to fill to end of string. |
| 526 | */ |
| 527 | void clear(strsize_t offset, strsize_t size = 0); |
| 528 | |
| 529 | /** |
| 530 | * Clear string by setting to empty. |
| 531 | */ |
| 532 | void clear(void); |
| 533 | |
| 534 | /** |
| 535 | * Convert string to upper case. |
| 536 | */ |
| 537 | void upper(void); |
| 538 | |
| 539 | /** |
| 540 | * Convert string to lower case. |
| 541 | */ |
| 542 | void lower(void); |
| 543 | |
| 544 | /** |
| 545 | * Erase string memory. |
| 546 | */ |
| 547 | void erase(void); |
| 548 | |
| 549 | /** |
| 550 | * Count number of occurrences of characters in string. |
| 551 | * @param list of characters to find. |
| 552 | * @return count of instances of characters in string. |
| 553 | */ |
| 554 | strsize_t ccount(const char *list) const; |
| 555 | |
| 556 | /** |
| 557 | * Count all characters in the string (strlen). |
| 558 | * @return count of characters. |
| 559 | */ |
| 560 | strsize_t count(void) const; |
| 561 | |
| 562 | /** |
| 563 | * Get the size of currently allocated space for string. |
| 564 | * @return size allocated for text. |
| 565 | */ |
| 566 | strsize_t size(void) const; |
| 567 | |
| 568 | /** |
| 569 | * Find offset of a pointer into our string buffer. This can be used |
| 570 | * to find the offset position of a pointer returned by find, for |
| 571 | * example. This is used when one needs to convert a member function |
| 572 | * that returns a pointer to call a member function that operates by |
| 573 | * a offset value. If the pointer is outside the range of the string |
| 574 | * then npos is returned. |
| 575 | * @param pointer into our object's string buffer. |
| 576 | */ |
| 577 | strsize_t offset(const char *pointer) const; |
| 578 | |
| 579 | /** |
| 580 | * Return character found at a specific position in the string. |
| 581 | * @param position in string, negative values computed from end. |
| 582 | * @return character code at specified position in string. |
| 583 | */ |
| 584 | char at(int position) const; |
| 585 | |
| 586 | /** |
| 587 | * Get pointer to first character in string for iteration. |
| 588 | * @return first character pointer or NULL if empty. |
| 589 | */ |
| 590 | const char *begin(void) const; |
| 591 | |
| 592 | /** |
| 593 | * Get pointer to last character in string for iteration. |
| 594 | * @return last character pointer or NULL if empty. |
| 595 | */ |
| 596 | const char *end(void) const; |
| 597 | |
| 598 | /** |
| 599 | * Skip lead characters in the string. |
| 600 | * @param list of characters to skip when found. |
| 601 | * @param offset to start of scan. |
| 602 | * @return pointer to first part of string past skipped characters. |
| 603 | */ |
| 604 | const char *skip(const char *list, strsize_t offset = 0) const; |
| 605 | |
| 606 | /** |
| 607 | * Skip trailing characters in the string. This searches the |
| 608 | * string in reverse order. |
| 609 | * @param list of characters to skip when found. |
| 610 | * @param offset to start of scan. Default is end of string. |
| 611 | * @return pointer to first part of string before skipped characters. |
| 612 | */ |
| 613 | const char *rskip(const char *list, strsize_t offset = npos) const; |
| 614 | |
| 615 | /** |
| 616 | * Search for a substring in the string. |
| 617 | * @param substring to search for. |
| 618 | * @param flags for case insensitive search. |
| 619 | */ |
| 620 | const char *search(const char *string, unsigned instance = 0, unsigned flags = 0) const; |
| 621 | |
| 622 | const char *search(regex& expr, unsigned instance = 0, unsigned flags = 0) const; |
| 623 | |
| 624 | unsigned replace(const char *string, const char *text = NULL, unsigned flags = 0); |
| 625 | |
| 626 | unsigned replace(regex& expr, const char *text = NULL, unsigned flags = 0); |
| 627 | |
| 628 | /** |
| 629 | * Find a character in the string. |
| 630 | * @param list of characters to search for. |
| 631 | * @param offset to start of search. |
| 632 | * @return pointer to first occurrence of character. |
| 633 | */ |
| 634 | const char *find(const char *list, strsize_t offset = 0) const; |
| 635 | |
| 636 | /** |
| 637 | * Find last occurrence of character in the string. |
| 638 | * @param list of characters to search for. |
| 639 | * @param offset to start of search. Default is end of string. |
| 640 | * @return pointer to last occurrence of character. |
| 641 | */ |
| 642 | const char *rfind(const char *list, strsize_t offset = npos) const; |
| 643 | |
| 644 | /** |
| 645 | * Split the string by a pointer position. Everything after the pointer |
| 646 | * is removed. |
| 647 | * @param pointer to split position in string. |
| 648 | */ |
| 649 | void split(const char *pointer); |
| 650 | |
| 651 | /** |
| 652 | * Split the string at a specific offset. Everything after the offset |
| 653 | * is removed. |
| 654 | * @param offset to split position in string. |
| 655 | */ |
| 656 | void split(strsize_t offset); |
| 657 | |
| 658 | /** |
| 659 | * Split the string by a pointer position. Everything before the pointer |
| 660 | * is removed. |
| 661 | * @param pointer to split position in string. |
| 662 | */ |
| 663 | void rsplit(const char *pointer); |
| 664 | |
| 665 | /** |
| 666 | * Split the string at a specific offset. Everything before the offset |
| 667 | * is removed. |
| 668 | * @param offset to split position in string. |
| 669 | */ |
| 670 | void rsplit(strsize_t offset); |
| 671 | |
| 672 | /** |
| 673 | * Find pointer in string where specified character appears. |
| 674 | * @param character to find. |
| 675 | * @return string pointer for character if found, NULL if not. |
| 676 | */ |
| 677 | const char *chr(char character) const; |
| 678 | |
| 679 | /** |
| 680 | * Find pointer in string where specified character last appears. |
| 681 | * @param character to find. |
| 682 | * @return string pointer for last occurrence of character if found, |
| 683 | * NULL if not. |
| 684 | */ |
| 685 | const char *rchr(char character) const; |
| 686 | |
| 687 | /** |
| 688 | * Get length of string. |
| 689 | * @return length of string. |
| 690 | */ |
| 691 | strsize_t len(void) const; |
| 692 | |
| 693 | /** |
| 694 | * Get filler character used for field array strings. |
| 695 | * @return filler character or 0 if none. |
| 696 | */ |
| 697 | char fill(void); |
| 698 | |
| 699 | /** |
| 700 | * Casting reference to raw text string. |
| 701 | * @return null terminated text of string. |
| 702 | */ |
| 703 | inline operator const char *() const |
| 704 | {return c_str();}; |
| 705 | |
| 706 | /** |
| 707 | * Reference raw text buffer by pointer operator. |
| 708 | * @return null terminated text of string. |
| 709 | */ |
| 710 | inline const char *operator*() const |
| 711 | {return c_str();}; |
| 712 | |
| 713 | /** |
| 714 | * Test if the string's allocated space is all used up. |
| 715 | * @return true if no more room for append. |
| 716 | */ |
| 717 | bool full(void) const; |
| 718 | |
| 719 | /** |
| 720 | * Get a new substring through object expression. |
| 721 | * @param offset of substring. |
| 722 | * @param size of substring or 0 if to end. |
| 723 | * @return string object holding substring. |
| 724 | */ |
| 725 | String operator()(int offset, strsize_t size) const; |
| 726 | |
| 727 | /** |
| 728 | * Convenience method for left of string. |
| 729 | * @param size of substring to gather. |
| 730 | * @return string object holding substring. |
| 731 | */ |
| 732 | inline String left(strsize_t size) const |
| 733 | {return operator()(0, size);} |
| 734 | |
| 735 | /** |
| 736 | * Convenience method for right of string. |
| 737 | * @param offset of substring from right. |
| 738 | * @return string object holding substring. |
| 739 | */ |
| 740 | inline String right(strsize_t offset) const |
| 741 | {return operator()(-((int)offset), 0);} |
| 742 | |
| 743 | /** |
| 744 | * Convenience method for substring extraction. |
| 745 | * @param offset into string. |
| 746 | * @param size of string to return. |
| 747 | * @return string object holding substring. |
| 748 | */ |
| 749 | inline String copy(strsize_t offset, strsize_t size) const |
| 750 | {return operator()((int)offset, size);} |
| 751 | |
| 752 | /** |
| 753 | * Reference a string in the object by relative offset. Positive |
| 754 | * offsets are from the start of the string, negative from the |
| 755 | * end. |
| 756 | * @param offset to string position. |
| 757 | * @return pointer to string data or NULL if invalid offset. |
| 758 | */ |
| 759 | const char *operator()(int offset) const; |
| 760 | |
| 761 | /** |
| 762 | * Reference a single character in string object by array offset. |
| 763 | * @param offset to character. |
| 764 | * @return character value at offset. |
| 765 | */ |
| 766 | const char operator[](int offset) const; |
| 767 | |
| 768 | /** |
| 769 | * Test if string is empty. |
| 770 | * @return true if string is empty. |
| 771 | */ |
| 772 | bool operator!() const; |
| 773 | |
| 774 | /** |
| 775 | * Test if string has data. |
| 776 | * @return true if string has data. |
| 777 | */ |
| 778 | operator bool() const; |
| 779 | |
| 780 | /** |
| 781 | * Create new cow instance and assign value from another string object. |
| 782 | * @param object to assign from. |
| 783 | * @return our object for expression use. |
| 784 | */ |
| 785 | String& operator^=(const String& object); |
| 786 | |
| 787 | /** |
| 788 | * Concatenate text to an existing string object. This will use the |
| 789 | * old behavior when +/= updated. |
| 790 | */ |
| 791 | String& operator|=(const char *text); |
| 792 | |
| 793 | String& operator&=(const char *text); |
| 794 | |
| 795 | /** |
| 796 | * Concatenate text to an existing string object. |
| 797 | * @param text to add. |
| 798 | * @return our object for expression use. |
| 799 | */ |
| 800 | String& operator+=(const char *text); |
| 801 | |
| 802 | /** |
| 803 | * Create new cow instance and assign value from null terminated text. |
| 804 | * @param text to assign from. |
| 805 | * @return our object for expression use. |
| 806 | */ |
| 807 | String& operator^=(const char *text); |
| 808 | |
| 809 | /** |
| 810 | * Concatenate null terminated text to our object. |
| 811 | * @param text to concatenate. |
| 812 | */ |
| 813 | String operator+(const char *text); |
| 814 | |
| 815 | /** |
| 816 | * Concatenate null terminated text to our object. This creates a new |
| 817 | * copy-on-write instance to hold the concatenated string. This will |
| 818 | * eventually replace '+' when + creates a new string instance instead. |
| 819 | * @param text to concatenate. |
| 820 | */ |
| 821 | String& operator|(const char *text); |
| 822 | |
| 823 | /** |
| 824 | * Concatenate null terminated text to our object. This directly |
| 825 | * appends the text to the string buffer and does not resize the |
| 826 | * object if the existing cstring allocation space is fully used. |
| 827 | * @param text to concatenate. |
| 828 | */ |
| 829 | String& operator&(const char *text); |
| 830 | |
| 831 | /** |
| 832 | * Assign our string with the cstring of another object. If we had |
| 833 | * an active string reference, it is released. The object now has |
| 834 | * a duplicate reference to the cstring of the other object. |
| 835 | * @param object to assign from. |
| 836 | */ |
| 837 | String& operator=(const String& object); |
| 838 | |
| 839 | bool operator*=(const char *substring); |
| 840 | |
| 841 | bool operator*=(regex& expr); |
| 842 | |
| 843 | /** |
| 844 | * Assign text to our existing buffer. This performs a set method. |
| 845 | * @param text to assign from. |
| 846 | */ |
| 847 | String& operator=(const char *text); |
| 848 | |
| 849 | /** |
| 850 | * Delete first character from string. |
| 851 | */ |
| 852 | String& operator++(void); |
| 853 | |
| 854 | /** |
| 855 | * Delete a specified number of characters from start of string. |
| 856 | * @param number of characters to delete. |
| 857 | */ |
| 858 | String& operator+=(strsize_t number); |
| 859 | |
| 860 | /** |
| 861 | * Delete last character from string. |
| 862 | */ |
| 863 | String& operator--(void); |
| 864 | |
| 865 | /** |
| 866 | * Delete a specified number of characters from end of string. |
| 867 | * @param number of characters to delete. |
| 868 | */ |
| 869 | String& operator-=(strsize_t number); |
| 870 | |
| 871 | /** |
| 872 | * Delete a specified number of characters from start of string. |
| 873 | * @param number of characters to delete. |
| 874 | */ |
| 875 | String& operator*=(strsize_t number); |
| 876 | |
| 877 | /** |
| 878 | * Compare our object with null terminated text. |
| 879 | * @param text to compare with. |
| 880 | * @return true if we are equal. |
| 881 | */ |
| 882 | bool operator==(const char *text) const; |
| 883 | |
| 884 | /** |
| 885 | * Compare our object with null terminated text. Compare method is used. |
| 886 | * @param text to compare with. |
| 887 | * @return true if we are not equal. |
| 888 | */ |
| 889 | bool operator!=(const char *text) const; |
| 890 | |
| 891 | /** |
| 892 | * Compare our object with null terminated text. Compare method is used. |
| 893 | * @param text to compare with. |
| 894 | * @return true if we are less than text. |
| 895 | */ |
| 896 | bool operator<(const char *text) const; |
| 897 | |
| 898 | /** |
| 899 | * Compare our object with null terminated text. Compare method is used. |
| 900 | * @param text to compare with. |
| 901 | * @return true if we are less than or equal to text. |
| 902 | */ |
| 903 | bool operator<=(const char *text) const; |
| 904 | |
| 905 | /** |
| 906 | * Compare our object with null terminated text. Compare method is used. |
| 907 | * @param text to compare with. |
| 908 | * @return true if we are greater than text. |
| 909 | */ |
| 910 | bool operator>(const char *text) const; |
| 911 | |
| 912 | /** |
| 913 | * Compare our object with null terminated text. Compare method is used. |
| 914 | * @param text to compare with. |
| 915 | * @return true if we are greater than or equal to text. |
| 916 | */ |
| 917 | bool operator>=(const char *text) const; |
| 918 | |
| 919 | inline String& operator<<(const char *text) |
| 920 | {add(text); return *this;} |
| 921 | |
| 922 | inline String& operator<<(char code) |
| 923 | {add(code); return *this;} |
| 924 | |
| 925 | /** |
| 926 | * Parse short integer value from a string. |
| 927 | * @param value to store. |
| 928 | * @return object in expression. |
| 929 | */ |
| 930 | String &operator%(short& value); |
| 931 | |
| 932 | /** |
| 933 | * Parse long integer value from a string. |
| 934 | * @param value to store. |
| 935 | * @return object in expression. |
| 936 | */ |
| 937 | String &operator%(unsigned short& value); |
| 938 | |
| 939 | /** |
| 940 | * Parse long integer value from a string. |
| 941 | * @param value to store. |
| 942 | * @return object in expression. |
| 943 | */ |
| 944 | String &operator%(long& value); |
| 945 | |
| 946 | /** |
| 947 | * Parse long integer value from a string. |
| 948 | * @param value to store. |
| 949 | * @return object in expression. |
| 950 | */ |
| 951 | String &operator%(unsigned long& value); |
| 952 | |
| 953 | /** |
| 954 | * Parse double value from a string. |
| 955 | * @param value to store. |
| 956 | * @return object in expression. |
| 957 | */ |
| 958 | String &operator%(double& value); |
| 959 | |
| 960 | /** |
| 961 | * Parse text from a string in a scan expression. |
| 962 | * @param text to scan and bypass. |
| 963 | * @return object in expression. |
| 964 | */ |
| 965 | String &operator%(const char *text); |
| 966 | |
| 967 | /** |
| 968 | * Swap the cstring references between two strings. |
| 969 | * @param object1 to swap. |
| 970 | * @param object2 to swap. |
| 971 | */ |
| 972 | static void swap(String& object1, String& object2); |
| 973 | |
| 974 | /** |
| 975 | * Fix and reset string object filler. |
| 976 | * @param object to fix. |
| 977 | */ |
| 978 | static void fix(String& object); |
| 979 | |
| 980 | /** |
| 981 | * Erase string memory. Often used to clear out passwords. |
| 982 | * @param text string to erase. |
| 983 | */ |
| 984 | static void erase(char *text); |
| 985 | |
| 986 | /** |
| 987 | * Convert null terminated text to lower case. |
| 988 | * @param text to convert. |
| 989 | */ |
| 990 | static void lower(char *text); |
| 991 | |
| 992 | /** |
| 993 | * Convert null terminated text to upper case. |
| 994 | * @param text to convert. |
| 995 | */ |
| 996 | static void upper(char *text); |
| 997 | |
| 998 | /** |
| 999 | * A thread-safe token parsing routine for null terminated strings. This |
| 1000 | * is related to strtok, but with safety checks for NULL values and a |
| 1001 | * number of enhancements including support for quoted text that may |
| 1002 | * contain token separators within quotes. The text string is modified |
| 1003 | * as it is parsed. |
| 1004 | * @param text string to examine for tokens. |
| 1005 | * @param last token position or set to NULL for start of string. |
| 1006 | * @param list of characters to use as token separators. |
| 1007 | * @param quote pairs of characters for quoted text or NULL if not used. |
| 1008 | * @param end of line marker characters or NULL if not used. |
| 1009 | * @return token extracted from string or NULL if no more tokens found. |
| 1010 | */ |
| 1011 | static char *token(char *text, char **last, const char *list, const char *quote = NULL, const char *end = NULL); |
| 1012 | |
| 1013 | /** |
| 1014 | * Skip after lead characters in a null terminated string. |
| 1015 | * @param text pointer to start at. |
| 1016 | * @param list of characters to skip when found. |
| 1017 | * @return pointer to first part of string past skipped characters. |
| 1018 | */ |
| 1019 | static char *skip(char *text, const char *list); |
| 1020 | |
| 1021 | /** |
| 1022 | * Skip before trailing characters in a null terminated string. |
| 1023 | * @param text pointer to start at. |
| 1024 | * @param list of characters to skip when found. |
| 1025 | * @return pointer to last part of string past skipped characters. |
| 1026 | */ |
| 1027 | static char *rskip(char *text, const char *list); |
| 1028 | |
| 1029 | /** |
| 1030 | * Unquote a quoted null terminated string. Returns updated string |
| 1031 | * position and replaces trailing quote with null byte if quoted. |
| 1032 | * @param text to examine. |
| 1033 | * @param quote pairs of character for open and close quote. |
| 1034 | * @return new text pointer if quoted, NULL if unchanged. |
| 1035 | */ |
| 1036 | static char *unquote(char *text, const char *quote); |
| 1037 | |
| 1038 | /** |
| 1039 | * Set a field in a null terminated string relative to the end of text. |
| 1040 | * @param buffer to modify. |
| 1041 | * @param size of field to set. |
| 1042 | * @param text to replace end of string with. |
| 1043 | * @return pointer to text buffer. |
| 1044 | */ |
| 1045 | static char *rset(char *buffer, size_t size, const char *text); |
| 1046 | |
| 1047 | /** |
| 1048 | * Safely set a null terminated string buffer in memory. If the text |
| 1049 | * is too large to fit into the buffer, it is truncated to the size. |
| 1050 | * @param buffer to set. |
| 1051 | * @param size of buffer. Includes null byte at end of string. |
| 1052 | * @param text to set in buffer. |
| 1053 | * @return pointer to text buffer. |
| 1054 | */ |
| 1055 | static char *set(char *buffer, size_t size, const char *text); |
| 1056 | |
| 1057 | /** |
| 1058 | * Safely set a null terminated string buffer in memory. If the text |
| 1059 | * is too large to fit into the buffer, it is truncated to the size. |
| 1060 | * @param buffer to set. |
| 1061 | * @param size of buffer. Includes null byte at end of string. |
| 1062 | * @param text to set in buffer. |
| 1063 | * @param max size of text to set. |
| 1064 | * @return pointer to text buffer. |
| 1065 | */ |
| 1066 | static char *set(char *buffer, size_t size, const char *text, size_t max); |
| 1067 | |
| 1068 | /** |
| 1069 | * Safely append a null terminated string into an existing string in |
| 1070 | * memory. If the resulting string is too large to fit into the buffer, |
| 1071 | * it is truncated to the size. |
| 1072 | * @param buffer to set. |
| 1073 | * @param size of buffer. Includes null byte at end of string. |
| 1074 | * @param text to set in buffer. |
| 1075 | * @return pointer to text buffer. |
| 1076 | */ |
| 1077 | static char *add(char *buffer, size_t size, const char *text); |
| 1078 | |
| 1079 | /** |
| 1080 | * Safely append a null terminated string into an existing string in |
| 1081 | * memory. If the resulting string is too large to fit into the buffer, |
| 1082 | * it is truncated to the size. |
| 1083 | * @param buffer to set. |
| 1084 | * @param size of buffer. Includes null byte at end of string. |
| 1085 | * @param text to set in buffer. |
| 1086 | * @param max size of text to append. |
| 1087 | * @return pointer to text buffer. |
| 1088 | */ |
| 1089 | static char *add(char *buffer, size_t size, const char *text, size_t max); |
| 1090 | |
| 1091 | /** |
| 1092 | * Find position of case insensitive substring within a string. |
| 1093 | * @param text to search in. |
| 1094 | * @param key string to locate. |
| 1095 | * @param optional separator chars if formatted as list of keys. |
| 1096 | * @return substring position if found, or NULL. |
| 1097 | */ |
| 1098 | static const char *ifind(const char *text, const char *key, const char *optional); |
| 1099 | |
| 1100 | /** |
| 1101 | * Find position of substring within a string. |
| 1102 | * @param text to search in. |
| 1103 | * @param key string to locate. |
| 1104 | * @param optional separator chars if formatted as list of keys. |
| 1105 | * @return substring position if found, or NULL. |
| 1106 | */ |
| 1107 | static const char *find(const char *text, const char *key, const char *optional); |
| 1108 | |
| 1109 | /** |
| 1110 | * Safe version of strlen function. Accepts NULL as 0 length strings. |
| 1111 | * @param text string. |
| 1112 | * @return length of string. |
| 1113 | */ |
| 1114 | static size_t count(const char *text); |
| 1115 | |
| 1116 | /** |
| 1117 | * Safe string collation function. |
| 1118 | * @param text1 to compare. |
| 1119 | * @param text2 to compare. |
| 1120 | * @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2. |
| 1121 | */ |
| 1122 | static int compare(const char *text1, const char *text2); |
| 1123 | |
| 1124 | static inline int collate(const char *text1, const char *text2) |
| 1125 | {return compare(text1, text2);}; |
| 1126 | |
| 1127 | /** |
| 1128 | * Simple equal test for strings. |
| 1129 | * @param text1 to test. |
| 1130 | * @param text2 to test. |
| 1131 | * @return true if equal and case is same. |
| 1132 | */ |
| 1133 | static bool equal(const char *text1, const char *text2); |
| 1134 | |
| 1135 | /** |
| 1136 | * Depreciated string comparison function. |
| 1137 | * @param text1 to compare. |
| 1138 | * @param text2 to compare. |
| 1139 | * @param size limit of strings to compare. |
| 1140 | * @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2. |
| 1141 | */ |
| 1142 | static int compare(const char *text1, const char *text2, size_t size); |
| 1143 | |
| 1144 | /** |
| 1145 | * Simple equal test for strings. |
| 1146 | * @param text1 to test. |
| 1147 | * @param text2 to test. |
| 1148 | * @param size limit of strings to compare. |
| 1149 | * @return true if equal and case is same. |
| 1150 | */ |
| 1151 | static bool equal(const char *text1, const char *text2, size_t size); |
| 1152 | |
| 1153 | /** |
| 1154 | * Depreciated case insensitive string comparison function. |
| 1155 | * @param text1 to compare. |
| 1156 | * @param text2 to compare. |
| 1157 | * @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2. |
| 1158 | */ |
| 1159 | static int case_compare(const char *text1, const char *text2); |
| 1160 | |
| 1161 | /** |
| 1162 | * Simple case insensitive equal test for strings. |
| 1163 | * @param text1 to test. |
| 1164 | * @param text2 to test. |
| 1165 | * @return true if equal. |
| 1166 | */ |
| 1167 | static bool eq_case(const char *text1, const char *text2); |
| 1168 | |
| 1169 | /** |
| 1170 | * Depreciated case insensitive string comparison function. |
| 1171 | * @param text1 to compare. |
| 1172 | * @param text2 to compare. |
| 1173 | * @param size limit of strings to compare. |
| 1174 | * @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2. |
| 1175 | */ |
| 1176 | static int case_compare(const char *text1, const char *text2, size_t size); |
| 1177 | |
| 1178 | /** |
| 1179 | * Simple case insensitive equal test for strings. |
| 1180 | * @param text1 to test. |
| 1181 | * @param text2 to test. |
| 1182 | * @param size limit of strings to compare. |
| 1183 | * @return true if equal. |
| 1184 | */ |
| 1185 | static bool eq_case(const char *text1, const char *text2, size_t size); |
| 1186 | |
| 1187 | /** |
| 1188 | * Return start of string after characters to trim from beginning. |
| 1189 | * This function does not modify memory. |
| 1190 | * @param text buffer to examine. |
| 1191 | * @param list of characters to skip from start. |
| 1192 | * @return position in text past lead trim. |
| 1193 | */ |
| 1194 | static char *trim(char *text, const char *list); |
| 1195 | |
| 1196 | /** |
| 1197 | * Strip trailing characters from the text string. This function will |
| 1198 | * modify memory. |
| 1199 | * @param text buffer to examine. |
| 1200 | * @param list of characters to chop from trailing end of string. |
| 1201 | * @return pointer to text buffer. |
| 1202 | */ |
| 1203 | static char *chop(char *text, const char *list); |
| 1204 | |
| 1205 | /** |
| 1206 | * Skip lead and remove trailing characters from a text string. This |
| 1207 | * function will modify memory. |
| 1208 | * @param text buffer to examine. |
| 1209 | * @param list of characters to trim and chop. |
| 1210 | * @return position in text past lead trim. |
| 1211 | */ |
| 1212 | static char *strip(char *text, const char *list); |
| 1213 | |
| 1214 | /** |
| 1215 | * Fill a section of memory with a fixed text character. Adds a null |
| 1216 | * byte at the end. |
| 1217 | * @param text buffer to fill. |
| 1218 | * @param size of text buffer with null terminated byte. |
| 1219 | * @param character to fill with. |
| 1220 | * @return pointer to text buffer. |
| 1221 | */ |
| 1222 | static char *fill(char *text, size_t size, char character); |
| 1223 | |
| 1224 | /** |
| 1225 | * Count instances of characters in a list in a text buffer. |
| 1226 | * @param text buffer to examine. |
| 1227 | * @param list of characters to count in buffer. |
| 1228 | * @return number of instances of the characters in buffer. |
| 1229 | */ |
| 1230 | static unsigned ccount(const char *text, const char *list); |
| 1231 | |
| 1232 | /** |
| 1233 | * Find the first occurrence of a character in a text buffer. |
| 1234 | * @param text buffer to examine. |
| 1235 | * @param list of characters to search for. |
| 1236 | * @return pointer to first instance found or NULL. |
| 1237 | */ |
| 1238 | static char *find(char *text, const char *list); |
| 1239 | |
| 1240 | /** |
| 1241 | * Offset until next occurrence of character in a text or length. |
| 1242 | * @param text buffer to examine. |
| 1243 | * @param list of characters to search for. |
| 1244 | * @return offset to next occurrence or length of string. |
| 1245 | */ |
| 1246 | static size_t seek(char *text, const char *list); |
| 1247 | |
| 1248 | /** |
| 1249 | * Find the last occurrence of a character in a text buffer. |
| 1250 | * @param text buffer to examine. |
| 1251 | * @param list of characters to search for. |
| 1252 | * @return pointer to last instance found or NULL. |
| 1253 | */ |
| 1254 | static char *rfind(char *text, const char *list); |
| 1255 | |
| 1256 | /** |
| 1257 | * Duplicate null terminated text into the heap. |
| 1258 | * @param text to duplicate. |
| 1259 | * @return duplicate copy of text allocated from heap. |
| 1260 | */ |
| 1261 | static char *dup(const char *text); |
| 1262 | |
| 1263 | /** |
| 1264 | * Duplicate null terminated text of specific size to heap. |
| 1265 | * @param text to duplicate. |
| 1266 | * @param size of text, maximum space allocated. |
| 1267 | * @return duplicate copy of text allocated on heap. |
| 1268 | */ |
| 1269 | static char *left(const char *text, size_t size); |
| 1270 | |
| 1271 | /** |
| 1272 | * Compute position in string. |
| 1273 | * @param text of string. |
| 1274 | * @param offset from start, negative values from end. |
| 1275 | * @return pointer to string position. |
| 1276 | */ |
| 1277 | static const char *pos(const char *text, ssize_t offset); |
| 1278 | |
| 1279 | inline static char *right(const char *text, size_t size) |
| 1280 | {return dup(pos(text, -(signed)size));} |
| 1281 | |
| 1282 | inline static char *copy(const char *text, size_t offset, size_t len) |
| 1283 | {return left(pos(text, offset), len);} |
| 1284 | |
| 1285 | static void cut(char *text, size_t offset, size_t len); |
| 1286 | |
| 1287 | static void paste(char *text, size_t max, size_t offset, const char *data, size_t len = 0); |
| 1288 | |
| 1289 | /** |
| 1290 | * A thread-safe token parsing routine for strings objects. This |
| 1291 | * is related to strtok, but with safety checks for NULL values and a |
| 1292 | * number of enhancements including support for quoted text that may |
| 1293 | * contain token separators within quotes. The object is modified |
| 1294 | * as it is parsed. |
| 1295 | * @param last token position or set to NULL for start of string. |
| 1296 | * @param list of characters to use as token separators. |
| 1297 | * @param quote pairs of characters for quoted text or NULL if not used. |
| 1298 | * @param end of line marker characters or NULL if not used. |
| 1299 | * @return token extracted from string or NULL if no more tokens found. |
| 1300 | */ |
| 1301 | inline char *token(char **last, const char *list, const char *quote = NULL, const char *end = NULL) |
| 1302 | {return token(c_mem(), last, list, quote, end);}; |
| 1303 | |
| 1304 | /** |
| 1305 | * Convert string to a double value. |
| 1306 | * @param object to convert. |
| 1307 | * @param pointer to update with end of parsed value. |
| 1308 | * @return double value of object. |
| 1309 | */ |
| 1310 | inline double tod(char **pointer = NULL) |
| 1311 | {return strtod(c_mem(), pointer);} |
| 1312 | |
| 1313 | /** |
| 1314 | * Convert string to a long value. |
| 1315 | * @param object to convert. |
| 1316 | * @param pointer to update with end of parsed value. |
| 1317 | * @return long value of object. |
| 1318 | */ |
| 1319 | inline long tol(char **pointer = NULL) |
| 1320 | {return strtol(c_mem(), pointer, 0);} |
| 1321 | |
| 1322 | /** |
| 1323 | * Convert text to a double value. |
| 1324 | * @param text to convert. |
| 1325 | * @param pointer to update with end of parsed value. |
| 1326 | * @return double value of object. |
| 1327 | */ |
| 1328 | inline static double tod(const char *text, char **pointer = NULL) |
| 1329 | {return strtod(text, pointer);} |
| 1330 | |
| 1331 | /** |
| 1332 | * Convert text to a long value. |
| 1333 | * @param text to convert. |
| 1334 | * @param pointer to update with end of parsed value. |
| 1335 | * @return long value of object. |
| 1336 | */ |
| 1337 | inline static long tol(const char *text, char **pointer = NULL) |
| 1338 | {return strtol(text, pointer, 0);} |
| 1339 | |
| 1340 | /** |
| 1341 | * Standard radix 64 encoding. |
| 1342 | * @param string of encoded text save into. |
| 1343 | * @param binary data to encode. |
| 1344 | * @param size of binary data to encode. |
| 1345 | * @param width of string buffer for data if partial supported. |
| 1346 | * @return number of bytes encoded. |
| 1347 | */ |
| 1348 | static size_t b64encode(char *string, const uint8_t *binary, size_t size, size_t width = 0); |
| 1349 | |
| 1350 | /** |
| 1351 | * Standard radix 64 decoding. |
| 1352 | * @param binary data to save. |
| 1353 | * @param string of encoded text. |
| 1354 | * @param size of destination buffer. |
| 1355 | * @return number of bytes actually decoded. |
| 1356 | */ |
| 1357 | static size_t b64decode(uint8_t *binary, const char *string, size_t size); |
| 1358 | |
| 1359 | /** |
| 1360 | * 24 bit crc as used in openpgp. |
| 1361 | * @param binary data to sum. |
| 1362 | * @param size of binary data to sum. |
| 1363 | * @return 24 bit crc of data. |
| 1364 | */ |
| 1365 | static uint32_t crc24(uint8_t *binary, size_t size); |
| 1366 | |
| 1367 | /** |
| 1368 | * ccitt 16 bit crc for binary data. |
| 1369 | * @param binary data to sum. |
| 1370 | * @param size of binary data to sum. |
| 1371 | * @return 16 bit crc. |
| 1372 | */ |
| 1373 | static uint16_t crc16(uint8_t *binary, size_t size); |
| 1374 | |
| 1375 | /** |
| 1376 | * Dump hex data to a string buffer. |
| 1377 | * @param binary memory to dump. |
| 1378 | * @param string to save into. |
| 1379 | * @param format string to convert with. |
| 1380 | * @return number of bytes processed. |
| 1381 | */ |
| 1382 | static unsigned hexdump(const unsigned char *binary, char *string, const char *format); |
| 1383 | |
| 1384 | /** |
| 1385 | * Pack hex data from a string buffer. |
| 1386 | * @param binary memory to pack. |
| 1387 | * @param string to save into. |
| 1388 | * @param format string to convert with. |
| 1389 | * @return number of bytes processed. |
| 1390 | */ |
| 1391 | static unsigned hexpack(unsigned char *binary, const char *string, const char *format); |
| 1392 | |
| 1393 | static unsigned hexsize(const char *format); |
| 1394 | }; |
| 1395 | |
| 1396 | /** |
| 1397 | * A string class that uses a cstring buffer that is fixed in memory. |
| 1398 | * This allows one to manipulate a fixed buffer of text in memory through |
| 1399 | * the string class. The size of the memory used must include space for |
| 1400 | * the overhead() size needed for the cstring object control data. |
| 1401 | * @author David Sugar <dyfet@gnutelephony.org> |
| 1402 | */ |
| 1403 | class __EXPORT memstring : public String |
| 1404 | { |
| 1405 | public: |
| 1406 | #if _MSC_VER > 1400 // windows broken dll linkage issue... |
| 1407 | const static size_t header = sizeof(string::cstring); |
| 1408 | #else |
| 1409 | static const size_t header; |
| 1410 | #endif |
| 1411 | |
| 1412 | private: |
| 1413 | bool resize(strsize_t size); |
| 1414 | void cow(strsize_t adj = 0); |
| 1415 | void release(void); |
| 1416 | |
| 1417 | protected: |
| 1418 | cstring *c_copy(void) const; |
| 1419 | |
| 1420 | public: |
| 1421 | /** |
| 1422 | * Assign the text of a string to our object. |
| 1423 | * @param object to copy text from. |
| 1424 | */ |
| 1425 | inline void operator=(String& object) |
| 1426 | {set(object.c_str());}; |
| 1427 | |
| 1428 | /** |
| 1429 | * Assign null terminated text to our object. |
| 1430 | * @param text to copy. |
| 1431 | */ |
| 1432 | inline void operator=(const char *text) |
| 1433 | {set(text);}; |
| 1434 | |
| 1435 | /** |
| 1436 | * Create an instance of a memory string. |
| 1437 | * @param memory to use for cstring object. |
| 1438 | * @param size of string. Total size must include space for overhead. |
| 1439 | * @param fill character for fixed character fields. |
| 1440 | */ |
| 1441 | memstring(void *memory, strsize_t size, char fill = 0); |
| 1442 | |
| 1443 | /** |
| 1444 | * Destroy memory string. |
| 1445 | */ |
| 1446 | ~memstring(); |
| 1447 | |
| 1448 | /** |
| 1449 | * Create a memory string with memory allocated from the heap. |
| 1450 | * @param size of string to allocate. Automatically adds control size. |
| 1451 | * @param fill character for fixed field strings. |
| 1452 | */ |
| 1453 | static memstring *create(strsize_t size, char fill = 0); |
| 1454 | |
| 1455 | /** |
| 1456 | * Create a memory string with memory allocated from a pager. |
| 1457 | * @param pager to allocate memory from. |
| 1458 | * @param size of string to allocate. Automatically adds control size. |
| 1459 | * @param fill character for fixed field strings. |
| 1460 | */ |
| 1461 | static memstring *create(MemoryProtocol *pager, strsize_t size, char fill = 0); |
| 1462 | }; |
| 1463 | |
| 1464 | /** |
| 1465 | * A template to create a character array that can be manipulated as a string. |
| 1466 | * This is a mini string/stringbuf class that supports a subset of |
| 1467 | * functionality but does not require a complex supporting object. Like |
| 1468 | * stringbuf, this can be used to create local string variables. |
| 1469 | * @author David Sugar <dyfet@gnutelephony.org> |
| 1470 | */ |
| 1471 | template<size_t S> |
| 1472 | class charbuf |
| 1473 | { |
| 1474 | private: |
| 1475 | char buffer[S]; |
| 1476 | |
| 1477 | public: |
| 1478 | /** |
| 1479 | * Create a new character buffer with an empty string. |
| 1480 | */ |
| 1481 | inline charbuf() |
| 1482 | {buffer[0] = 0;} |
| 1483 | |
| 1484 | /** |
| 1485 | * Create a character buffer with assigned text. If the text is |
| 1486 | * larger than the size of the object, it is truncated. |
| 1487 | * @param text to assign. |
| 1488 | */ |
| 1489 | inline charbuf(const char *text) |
| 1490 | {String::set(buffer, S, text);} |
| 1491 | |
| 1492 | /** |
| 1493 | * Copy constructor. |
| 1494 | */ |
| 1495 | inline charbuf(const charbuf& copy) |
| 1496 | {String::set(buffer, S, copy.buffer);} |
| 1497 | |
| 1498 | /** |
| 1499 | * Assign null terminated text to the object. |
| 1500 | * @param text to assign. |
| 1501 | */ |
| 1502 | inline void operator=(const char *text) |
| 1503 | {String::set(buffer, S, text);} |
| 1504 | |
| 1505 | /** |
| 1506 | * Concatenate text into the object. If the text is larger than the |
| 1507 | * size of the object, then it is truncated. |
| 1508 | * @param text to append. |
| 1509 | */ |
| 1510 | inline void operator+=(const char *text) |
| 1511 | {String::add(buffer, S, text);} |
| 1512 | |
| 1513 | /** |
| 1514 | * Test if data is contained in the object. |
| 1515 | * @return true if there is text. |
| 1516 | */ |
| 1517 | inline operator bool() const |
| 1518 | {return buffer[0];} |
| 1519 | |
| 1520 | /** |
| 1521 | * Test if the object is empty. |
| 1522 | * @return true if the object is empty. |
| 1523 | */ |
| 1524 | inline bool operator!() const |
| 1525 | {return buffer[0] == 0;} |
| 1526 | |
| 1527 | /** |
| 1528 | * Get text by casting reference. |
| 1529 | * @return pointer to text in object. |
| 1530 | */ |
| 1531 | inline operator char *() |
| 1532 | {return buffer;}; |
| 1533 | |
| 1534 | /** |
| 1535 | * Get text by object pointer reference. |
| 1536 | * @return pointer to text in object. |
| 1537 | */ |
| 1538 | inline char *operator*() |
| 1539 | {return buffer;}; |
| 1540 | |
| 1541 | /** |
| 1542 | * Array operator to get a character from the object. |
| 1543 | * @param offset of character in string buffer. |
| 1544 | * @return character at offset. |
| 1545 | */ |
| 1546 | inline char& operator[](size_t offset) const |
| 1547 | {return buffer[offset];} |
| 1548 | |
| 1549 | /** |
| 1550 | * Get a pointer to an offset in the object by expression operator. |
| 1551 | * @param offset of character in string buffer. |
| 1552 | * @return pointer to offset in object. |
| 1553 | */ |
| 1554 | inline char *operator()(size_t offset) |
| 1555 | {return buffer + offset;} |
| 1556 | |
| 1557 | /** |
| 1558 | * Get allocated size of the object. |
| 1559 | * @return allocated size. |
| 1560 | */ |
| 1561 | inline size_t size(void) const |
| 1562 | {return S;} |
| 1563 | |
| 1564 | /** |
| 1565 | * Get current length of string. |
| 1566 | * @return length of string. |
| 1567 | */ |
| 1568 | inline size_t len(void) const |
| 1569 | {return strlen(buffer);} |
| 1570 | }; |
| 1571 | |
| 1572 | /** |
| 1573 | * A convenience type for string. |
| 1574 | */ |
| 1575 | typedef String string_t; |
| 1576 | |
| 1577 | typedef String::regex stringex_t; |
| 1578 | |
| 1579 | /** |
| 1580 | * A string class that has a predefined string buffer. The string class |
| 1581 | * and buffer are allocated together as one object. This allows one to use |
| 1582 | * string objects entirely resident on the local stack as well as on the |
| 1583 | * heap. Using a string class on the local stack may be more convenient |
| 1584 | * than a char array since one can use all the features of the class |
| 1585 | * including assignment and concatenation which a char buffer cannot as |
| 1586 | * easily do. |
| 1587 | * @author David Sugar <dyfet@gnutelephony.org> |
| 1588 | */ |
| 1589 | template<strsize_t S> |
| 1590 | class stringbuf : public memstring |
| 1591 | { |
| 1592 | private: |
| 1593 | char buffer[sizeof(cstring) + S]; |
| 1594 | |
| 1595 | public: |
| 1596 | /** |
| 1597 | * Create an empty instance of a string buffer. |
| 1598 | */ |
| 1599 | inline stringbuf() : memstring(buffer, S) {}; |
| 1600 | |
| 1601 | /** |
| 1602 | * Create a string buffer from a null terminated string. |
| 1603 | * @param text to place in object. |
| 1604 | */ |
| 1605 | inline stringbuf(const char *text) : memstring(buffer, S) {set(text);}; |
| 1606 | |
| 1607 | /** |
| 1608 | * Assign a string buffer from a null terminated string. |
| 1609 | * @param text to assign to object. |
| 1610 | */ |
| 1611 | inline void operator=(const char *text) |
| 1612 | {set(text);}; |
| 1613 | |
| 1614 | /** |
| 1615 | * Assign a string buffer from another string object. |
| 1616 | * @param object to assign from. |
| 1617 | */ |
| 1618 | inline void operator=(String& object) |
| 1619 | {set(object.c_str());}; |
| 1620 | }; |
| 1621 | |
| 1622 | #if !defined(_MSWINDOWS_) && !defined(__QNX__) |
| 1623 | |
| 1624 | /** |
| 1625 | * Convenience function for case insensitive null terminated string compare. |
| 1626 | * @param string1 to compare. |
| 1627 | * @param string2 to compare. |
| 1628 | * @return 0 if equal, > 0 if s2 > s1, < 0 if s2 < s1. |
| 1629 | */ |
| 1630 | extern "C" inline int stricmp(const char *string1, const char *string2) |
| 1631 | {return String::case_compare(string1, string2);} |
| 1632 | |
| 1633 | /** |
| 1634 | * Convenience function for case insensitive null terminated string compare. |
| 1635 | * @param string1 to compare. |
| 1636 | * @param string2 to compare. |
| 1637 | * @param max size of string to compare. |
| 1638 | * @return 0 if equal, > 0 if s2 > s1, < 0 if s2 < s1. |
| 1639 | */ |
| 1640 | extern "C" inline int strnicmp(const char *string1, const char *string2, size_t max) |
| 1641 | {return String::case_compare(string1, string2, max);} |
| 1642 | |
| 1643 | #endif |
| 1644 | |
| 1645 | /** |
| 1646 | * Compare two null terminated strings if equal. |
| 1647 | * @param s1 string to compare. |
| 1648 | * @param s2 string to compare. |
| 1649 | * @return true if equal. |
| 1650 | */ |
| 1651 | inline bool eq(char const *s1, char const *s2) |
| 1652 | {return String::equal(s1, s2);} |
| 1653 | |
| 1654 | inline bool ne(char const *s1, char const *s2) |
| 1655 | {return !String::equal(s1, s2);} |
| 1656 | |
| 1657 | /** |
| 1658 | * Compare two null terminated strings if equal up to specified size. |
| 1659 | * @param s1 string to compare. |
| 1660 | * @param s2 string to compare. |
| 1661 | * @param size of string to compare. |
| 1662 | * @return true if equal. |
| 1663 | */ |
| 1664 | inline bool eq(char const *s1, char const *s2, size_t size) |
| 1665 | {return String::equal(s1, s2, size);} |
| 1666 | |
| 1667 | inline bool ne(char const *s1, char const *s2, size_t size) |
| 1668 | {return !String::equal(s1, s2, size);} |
| 1669 | |
| 1670 | /** |
| 1671 | * Compare two string objects if equal. The left string is an object, |
| 1672 | * the right may be an object or converted to a const string. The |
| 1673 | * compare virtual method of the left object is used, so we can do |
| 1674 | * things like collation order or derived class specific sorting. |
| 1675 | * @param s1 string to compare. |
| 1676 | * @param s2 string to compare. |
| 1677 | * @return true if equal. |
| 1678 | */ |
| 1679 | inline bool eq(String &s1, const char *s2) |
| 1680 | {return s1.compare(s2) == 0;} |
| 1681 | |
| 1682 | inline bool ne(String &s1, String &s2) |
| 1683 | {return s1.compare(s2) != 0;} |
| 1684 | |
| 1685 | inline bool lt(String &s1, const char *s2) |
| 1686 | {return s1.compare(s2) < 0;} |
| 1687 | |
| 1688 | inline bool gt(String &s1, const char *s2) |
| 1689 | {return s1.compare(s2) > 0;} |
| 1690 | |
| 1691 | inline bool le(String &s1, const char *s2) |
| 1692 | {return s1.compare(s2) <= 0;} |
| 1693 | |
| 1694 | inline bool ge(String &s1, const char *s2) |
| 1695 | {return s1.compare(s2) >= 0;} |
| 1696 | |
| 1697 | /** |
| 1698 | * Compare two null terminated strings if equal ignoring case. This is |
| 1699 | * related to stricmp or gcc strcasecmp. |
| 1700 | * @param s1 string to compare. |
| 1701 | * @param s2 string to compare. |
| 1702 | * @return true if equal. |
| 1703 | */ |
| 1704 | inline bool eq_case(char const *s1, char const *s2) |
| 1705 | {return String::eq_case(s1, s2);} |
| 1706 | |
| 1707 | inline bool ne_case(char const *s1, char const *s2) |
| 1708 | {return !String::eq_case(s1, s2);} |
| 1709 | |
| 1710 | /** |
| 1711 | * Compare two null terminated strings if equal for a specified size |
| 1712 | * ignoring case. This is related to stricmp or gcc strcasecmp. |
| 1713 | * @param s1 string to compare. |
| 1714 | * @param s2 string to compare. |
| 1715 | * @param size of string to compare. |
| 1716 | * @return true if equal. |
| 1717 | */ |
| 1718 | inline bool eq_case(char const *s1, char const *s2, size_t size) |
| 1719 | {return String::eq_case(s1, s2, size);} |
| 1720 | |
| 1721 | inline String str(const char *string) |
| 1722 | {return (String)string;} |
| 1723 | |
| 1724 | inline String str(String& string) |
| 1725 | {return (String)string;} |
| 1726 | |
| 1727 | inline String str(short value) |
| 1728 | {String temp(16, "%hd", value); return temp;} |
| 1729 | |
| 1730 | inline String str(unsigned short value) |
| 1731 | {String temp(16, "%hu", value); return temp;} |
| 1732 | |
| 1733 | inline String str(long value) |
| 1734 | {String temp(32, "%ld", value); return temp;} |
| 1735 | |
| 1736 | inline String str(unsigned long value) |
| 1737 | {String temp(32, "%lu", value); return temp;} |
| 1738 | |
| 1739 | inline String str(double value) |
| 1740 | {String temp(40, "%f", value); return temp;} |
| 1741 | |
| 1742 | String str(CharacterProtocol& cp, strsize_t size); |
| 1743 | |
| 1744 | template<> |
| 1745 | inline void swap<string_t>(string_t& s1, string_t& s2) |
| 1746 | {String::swap(s1, s2);} |
| 1747 | |
| 1748 | class __EXPORT strdup_t |
| 1749 | { |
| 1750 | private: |
| 1751 | char *data; |
| 1752 | |
| 1753 | public: |
| 1754 | inline strdup_t() |
| 1755 | {data = NULL;} |
| 1756 | |
| 1757 | inline strdup_t(char *str) |
| 1758 | {data = str;} |
| 1759 | |
| 1760 | inline ~strdup_t() |
| 1761 | {if(data) ::free(data);} |
| 1762 | |
| 1763 | inline strdup_t& operator=(char *str) |
| 1764 | {if(data) ::free(data); data = str; return *this;} |
| 1765 | |
| 1766 | inline operator bool() const |
| 1767 | {return data != NULL;} |
| 1768 | |
| 1769 | inline bool operator!() const |
| 1770 | {return data == NULL;} |
| 1771 | |
| 1772 | inline operator char*() const |
| 1773 | {return data;} |
| 1774 | |
| 1775 | inline const char *c_str(void) const |
| 1776 | {return data;}; |
| 1777 | |
| 1778 | inline const char *operator*() const |
| 1779 | {return data;} |
| 1780 | |
| 1781 | inline char& operator[](int size) |
| 1782 | {return data[size];} |
| 1783 | |
| 1784 | inline char *operator+(size_t size) |
| 1785 | {return data + size;} |
| 1786 | }; |
| 1787 | |
| 1788 | |
| 1789 | END_NAMESPACE |
| 1790 | |
| 1791 | #endif |