Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/include/lcdf/string.hh

raw
1 // -*- related-file-name: "../../liblcdf/string.cc" -*-
2 #ifndef LCDF_STRING_HH
3 #define LCDF_STRING_HH
4 #include <lcdf/hashcode.hh>
5 #ifdef HAVE_PERMSTRING
6 # include <lcdf/permstr.hh>
7 #endif
8 #include <assert.h>
9 class StringAccum;
10
11 class String { public:
12
13 /** @brief Construct an empty String (with length 0). */
14 inline String() {
15 assign_memo(&null_data, 0, 0);
16 }
17
18 /** @brief Construct a copy of the String @a x. */
19 inline String(const String &x) {
20 assign(x);
21 }
22
23 /** @brief Construct a String containing the C string @a cstr.
24 * @param cstr a null-terminated C string
25 * @return A String containing the characters of @a cstr, up to but not
26 * including the terminating null character.
27 *
28 * If @a cstr equals String::out_of_memory_data(), returns an
29 * out-of-memory string. */
30 inline String(const char *cstr) {
31 assign(cstr, -1, false);
32 }
33
34 /** @brief Construct a String containing the first @a len characters of
35 * string @a s.
36 * @param s a string
37 * @param len number of characters to take from @a s. If @a len @< 0,
38 * then takes @c strlen(@a s) characters.
39 * @return A String containing @a len characters of @a s.
40 *
41 * If @a s equals String::out_of_memory_data(), returns an out-of-memory
42 * string. */
43 inline String(const char *s, int len) {
44 assign(s, len, false);
45 }
46 /** @overload */
47 inline String(const unsigned char *s, int len) {
48 assign(reinterpret_cast<const char *>(s), len, false);
49 }
50
51 /** @brief Construct a String containing the characters from @a begin
52 * to @a end.
53 * @param begin first character in string (begin iterator)
54 * @param end pointer one past last character in string (end iterator)
55 * @return A String containing the characters from @a begin to @a end.
56 *
57 * Returns a null string if @a begin @>= @a end. If @a begin equals
58 * String::out_of_memory_data(), returns an out-of-memory string. */
59 inline String(const char *begin, const char *end) {
60 assign(begin, (end > begin ? end - begin : 0), false);
61 }
62 /** @overload */
63 inline String(const unsigned char *begin, const unsigned char *end) {
64 assign(reinterpret_cast<const char *>(begin),
65 (end > begin ? end - begin : 0), false);
66 }
67
68 /** @brief Construct a String equal to "true" or "false" depending on the
69 * value of @a x. */
70 explicit inline String(bool x) {
71 assign_memo(bool_data + (x ? 0 : 5), x ? 4 : 5, 0);
72 }
73
74 /** @brief Construct a String containing the single character @a c. */
75 explicit inline String(char c) {
76 assign(&c, 1, false);
77 }
78
79 /** @overload */
80 explicit inline String(unsigned char c) {
81 assign(reinterpret_cast<char *>(&c), 1, false);
82 }
83
84 /** @brief Construct a base-10 string representation of @a x. */
85 explicit String(int x);
86 /** @overload */
87 explicit String(unsigned x);
88 /** @overload */
89 explicit String(long x);
90 /** @overload */
91 explicit String(unsigned long x);
92 /** @overload */
93 explicit String(double x);
94 #if HAVE_PERMSTRING
95 inline String(PermString x) {
96 assign(x);
97 }
98 #endif
99
100 /** @brief Destroy a String, freeing memory if necessary. */
101 inline ~String() {
102 deref();
103 }
104
105
106 /** @brief Return a const reference to an empty String.
107 *
108 * May be quicker than String::String(). */
109 static inline const String &make_empty() {
110 return reinterpret_cast<const String &>(null_string_rep);
111 }
112
113 /** @brief Return a String containing @a len unknown characters. */
114 static String make_uninitialized(int len) {
115 String s;
116 s.append_uninitialized(len);
117 return s;
118 }
119
120 /** @brief Return a String that directly references the first @a len
121 * characters of @a s.
122 *
123 * This function is suitable for static constant strings whose data is
124 * known to stay around forever, such as C string constants. If @a len @<
125 * 0, treats @a s as a null-terminated C string.
126 *
127 * @warning The String implementation may access @a s[@a len], which
128 * should remain constant even though it's not part of the String. */
129 static String make_stable(const char *s, int len = -1);
130
131 /** @brief Return a String that directly references the character data in
132 * [@a begin, @a end).
133 * @param begin pointer to the first character in the character data
134 * @param end pointer one beyond the last character in the character data
135 * (but see the warning)
136 *
137 * This function is suitable for static constant strings whose data is
138 * known to stay around forever, such as C string constants. Returns an
139 * empty string if @a begin @>= @a end.
140 *
141 * @warning The String implementation may access *@a end, which should
142 * remain constant even though it's not part of the String. */
143 static inline String make_stable(const char *begin, const char *end) {
144 if (begin < end)
145 return String::make_stable(begin, end - begin);
146 else
147 return String();
148 }
149 static String make_fill(int c, int n); // n copies of c
150
151
152 /** @brief Return the string's length. */
153 inline int length() const {
154 return _r.length;
155 }
156
157 /** @brief Return a pointer to the string's data.
158 *
159 * Only the first length() characters are valid, and the string data
160 * might not be null-terminated. */
161 inline const char *data() const {
162 return _r.data;
163 }
164
165 /** @brief Return a pointer to the string's data as unsigned chars.
166 *
167 * Only the first length() characters are valid, and the string data
168 * might not be null-terminated. @sa data() */
169 inline const unsigned char *udata() const {
170 return reinterpret_cast<const unsigned char *>(_r.data);
171 }
172
173
174 typedef const char *const_iterator;
175 typedef const_iterator iterator;
176
177 /** @brief Return an iterator for the first character in the string.
178 *
179 * String iterators are simply pointers into string data, so they are
180 * quite efficient. @sa String::data */
181 inline const_iterator begin() const {
182 return _r.data;
183 }
184
185 /** @brief Return an iterator for the end of the string.
186 *
187 * The return value points one character beyond the last character in the
188 * string. */
189 inline const_iterator end() const {
190 return _r.data + _r.length;
191 }
192
193 inline const unsigned char* ubegin() const {
194 return reinterpret_cast<const unsigned char*>(_r.data);
195 }
196
197 inline const unsigned char* uend() const {
198 return reinterpret_cast<const unsigned char*>(_r.data + _r.length);
199 }
200
201
202 typedef int (String::*unspecified_bool_type)() const;
203 /** @brief Return true iff the string is nonempty. */
204 inline operator unspecified_bool_type() const {
205 return _r.length != 0 ? &String::length : 0;
206 }
207
208 /** @brief Return true iff the string is empty. */
209 inline bool empty() const {
210 return _r.length == 0;
211 }
212
213 /** @brief Return true iff the string is empty. */
214 inline bool operator!() const {
215 return empty();
216 }
217
218
219 /** @brief Return the @a i th character in the string.
220 *
221 * Does not check bounds. @sa String::at */
222 inline char operator[](int i) const {
223 return _r.data[i];
224 }
225
226 /** @brief Return the @a i th character in the string.
227 *
228 * Checks bounds: an assertion will fail if @a i is less than 0 or not
229 * less than length(). @sa String::operator[] */
230 inline char at(int i) const {
231 assert((unsigned) i < (unsigned) _r.length);
232 return _r.data[i];
233 }
234
235 /** @brief Return the first character in the string.
236 *
237 * Does not check bounds. Same as (*this)[0]. */
238 inline char front() const {
239 return _r.data[0];
240 }
241
242 /** @brief Return the last character in the string.
243 *
244 * Does not check bounds. Same as (*this)[length() - 1]. */
245 inline char back() const {
246 return _r.data[_r.length - 1];
247 }
248
249
250 /** @brief Null-terminate the string.
251 *
252 * The terminating null character isn't considered part of the string, so
253 * this->length() doesn't change. Returns a corresponding C string
254 * pointer. The returned pointer is semi-temporary; it will persist until
255 * the string is destroyed or appended to. */
256 inline const char *c_str() const {
257 // We may already have a '\0' in the right place. If _memo has no
258 // capacity, then this is one of the special strings (null or
259 // stable). We are guaranteed, in these strings, that _data[_length]
260 // exists. Otherwise must check that _data[_length] exists.
261 const char *end_data = _r.data + _r.length;
262 if ((_r.memo && end_data >= _r.memo->real_data + _r.memo->dirty)
263 || *end_data != '\0') {
264 if (char *x = const_cast<String *>(this)->append_uninitialized(1)) {
265 *x = '\0';
266 --_r.length;
267 }
268 }
269 return _r.data;
270 }
271
272 #if HAVE_PERMSTRING
273 operator PermString() const {
274 return PermString(_r.data, _r.length);
275 }
276 #endif
277
278
279 /** @brief Return a 32-bit hash function of the characters in [begin, end).
280 *
281 * Uses Paul Hsieh's "SuperFastHash" algorithm, described at
282 * http://www.azillionmonkeys.com/qed/hash.html
283 * This hash function uses all characters in the string.
284 *
285 * @invariant If end1 - begin1 == end2 - begin2 and memcmp(begin1, begin2,
286 * end1 - begin1) == 0, then hashcode(begin1, end1) == hashcode(begin2,
287 * end2). */
288 static hashcode_t hashcode(const char *begin, const char *end);
289
290 /** @overload */
291 static inline hashcode_t hashcode(const unsigned char *begin,
292 const unsigned char *end) {
293 return hashcode(reinterpret_cast<const char *>(begin),
294 reinterpret_cast<const char *>(end));
295 }
296
297 /** @brief Returns a 32-bit hash function of this string's characters.
298 *
299 * Equivalent to String::hashcode(begin(), end()). Uses Paul Hsieh's
300 * "SuperFastHash."
301 *
302 * @invariant If s1 == s2, then s1.hashcode() == s2.hashcode(). */
303 inline hashcode_t hashcode() const {
304 return length() ? hashcode(begin(), end()) : 0;
305 }
306
307
308 /** @brief Return true iff this string is equal to the data in @a s.
309 * @param s string data to compare to
310 * @param len length of @a s
311 *
312 * Same as String::compare(*this, String(s, len)) == 0. If @a len @< 0,
313 * then treats @a s as a null-terminated C string.
314 *
315 * @sa String::compare(const String &a, const String &b) */
316 bool equals(const char *s, int len) const;
317
318 // bool operator==(const String &, const String &);
319 // bool operator==(const String &, const char *);
320 // bool operator==(const char *, const String &);
321 // bool operator!=(const String &, const String &);
322 // bool operator!=(const String &, const char *);
323 // bool operator!=(const char *, const String &);
324
325
326 /** @brief Compare two strings.
327 * @param a first string to compare
328 * @param b second string to compare
329 *
330 * Returns 0 if @a a == @a b, negative if @a a @< @a b in lexicographic
331 * order, and positive if @a a @> @a b in lexicographic order. The
332 * lexicographic order treats all characters as unsigned. */
333 static inline int compare(const String &a, const String &b) {
334 return a.compare(b);
335 }
336
337 /** @brief Compare this string with string @a x.
338 *
339 * Same as String::compare(*this, @a x).
340 * @sa String::compare(const String &a, const String &b) */
341 inline int compare(const String &x) const {
342 return compare(x._r.data, x._r.length);
343 }
344
345 /** @brief Compare this string with the data in @a s.
346 * @param s string data to compare to
347 * @param len length of @a s
348 *
349 * Same as String::compare(*this, String(s, len)). If @a len @< 0, then
350 * treats @a s as a null-terminated C string.
351 *
352 * @sa String::compare(const String &a, const String &b) */
353 int compare(const char *s, int len) const;
354
355 // bool operator<(const String &, const String &);
356 // bool operator<=(const String &, const String &);
357 // bool operator>(const String &, const String &);
358 // bool operator>=(const String &, const String &);
359
360
361 /** @brief Return a substring of the current string starting at @a begin
362 * and ending before @a end.
363 * @param begin pointer to the first substring character
364 * @param end pointer one beyond the last substring character
365 *
366 * Returns an empty string if @a begin @>= @a end. Also returns an empty
367 * string if @a begin or @a end is out of range (i.e., either less than
368 * this->begin() or greater than this->end()), but this should be
369 * considered a programming error; a future version may generate a warning
370 * for this case. */
371 inline String substring(const char *begin, const char *end) const {
372 if (begin < end && begin >= _r.data && end <= _r.data + _r.length)
373 return String(begin, end - begin, _r.memo);
374 else
375 return String();
376 }
377
378 /** @overload */
379 inline String substring(const unsigned char* first, const unsigned char* last) const {
380 return substring(reinterpret_cast<const char*>(first),
381 reinterpret_cast<const char*>(last));
382 }
383
384 /** @brief Return a substring of this string, consisting of the @a len
385 * characters starting at index @a pos.
386 * @param pos substring's first position relative to the string
387 * @param len length of substring
388 *
389 * If @a pos is negative, starts that far from the end of the string. If
390 * @a len is negative, leaves that many characters off the end of the
391 * string. If @a pos and @a len specify a substring that is partly
392 * outside the string, only the part within the string is returned. If
393 * the substring is beyond either end of the string, returns an empty
394 * string (but this should be considered a programming error; a future
395 * version may generate a warning for this case).
396 *
397 * @note String::substring() is intended to behave like Perl's substr(). */
398 String substring(int pos, int len) const;
399
400 /** @brief Return the suffix of the current string starting at index @a pos.
401 *
402 * If @a pos is negative, starts that far from the end of the string. If
403 * @a pos is so negative that the suffix starts outside the string, then
404 * the entire string is returned. If the substring is beyond the end of
405 * the string (@a pos > length()), returns an empty string (but this
406 * should be considered a programming error; a future version may generate
407 * a warning for this case).
408 *
409 * @note String::substring() is intended to behave like Perl's substr(). */
410 inline String substring(int pos) const {
411 return substring((pos <= -_r.length ? 0 : pos), _r.length);
412 }
413
414
415 /** @brief Search for a character in a string.
416 * @param c character to search for
417 * @param start initial search position
418 *
419 * Return the index of the leftmost occurence of @a c, starting at index
420 * @a start and working up to the end of the string. Returns -1 if @a c
421 * is not found. */
422 int find_left(char c, int start = 0) const;
423
424 /** @brief Search for a substring in a string.
425 * @param x substring to search for
426 * @param start initial search position
427 *
428 * Return the index of the leftmost occurence of the substring @a str,
429 * starting at index @a start and working up to the end of the string.
430 * Returns -1 if @a str is not found. */
431 int find_left(const String &x, int start = 0) const;
432
433 /** @brief Search for a character in a string.
434 * @param c character to search for
435 * @param start initial search position
436 *
437 * Return the index of the rightmost occurence of the character @a c,
438 * starting at index @a start and working back to the beginning of the
439 * string. Returns -1 if @a c is not found. @a start may start beyond
440 * the end of the string. */
441 int find_right(char c, int start = 0x7FFFFFFF) const;
442
443 /** @brief Return true iff this string begins with prefix @a x.
444 *
445 * Same as String::starts_with(@a x.data(), @a x.length()). */
446 inline bool starts_with(const String &x) const {
447 return starts_with(x._r.data, x._r.length);
448 }
449
450 /** @brief Return true iff this string begins with the data in @a s.
451 * @param s string data to compare to
452 * @param len length of @a s
453 *
454 * If @a len @< 0, then treats @a s as a null-terminated C string.
455 *
456 * @sa String::compare(const String &a, const String &b) */
457 bool starts_with(const char *s, int len) const;
458
459 String lower() const;
460 String upper() const;
461 String printable(int type = 0) const;
462
463
464 /** @brief Assign this string to @a x. */
465 inline String &operator=(const String &x) {
466 if (&x != this) {
467 deref();
468 assign(x);
469 }
470 return *this;
471 }
472
473 /** @brief Assign this string to the C string @a cstr. */
474 inline String &operator=(const char *cstr) {
475 assign(cstr, -1, true);
476 return *this;
477 }
478
479 #ifdef HAVE_PERMSTRING
480 inline String &operator=(PermString p) {
481 deref();
482 assign(p);
483 return *this;
484 }
485 #endif
486
487 /** @brief Append the null-terminated C string @a cstr to this string.
488 * @param cstr data to append */
489 inline void append(const char *cstr) {
490 append(cstr, -1, 0);
491 }
492
493 /** @brief Append the first @a len characters of @a s to this string.
494 * @param s data to append
495 * @param len length of data
496 * @pre @a len @>= 0 */
497 inline void append(const char *s, int len) {
498 append(s, len, 0);
499 }
500
501 /** @brief Appends the data from @a begin to @a end to the end of this
502 * string.
503 *
504 * Does nothing if @a begin @>= @a end. */
505 inline void append(const char *begin, const char *end) {
506 if (begin < end)
507 append(begin, end - begin);
508 }
509
510 /** @brief Append @a len copies of character @a c to this string. */
511 void append_fill(int c, int len);
512
513 /** @brief Append @a len unknown characters to this string.
514 * @return Modifiable pointer to the appended characters.
515 *
516 * The caller may safely modify the returned memory. Null is returned if
517 * the string becomes out-of-memory. */
518 char *append_uninitialized(int len);
519
520
521 /** @brief Append a copy of @a x to the end of this string.
522 *
523 * Returns the result. */
524 inline String &operator+=(const String &x) {
525 append(x._r.data, x._r.length, x._r.memo);
526 return *this;
527 }
528
529 /** @brief Append a copy of the C string @a cstr to the end of this string.
530 *
531 * Returns the result. */
532 inline String &operator+=(const char *cstr) {
533 append(cstr);
534 return *this;
535 }
536
537 /** @brief Append the character @a c to the end of this string.
538 *
539 * Returns the result. */
540 inline String &operator+=(char c) {
541 append(&c, 1);
542 return *this;
543 }
544
545 #if HAVE_PERMSTRING
546 inline String &operator+=(PermString p) {
547 append(p.c_str(), p.length());
548 return *this;
549 }
550 #endif
551
552
553 // String operator+(String, const String &);
554 // String operator+(String, const char *);
555 // String operator+(const char *, const String &);
556 // String operator+(String, PermString);
557 // String operator+(PermString, String);
558 // String operator+(PermString, const char *);
559 // String operator+(const char *, PermString);
560 // String operator+(PermString, PermString);
561 // String operator+(String, char);
562
563
564 /** @brief Return true iff the String's data is shared or immutable. */
565 inline bool data_shared() const {
566 return !_r.memo || _r.memo->refcount != 1;
567 }
568
569 /** @brief Return a compact version of this String.
570 *
571 * The compact version shares no more than 256 bytes of data with any
572 * other non-stable String. */
573 inline String compact() const {
574 if (!_r.memo || _r.memo->refcount == 1
575 || (uint32_t) _r.length + 256 >= _r.memo->capacity)
576 return *this;
577 else
578 return String(_r.data, _r.data + _r.length);
579 }
580
581 /** @brief Ensure the string's data is unshared and return a mutable
582 * pointer to it. */
583 char *mutable_data();
584
585 /** @brief Null-terminate the string and return a mutable pointer to its
586 * data.
587 * @sa String::c_str */
588 char *mutable_c_str();
589
590 unsigned char *mutable_udata() {
591 return reinterpret_cast<unsigned char *>(mutable_data());
592 }
593
594 void align(int);
595
596
597 /** @brief Return true iff this is an out-of-memory string. */
598 inline bool out_of_memory() const {
599 return _r.data == &oom_data;
600 }
601
602 /** @brief Return a const reference to an out-of-memory String. */
603 static inline const String &make_out_of_memory() {
604 return reinterpret_cast<const String &>(oom_string_rep);
605 }
606
607 /** @brief Return the data pointer used for out-of-memory strings.
608 *
609 * The returned value may be dereferenced; it points to a null
610 * character. */
611 static inline const char *out_of_memory_data() {
612 return &oom_data;
613 }
614
615 private:
616
617 /** @cond never */
618 struct memo_t {
619 volatile uint32_t refcount;
620 uint32_t capacity;
621 volatile uint32_t dirty;
622 #if HAVE_STRING_PROFILING > 1
623 memo_t **pprev;
624 memo_t *next;
625 #endif
626 char real_data[8]; // but it might be more or less
627 };
628
629 enum {
630 MEMO_SPACE = sizeof(memo_t) - 8
631 };
632
633 struct rep_t {
634 const char *data;
635 int length;
636 memo_t *memo;
637 };
638 /** @endcond never */
639
640 mutable rep_t _r; // mutable for c_str()
641
642 #if HAVE_STRING_PROFILING
643 static uint64_t live_memo_count;
644 static uint64_t memo_sizes[55];
645 static uint64_t live_memo_sizes[55];
646 static uint64_t live_memo_bytes[55];
647 # if HAVE_STRING_PROFILING > 1
648 static memo_t *live_memos[55];
649 # endif
650
651 static inline int profile_memo_size_bucket(uint32_t dirty, uint32_t capacity) {
652 if (capacity <= 16)
653 return dirty;
654 else if (capacity <= 32)
655 return 17 + (capacity - 17) / 2;
656 else if (capacity <= 64)
657 return 25 + (capacity - 33) / 8;
658 else
659 return 29 + 26 - ffs_msb(capacity - 1);
660 }
661
662 static void profile_update_memo_dirty(memo_t *memo, uint32_t old_dirty, uint32_t new_dirty, uint32_t capacity) {
663 if (capacity <= 16 && new_dirty != old_dirty) {
664 ++memo_sizes[new_dirty];
665 ++live_memo_sizes[new_dirty];
666 live_memo_bytes[new_dirty] += capacity;
667 --live_memo_sizes[old_dirty];
668 live_memo_bytes[old_dirty] -= capacity;
669 # if HAVE_STRING_PROFILING > 1
670 if ((*memo->pprev = memo->next))
671 memo->next->pprev = memo->pprev;
672 memo->pprev = &live_memos[new_dirty];
673 if ((memo->next = *memo->pprev))
674 memo->next->pprev = &memo->next;
675 *memo->pprev = memo;
676 # else
677 (void) memo;
678 # endif
679 }
680 }
681
682 static void one_profile_report(StringAccum &sa, int i, int examples);
683 #endif
684
685 inline void assign_memo(const char *data, int length, memo_t *memo) const {
686 _r.data = data;
687 _r.length = length;
688 if ((_r.memo = memo))
689 ++memo->refcount;
690 }
691
692 inline String(const char *data, int length, memo_t *memo) {
693 assign_memo(data, length, memo);
694 }
695
696 inline void assign(const String &x) const {
697 assign_memo(x._r.data, x._r.length, x._r.memo);
698 }
699
700 inline void deref() const {
701 if (_r.memo && --_r.memo->refcount == 0)
702 delete_memo(_r.memo);
703 }
704
705 void assign(const char *cstr, int len, bool need_deref);
706 #if HAVE_PERMSTRING
707 inline void assign(PermString x) const {
708 assign_memo(x.c_str(), x.length(), 0);
709 }
710 #endif
711 void assign_out_of_memory();
712 void append(const char *s, int len, memo_t *memo);
713 static memo_t *create_memo(char *space, int dirty, int capacity);
714 static void delete_memo(memo_t *memo);
715
716 static const char null_data;
717 static const char oom_data;
718 static const char bool_data[11];
719 static const char int_data[20];
720 static const rep_t null_string_rep;
721 static const rep_t oom_string_rep;
722
723 static String make_claim(char *, int, int); // claim memory
724
725 friend struct rep_t;
726 friend class StringAccum;
727
728 };
729
730
731 /** @relates String
732 * @brief Compares two strings for equality.
733 *
734 * Returns true iff the two operands have the same lengths and the same
735 * characters in the same order. At most one of the operands can be a
736 * null-terminated C string.
737 * @sa String::compare
738 */
739 inline bool operator==(const String &a, const String &b) {
740 return a.equals(b.data(), b.length());
741 }
742
743 /** @relates String */
744 inline bool operator==(const char *a, const String &b) {
745 return b.equals(a, -1);
746 }
747
748 /** @relates String */
749 inline bool operator==(const String &a, const char *b) {
750 return a.equals(b, -1);
751 }
752
753 /** @relates String
754 * @brief Compare two Strings for inequality.
755 *
756 * Returns true iff !(@a a == @a b). At most one of the operands can be a
757 * null-terminated C string. */
758 inline bool operator!=(const String &a, const String &b) {
759 return !a.equals(b.data(), b.length());
760 }
761
762 /** @relates String */
763 inline bool operator!=(const char *a, const String &b) {
764 return !b.equals(a, -1);
765 }
766
767 /** @relates String */
768 inline bool operator!=(const String &a, const char *b) {
769 return !a.equals(b, -1);
770 }
771
772 /** @relates String
773 * @brief Compare two Strings.
774 *
775 * Returns true iff @a a @< @a b in lexicographic order.
776 * @sa String::compare
777 */
778 inline bool operator<(const String &a, const String &b) {
779 return a.compare(b.data(), b.length()) < 0;
780 }
781
782 /** @relates String
783 * @brief Compare two Strings.
784 *
785 * Returns true iff @a a @<= @a b in lexicographic order.
786 * @sa String::compare
787 */
788 inline bool operator<=(const String &a, const String &b) {
789 return a.compare(b.data(), b.length()) <= 0;
790 }
791
792 /** @relates String
793 * @brief Compare two Strings.
794 *
795 * Returns true iff @a a @> @a b in lexicographic order.
796 * @sa String::compare
797 */
798 inline bool operator>(const String &a, const String &b) {
799 return a.compare(b.data(), b.length()) > 0;
800 }
801
802 /** @relates String
803 * @brief Compare two Strings.
804 *
805 * Returns true iff @a a @>= @a b in lexicographic order.
806 * @sa String::compare
807 */
808 inline bool operator>=(const String &a, const String &b) {
809 return a.compare(b.data(), b.length()) >= 0;
810 }
811
812 /** @relates String
813 * @brief Concatenate the operands and return the result.
814 *
815 * At most one of the two operands can be a null-terminated C string. */
816 inline String operator+(String a, const String &b) {
817 a += b;
818 return a;
819 }
820
821 /** @relates String */
822 inline String operator+(String a, const char *b) {
823 a.append(b);
824 return a;
825 }
826
827 /** @relates String */
828 inline String operator+(const char *a, const String &b) {
829 String s1(a);
830 s1 += b;
831 return s1;
832 }
833
834 /** @relates String
835 * @brief Concatenate the operands and return the result.
836 *
837 * The second operand is a single character. */
838 inline String operator+(String a, char b) {
839 a.append(&b, 1);
840 return a;
841 }
842
843 #if HAVE_PERMSTRING
844
845 inline bool operator==(PermString a, const String &b) {
846 return b.equals(a.c_str(), a.length());
847 }
848
849 inline bool operator==(const String &a, PermString b) {
850 return a.equals(b.c_str(), b.length());
851 }
852
853 inline bool operator!=(PermString a, const String &b) {
854 return !b.equals(a.c_str(), a.length());
855 }
856
857 inline bool operator!=(const String &a, PermString b) {
858 return !a.equals(b.c_str(), b.length());
859 }
860
861 inline String operator+(String a, PermString b) {
862 a.append(b.c_str(), b.length());
863 return a;
864 }
865
866 inline String operator+(PermString a, String b) {
867 return String(a) + b;
868 }
869
870 inline String operator+(PermString a, const char *b) {
871 return String(a) + b;
872 }
873
874 inline String operator+(const char *a, PermString b) {
875 return String(a) + b;
876 }
877
878 inline String operator+(PermString a, PermString b) {
879 return String(a) + b;
880 }
881
882 #endif
883
884 inline hashcode_t hashcode(const String &str) {
885 return str.hashcode();
886 }
887
888 #endif