| 1 |
// -*- related-file-name: "../../liblcdf/straccum.cc" -*- |
| 2 |
#ifndef LCDF_STRACCUM_HH |
| 3 |
#define LCDF_STRACCUM_HH |
| 4 |
#include <string.h> |
| 5 |
#include <assert.h> |
| 6 |
#include <lcdf/string.hh> |
| 7 |
#if HAVE_PERMSTRING |
| 8 |
# include <lcdf/permstr.hh> |
| 9 |
#endif |
| 10 |
#if __GNUC__ > 4 |
| 11 |
# define LCDF_SNPRINTF_ATTR __attribute__((__format__(__printf__, 3, 4))) |
| 12 |
#else |
| 13 |
# define LCDF_SNPRINTF_ATTR /* nothing */ |
| 14 |
#endif |
| 15 |
template<typename T> class Vector; |
| 16 |
|
| 17 |
/** @file <lcdf/straccum.hh> |
| 18 |
@brief Click's StringAccum class, used to construct Strings efficiently from pieces. |
| 19 |
*/ |
| 20 |
|
| 21 |
class StringAccum { public: |
| 22 |
|
| 23 |
typedef const char* const_iterator; |
| 24 |
typedef char* iterator; |
| 25 |
|
| 26 |
typedef int (StringAccum::*unspecified_bool_type)() const; |
| 27 |
|
| 28 |
inline StringAccum(); |
| 29 |
explicit inline StringAccum(int capacity); |
| 30 |
explicit inline StringAccum(const char* cstr); |
| 31 |
inline StringAccum(const char* s, int len); |
| 32 |
inline StringAccum(const String& str); |
| 33 |
inline StringAccum(const StringAccum& x); |
| 34 |
inline ~StringAccum(); |
| 35 |
|
| 36 |
inline const char* data() const; |
| 37 |
inline char* data(); |
| 38 |
inline const unsigned char* udata() const; |
| 39 |
inline unsigned char* udata(); |
| 40 |
inline int length() const; |
| 41 |
inline int capacity() const; |
| 42 |
|
| 43 |
inline bool empty() const; |
| 44 |
inline bool operator!() const; |
| 45 |
inline bool out_of_memory() const; |
| 46 |
|
| 47 |
inline const_iterator begin() const; |
| 48 |
inline iterator begin(); |
| 49 |
inline const_iterator end() const; |
| 50 |
inline iterator end(); |
| 51 |
|
| 52 |
|
| 53 |
/** @brief Return true iff the StringAccum contains characters. |
| 54 |
* |
| 55 |
* Returns false for empty and out-of-memory StringAccums. */ |
| 56 |
inline operator unspecified_bool_type() const { |
| 57 |
return _len != 0 ? &StringAccum::capacity : 0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** @brief Null-terminate this StringAccum and return its data. |
| 62 |
* |
| 63 |
* Note that the null character does not contribute to the StringAccum's |
| 64 |
* length(), and later append() and similar operations can overwrite it. |
| 65 |
* If appending the null character fails, the StringAccum becomes |
| 66 |
* out-of-memory and the returned value is a null string. */ |
| 67 |
const char* c_str(); |
| 68 |
|
| 69 |
|
| 70 |
inline char operator[](int i) const; |
| 71 |
inline char& operator[](int i); |
| 72 |
inline char front() const; |
| 73 |
inline char& front(); |
| 74 |
inline char back() const; |
| 75 |
inline char& back(); |
| 76 |
|
| 77 |
inline void clear(); |
| 78 |
|
| 79 |
inline char* reserve(int n); |
| 80 |
inline void set_length(int len); |
| 81 |
|
| 82 |
/** @brief Set the StringAccum's length to @a len. |
| 83 |
* @pre @a len >= 0 |
| 84 |
* @return 0 on success, -ENOMEM on failure */ |
| 85 |
int resize(int len); |
| 86 |
|
| 87 |
inline void adjust_length(int delta); |
| 88 |
inline char* extend(int nadjust, int nreserve = 0); |
| 89 |
|
| 90 |
inline void pop_back(int n = 1); |
| 91 |
|
| 92 |
|
| 93 |
inline void append(char c); |
| 94 |
inline void append(unsigned char c); |
| 95 |
|
| 96 |
/** @brief Append @a len copies of character @a c to the StringAccum. */ |
| 97 |
void append_fill(int c, int len); |
| 98 |
|
| 99 |
inline void append_utf8(unsigned ch); |
| 100 |
|
| 101 |
/** @brief Append the null-terminated C string @a s to this StringAccum. |
| 102 |
* @param s data to append */ |
| 103 |
void append(const char* s); |
| 104 |
inline void append(const char* s, int len); |
| 105 |
inline void append(const unsigned char* s, int len); |
| 106 |
inline void append(const char* begin, const char* end); |
| 107 |
inline void append(const unsigned char* begin, const unsigned char* end); |
| 108 |
|
| 109 |
// word joining |
| 110 |
void append_break_lines(const String& text, int linelen, const String& leftmargin = String()); |
| 111 |
|
| 112 |
/** @brief Append result of snprintf() to this StringAccum. |
| 113 |
* @param n maximum number of characters to print |
| 114 |
* @param format format argument to snprintf() |
| 115 |
* |
| 116 |
* The terminating null character is not appended to the string. |
| 117 |
* |
| 118 |
* @note The safe vsnprintf() variant is called if it exists. It does in |
| 119 |
* the Linux kernel, and on modern Unix variants. However, if it does not |
| 120 |
* exist on your machine, then this function is actually unsafe, and you |
| 121 |
* should make sure that the printf() invocation represented by your |
| 122 |
* arguments will never write more than @a n characters, not including the |
| 123 |
* terminating null. */ |
| 124 |
StringAccum& snprintf(int n, const char* format, ...) LCDF_SNPRINTF_ATTR; |
| 125 |
|
| 126 |
|
| 127 |
/** @brief Return a String object with this StringAccum's contents. |
| 128 |
* |
| 129 |
* This operation donates the StringAccum's memory to the returned String. |
| 130 |
* After a call to take_string(), the StringAccum object becomes empty, |
| 131 |
* and any future append() operations may cause memory allocations. If |
| 132 |
* the StringAccum is out-of-memory, the returned String is also |
| 133 |
* out-of-memory, but the StringAccum's out-of-memory state is reset. */ |
| 134 |
String take_string(); |
| 135 |
|
| 136 |
|
| 137 |
inline StringAccum& operator=(const StringAccum& x); |
| 138 |
|
| 139 |
/** @brief Swap this StringAccum's contents with @a x. */ |
| 140 |
void swap(StringAccum& x); |
| 141 |
|
| 142 |
// see also operator<< declarations below |
| 143 |
|
| 144 |
static const char* double_format; |
| 145 |
|
| 146 |
private: |
| 147 |
|
| 148 |
enum { |
| 149 |
MEMO_SPACE = String::MEMO_SPACE |
| 150 |
}; |
| 151 |
|
| 152 |
unsigned char* _s; |
| 153 |
int _len; |
| 154 |
int _cap; |
| 155 |
|
| 156 |
char* grow(int); |
| 157 |
void assign_out_of_memory(); |
| 158 |
|
| 159 |
char* hard_extend(int nadjust, int nreserve); |
| 160 |
void hard_append(const char* s, int len); |
| 161 |
void append_utf8_hard(unsigned ch); |
| 162 |
|
| 163 |
friend StringAccum& operator<<(StringAccum& sa, const char* s); |
| 164 |
friend StringAccum& operator<<(StringAccum& sa, const String& str); |
| 165 |
#if HAVE_PERMSTRING |
| 166 |
friend StringAccum& operator<<(StringAccum& sa, PermString s); |
| 167 |
#endif |
| 168 |
|
| 169 |
}; |
| 170 |
|
| 171 |
inline StringAccum& operator<<(StringAccum&, char); |
| 172 |
inline StringAccum& operator<<(StringAccum&, unsigned char); |
| 173 |
inline StringAccum& operator<<(StringAccum&, const char*); |
| 174 |
inline StringAccum& operator<<(StringAccum&, const String&); |
| 175 |
inline StringAccum& operator<<(StringAccum&, const StringAccum&); |
| 176 |
#ifdef HAVE_PERMSTRING |
| 177 |
inline StringAccum& operator<<(StringAccum&, PermString); |
| 178 |
#endif |
| 179 |
|
| 180 |
inline StringAccum& operator<<(StringAccum&, bool); |
| 181 |
inline StringAccum& operator<<(StringAccum&, short); |
| 182 |
inline StringAccum& operator<<(StringAccum&, unsigned short); |
| 183 |
inline StringAccum& operator<<(StringAccum&, int); |
| 184 |
inline StringAccum& operator<<(StringAccum&, unsigned); |
| 185 |
StringAccum& operator<<(StringAccum&, long); |
| 186 |
StringAccum& operator<<(StringAccum&, unsigned long); |
| 187 |
StringAccum& operator<<(StringAccum&, double); |
| 188 |
|
| 189 |
|
| 190 |
/** @brief Construct an empty StringAccum (with length 0). */ |
| 191 |
inline StringAccum::StringAccum() |
| 192 |
: _s(0), _len(0), _cap(0) { |
| 193 |
} |
| 194 |
|
| 195 |
/** @brief Construct a StringAccum with room for at least @a capacity |
| 196 |
* characters. |
| 197 |
* @param capacity initial capacity |
| 198 |
* |
| 199 |
* If @a capacity <= 0, the StringAccum is created empty. If @a capacity |
| 200 |
* is too large (so that @a capacity bytes of memory can't be allocated), |
| 201 |
* the StringAccum falls back to a smaller capacity (possibly zero). */ |
| 202 |
inline StringAccum::StringAccum(int capacity) |
| 203 |
: _len(0) { |
| 204 |
assert(capacity >= 0); |
| 205 |
if (capacity |
| 206 |
&& (_s = new unsigned char[capacity + MEMO_SPACE])) { |
| 207 |
_s += MEMO_SPACE; |
| 208 |
_cap = capacity; |
| 209 |
} else { |
| 210 |
_s = 0; |
| 211 |
_cap = 0; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** @brief Construct a StringAccum containing the characters in @a s. */ |
| 216 |
inline StringAccum::StringAccum(const char* cstr) |
| 217 |
: _s(0), _len(0), _cap(0) { |
| 218 |
append(cstr); |
| 219 |
} |
| 220 |
|
| 221 |
/** @brief Construct a StringAccum containing the characters in @a s. */ |
| 222 |
inline StringAccum::StringAccum(const char* s, int len) |
| 223 |
: _s(0), _len(0), _cap(0) { |
| 224 |
append(s, len); |
| 225 |
} |
| 226 |
|
| 227 |
/** @brief Construct a StringAccum containing the characters in @a str. */ |
| 228 |
inline StringAccum::StringAccum(const String& str) |
| 229 |
: _s(0), _len(0), _cap(0) { |
| 230 |
append(str.begin(), str.end()); |
| 231 |
} |
| 232 |
|
| 233 |
/** @brief Construct a StringAccum containing a copy of @a x. */ |
| 234 |
inline StringAccum::StringAccum(const StringAccum& x) |
| 235 |
: _s(0), _len(0), _cap(0) { |
| 236 |
append(x.data(), x.length()); |
| 237 |
} |
| 238 |
|
| 239 |
/** @brief Destroy a StringAccum, freeing its memory. */ |
| 240 |
inline StringAccum::~StringAccum() { |
| 241 |
if (_cap > 0) |
| 242 |
delete[] (_s - MEMO_SPACE); |
| 243 |
} |
| 244 |
|
| 245 |
/** @brief Return the contents of the StringAccum. |
| 246 |
* @return The StringAccum's contents. |
| 247 |
* |
| 248 |
* The return value is null if the StringAccum is empty or out-of-memory. |
| 249 |
* The returned data() value points to writable memory (unless the |
| 250 |
* StringAccum itself is const). */ |
| 251 |
inline const char* StringAccum::data() const { |
| 252 |
return reinterpret_cast<const char*>(_s); |
| 253 |
} |
| 254 |
|
| 255 |
/** @overload */ |
| 256 |
inline char* StringAccum::data() { |
| 257 |
return reinterpret_cast<char*>(_s); |
| 258 |
} |
| 259 |
|
| 260 |
inline const unsigned char* StringAccum::udata() const { |
| 261 |
return _s; |
| 262 |
} |
| 263 |
|
| 264 |
inline unsigned char* StringAccum::udata() { |
| 265 |
return _s; |
| 266 |
} |
| 267 |
|
| 268 |
/** @brief Return true iff the StringAccum is empty or out-of-memory. */ |
| 269 |
inline bool StringAccum::empty() const { |
| 270 |
return _len == 0; |
| 271 |
} |
| 272 |
|
| 273 |
/** @brief Return the length of the StringAccum. */ |
| 274 |
inline int StringAccum::length() const { |
| 275 |
return _len; |
| 276 |
} |
| 277 |
|
| 278 |
/** @brief Return the StringAccum's current capacity. |
| 279 |
* |
| 280 |
* The capacity is the maximum length the StringAccum can hold without |
| 281 |
* incurring a memory allocation. Returns -1 for out-of-memory |
| 282 |
* StringAccums. */ |
| 283 |
inline int StringAccum::capacity() const { |
| 284 |
return _cap; |
| 285 |
} |
| 286 |
|
| 287 |
/** @brief Return an iterator for the first character in the StringAccum. |
| 288 |
* |
| 289 |
* StringAccum iterators are simply pointers into string data, so they are |
| 290 |
* quite efficient. @sa StringAccum::data */ |
| 291 |
inline StringAccum::const_iterator StringAccum::begin() const { |
| 292 |
return reinterpret_cast<char*>(_s); |
| 293 |
} |
| 294 |
|
| 295 |
/** @overload */ |
| 296 |
inline StringAccum::iterator StringAccum::begin() { |
| 297 |
return reinterpret_cast<char*>(_s); |
| 298 |
} |
| 299 |
|
| 300 |
/** @brief Return an iterator for the end of the StringAccum. |
| 301 |
* |
| 302 |
* The return value points one character beyond the last character in the |
| 303 |
* StringAccum. */ |
| 304 |
inline StringAccum::const_iterator StringAccum::end() const { |
| 305 |
return reinterpret_cast<char*>(_s + _len); |
| 306 |
} |
| 307 |
|
| 308 |
/** @overload */ |
| 309 |
inline StringAccum::iterator StringAccum::end() { |
| 310 |
return reinterpret_cast<char*>(_s + _len); |
| 311 |
} |
| 312 |
|
| 313 |
/** @brief Return true iff the StringAccum does not contain characters. |
| 314 |
* |
| 315 |
* Returns true for empty and out-of-memory StringAccums. */ |
| 316 |
inline bool StringAccum::operator!() const { |
| 317 |
return _len == 0; |
| 318 |
} |
| 319 |
|
| 320 |
/** @brief Return true iff the StringAccum is out-of-memory. */ |
| 321 |
inline bool StringAccum::out_of_memory() const { |
| 322 |
return _cap < 0; |
| 323 |
} |
| 324 |
|
| 325 |
/** @brief Return the <a>i</a>th character in the string. |
| 326 |
* @param i character index |
| 327 |
* @pre 0 <= @a i < length() */ |
| 328 |
inline char StringAccum::operator[](int i) const { |
| 329 |
assert((unsigned) i < (unsigned) _len); |
| 330 |
return static_cast<char>(_s[i]); |
| 331 |
} |
| 332 |
|
| 333 |
/** @brief Return a reference to the <a>i</a>th character in the string. |
| 334 |
* @param i character index |
| 335 |
* @pre 0 <= @a i < length() */ |
| 336 |
inline char& StringAccum::operator[](int i) { |
| 337 |
assert((unsigned) i < (unsigned) _len); |
| 338 |
return reinterpret_cast<char&>(_s[i]); |
| 339 |
} |
| 340 |
|
| 341 |
/** @brief Return the first character in the string. |
| 342 |
* @pre length() > 0 */ |
| 343 |
inline char StringAccum::front() const { |
| 344 |
assert(_len > 0); |
| 345 |
return static_cast<char>(_s[0]); |
| 346 |
} |
| 347 |
|
| 348 |
/** @brief Return a reference to the first character in the string. |
| 349 |
* @pre length() > 0 */ |
| 350 |
inline char& StringAccum::front() { |
| 351 |
assert(_len > 0); |
| 352 |
return reinterpret_cast<char&>(_s[0]); |
| 353 |
} |
| 354 |
|
| 355 |
/** @brief Return the last character in the string. |
| 356 |
* @pre length() > 0 */ |
| 357 |
inline char StringAccum::back() const { |
| 358 |
assert(_len > 0); |
| 359 |
return static_cast<char>(_s[_len - 1]); |
| 360 |
} |
| 361 |
|
| 362 |
/** @brief Return a reference to the last character in the string. |
| 363 |
* @pre length() > 0 */ |
| 364 |
inline char& StringAccum::back() { |
| 365 |
assert(_len > 0); |
| 366 |
return reinterpret_cast<char&>(_s[_len - 1]); |
| 367 |
} |
| 368 |
|
| 369 |
/** @brief Clear the StringAccum's comments. |
| 370 |
* |
| 371 |
* All characters in the StringAccum are erased. Also resets the |
| 372 |
* StringAccum's out-of-memory status. */ |
| 373 |
inline void StringAccum::clear() { |
| 374 |
if (_cap < 0) { |
| 375 |
_cap = 0; |
| 376 |
_s = 0; |
| 377 |
} |
| 378 |
_len = 0; |
| 379 |
} |
| 380 |
|
| 381 |
/** @brief Reserve space for at least @a n characters. |
| 382 |
* @return a pointer to at least @a n characters, or null if allocation |
| 383 |
* fails |
| 384 |
* @pre @a n >= 0 |
| 385 |
* |
| 386 |
* reserve() does not change the string's length(), only its capacity(). |
| 387 |
* In a frequent usage pattern, code calls reserve(), passing an upper |
| 388 |
* bound on the characters that could be written by a series of |
| 389 |
* operations. After writing into the returned buffer, adjust_length() is |
| 390 |
* called to account for the number of characters actually written. |
| 391 |
* |
| 392 |
* On failure, null is returned and errno is set to ENOMEM. */ |
| 393 |
inline char* StringAccum::reserve(int n) { |
| 394 |
assert(n >= 0); |
| 395 |
if (_len + n <= _cap) |
| 396 |
return reinterpret_cast<char*>(_s + _len); |
| 397 |
else |
| 398 |
return grow(_len + n); |
| 399 |
} |
| 400 |
|
| 401 |
/** @brief Set the StringAccum's length to @a len. |
| 402 |
* @param len new length in characters |
| 403 |
* @pre 0 <= @a len <= capacity() |
| 404 |
* @sa adjust_length */ |
| 405 |
inline void StringAccum::set_length(int len) { |
| 406 |
assert(len >= 0 && _len <= _cap); |
| 407 |
_len = len; |
| 408 |
} |
| 409 |
|
| 410 |
/** @brief Adjust the StringAccum's length. |
| 411 |
* @param delta length adjustment |
| 412 |
* @pre If @a delta > 0, then length() + @a delta <= capacity(). |
| 413 |
* If @a delta < 0, then length() + delta >= 0. |
| 414 |
* |
| 415 |
* The StringAccum's length after adjust_length(@a delta) equals its old |
| 416 |
* length plus @a delta. Generally adjust_length() is used after a call |
| 417 |
* to reserve(). @sa set_length */ |
| 418 |
inline void StringAccum::adjust_length(int delta) { |
| 419 |
assert(_len + delta >= 0 && _len + delta <= _cap); |
| 420 |
_len += delta; |
| 421 |
} |
| 422 |
|
| 423 |
/** @brief Reserve space and adjust length in one operation. |
| 424 |
* @param nadjust number of characters to reserve and adjust length |
| 425 |
* @param nreserve additional characters to reserve |
| 426 |
* @pre @a nadjust >= 0 and @a nreserve >= 0 |
| 427 |
* |
| 428 |
* This operation combines the effects of reserve(@a nadjust + @a |
| 429 |
* nreserve) and adjust_length(@a nadjust). Returns the result of the |
| 430 |
* reserve() call. */ |
| 431 |
inline char* StringAccum::extend(int nadjust, int nreserve) { |
| 432 |
assert(nadjust >= 0 && nreserve >= 0); |
| 433 |
if (_len + nadjust + nreserve <= _cap) { |
| 434 |
char* x = reinterpret_cast<char*>(_s + _len); |
| 435 |
_len += nadjust; |
| 436 |
return x; |
| 437 |
} else |
| 438 |
return hard_extend(nadjust, nreserve); |
| 439 |
} |
| 440 |
|
| 441 |
/** @brief Remove characters from the end of the StringAccum. |
| 442 |
* @param n number of characters to remove |
| 443 |
* @pre @a n >= 0 and @a n <= length() |
| 444 |
* |
| 445 |
* Same as adjust_length(-@a n). */ |
| 446 |
inline void StringAccum::pop_back(int n) { |
| 447 |
assert(n >= 0 && _len >= n); |
| 448 |
_len -= n; |
| 449 |
} |
| 450 |
|
| 451 |
/** @brief Append character @a c to the StringAccum. |
| 452 |
* @param c character to append */ |
| 453 |
inline void StringAccum::append(char c) { |
| 454 |
if (_len < _cap || grow(_len)) |
| 455 |
_s[_len++] = c; |
| 456 |
} |
| 457 |
|
| 458 |
/** @overload */ |
| 459 |
inline void StringAccum::append(unsigned char c) { |
| 460 |
append(static_cast<char>(c)); |
| 461 |
} |
| 462 |
|
| 463 |
/** @brief Append the UTF-8 encoding of Unicode character @a ch. */ |
| 464 |
inline void StringAccum::append_utf8(unsigned ch) { |
| 465 |
if (ch < 0x80) |
| 466 |
append((unsigned char) ch); |
| 467 |
else |
| 468 |
append_utf8_hard(ch); |
| 469 |
} |
| 470 |
|
| 471 |
/** @brief Append the first @a len characters of @a s to this StringAccum. |
| 472 |
* @param s data to append |
| 473 |
* @param len length of data |
| 474 |
* @pre @a len >= 0 */ |
| 475 |
inline void StringAccum::append(const char* s, int len) { |
| 476 |
assert(len >= 0); |
| 477 |
if (_len + len <= _cap) { |
| 478 |
memcpy(_s + _len, s, len); |
| 479 |
_len += len; |
| 480 |
} else |
| 481 |
hard_append(s, len); |
| 482 |
} |
| 483 |
|
| 484 |
/** @overload */ |
| 485 |
inline void StringAccum::append(const unsigned char* s, int len) { |
| 486 |
append(reinterpret_cast<const char*>(s), len); |
| 487 |
} |
| 488 |
|
| 489 |
/** @brief Append the data from @a begin to @a end to the end of this |
| 490 |
* StringAccum. |
| 491 |
* |
| 492 |
* Does nothing if @a begin >= @a end. */ |
| 493 |
inline void StringAccum::append(const char* begin, const char* end) { |
| 494 |
if (begin < end) |
| 495 |
append(begin, end - begin); |
| 496 |
} |
| 497 |
|
| 498 |
/** @overload */ |
| 499 |
inline void StringAccum::append(const unsigned char* begin, const unsigned char* end) { |
| 500 |
if (begin < end) |
| 501 |
append(begin, end - begin); |
| 502 |
} |
| 503 |
|
| 504 |
/** @brief Assign this StringAccum to @a x. */ |
| 505 |
inline StringAccum& StringAccum::operator=(const StringAccum& x) { |
| 506 |
if (&x != this) { |
| 507 |
if (out_of_memory()) |
| 508 |
_s = 0, _cap = 0; |
| 509 |
_len = 0; |
| 510 |
append(x.data(), x.length()); |
| 511 |
} |
| 512 |
return *this; |
| 513 |
} |
| 514 |
|
| 515 |
/** @relates StringAccum |
| 516 |
@brief Append character @a c to StringAccum @a sa. |
| 517 |
@return @a sa |
| 518 |
@note Same as @a sa.append(@a c). */ |
| 519 |
inline StringAccum& |
| 520 |
operator<<(StringAccum& sa, char c) |
| 521 |
{ |
| 522 |
sa.append(c); |
| 523 |
return sa; |
| 524 |
} |
| 525 |
|
| 526 |
/** @relates StringAccum |
| 527 |
@brief Append character @a c to StringAccum @a sa. |
| 528 |
@return @a sa |
| 529 |
@note Same as @a sa.append(@a c). */ |
| 530 |
inline StringAccum& |
| 531 |
operator<<(StringAccum& sa, unsigned char c) |
| 532 |
{ |
| 533 |
sa.append(c); |
| 534 |
return sa; |
| 535 |
} |
| 536 |
|
| 537 |
/** @relates StringAccum |
| 538 |
@brief Append null-terminated C string @a cstr to StringAccum @a sa. |
| 539 |
@return @a sa |
| 540 |
@note Same as @a sa.append(@a cstr). */ |
| 541 |
inline StringAccum& |
| 542 |
operator<<(StringAccum& sa, const char* cstr) |
| 543 |
{ |
| 544 |
sa.append(cstr); |
| 545 |
return sa; |
| 546 |
} |
| 547 |
|
| 548 |
/** @relates StringAccum |
| 549 |
@brief Append "true" or "false" to @a sa, depending on @a b. |
| 550 |
@return @a sa */ |
| 551 |
inline StringAccum& |
| 552 |
operator<<(StringAccum& sa, bool b) |
| 553 |
{ |
| 554 |
static const char truefalse[] = "truefalse"; |
| 555 |
if (b) |
| 556 |
sa.append(truefalse, 4); |
| 557 |
else |
| 558 |
sa.append(truefalse + 4, 5); |
| 559 |
return sa; |
| 560 |
} |
| 561 |
|
| 562 |
/** @relates StringAccum |
| 563 |
@brief Append decimal representation of @a i to @a sa. |
| 564 |
@return @a sa */ |
| 565 |
inline StringAccum& |
| 566 |
operator<<(StringAccum& sa, short i) |
| 567 |
{ |
| 568 |
return sa << static_cast<long>(i); |
| 569 |
} |
| 570 |
|
| 571 |
/** @relates StringAccum |
| 572 |
@brief Append decimal representation of @a u to @a sa. |
| 573 |
@return @a sa */ |
| 574 |
inline StringAccum& |
| 575 |
operator<<(StringAccum& sa, unsigned short u) |
| 576 |
{ |
| 577 |
return sa << static_cast<unsigned long>(u); |
| 578 |
} |
| 579 |
|
| 580 |
/** @relates StringAccum |
| 581 |
@brief Append decimal representation of @a i to @a sa. |
| 582 |
@return @a sa */ |
| 583 |
inline StringAccum& |
| 584 |
operator<<(StringAccum& sa, int i) |
| 585 |
{ |
| 586 |
return sa << static_cast<long>(i); |
| 587 |
} |
| 588 |
|
| 589 |
/** @relates StringAccum |
| 590 |
@brief Append decimal representation of @a u to @a sa. |
| 591 |
@return @a sa */ |
| 592 |
inline StringAccum& |
| 593 |
operator<<(StringAccum& sa, unsigned u) |
| 594 |
{ |
| 595 |
return sa << static_cast<unsigned long>(u); |
| 596 |
} |
| 597 |
|
| 598 |
/** @relates StringAccum |
| 599 |
@brief Append the contents of @a str to @a sa. |
| 600 |
@return @a sa */ |
| 601 |
inline StringAccum& |
| 602 |
operator<<(StringAccum& sa, const String& str) |
| 603 |
{ |
| 604 |
if (!str.out_of_memory()) |
| 605 |
sa.hard_append(str.begin(), str.length()); |
| 606 |
else |
| 607 |
sa.assign_out_of_memory(); |
| 608 |
return sa; |
| 609 |
} |
| 610 |
|
| 611 |
#ifdef HAVE_PERMSTRING |
| 612 |
inline StringAccum& |
| 613 |
operator<<(StringAccum& sa, PermString s) |
| 614 |
{ |
| 615 |
sa.append(s.c_str(), s.length()); |
| 616 |
return sa; |
| 617 |
} |
| 618 |
#endif |
| 619 |
|
| 620 |
/** @relates StringAccum |
| 621 |
@brief Append the contents of @a sb to @a sa. |
| 622 |
@return @a sa */ |
| 623 |
inline StringAccum& |
| 624 |
operator<<(StringAccum& sa, const StringAccum& sb) |
| 625 |
{ |
| 626 |
sa.append(sb.begin(), sb.end()); |
| 627 |
return sa; |
| 628 |
} |
| 629 |
|
| 630 |
inline bool |
| 631 |
operator==(StringAccum& sa, const char* s) |
| 632 |
{ |
| 633 |
return strcmp(sa.c_str(), s) == 0; |
| 634 |
} |
| 635 |
|
| 636 |
inline bool |
| 637 |
operator!=(StringAccum& sa, const char* s) |
| 638 |
{ |
| 639 |
return strcmp(sa.c_str(), s) != 0; |
| 640 |
} |
| 641 |
|
| 642 |
#undef LCDF_SNPRINTF_ATTR |
| 643 |
#endif |