1 // -*- related-file-name: "../../lib/string.cc" -*-
2 #ifndef CLICK_STRING_HH
3 #define CLICK_STRING_HH
4 #include <click/algorithm.hh>
5 #include <click/atomic.hh>
6 #if HAVE_STRING_PROFILING
7 # include <click/integers.hh>
8 #endif
9 #if CLICK_LINUXMODULE || CLICK_BSDMODULE
10 # include <click/glue.hh>
11 #else
12 # include <string.h>
13 #endif
14 #define CLICK_CONSTANT_CSTR(cstr) ((cstr) && __builtin_constant_p(strlen((cstr))))
15 CLICK_DECLS
16 class StringAccum;
17 
18 class String { public:
19 
20     typedef const char *const_iterator;
21     typedef const_iterator iterator;
22 
23     typedef int (String::*unspecified_bool_type)() const;
24 
25 #if HAVE_INT64_TYPES && (!HAVE_LONG_LONG || SIZEOF_LONG_LONG <= 8)
26     typedef int64_t intmax_t;
27     typedef uint64_t uintmax_t;
28 #elif HAVE_LONG_LONG
29     typedef long long intmax_t;
30     typedef unsigned long long uintmax_t;
31 #else
32     typedef long intmax_t;
33     typedef unsigned long uintmax_t;
34 #endif
35     typedef intmax_t int_large_t;
36     typedef uintmax_t uint_large_t;
37 
38     inline String();
39     inline String(const String& x);
40 #if HAVE_CXX_RVALUE_REFERENCES
41     inline String(String&& x);
42 #endif
43     inline String(const char* cstr);
44     inline String(const char* s, int len);
45     inline String(const unsigned char* s, int len);
46     inline String(const char* first, const char* last);
47     inline String(const unsigned char* first, const unsigned char* last);
48     explicit inline String(bool x);
49     explicit inline String(char c);
50     explicit inline String(unsigned char c);
51     explicit String(int x);
52     explicit String(unsigned x);
53     explicit String(long x);
54     explicit String(unsigned long x);
55 #if HAVE_LONG_LONG
56     explicit String(long long x);
57     explicit String(unsigned long long x);
58 #endif
59 #if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
60     explicit String(int64_t x);
61     explicit String(uint64_t x);
62 #endif
63 #if HAVE_FLOAT_TYPES
64     explicit String(double x);
65 #endif
66     inline ~String();
67 
68     static inline const String& make_empty();
69     static inline String make_uninitialized(int len);
70     static inline String make_garbage(int len) CLICK_DEPRECATED;
71     static inline String make_stable(const char* cstr);
72     static inline String make_stable(const char* s, int len);
73     static inline String make_stable(const char* first, const char* last);
74     static String make_numeric(intmax_t x, int base = 10, bool uppercase = true);
75     static String make_numeric(uintmax_t x, int base = 10, bool uppercase = true);
76 
77     inline const char* data() const;
78     inline int length() const;
79 
80     inline const char *c_str() const;
81 
82     inline operator unspecified_bool_type() const;
83     inline bool empty() const;
84     inline bool operator!() const;
85 
86     inline const_iterator begin() const;
87     inline const_iterator end() const;
88 
89     inline char operator[](int i) const;
90     inline char at(int i) const;
91     inline char front() const;
92     inline char back() const;
93 
94     static uint32_t hashcode(const char *begin, const char *end);
95     static inline uint32_t hashcode(const unsigned char *begin,
96 				    const unsigned char *end);
97     inline uint32_t hashcode() const;
98 
99     inline String substring(const char *begin, const char *end) const;
100     String substring(int pos, int len) const;
101     inline String substring(int pos) const;
102     String trim_space() const;
103 
104     inline bool equals(const String &x) const;
105     inline bool equals(const char *s, int len) const;
106     static inline int compare(const String &a, const String &b);
107     inline int compare(const String &x) const;
108     int compare(const char *s, int len) const;
109     inline bool starts_with(const String &x) const;
110     bool starts_with(const char *s, int len) const;
111     bool glob_match(const String& pattern) const;
112 
113     // bool operator==(const String &, const String &);
114     // bool operator==(const String &, const char *);
115     // bool operator==(const char *, const String &);
116     // bool operator!=(const String &, const String &);
117     // bool operator!=(const String &, const char *);
118     // bool operator!=(const char *, const String &);
119     // bool operator<(const String &, const String &);
120     // bool operator<=(const String &, const String &);
121     // bool operator>(const String &, const String &);
122     // bool operator>=(const String &, const String &);
123 
124     int find_left(char c, int start = 0) const;
125     int find_left(const String &x, int start = 0) const;
126     int find_right(char c, int start = 0x7FFFFFFF) const;
127 
128     String lower() const;
129     String upper() const;
130     String printable() const;
131     String quoted_hex() const;
132     String encode_json() const;
133 
134     inline String &operator=(const String &x);
135 #if HAVE_CXX_RVALUE_REFERENCES
136     inline String &operator=(String &&x);
137 #endif
138     inline String &operator=(const char *cstr);
139 
140     inline void swap(String &x);
141 
142     inline void append(const String &x);
143     inline void append(const char *cstr);
144     inline void append(const char *s, int len);
145     inline void append(const char *first, const char *last);
146     inline void append(char c);
147     void append_fill(int c, int len);
148     char *append_uninitialized(int len);
149     inline char *append_garbage(int len) CLICK_DEPRECATED;
150 
151     inline String &operator+=(const String &x);
152     inline String &operator+=(const char *cstr);
153     inline String &operator+=(char c);
154 
155     // String operator+(String, const String &);
156     // String operator+(String, const char *);
157     // String operator+(const char *, const String &);
158 
159     inline bool is_shared() const;
160     inline bool is_stable() const;
161 
162     inline String unique() const CLICK_DEPRECATED;
163     inline String unshared() const;
164     inline String compact() const;
165 
166     char *mutable_data();
167     char *mutable_c_str();
168 
169     static inline const String &make_out_of_memory();
170     inline bool out_of_memory() const;
171     static inline const char *out_of_memory_data();
172     static inline int out_of_memory_length();
173     static inline const char *empty_data();
174 
175 #if HAVE_STRING_PROFILING
176     static void profile_report(StringAccum &sa, int examples = 0);
177 #endif
178 
179     static inline const char *skip_utf8_char(const char *first, const char *last);
180     static const unsigned char *skip_utf8_char(const unsigned char *first,
181 					       const unsigned char *last);
182 
183     static const char bool_data[11];
184 
185   private:
186 
187     /** @cond never */
188     struct memo_t {
189 	volatile uint32_t refcount;
190 	uint32_t capacity;
191 	volatile uint32_t dirty;
192 #if HAVE_STRING_PROFILING > 1
193 	memo_t **pprev;
194 	memo_t *next;
195 #endif
196 	char real_data[8];	// but it might be more or less
197     };
198 
199     enum {
200 	MEMO_SPACE = sizeof(memo_t) - 8
201     };
202 
203     struct rep_t {
204 	const char *data;
205 	int length;
206 	memo_t *memo;
207     };
208     /** @endcond never */
209 
210     mutable rep_t _r;		// mutable for c_str()
211 
212 #if HAVE_STRING_PROFILING
213     static uint64_t live_memo_count;
214     static uint64_t memo_sizes[55];
215     static uint64_t live_memo_sizes[55];
216     static uint64_t live_memo_bytes[55];
217 # if HAVE_STRING_PROFILING > 1
218     static memo_t *live_memos[55];
219 # endif
220 
profile_memo_size_bucket(uint32_t dirty,uint32_t capacity)221     static inline int profile_memo_size_bucket(uint32_t dirty, uint32_t capacity) {
222 	if (capacity <= 16)
223 	    return dirty;
224 	else if (capacity <= 32)
225 	    return 17 + (capacity - 17) / 2;
226 	else if (capacity <= 64)
227 	    return 25 + (capacity - 33) / 8;
228 	else
229 	    return 29 + 26 - ffs_msb(capacity - 1);
230     }
231 
profile_update_memo_dirty(memo_t * memo,uint32_t old_dirty,uint32_t new_dirty,uint32_t capacity)232     static void profile_update_memo_dirty(memo_t *memo, uint32_t old_dirty, uint32_t new_dirty, uint32_t capacity) {
233 	if (capacity <= 16 && new_dirty != old_dirty) {
234 	    ++memo_sizes[new_dirty];
235 	    ++live_memo_sizes[new_dirty];
236 	    live_memo_bytes[new_dirty] += capacity;
237 	    --live_memo_sizes[old_dirty];
238 	    live_memo_bytes[old_dirty] -= capacity;
239 # if HAVE_STRING_PROFILING > 1
240 	    if ((*memo->pprev = memo->next))
241 		memo->next->pprev = memo->pprev;
242 	    memo->pprev = &live_memos[new_dirty];
243 	    if ((memo->next = *memo->pprev))
244 		memo->next->pprev = &memo->next;
245 	    *memo->pprev = memo;
246 # else
247 	    (void) memo;
248 # endif
249 	}
250     }
251 
252     static void one_profile_report(StringAccum &sa, int i, int examples);
253 #endif
254 
assign_memo(const char * data,int length,memo_t * memo) const255     inline void assign_memo(const char *data, int length, memo_t *memo) const {
256 	_r.data = data;
257 	_r.length = length;
258 	if ((_r.memo = memo))
259 	    atomic_uint32_t::inc(memo->refcount);
260     }
261 
String(const char * data,int length,memo_t * memo)262     inline String(const char *data, int length, memo_t *memo) {
263 	assign_memo(data, length, memo);
264     }
265 
assign(const String & x) const266     inline void assign(const String &x) const {
267 	assign_memo(x._r.data, x._r.length, x._r.memo);
268     }
269 
deref() const270     inline void deref() const {
271 	if (_r.memo) {
272 	    assert(_r.memo->refcount);
273 	    if (atomic_uint32_t::dec_and_test(_r.memo->refcount))
274 		delete_memo(_r.memo);
275 	    _r.memo = 0;
276 	}
277     }
278 
279     void assign(const char *s, int len, bool need_deref);
280     void assign_out_of_memory();
281     void append(const char *s, int len, memo_t *memo);
282     static String hard_make_stable(const char *s, int len);
absent_memo()283     static inline memo_t *absent_memo() {
284 	return reinterpret_cast<memo_t *>(uintptr_t(1));
285     }
286     static memo_t *create_memo(char *space, int dirty, int capacity);
287     static void delete_memo(memo_t *memo);
288     const char *hard_c_str() const;
289     bool hard_equals(const char *s, int len) const;
290 
291     static const char null_data;
292     static const char oom_data[15];
293     static const char int_data[20];
294     static const rep_t null_string_rep;
295     static const rep_t oom_string_rep;
296     enum { oom_len = 14 };
297 
298     static String make_claim(char *, int, int); // claim memory
299 
300     friend struct rep_t;
301     friend class StringAccum;
302 
303 };
304 
305 class StringRef {
306   public:
307 
308     inline StringRef();
309     inline StringRef(const StringRef &x);
310     inline StringRef(const char *cstr);
311     inline StringRef(const char *s, int len);
312     inline StringRef(const String &x);
313 
314     inline const char *data() const;
315     inline int length() const;
316 
317     inline const char *begin() const;
318     inline const char *end() const;
319 
320     inline uint32_t hashcode() const;
321 
322   private:
323     const char *data_;
324     int len_;
325 };
326 
327 /** @brief Construct an empty String (with length 0). */
String()328 inline String::String() {
329     assign_memo(&null_data, 0, 0);
330 }
331 
332 /** @brief Construct a copy of the String @a x. */
String(const String & x)333 inline String::String(const String &x) {
334     assign(x);
335 }
336 
337 #if HAVE_CXX_RVALUE_REFERENCES
338 /** @brief Move-construct a String from @a x. */
String(String && x)339 inline String::String(String &&x)
340     : _r(x._r) {
341     x._r.memo = 0;
342 }
343 #endif
344 
345 /** @brief Construct a String containing the C string @a cstr.
346     @param cstr a null-terminated C string
347     @return A String containing the characters of @a cstr, up to but not
348     including the terminating null character. */
String(const char * cstr)349 inline String::String(const char *cstr) {
350     if (CLICK_CONSTANT_CSTR(cstr))
351 	assign_memo(cstr, strlen(cstr), 0);
352     else
353 	assign(cstr, -1, false);
354 }
355 
356 /** @brief Construct a String containing the first @a len characters of
357     string @a s.
358     @param s a string
359     @param len number of characters to take from @a s.  If @a len @< 0,
360     then takes @c strlen(@a s) characters.
361     @return A String containing @a len characters of @a s. */
String(const char * s,int len)362 inline String::String(const char *s, int len) {
363     assign(s, len, false);
364 }
365 
366 /** @overload */
String(const unsigned char * s,int len)367 inline String::String(const unsigned char *s, int len) {
368     assign(reinterpret_cast<const char *>(s), len, false);
369 }
370 
371 /** @brief Construct a String containing the characters from @a first
372     to @a last.
373     @param first first character in string (begin iterator)
374     @param last pointer one past last character in string (end iterator)
375     @return A String containing the characters from @a first to @a last.
376 
377     Constructs an empty string if @a first @>= @a last. */
String(const char * first,const char * last)378 inline String::String(const char *first, const char *last) {
379     assign(first, (first < last ? last - first : 0), false);
380 }
381 
382 /** @overload */
String(const unsigned char * first,const unsigned char * last)383 inline String::String(const unsigned char *first, const unsigned char *last) {
384     assign(reinterpret_cast<const char *>(first),
385 	   (first < last ? last - first : 0), false);
386 }
387 
388 /** @brief Construct a String equal to "true" or "false" depending on the
389     value of @a x. */
String(bool x)390 inline String::String(bool x) {
391     // bool_data equals "false\0true\0"
392     assign_memo(bool_data + (-x & 6), 5 - x, 0);
393 }
394 
395 /** @brief Construct a String containing the single character @a c. */
String(char c)396 inline String::String(char c) {
397     assign(&c, 1, false);
398 }
399 
400 /** @overload */
String(unsigned char c)401 inline String::String(unsigned char c) {
402     assign(reinterpret_cast<char *>(&c), 1, false);
403 }
404 
405 /** @brief Destroy a String, freeing memory if necessary. */
~String()406 inline String::~String() {
407     deref();
408 }
409 
410 /** @brief Return a const reference to an empty String.
411 
412     May be quicker than String::String(). */
make_empty()413 inline const String &String::make_empty() {
414     return reinterpret_cast<const String &>(null_string_rep);
415 }
416 
417 /** @brief Return a String containing @a len unknown characters. */
make_uninitialized(int len)418 inline String String::make_uninitialized(int len) {
419     String s;
420     s.append_uninitialized(len);
421     return s;
422 }
423 
424 /** @cond never */
make_garbage(int len)425 inline String String::make_garbage(int len) {
426     return make_uninitialized(len);
427 }
428 /** @endcond never */
429 
430 /** @brief Return a String that directly references the C string @a cstr.
431 
432     The make_stable() functions are suitable for static constant strings
433     whose data is known to stay around forever, such as C string constants.
434 
435     @warning The String implementation may access @a cstr's terminating null
436     character. */
make_stable(const char * cstr)437 inline String String::make_stable(const char *cstr) {
438     if (CLICK_CONSTANT_CSTR(cstr))
439 	return String(cstr, strlen(cstr), 0);
440     else
441 	return hard_make_stable(cstr, -1);
442 }
443 
444 /** @brief Return a String that directly references the first @a len
445     characters of @a s.
446 
447     If @a len @< 0, treats @a s as a null-terminated C string.
448 
449     @warning The String implementation may access @a s[@a len], which
450     should remain constant even though it's not part of the String. */
make_stable(const char * s,int len)451 inline String String::make_stable(const char *s, int len) {
452     if (__builtin_constant_p(len) && len >= 0)
453 	return String(s, len, 0);
454     else
455 	return hard_make_stable(s, len);
456 }
457 
458 /** @brief Return a String that directly references the character data in
459     [@a first, @a last).
460     @param first pointer to the first character in the character data
461     @param last pointer one beyond the last character in the character data
462     (but see the warning)
463 
464     This function is suitable for static constant strings whose data is
465     known to stay around forever, such as C string constants.  Returns an
466     empty string if @a first @>= @a last.
467 
468     @warning The String implementation may access *@a last, which should
469     remain constant even though it's not part of the String. */
make_stable(const char * first,const char * last)470 inline String String::make_stable(const char *first, const char *last) {
471     return String(first, (first < last ? last - first : 0), 0);
472 }
473 
474 /** @brief Return a pointer to the string's data.
475 
476     Only the first length() characters are valid, and the string
477     might not be null-terminated. */
data() const478 inline const char *String::data() const {
479     return _r.data;
480 }
481 
482 /** @brief Return the string's length. */
length() const483 inline int String::length() const {
484     return _r.length;
485 }
486 
487 /** @brief Null-terminate the string.
488 
489     The terminating null character isn't considered part of the string, so
490     this->length() doesn't change.  Returns a corresponding C string
491     pointer.  The returned pointer is semi-temporary; it will persist until
492     the string is destroyed or appended to. */
c_str() const493 inline const char *String::c_str() const {
494     // See also hard_c_str().
495 #if CLICK_OPTIMIZE_SIZE || __OPTIMIZE_SIZE__
496     return hard_c_str();
497 #else
498     // We may already have a '\0' in the right place.  If _memo has no
499     // capacity, then this is one of the special strings (null or
500     // stable). We are guaranteed, in these strings, that _data[_length]
501     // exists. Otherwise must check that _data[_length] exists.
502     const char *end_data = _r.data + _r.length;
503     if ((_r.memo && end_data >= _r.memo->real_data + _r.memo->dirty)
504 	|| *end_data != '\0') {
505 	if (char *x = const_cast<String *>(this)->append_uninitialized(1)) {
506 	    *x = '\0';
507 	    --_r.length;
508 	}
509     }
510     return _r.data;
511 #endif
512 }
513 
514 /** @brief Return a substring of the current string starting at @a first
515     and ending before @a last.
516     @param first pointer to the first substring character
517     @param last pointer one beyond the last substring character
518 
519     Returns an empty string if @a first @>= @a last. Also returns an empty
520     string if @a first or @a last is out of range (i.e., either less than
521     this->begin() or greater than this->end()), but this should be
522     considered a programming error; a future version may generate a warning
523     for this case. */
substring(const char * first,const char * last) const524 inline String String::substring(const char *first, const char *last) const {
525     if (first < last && first >= _r.data && last <= _r.data + _r.length)
526 	return String(first, last - first, _r.memo);
527     else
528 	return String();
529 }
530 
531 /** @brief Return the suffix of the current string starting at index @a pos.
532 
533     If @a pos is negative, starts that far from the end of the string.
534     If @a pos is so negative that the suffix starts outside the string,
535     then the entire string is returned. If the substring is beyond the
536     end of the string (@a pos > length()), returns an empty string (but
537     this should be considered a programming error; a future version may
538     generate a warning for this case).
539 
540     @note String::substring() is intended to behave like Perl's
541     substr(). */
substring(int pos) const542 inline String String::substring(int pos) const {
543     return substring((pos <= -_r.length ? 0 : pos), _r.length);
544 }
545 
546 /** @brief Return an iterator for the first character in the string.
547 
548     String iterators are simply pointers into string data, so they are
549     quite efficient.  @sa String::data */
begin() const550 inline String::const_iterator String::begin() const {
551     return _r.data;
552 }
553 
554 /** @brief Return an iterator for the end of the string.
555 
556     The return value points one character beyond the last character in the
557     string. */
end() const558 inline String::const_iterator String::end() const {
559     return _r.data + _r.length;
560 }
561 
562 /** @brief Test if the string is nonempty. */
operator unspecified_bool_type() const563 inline String::operator unspecified_bool_type() const {
564     return (_r.length != 0 ? &String::length : 0);
565 }
566 
567 /** @brief Test if the string is empty. */
empty() const568 inline bool String::empty() const {
569     return _r.length == 0;
570 }
571 
572 /** @brief Test if the string is empty. */
operator !() const573 inline bool String::operator!() const {
574     return empty();
575 }
576 
577 /** @brief Return the @a i th character in the string.
578 
579     Does not check bounds.  @sa String::at */
operator [](int i) const580 inline char String::operator[](int i) const {
581     return _r.data[i];
582 }
583 
584 /** @brief Return the @a i th character in the string.
585 
586     Checks bounds: an assertion will fail if @a i is less than 0 or not less
587     than length(). @sa String::operator[] */
at(int i) const588 inline char String::at(int i) const {
589     assert((unsigned) i < (unsigned) _r.length);
590     return _r.data[i];
591 }
592 
593 /** @brief Return the first character in the string.
594 
595     Does not check bounds.  Same as (*this)[0]. */
front() const596 inline char String::front() const {
597     return _r.data[0];
598 }
599 
600 /** @brief Return the last character in the string.
601 
602     Does not check bounds.  Same as (*this)[length() - 1]. */
back() const603 inline char String::back() const {
604     return _r.data[_r.length - 1];
605 }
606 
607 /** @overload */
hashcode(const unsigned char * first,const unsigned char * last)608 inline uint32_t String::hashcode(const unsigned char *first,
609 				 const unsigned char *last) {
610     return hashcode(reinterpret_cast<const char *>(first),
611 		    reinterpret_cast<const char *>(last));
612 }
613 
614 /** @brief Returns a 32-bit hash function of this string's characters.
615 
616     Equivalent to String::hashcode(begin(), end()).  Uses Paul Hsieh's
617     "SuperFastHash."
618 
619     @invariant  If s1 == s2, then s1.hashcode() == s2.hashcode(). */
hashcode() const620 inline uint32_t String::hashcode() const {
621     return (length() ? hashcode(begin(), end()) : 0);
622 }
623 
624 /** @brief Test if this string equals @a x. */
equals(const String & x) const625 inline bool String::equals(const String &x) const {
626     return equals(x.data(), x.length());
627 }
628 
629 /** @brief Test if this string is equal to the data in @a s.
630     @param s string data to compare to
631     @param len length of @a s
632 
633     Same as String::compare(*this, String(s, len)) == 0. If @a len @< 0,
634     then treats @a s as a null-terminated C string.
635 
636     @sa String::compare(const String &a, const String &b) */
equals(const char * s,int len) const637 inline bool String::equals(const char *s, int len) const {
638 #if CLICK_OPTIMIZE_SIZE || __OPTIMIZE_SIZE__
639     return hard_equals(s, len);
640 #else
641     if (__builtin_constant_p(len) && len >= 0)
642 	return length() == len && memcmp(data(), s, len) == 0;
643     else
644 	return hard_equals(s, len);
645 #endif
646 }
647 
648 /** @brief Compare two strings.
649     @param a first string to compare
650     @param b second string to compare
651 
652     Returns 0 if @a a == @a b, negative if @a a @< @a b in lexicographic
653     order, and positive if @a a @> @a b in lexicographic order. The
654     lexicographic order treats all characters as unsigned. */
compare(const String & a,const String & b)655 inline int String::compare(const String &a, const String &b) {
656     return a.compare(b);
657 }
658 
659 /** @brief Compare this string with string @a x.
660 
661     Same as String::compare(*this, @a x).
662     @sa String::compare(const String &a, const String &b) */
compare(const String & x) const663 inline int String::compare(const String &x) const {
664     return compare(x.data(), x.length());
665 }
666 
667 /** @brief Test if this string begins with prefix @a x.
668 
669     Same as String::starts_with(@a x.data(), @a x.length()). */
starts_with(const String & x) const670 inline bool String::starts_with(const String &x) const {
671     return starts_with(x.data(), x.length());
672 }
673 
674 /** @brief Assign this string to @a x. */
operator =(const String & x)675 inline String &String::operator=(const String &x) {
676     if (likely(&x != this)) {
677 	deref();
678 	assign(x);
679     }
680     return *this;
681 }
682 
683 #if HAVE_CXX_RVALUE_REFERENCES
684 /** @brief Move-assign this string to @a x. */
operator =(String && x)685 inline String &String::operator=(String &&x) {
686     deref();
687     _r = x._r;
688     x._r.memo = 0;
689     return *this;
690 }
691 #endif
692 
693 /** @brief Assign this string to the C string @a cstr. */
operator =(const char * cstr)694 inline String &String::operator=(const char *cstr) {
695     if (CLICK_CONSTANT_CSTR(cstr)) {
696 	deref();
697 	assign_memo(cstr, strlen(cstr), 0);
698     } else
699 	assign(cstr, -1, true);
700     return *this;
701 }
702 
703 /** @brief Swap the values of this string and @a x. */
swap(String & x)704 inline void String::swap(String &x) {
705     rep_t r = _r;
706     _r = x._r;
707     x._r = r;
708 }
709 
710 /** @brief Append @a x to this string. */
append(const String & x)711 inline void String::append(const String &x) {
712     append(x.data(), x.length(), x._r.memo);
713 }
714 
715 /** @brief Append the null-terminated C string @a cstr to this string.
716     @param cstr data to append */
append(const char * cstr)717 inline void String::append(const char *cstr) {
718     if (CLICK_CONSTANT_CSTR(cstr))
719 	append(cstr, strlen(cstr), absent_memo());
720     else
721 	append(cstr, -1, absent_memo());
722 }
723 
724 /** @brief Append the first @a len characters of @a s to this string.
725     @param s data to append
726     @param len length of data
727     @pre @a len @>= 0 */
append(const char * s,int len)728 inline void String::append(const char *s, int len) {
729     append(s, len, absent_memo());
730 }
731 
732 /** @brief Appends the data from @a first to @a last to this string.
733 
734     Does nothing if @a first @>= @a last. */
append(const char * first,const char * last)735 inline void String::append(const char *first, const char *last) {
736     if (first < last)
737 	append(first, last - first);
738 }
739 
740 /** @brief Append the character @a c to this string. */
append(char c)741 inline void String::append(char c) {
742     append(&c, 1, absent_memo());
743 }
744 
745 /** @cond never */
append_garbage(int len)746 inline char *String::append_garbage(int len) {
747     return append_uninitialized(len);
748 }
749 /** @endcond never */
750 
751 /** @brief Append @a x to this string.
752     @return *this */
operator +=(const String & x)753 inline String &String::operator+=(const String &x) {
754     append(x.data(), x.length(), x._r.memo);
755     return *this;
756 }
757 
758 /** @brief Append the null-terminated C string @a cstr to this string.
759     @return *this */
operator +=(const char * cstr)760 inline String &String::operator+=(const char *cstr) {
761     append(cstr);
762     return *this;
763 }
764 
765 /** @brief Append the character @a c to this string.
766     @return *this */
operator +=(char c)767 inline String &String::operator+=(char c) {
768     append(&c, 1);
769     return *this;
770 }
771 
772 /** @brief Test if the String's data is shared or immutable. */
is_shared() const773 inline bool String::is_shared() const {
774     return !_r.memo || _r.memo->refcount != 1;
775 }
776 
777 /** @brief Test if the String's data is immutable. */
is_stable() const778 inline bool String::is_stable() const {
779     return !_r.memo;
780 }
781 
782 /** @brief Return an unshared version of this String.
783 
784     The return value shares no data with any other non-stable String. */
unshared() const785 inline String String::unshared() const {
786     if (!_r.memo || _r.memo->refcount == 1)
787 	return *this;
788     else
789 	return String(_r.data, _r.data + _r.length);
790 }
791 
792 /** @brief Return an unshared version of this String.
793     @deprecated Use String::unshared() instead.
794 
795     The return value shares no data with any other non-stable String. */
unique() const796 inline String String::unique() const {
797     return unshared();
798 }
799 
800 /** @brief Return a compact version of this String.
801 
802     The return value shares no more than 256 bytes of data with any other
803     non-stable String. */
compact() const804 inline String String::compact() const {
805     if (!_r.memo || _r.memo->refcount == 1
806 	|| (uint32_t) _r.length + 256 >= _r.memo->capacity)
807 	return *this;
808     else
809 	return String(_r.data, _r.data + _r.length);
810 }
811 
812 /** @brief Test if this is an out-of-memory string. */
out_of_memory() const813 inline bool String::out_of_memory() const {
814     return unlikely(data() == oom_data);
815 }
816 
817 /** @brief Return a const reference to a canonical out-of-memory String. */
make_out_of_memory()818 inline const String &String::make_out_of_memory() {
819     return reinterpret_cast<const String &>(oom_string_rep);
820 }
821 
822 /** @brief Return the data pointer used for out-of-memory strings. */
out_of_memory_data()823 inline const char *String::out_of_memory_data() {
824     return oom_data;
825 }
826 
827 /** @brief Return the length of canonical out-of-memory strings. */
out_of_memory_length()828 inline int String::out_of_memory_length() {
829     return oom_len;
830 }
831 
832 /** @brief Return the data pointer used for canonical empty strings.
833 
834     The returned value may be dereferenced; it points to a null
835     character. */
empty_data()836 inline const char *String::empty_data() {
837     return &null_data;
838 }
839 
840 /** @brief Return a pointer to the next character in UTF-8 encoding.
841     @pre @a first @< @a last
842 
843     If @a first doesn't point at a valid UTF-8 character, returns @a first. */
skip_utf8_char(const char * first,const char * last)844 inline const char *String::skip_utf8_char(const char *first, const char *last) {
845     return reinterpret_cast<const char *>(skip_utf8_char(reinterpret_cast<const unsigned char *>(first), reinterpret_cast<const unsigned char *>(last)));
846 }
847 
StringRef()848 inline StringRef::StringRef()
849     : data_(0), len_(0) {
850 }
851 
StringRef(const StringRef & x)852 inline StringRef::StringRef(const StringRef &x)
853     : data_(x.data_), len_(x.len_) {
854 }
855 
StringRef(const char * cstr)856 inline StringRef::StringRef(const char *cstr)
857     : data_(cstr), len_(strlen(cstr)) {
858 }
859 
StringRef(const char * s,int len)860 inline StringRef::StringRef(const char *s, int len)
861     : data_(s), len_(len) {
862 }
863 
StringRef(const String & x)864 inline StringRef::StringRef(const String &x)
865     : data_(x.data()), len_(x.length()) {
866 }
867 
data() const868 inline const char *StringRef::data() const {
869     return data_;
870 }
871 
length() const872 inline int StringRef::length() const {
873     return len_;
874 }
875 
begin() const876 inline const char *StringRef::begin() const {
877     return data();
878 }
879 
end() const880 inline const char *StringRef::end() const {
881     return data() + length();
882 }
883 
884 /** @relates String
885     @brief Compares two strings for equality.
886 
887     Returns true iff the two operands have the same lengths and the same
888     characters in the same order.  At most one of the operands can be a
889     null-terminated C string.
890     @sa String::compare */
operator ==(const String & a,const String & b)891 inline bool operator==(const String &a, const String &b) {
892     return a.equals(b.data(), b.length());
893 }
894 
895 /** @relates String */
operator ==(const char * a,const String & b)896 inline bool operator==(const char *a, const String &b) {
897     if (CLICK_CONSTANT_CSTR(a))
898 	return b.equals(a, strlen(a));
899     else
900 	return b.equals(a, -1);
901 }
902 
903 /** @relates String */
operator ==(const String & a,const char * b)904 inline bool operator==(const String &a, const char *b) {
905     if (CLICK_CONSTANT_CSTR(b))
906 	return a.equals(b, strlen(b));
907     else
908 	return a.equals(b, -1);
909 }
910 
911 /** @relates String
912     @brief Compare two Strings for inequality.
913 
914     Returns true iff !(@a a == @a b).  At most one of the operands can be a
915     null-terminated C string. */
operator !=(const String & a,const String & b)916 inline bool operator!=(const String &a, const String &b) {
917     return !(a == b);
918 }
919 
920 /** @relates String */
operator !=(const char * a,const String & b)921 inline bool operator!=(const char *a, const String &b) {
922     return !(a == b);
923 }
924 
925 /** @relates String */
operator !=(const String & a,const char * b)926 inline bool operator!=(const String &a, const char *b) {
927     return !(a == b);
928 }
929 
930 /** @relates String
931     @brief Compare two Strings.
932 
933     Returns true iff @a a @< @a b in lexicographic order.
934     @sa String::compare */
operator <(const String & a,const String & b)935 inline bool operator<(const String &a, const String &b) {
936     return a.compare(b.data(), b.length()) < 0;
937 }
938 
939 /** @relates String
940     @brief Compare two Strings.
941 
942     Returns true iff @a a @<= @a b in lexicographic order.
943     @sa String::compare */
operator <=(const String & a,const String & b)944 inline bool operator<=(const String &a, const String &b) {
945     return a.compare(b.data(), b.length()) <= 0;
946 }
947 
948 /** @relates String
949     @brief Compare two Strings.
950 
951     Returns true iff @a a @> @a b in lexicographic order.
952     @sa String::compare */
operator >(const String & a,const String & b)953 inline bool operator>(const String &a, const String &b) {
954     return a.compare(b.data(), b.length()) > 0;
955 }
956 
957 /** @relates String
958     @brief Compare two Strings.
959 
960     Returns true iff @a a @>= @a b in lexicographic order.
961     @sa String::compare */
operator >=(const String & a,const String & b)962 inline bool operator>=(const String &a, const String &b) {
963     return a.compare(b.data(), b.length()) >= 0;
964 }
965 
966 /** @relates String
967     @brief Concatenate the operands and return the result.
968 
969     At most one of the two operands can be a null-terminated C string. */
operator +(String a,const String & b)970 inline String operator+(String a, const String &b) {
971     a += b;
972     return a;
973 }
974 
975 /** @relates String */
operator +(String a,const char * b)976 inline String operator+(String a, const char *b) {
977     a.append(b);
978     return a;
979 }
980 
981 /** @relates String */
operator +(const char * a,const String & b)982 inline String operator+(const char *a, const String &b) {
983     String s1(a);
984     s1 += b;
985     return s1;
986 }
987 
988 /** @relates String
989     @brief Concatenate the operands and return the result.
990 
991     The second operand is a single character. */
operator +(String a,char b)992 inline String operator+(String a, char b) {
993     a.append(&b, 1);
994     return a;
995 }
996 
997 // find methods
998 
rfind(const char * first,const char * last,char c)999 inline const char *rfind(const char *first, const char *last, char c) {
1000     for (const char *bb = last - 1; bb >= first; bb--)
1001 	if (*bb == c)
1002 	    return bb;
1003     return last;
1004 }
1005 
find(const String & s,char c)1006 inline const char *find(const String &s, char c) {
1007     return find(s.begin(), s.end(), c);
1008 }
1009 
1010 // sort methods
1011 
1012 template <typename T> int click_compare(const void *, const void *);
1013 
click_compare(const void * a,const void * b)1014 template <> inline int click_compare<String>(const void *a, const void *b) {
1015     const String *ta = reinterpret_cast<const String *>(a);
1016     const String *tb = reinterpret_cast<const String *>(b);
1017     return String::compare(*ta, *tb);
1018 }
1019 
1020 CLICK_ENDDECLS
1021 #endif
1022