2#ifndef CLICK_STRACCUM_HH
3#define CLICK_STRACCUM_HH
4#include <click/glue.hh>
6#ifdef CLICK_LINUXMODULE
7# include <click/cxxprotect.h>
9# include <asm/string.h>
11# include <click/cxxunprotect.h>
12#elif defined(CLICK_BSDMODULE)
13# include <sys/systm.h>
18# define CLICK_SNPRINTF_ATTR __attribute__((__format__(__printf__, 3, 4)))
20# define CLICK_SNPRINTF_ATTR
30 typedef const char *const_iterator;
31 typedef char *iterator;
33 typedef int (
StringAccum::*unspecified_bool_type)()
const;
39#if HAVE_CXX_RVALUE_REFERENCES
45#if HAVE_CXX_RVALUE_REFERENCES
49 inline const char *
data()
const;
56 inline operator unspecified_bool_type()
const;
57 inline bool empty()
const;
60 inline const_iterator
begin()
const;
61 inline iterator
begin();
62 inline const_iterator
end()
const;
63 inline iterator
end();
67 inline char front()
const;
69 inline char back()
const;
80 inline char *
extend(
int nadjust,
int nreserve = 0);
84 inline void append(
char c);
85 inline void append(
unsigned char c);
87 inline void append(
const char *cstr);
88 inline void append(
const char *s,
int len);
89 inline void append(
const unsigned char *s,
int len);
90 inline void append(
const char *first,
const char *last);
91 inline void append(
const unsigned char *first,
const unsigned char *last);
93 void append_numeric(String::intmax_t x,
int base = 10,
bool uppercase =
true);
94 void append_numeric(String::uintmax_t x,
int base = 10,
bool uppercase =
true);
104 inline void forward(
int delta) CLICK_DEPRECATED;
109 MEMO_SPACE = String::MEMO_SPACE
120 explicit rep_t(uninitialized_type) {
127 char *hard_extend(
int nadjust,
int nreserve);
128 void hard_append(
const char *s,
int len);
129 void hard_append_cstr(
const char *cstr);
130 bool append_utf8_hard(
int ch);
156#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
160#if defined(CLICK_USERLEVEL) || defined(CLICK_TOOL) || defined(CLICK_MINIOS)
181 && (s = (
unsigned char *) CLICK_LALLOC(
capacity + MEMO_SPACE))) {
182 r_.s = s + MEMO_SPACE;
197#if HAVE_CXX_RVALUE_REFERENCES
208 CLICK_LFREE(r_.s - MEMO_SPACE, r_.cap + MEMO_SPACE);
217 return reinterpret_cast<const char *
>(r_.s);
222 return reinterpret_cast<char *
>(r_.s);
244 return reinterpret_cast<char *
>(r_.s);
249 return reinterpret_cast<char *
>(r_.s);
257 return reinterpret_cast<char *
>(r_.s + r_.len);
262 return reinterpret_cast<char *
>(r_.s + r_.len);
266inline StringAccum::operator unspecified_bool_type()
const {
284 return unlikely(r_.cap < 0);
291 assert((
unsigned) i < (
unsigned) r_.len);
292 return static_cast<char>(r_.s[i]);
299 assert((
unsigned) i < (
unsigned) r_.len);
300 return reinterpret_cast<char &
>(r_.s[i]);
307 return static_cast<char>(r_.s[0]);
314 return reinterpret_cast<char &
>(r_.s[0]);
321 return static_cast<char>(r_.s[r_.len - 1]);
328 return reinterpret_cast<char &
>(r_.s[r_.len - 1]);
353 if (r_.len + n <= r_.cap)
354 return reinterpret_cast<char *
>(r_.s + r_.len);
356 return grow(r_.len + n);
364 assert(len >= 0 && r_.len <= r_.cap);
377 assert(r_.len + delta >= 0 && r_.len + delta <= r_.cap);
390#if CLICK_OPTIMIZE_SIZE || __OPTIMIZE_SIZE__
391 return hard_extend(nadjust, nreserve);
393 assert(nadjust >= 0 && nreserve >= 0);
394 if (r_.len + nadjust + nreserve <= r_.cap) {
395 char *x =
reinterpret_cast<char *
>(r_.s + r_.len);
399 return hard_extend(nadjust, nreserve);
409 assert(n >= 0 && r_.len >= n);
416 if (r_.len < r_.cap || grow(r_.len))
422 append(
static_cast<char>(c));
430#if CLICK_OPTIMIZE_SIZE || __OPTIMIZE_SIZE__
434 if (r_.len + len <= r_.cap) {
435 memcpy(r_.s + r_.len, s, len);
444 append(
reinterpret_cast<const char *
>(s), len);
450 if (CLICK_CONSTANT_CSTR(cstr))
451 append(cstr, strlen(cstr));
453 hard_append_cstr(cstr);
462 append(first, last - first);
468 append(first, last - first);
476 if (unlikely(ch <= 0))
478 else if (likely(ch <= 0x7F)) {
479 append(
static_cast<char>(ch));
482 return append_utf8_hard(ch);
496#if HAVE_CXX_RVALUE_REFERENCES
508inline void StringAccum::forward(
int delta) {
544 sa.
append(String::bool_data + (-x & 6), 5 - x);
552 return sa << static_cast<long>(x);
557 return sa << static_cast<unsigned long>(x);
562 return sa << static_cast<long>(x);
567 return sa << static_cast<unsigned long>(x);
584#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
622#undef CLICK_SNPRINTF_ATTR
Efficiently build up Strings from pieces.
Definition straccum.hh:28
void adjust_length(int delta)
Adjust the StringAccum's length.
Definition straccum.hh:376
StringAccum & operator=(const StringAccum &x)
Assign this StringAccum to x.
Definition straccum.hh:486
StringAccum & operator<<(StringAccum &sa, const StringAccum &x)
Append the contents of x to sa.
Definition straccum.hh:609
friend StringAccum & operator<<(StringAccum &sa, const String &str)
Append the contents of str to sa.
Definition straccum.hh:601
StringAccum & snprintf(int n, const char *format,...) CLICK_SNPRINTF_ATTR
Append result of snprintf() to this StringAccum.
Definition straccum.cc:373
StringAccum & operator<<(StringAccum &sa, char c)
Append character c to StringAccum sa.
Definition straccum.hh:517
const char * data() const
Return the contents of the StringAccum.
Definition straccum.hh:216
String take_string()
Return a String object with this StringAccum's contents.
Definition straccum.cc:211
const_iterator end() const
Return an iterator for the end of the StringAccum.
Definition straccum.hh:256
StringAccum & operator<<(StringAccum &sa, unsigned char c)
Append character c to StringAccum sa.
Definition straccum.hh:526
~StringAccum()
Destroy a StringAccum, freeing its memory.
Definition straccum.hh:206
int resize(int len)
Set the StringAccum's length to len.
Definition straccum.cc:103
int length() const
Return the length of the StringAccum.
Definition straccum.hh:226
const char * c_str()
Null-terminate this StringAccum and return its data.
Definition straccum.cc:134
bool out_of_memory() const
Test if the StringAccum is out-of-memory.
Definition straccum.hh:283
void append_numeric(String::intmax_t x, int base=10, bool uppercase=true)
Append string representation of x to this StringAccum.
Definition straccum.cc:322
void assign_out_of_memory()
Change this StringAccum into an out-of-memory StringAccum.
Definition straccum.cc:63
char back() const
Return the last character in the string.
Definition straccum.hh:319
StringAccum & operator<<(StringAccum &sa, const char *cstr)
Append null-terminated C string cstr to StringAccum sa.
Definition straccum.hh:535
void set_length(int len)
Set the StringAccum's length to len.
Definition straccum.hh:363
StringAccum()
Construct an empty StringAccum (with length 0).
Definition straccum.hh:167
void pop_back(int n=1)
Remove characters from the end of the StringAccum.
Definition straccum.hh:408
char front() const
Return the first character in the string.
Definition straccum.hh:305
void append_fill(int c, int len)
Append len copies of character c to the StringAccum.
Definition straccum.cc:143
bool operator!() const
Test if the StringAccum is empty.
Definition straccum.hh:273
char operator[](int i) const
Return the ith character in the string.
Definition straccum.hh:290
void swap(StringAccum &x)
Swap this StringAccum's contents with x.
Definition straccum.cc:229
void clear()
Clear the StringAccum's comments.
Definition straccum.hh:335
const_iterator begin() const
Return an iterator for the first character in the StringAccum.
Definition straccum.hh:243
bool empty() const
Test if the StringAccum is empty.
Definition straccum.hh:278
int capacity() const
Return the StringAccum's current capacity.
Definition straccum.hh:235
void append(char c)
Append character c to the StringAccum.
Definition straccum.hh:415
StringAccum & operator<<(StringAccum &sa, bool x)
Append "true" or "false" to sa, depending on x.
Definition straccum.hh:543
char * reserve(int n)
Reserve space for at least n characters.
Definition straccum.hh:351
bool append_utf8(int ch)
Append Unicode character ch encoded in UTF-8.
Definition straccum.hh:475
StringAccum & operator<<(StringAccum &sa, short x)
Append decimal representation of x to sa.
Definition straccum.hh:551
char * extend(int nadjust, int nreserve=0)
Reserve space and adjust length in one operation.
Definition straccum.hh:389
A string of characters.
Definition string.hh:22
static const char * empty_data()
Return the data pointer used for canonical empty strings.
Definition string.hh:862
int length() const
Return the string's length.
Definition string.hh:509
const_iterator end() const
Return an iterator for the end of the string.
Definition string.hh:584
const_iterator begin() const
Return an iterator for the first character in the string.
Definition string.hh:576
const char * data() const
Return a pointer to the string's data.
Definition string.hh:504