updated on Sat Jan 21 20:03:50 UTC 2012
[aur-mirror.git] / cegui-0.6 / CEGUIString.h
blob27366afb43d91041672f5c0aa04350fb4c39e5d4
1 /***********************************************************************
2 filename: CEGUIString.h
3 created: 26/2/2004
4 author: Paul D Turner
6 purpose: Defines string class used within the GUI system.
7 *************************************************************************/
8 /***************************************************************************
9 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * a copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 ***************************************************************************/
30 #ifndef _CEGUIString_h_
31 #define _CEGUIString_h_
33 #include "CEGUIBase.h"
34 #include <string>
35 #include <string.h>
36 #include <stdexcept>
37 #include <cstddef>
39 // Start of CEGUI namespace section
40 namespace CEGUI
42 #define STR_QUICKBUFF_SIZE 32
43 /*************************************************************************
44 Basic Types
45 *************************************************************************/
46 typedef uint8 utf8;
47 //typedef uint16 utf16; // removed typedef to prevent usage, as utf16 is not supported (yet)
48 typedef uint32 utf32;
50 /*!
51 \brief
52 String class used within the GUI system.
54 For the most part, this class can replace std::string in basic usage. However, currently String does not use the
55 current locale, and also comparisons do not take into account the Unicode data tables, so are not 'correct'
56 as such.
58 class CEGUIEXPORT String
60 public:
61 /*************************************************************************
62 Integral Types
63 *************************************************************************/
64 typedef utf32 value_type; //!< Basic 'code point' type used for String (utf32)
65 typedef size_t size_type; //!< Unsigned type used for size values and indices
66 typedef ptrdiff_t difference_type; //!< Signed type used for differences
67 typedef utf32& reference; //!< Type used for utf32 code point references
68 typedef const utf32& const_reference; //!< Type used for constant utf32 code point references
69 typedef utf32* pointer; //!< Type used for utf32 code point pointers
70 typedef const utf32* const_pointer; //!< Type used for constant utf32 code point pointers
72 static const size_type npos; //!< Value used to represent 'not found' conditions and 'all code points' etc.
74 private:
75 /*************************************************************************
76 Implementation data
77 *************************************************************************/
78 size_type d_cplength; //!< holds length of string in code points (not including null termination)
79 size_type d_reserve; //!< code point reserve size (currently allocated buffer size in code points).
81 mutable utf8* d_encodedbuff; //!< holds string data encoded as utf8 (generated only by calls to c_str() and data())
82 mutable size_type d_encodeddatlen; //!< holds length of encoded data (in case it's smaller than buffer).
83 mutable size_type d_encodedbufflen; //!< length of above buffer (since buffer can be bigger then the data it holds to save re-allocations).
85 utf32 d_quickbuff[STR_QUICKBUFF_SIZE]; //!< This is a integrated 'quick' buffer to save allocations for smallish strings
86 utf32* d_buffer; //!< Pointer the the main buffer memory. This is only valid when quick-buffer is not being used
88 public:
89 /*************************************************************************
90 Iterator Classes
91 *************************************************************************/
92 /*!
93 \brief
94 Constant forward iterator class for String objects
96 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
97 class const_iterator : public std::iterator<std::random_access_iterator_tag, utf32>
98 #else
99 class const_iterator : public std::iterator<std::random_access_iterator_tag, utf32, std::ptrdiff_t, const utf32*, const utf32&>
100 #endif
103 public:
104 //////////////////////////////////////////////////////////////////////////
105 // data
106 //////////////////////////////////////////////////////////////////////////
107 const utf32* d_ptr;
110 //////////////////////////////////////////////////////////////////////////
111 // Methods
112 //////////////////////////////////////////////////////////////////////////
113 const_iterator(void)
115 d_ptr = 0;
117 const_iterator(const_pointer ptr)
119 d_ptr = ptr;
122 const_reference operator*() const
124 return *d_ptr;
127 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
128 # pragma warning (push)
129 # pragma warning (disable : 4284)
130 #endif
131 const_pointer operator->() const
133 return &**this;
136 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
137 # pragma warning (pop)
138 #endif
140 const_iterator& operator++()
142 ++d_ptr;
143 return *this;
146 const_iterator operator++(int)
148 const_iterator temp = *this;
149 ++*this;
150 return temp;
153 const_iterator& operator--()
155 --d_ptr;
156 return *this;
159 const_iterator operator--(int)
161 const_iterator temp = *this;
162 --*this;
163 return temp;
166 const_iterator& operator+=(difference_type offset)
168 d_ptr += offset;
169 return *this;
172 const_iterator operator+(difference_type offset) const
174 const_iterator temp = *this;
175 return temp += offset;
178 const_iterator& operator-=(difference_type offset)
180 return *this += -offset;
183 const_iterator operator-(difference_type offset) const
185 const_iterator temp = *this;
186 return temp -= offset;
189 difference_type operator-(const const_iterator& iter) const
191 return d_ptr - iter.d_ptr;
194 const_reference operator[](difference_type offset) const
196 return *(*this + offset);
199 bool operator==(const const_iterator& iter) const
201 return d_ptr == iter.d_ptr;
204 bool operator!=(const const_iterator& iter) const
206 return !(*this == iter);
209 bool operator<(const const_iterator& iter) const
211 return d_ptr < iter.d_ptr;
214 bool operator>(const const_iterator& iter) const
216 return (!(iter < *this));
219 bool operator<=(const const_iterator& iter) const
221 return (!(iter < *this));
224 bool operator>=(const const_iterator& iter) const
226 return (!(*this < iter));
229 friend const_iterator operator+(difference_type offset, const const_iterator& iter)
231 return iter + offset;
237 \brief
238 Forward iterator class for String objects
240 class iterator : public const_iterator
242 public:
243 iterator(void) {}
244 iterator(pointer ptr) : const_iterator(ptr) {}
247 reference operator*() const
249 return ((reference)**(const_iterator *)this);
252 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
253 # pragma warning (push)
254 # pragma warning (disable : 4284)
255 #endif
257 pointer operator->() const
259 return &**this;
262 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
263 # pragma warning (pop)
264 #endif
266 iterator& operator++()
268 ++this->d_ptr;
269 return *this;
272 iterator operator++(int)
274 iterator temp = *this;
275 ++*this;
276 return temp;
279 iterator& operator--()
281 --this->d_ptr;
282 return *this;
285 iterator operator--(int)
287 iterator temp = *this;
288 --*this;
289 return temp;
292 iterator& operator+=(difference_type offset)
294 this->d_ptr += offset;
295 return *this;
298 iterator operator+(difference_type offset) const
300 iterator temp = *this;
301 return temp + offset;
304 iterator& operator-=(difference_type offset)
306 return *this += -offset;
309 iterator operator-(difference_type offset) const
311 iterator temp = *this;
312 return temp -= offset;
315 difference_type operator-(const const_iterator& iter) const
317 return ((const_iterator)*this - iter);
320 reference operator[](difference_type offset) const
322 return *(*this + offset);
325 friend iterator operator+(difference_type offset, const iterator& iter)
327 return iter + offset;
333 \brief
334 Constant reverse iterator class for String objects
336 #if defined(_MSC_VER) && ((_MSC_VER <= 1200) || ((_MSC_VER <= 1300) && defined(_STLPORT_VERSION)))
337 typedef std::reverse_iterator<const_iterator, const_pointer, const_reference, difference_type> const_reverse_iterator;
338 #else
339 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
340 #endif
343 \brief
344 Reverse iterator class for String objects
346 #if defined(_MSC_VER) && ((_MSC_VER <= 1200) || ((_MSC_VER <= 1300) && defined(_STLPORT_VERSION)))
347 typedef std::reverse_iterator<iterator, pointer, reference, difference_type> reverse_iterator;
348 #else
349 typedef std::reverse_iterator<iterator> reverse_iterator;
350 #endif
352 public:
354 \brief
355 Functor that can be used as comparator in a std::map with String keys.
356 It's faster than using the default, but the map will no longer be sorted alphabetically.
358 struct FastLessCompare
360 bool operator() (const String& a, const String& b) const
362 const size_t la = a.length();
363 const size_t lb = b.length();
364 if (la == lb)
365 return (memcmp(a.ptr(), b.ptr(), la*sizeof(utf32)) < 0);
366 return (la < lb);
370 public:
371 //////////////////////////////////////////////////////////////////////////
372 // Default Construction and Destructor
373 //////////////////////////////////////////////////////////////////////////
375 \brief
376 Constructs an empty string
378 String(void)
380 init();
384 \brief
385 Destructor for String objects
387 ~String(void);
389 //////////////////////////////////////////////////////////////////////////
390 // Construction via CEGUI::String
391 //////////////////////////////////////////////////////////////////////////
393 \brief
394 Copy constructor - Creates a new string with the same value as \a str
396 \param str
397 String object used to initialise the newly created string
399 \return
400 Nothing
402 String(const String& str)
404 init();
405 assign(str);
410 \brief
411 Constructs a new string initialised with code points from another String object.
413 \param str
414 String object used to initialise the newly created string
416 \param str_idx
417 Starting code-point of \a str to be used when initialising the new String
419 \param str_num
420 Maximum number of code points from \a str that are to be assigned to the new String
422 \return
423 Nothing
425 String(const String& str, size_type str_idx, size_type str_num = npos)
427 init();
428 assign(str, str_idx, str_num);
431 //////////////////////////////////////////////////////////////////////////
432 // Construction via std::string
433 //////////////////////////////////////////////////////////////////////////
435 \brief
436 Constructs a new string and initialises it using the std::string std_str
438 \param std_str
439 The std::string object that is to be used to initialise the new String object.
441 \note
442 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
443 the provided data will occur.
445 \return
446 Nothing
448 \exception std::length_error Thrown if resulting String object would be too big.
450 String(const std::string& std_str)
452 init();
453 assign(std_str);
457 \brief
458 Constructs a new string initialised with characters from the given std::string object.
460 \param std_str
461 std::string object used to initialise the newly created string
463 \param str_idx
464 Starting character of \a std_str to be used when initialising the new String
466 \note
467 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
468 the provided data will occur.
470 \param str_num
471 Maximum number of characters from \a std_str that are to be assigned to the new String
473 \return
474 Nothing
476 \exception std::length_error Thrown if resulting String object would be too big.
478 String(const std::string& std_str, size_type str_idx, size_type str_num = npos)
480 init();
481 assign(std_str, str_idx, str_num);
485 //////////////////////////////////////////////////////////////////////////
486 // Construction via UTF-8 stream (for straight ASCII use, only codes 0x00 - 0x7f are valid)
487 //////////////////////////////////////////////////////////////////////////
489 \brief
490 Constructs a new String object and initialise it using the provided utf8 encoded string buffer.
492 \param utf8_str
493 Pointer to a buffer containing a null-terminated Unicode string encoded as utf8 data.
495 \note
496 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
497 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
498 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
499 results.
501 \return
502 Nothing
504 \exception std::length_error Thrown if resulting String object would be too big.
506 String(const utf8* utf8_str)
508 init();
509 assign(utf8_str);
513 \brief
514 Constructs a new String object and initialise it using the provided utf8 encoded string buffer.
516 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
517 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
518 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
519 results.
521 \param utf8_str
522 Pointer to a buffer containing Unicode string data encoded as utf8.
524 \note
525 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
526 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
527 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
528 results.
530 \param chars_len
531 Length of the provided utf8 string in code units (not code-points).
533 \return
534 Nothing
536 \exception std::length_error Thrown if resulting String object would be too big.
538 String(const utf8* utf8_str, size_type chars_len)
540 init();
541 assign(utf8_str, chars_len);
544 //////////////////////////////////////////////////////////////////////////
545 // Construction via code-point (using a UTF-32 code unit)
546 //////////////////////////////////////////////////////////////////////////
548 \brief
549 Constructs a new String that is initialised with the specified code point
551 \param num
552 The number of times \a code_point is to be put into new String object
554 \param code_point
555 The Unicode code point to be used when initialising the String object
557 \return
558 Nothing
560 \exception std::length_error Thrown if resulting String object would be too big.
562 String(size_type num, utf32 code_point)
564 init();
565 assign(num, code_point);
568 //////////////////////////////////////////////////////////////////////////
569 // Construction via iterator
570 //////////////////////////////////////////////////////////////////////////
571 // Create string with characters in the range [beg, end)
573 \brief
574 Construct a new string object and initialise it with code-points from the range [beg, end).
576 \param beg
577 Iterator describing the start of the data to be used when initialising the String object
579 \param end
580 Iterator describing the (exclusive) end of the data to be used when initialising the String object
582 \return
583 Nothing
585 String(const_iterator iter_beg, const_iterator iter_end)
587 init();
588 append(iter_beg, iter_end);
592 //////////////////////////////////////////////////////////////////////////
593 // Construction via c-string
594 //////////////////////////////////////////////////////////////////////////
596 \brief
597 Constructs a new String object and initialise it using the provided c-string.
599 \param c_str
600 Pointer to a c-string.
602 \return
603 Nothing
605 \exception std::length_error Thrown if resulting String object would be too big.
607 String(const char* cstr)
609 init();
610 assign(cstr);
614 \brief
615 Constructs a new String object and initialise it using characters from the provided char array.
617 \param chars
618 char array.
620 \param chars_len
621 Number of chars from the array to be used.
623 \return
624 Nothing
626 \exception std::length_error Thrown if resulting String object would be too big.
628 String(const char* chars, size_type chars_len)
630 init();
631 assign(chars, chars_len);
635 //////////////////////////////////////////////////////////////////////////
636 // Size operations
637 //////////////////////////////////////////////////////////////////////////
639 \brief
640 Returns the size of the String in code points
642 \return
643 Number of code points currently in the String
645 size_type size(void) const
647 return d_cplength;
651 \brief
652 Returns the size of the String in code points
654 \return
655 Number of code points currently in the String
657 size_type length(void) const
659 return d_cplength;
663 \brief
664 Returns true if the String is empty
666 \return
667 true if the String is empty, else false.
669 bool empty(void) const
671 return (d_cplength == 0);
675 \brief
676 Returns the maximum size of a String.
678 Any operation that would result in a String that is larger than this value will throw the std::length_error exception.
680 \return
681 The maximum number of code points that a string can contain
683 static size_type max_size(void)
685 return (((size_type)-1) / sizeof(utf32));
688 //////////////////////////////////////////////////////////////////////////
689 // Capacity Operations
690 //////////////////////////////////////////////////////////////////////////
691 // return the number of code points the string could hold without re-allocation
692 // (due to internal encoding this will always report the figure for worst-case encoding, and could even be < size()!)
694 \brief
695 Return the number of code points that the String could hold before a re-allocation would be required.
697 \return
698 Size of the current reserve buffer. This is the maximum number of code points the String could hold before a buffer
699 re-allocation would be required
701 size_type capacity(void) const
703 return d_reserve - 1;
706 // reserve internal memory for at-least 'num' code-points (characters). if num is 0, request is shrink-to-fit.
708 \brief
709 Specifies the amount of reserve capacity to allocate.
711 \param num
712 The number of code points to allocate space for. If \a num is larger that the current reserve, then a re-allocation will occur. If
713 \a num is smaller than the current reserve (but not 0) the buffer may be shrunk to the larger of the specified number, or the current
714 String size (operation is currently not implemented). If \a num is 0, then the buffer is re-allocated to fit the current String size.
716 \return
717 Nothing
719 \exception std::length_error Thrown if resulting String object would be too big.
721 void reserve(size_type num = 0)
723 if (num == 0)
724 trim();
725 else
726 grow(num);
729 //////////////////////////////////////////////////////////////////////////
730 // Comparisons
731 //////////////////////////////////////////////////////////////////////////
733 \brief
734 Compares this String with the String 'str'.
736 \note
737 This does currently not properly consider Unicode and / or the system locale.
739 \param str
740 The String object that is to compared with this String.
742 \return
743 - 0 if the String objects are equal
744 - <0 if this String is lexicographically smaller than \a str
745 - >0 if this String is lexicographically greater than \a str
747 int compare(const String& str) const
749 return compare(0, d_cplength, str);
753 \brief
754 Compares code points from this String with code points from the String 'str'.
756 \note
757 This does currently not properly consider Unicode and / or the system locale.
759 \param idx
760 Index of the first code point from this String to consider.
762 \param len
763 Maximum number of code points from this String to consider.
765 \param str
766 The String object that is to compared with this String.
768 \param str_idx
769 Index of the first code point from String \a str to consider.
771 \param str_len
772 Maximum number of code points from String \a str to consider
774 \return
775 - 0 if the specified sub-strings are equal
776 - <0 if specified sub-strings are lexicographically smaller than \a str
777 - >0 if specified sub-strings are lexicographically greater than \a str
779 \exception std::out_of_range Thrown if either \a idx or \a str_idx are invalid.
781 int compare(size_type idx, size_type len, const String& str, size_type str_idx = 0, size_type str_len = npos) const
783 if ((d_cplength < idx) || (str.d_cplength < str_idx))
784 throw std::out_of_range("Index is out of range for CEGUI::String");
786 if ((len == npos) || (idx + len > d_cplength))
787 len = d_cplength - idx;
789 if ((str_len == npos) || (str_idx + str_len > str.d_cplength))
790 str_len = str.d_cplength - str_idx;
792 int val = (len == 0) ? 0 : utf32_comp_utf32(&ptr()[idx], &str.ptr()[str_idx], (len < str_len) ? len : str_len);
794 return (val != 0) ? ((val < 0) ? -1 : 1) : (len < str_len) ? -1 : (len == str_len) ? 0 : 1;
799 \brief
800 Compares this String with the std::string 'std_str'.
802 \note
803 This does currently not properly consider Unicode and / or the system locale.
805 \param std_str
806 The std::string object that is to compared with this String.
808 \note
809 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
810 the encountered data is performed.
812 \return
813 - 0 if the string objects are equal
814 - <0 if this string is lexicographically smaller than \a std_str
815 - >0 if this string is lexicographically greater than \a std_str
817 int compare(const std::string& std_str) const
819 return compare(0, d_cplength, std_str);
824 \brief
825 Compares code points from this String with code points from the std::string 'std_str'.
827 \note
828 This does currently not properly consider Unicode and / or the system locale.
830 \param idx
831 Index of the first code point from this String to consider.
833 \param len
834 Maximum number of code points from this String to consider.
836 \param std_str
837 The std::string object that is to compared with this String.
839 \note
840 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
841 the encountered data is performed.
843 \param str_idx
844 Index of the first character from std::string \a std_str to consider.
846 \param str_len
847 Maximum number of characters from std::string \a std_str to consider
849 \return
850 - 0 if the specified sub-strings are equal
851 - <0 if specified sub-strings are lexicographically smaller than \a std_str
852 - >0 if specified sub-strings are lexicographically greater than \a std_str
854 \exception std::out_of_range Thrown if either \a idx or \a str_idx are invalid.
856 int compare(size_type idx, size_type len, const std::string& std_str, size_type str_idx = 0, size_type str_len = npos) const
858 if (d_cplength < idx)
859 throw std::out_of_range("Index is out of range for CEGUI::String");
861 if (std_str.size() < str_idx)
862 throw std::out_of_range("Index is out of range for std::string");
864 if ((len == npos) || (idx + len > d_cplength))
865 len = d_cplength - idx;
867 if ((str_len == npos) || (str_idx + str_len > std_str.size()))
868 str_len = (size_type)std_str.size() - str_idx;
870 int val = (len == 0) ? 0 : utf32_comp_char(&ptr()[idx], &std_str.c_str()[str_idx], (len < str_len) ? len : str_len);
872 return (val != 0) ? ((val < 0) ? -1 : 1) : (len < str_len) ? -1 : (len == str_len) ? 0 : 1;
877 \brief
878 Compares this String with the null-terminated utf8 encoded 'utf8_str'.
880 \note
881 This does currently not properly consider Unicode and / or the system locale.
883 \param utf8_str
884 The buffer containing valid Unicode data encoded as utf8 that is to compared with this String.
886 \note
887 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
888 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
889 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
890 results.
892 \return
893 - 0 if the strings are equal
894 - <0 if this string is lexicographically smaller than \a utf8_str
895 - >0 if this string is lexicographically greater than \a utf8_str
897 int compare(const utf8* utf8_str) const
899 return compare(0, d_cplength, utf8_str, encoded_size(utf8_str));
904 \brief
905 Compares code points from this String with the null-terminated utf8 encoded 'utf8_str'.
907 \note
908 This does currently not properly consider Unicode and / or the system locale.
910 \param idx
911 Index of the first code point from this String to consider.
913 \param len
914 Maximum number of code points from this String to consider.
916 \param utf8_str
917 The buffer containing valid Unicode data encoded as utf8 that is to compared with this String.
919 \note
920 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
921 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
922 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
923 results.
925 \return
926 - 0 if the specified sub-strings are equal
927 - <0 if specified sub-strings are lexicographically smaller than \a utf8_str
928 - >0 if specified sub-strings are lexicographically greater than \a utf8_str
930 \exception std::out_of_range Thrown if \a idx is invalid.
932 int compare(size_type idx, size_type len, const utf8* utf8_str) const
934 return compare(idx, len, utf8_str, encoded_size(utf8_str));
938 \brief
939 Compares code points from this String with the utf8 encoded data in buffer 'utf8_str'.
941 \note
942 This does currently not properly consider Unicode and / or the system locale.
944 \param idx
945 Index of the first code point from this String to consider.
947 \param len
948 Maximum number of code points from this String to consider.
950 \param utf8_str
951 The buffer containing valid Unicode data encoded as utf8 that is to compared with this String.
953 \note
954 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
955 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
956 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
957 results.
959 \param str_cplen
960 The number of encoded code points in the buffer \a utf8_str (this is not the same as the number of code units).
962 \return
963 - 0 if the specified sub-strings are equal
964 - <0 if specified sub-strings are lexicographically smaller than \a utf8_str
965 - >0 if specified sub-strings are lexicographically greater than \a utf8_str
967 \exception std::out_of_range Thrown if \a idx is invalid.
968 \exception std::length_error Thrown if \a str_cplen is set to npos.
970 int compare(size_type idx, size_type len, const utf8* utf8_str, size_type str_cplen) const
972 if (d_cplength < idx)
973 throw std::out_of_range("Index is out of range for CEGUI::String");
975 if (str_cplen == npos)
976 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
978 if ((len == npos) || (idx + len > d_cplength))
979 len = d_cplength - idx;
981 int val = (len == 0) ? 0 : utf32_comp_utf8(&ptr()[idx], utf8_str, (len < str_cplen) ? len : str_cplen);
983 return (val != 0) ? ((val < 0) ? -1 : 1) : (len < str_cplen) ? -1 : (len == str_cplen) ? 0 : 1;
988 \brief
989 Compares this String with the given c-string.
991 \note
992 This does currently not properly consider Unicode and / or the system locale.
994 \param c_str
995 The c-string that is to compared with this String.
997 \return
998 - 0 if the strings are equal
999 - <0 if this string is lexicographically smaller than \a c_str
1000 - >0 if this string is lexicographically greater than \a c_str
1002 int compare(const char* cstr) const
1004 return compare(0, d_cplength, cstr, strlen(cstr));
1009 \brief
1010 Compares code points from this String with the given c-string.
1012 \note
1013 This does currently not properly consider Unicode and / or the system locale.
1015 \param idx
1016 Index of the first code point from this String to consider.
1018 \param len
1019 Maximum number of code points from this String to consider.
1021 \param c_str
1022 The c-string that is to compared with this String.
1024 \return
1025 - 0 if the specified sub-strings are equal
1026 - <0 if specified sub-strings are lexicographically smaller than \a c_str
1027 - >0 if specified sub-strings are lexicographically greater than \a c_str
1029 \exception std::out_of_range Thrown if \a idx is invalid.
1031 int compare(size_type idx, size_type len, const char* cstr) const
1033 return compare(idx, len, cstr, strlen(cstr));
1038 \brief
1039 Compares code points from this String with chars in the given char array.
1041 \note
1042 This does currently not properly consider Unicode and / or the system locale.
1044 \param idx
1045 Index of the first code point from this String to consider.
1047 \param len
1048 Maximum number of code points from this String to consider.
1050 \param chars
1051 The array containing the chars that are to compared with this String.
1053 \param chars_len
1054 The number of chars in the array.
1056 \return
1057 - 0 if the specified sub-strings are equal
1058 - <0 if specified sub-strings are lexicographically smaller than \a chars
1059 - >0 if specified sub-strings are lexicographically greater than \a chars
1061 \exception std::out_of_range Thrown if \a idx is invalid.
1062 \exception std::length_error Thrown if \a chars_len is set to npos.
1064 int compare(size_type idx, size_type len, const char* chars, size_type chars_len) const
1066 if (d_cplength < idx)
1067 throw std::out_of_range("Index is out of range for CEGUI::String");
1069 if (chars_len == npos)
1070 throw std::length_error("Length for char array can not be 'npos'");
1072 if ((len == npos) || (idx + len > d_cplength))
1073 len = d_cplength - idx;
1075 int val = (len == 0) ? 0 : utf32_comp_char(&ptr()[idx], chars, (len < chars_len) ? len : chars_len);
1077 return (val != 0) ? ((val < 0) ? -1 : 1) : (len < chars_len) ? -1 : (len == chars_len) ? 0 : 1;
1081 //////////////////////////////////////////////////////////////////////////
1082 // Character access
1083 //////////////////////////////////////////////////////////////////////////
1085 \brief
1086 Returns the code point at the given index.
1088 \param idx
1089 Zero based index of the code point to be returned.
1091 \note
1092 - For constant strings length()/size() provide a valid index and will access the default utf32 value.
1093 - For non-constant strings length()/size() is an invalid index, and acceesing (especially writing) this index could cause string corruption.
1095 \return
1096 The utf32 code point at the given index within the String.
1098 reference operator[](size_type idx)
1100 return (ptr()[idx]);
1104 \brief
1105 Returns the code point at the given index.
1107 \param idx
1108 Zero based index of the code point to be returned.
1110 \note
1111 - For constant strings length()/size() provide a valid index and will access the default utf32 value.
1112 - For non-constant strings length()/size() is an invalid index, and acceesing (especially writing) this index could cause string corruption.
1114 \return
1115 The utf32 code point at the given index within the String.
1117 value_type operator[](size_type idx) const
1119 return ptr()[idx];
1123 \brief
1124 Returns the code point at the given index.
1126 \param idx
1127 Zero based index of the code point to be returned.
1129 \return
1130 The utf32 code point at the given index within the String.
1132 \exception std::out_of_range Thrown if \a idx is >= length().
1134 reference at(size_type idx)
1136 if (d_cplength <= idx)
1137 throw std::out_of_range("Index is out of range for CEGUI::String");
1139 return ptr()[idx];
1143 \brief
1144 Returns the code point at the given index.
1146 \param idx
1147 Zero based index of the code point to be returned.
1149 \return
1150 The utf32 code point at the given index within the String.
1152 \exception std::out_of_range Thrown if \a idx is >= length().
1154 const_reference at(size_type idx) const
1156 if (d_cplength <= idx)
1157 throw std::out_of_range("Index is out of range for CEGUI::String");
1159 return ptr()[idx];
1163 //////////////////////////////////////////////////////////////////////////
1164 // C-Strings and arrays
1165 //////////////////////////////////////////////////////////////////////////
1167 \brief
1168 Returns contents of the String as a null terminated string of utf8 encoded data.
1170 \return
1171 Pointer to a char buffer containing the contents of the String encoded as null-terminated utf8 data.
1173 \note
1174 The buffer returned from this function is owned by the String object.
1176 \note
1177 Any function that modifies the String data will invalidate the buffer returned by this call.
1179 const char* c_str(void) const
1181 return (const char*)build_utf8_buff();
1185 \brief
1186 Returns contents of the String as utf8 encoded data.
1188 \return
1189 Pointer to a buffer containing the contents of the String encoded utf8 data.
1191 \note
1192 The buffer returned from this function is owned by the String object.
1194 \note
1195 Any function that modifies the String data will invalidate the buffer returned by this call.
1197 const utf8* data(void) const
1199 return build_utf8_buff();
1203 \brief
1204 Returns a pointer to the buffer in use.
1206 utf32* ptr(void)
1208 return (d_reserve > STR_QUICKBUFF_SIZE) ? d_buffer : d_quickbuff;
1212 \brief
1213 Returns a pointer to the buffer in use. (const version)
1215 const utf32* ptr(void) const
1217 return (d_reserve > STR_QUICKBUFF_SIZE) ? d_buffer : d_quickbuff;
1220 // copy, at most, 'len' code-points of the string, begining with code-point 'idx', into the array 'buf' as valid utf8 encoded data
1221 // return number of utf8 code units placed into the buffer
1223 \brief
1224 Copies an area of the String into the provided buffer as encoded utf8 data.
1226 \param buf
1227 Pointer to a buffer that is to receive the encoded data (this must be big enough to hold the encoded data)
1229 \param len
1230 Maximum number of code points from the String that should be encoded into the buffer
1232 \param idx
1233 Index of the first code point to be encoded into the buffer
1235 \return
1236 The number of utf8 encoded code units transferred to the buffer.
1238 \note A code unit does not equal a code point. A utf32 code point, when encoded as utf8, can occupy between 1 and 4 code units.
1240 \exception std::out_of_range Thrown if \a idx was invalid for this String.
1242 size_type copy(utf8* buf, size_type len = npos, size_type idx = 0) const
1244 if (d_cplength < idx)
1245 throw std::out_of_range("Index is out of range for CEGUI::String");
1247 if (len == npos)
1248 len = d_cplength;
1250 return encode(&ptr()[idx], buf, npos, len);
1253 //////////////////////////////////////////////////////////////////////////
1254 // UTF8 Encoding length information
1255 //////////////////////////////////////////////////////////////////////////
1256 // return the number of bytes required to hold 'num' code-points, starting at code-point 'idx', of the the string when encoded as utf8 data.
1258 \brief
1259 Return the number of utf8 code units required to hold an area of the String when encoded as utf8 data
1261 \param num
1262 Maximum number of code points to consider when calculating utf8 encoded size.
1264 \param idx
1265 Index of the first code point to consider when calculating the utf8 encoded size
1267 \return
1268 The number of utf8 code units (bytes) required to hold the specified sub-string when encoded as utf8 data.
1270 \exception std::out_of_range Thrown if \a idx was invalid for this String.
1272 size_type utf8_stream_len(size_type num = npos, size_type idx = 0) const
1274 using namespace std;
1276 if (d_cplength < idx)
1277 throw out_of_range("Index was out of range for CEGUI::String object");
1279 size_type maxlen = d_cplength - idx;
1281 return encoded_size(&ptr()[idx], ceguimin(num, maxlen));
1284 //////////////////////////////////////////////////////////////////////////
1285 // Assignment Functions
1286 //////////////////////////////////////////////////////////////////////////
1288 \brief
1289 Assign the value of String \a str to this String
1291 \param str
1292 String object containing the string value to be assigned.
1294 \return
1295 This String after the assignment has happened
1297 String& operator=(const String& str)
1299 return assign(str);
1303 \brief
1304 Assign a sub-string of String \a str to this String
1306 \param str
1307 String object containing the string data to be assigned.
1309 \param str_idx
1310 Index of the first code point in \a str that is to be assigned
1312 \param str_num
1313 Maximum number of code points from \a str that are be be assigned
1315 \return
1316 This String after the assignment has happened
1318 \exception std::out_of_range Thrown if str_idx is invalid for \a str
1320 String& assign(const String& str, size_type str_idx = 0, size_type str_num = npos)
1322 if (str.d_cplength < str_idx)
1323 throw std::out_of_range("Index was out of range for CEGUI::String object");
1325 if ((str_num == npos) || (str_num > str.d_cplength - str_idx))
1326 str_num = str.d_cplength - str_idx;
1328 grow(str_num);
1329 setlen(str_num);
1330 memcpy(ptr(), &str.ptr()[str_idx], str_num * sizeof(utf32));
1332 return *this;
1336 \brief
1337 Assign the value of std::string \a std_str to this String
1339 \note
1340 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
1341 the provided data will occur.
1343 \param std_str
1344 std::string object containing the string value to be assigned.
1346 \return
1347 This String after the assignment has happened
1349 \exception std::length_error Thrown if the resulting String would have been too large.
1351 String& operator=(const std::string& std_str)
1353 return assign(std_str);
1357 \brief
1358 Assign a sub-string of std::string \a std_str to this String
1360 \note
1361 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
1362 the provided data will occur.
1364 \param std_str
1365 std::string object containing the string value to be assigned.
1367 \param str_idx
1368 Index of the first character of \a std_str to be assigned
1370 \param str_num
1371 Maximum number of characters from \a std_str to be assigned
1373 \return
1374 This String after the assignment has happened
1376 \exception std::out_of_range Thrown if \a str_idx is invalid for \a std_str
1377 \exception std::length_error Thrown if the resulting String would have been too large.
1379 String& assign(const std::string& std_str, size_type str_idx = 0, size_type str_num = npos)
1381 if (std_str.size() < str_idx)
1382 throw std::out_of_range("Index was out of range for std::string object");
1384 if ((str_num == npos) || (str_num > (size_type)std_str.size() - str_idx))
1385 str_num = (size_type)std_str.size() - str_idx;
1387 grow(str_num);
1388 setlen(str_num);
1390 while(str_num--)
1392 ((*this)[str_num]) = static_cast<utf32>(static_cast<unsigned char>(std_str[str_num + str_idx]));
1395 return *this;
1399 \brief
1400 Assign to this String the string value represented by the given null-terminated utf8 encoded data
1402 \note
1403 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
1404 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
1405 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
1406 results.
1408 \param utf8_str
1409 Buffer containing valid null-terminated utf8 encoded data
1411 \return
1412 This String after the assignment has happened
1414 \exception std::length_error Thrown if the resulting String would have been too large.
1416 String& operator=(const utf8* utf8_str)
1418 return assign(utf8_str, utf_length(utf8_str));
1422 \brief
1423 Assign to this String the string value represented by the given null-terminated utf8 encoded data
1425 \note
1426 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
1427 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
1428 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
1429 results.
1431 \param utf8_str
1432 Buffer containing valid null-terminated utf8 encoded data
1434 \return
1435 This String after the assignment has happened
1437 \exception std::length_error Thrown if the resulting String would have been too large.
1439 String& assign(const utf8* utf8_str)
1441 return assign(utf8_str, utf_length(utf8_str));
1445 \brief
1446 Assign to this String the string value represented by the given utf8 encoded data
1448 \note
1449 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
1450 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
1451 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
1452 results.
1454 \param utf8_str
1455 Buffer containing valid utf8 encoded data
1457 \param str_num
1458 Number of code units (not code points) in the buffer pointed to by \a utf8_str
1460 \return
1461 This String after the assignment has happened
1463 \exception std::length_error Thrown if the resulting String would have been too large, or if str_num is 'npos'.
1465 String& assign(const utf8* utf8_str, size_type str_num)
1467 if (str_num == npos)
1468 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
1470 size_type enc_sze = encoded_size(utf8_str, str_num);
1472 grow(enc_sze);
1473 encode(utf8_str, ptr(), d_reserve, str_num);
1474 setlen(enc_sze);
1475 return *this;
1479 \brief
1480 Assigns the specified utf32 code point to this String. Result is always a String 1 code point in length.
1482 \param code_point
1483 Valid utf32 Unicode code point to be assigned to the string
1485 \return
1486 This String after assignment
1488 String& operator=(utf32 code_point)
1490 return assign(1, code_point);
1494 \brief
1495 Assigns the specified code point repeatedly to the String
1497 \param num
1498 The number of times to assign the code point
1500 \param code_point
1501 Valid utf32 Unicode code point to be assigned to the string
1503 \return
1504 This String after assignment.
1506 \exception std::length_error Thrown if \a num was 'npos'
1508 String& assign(size_type num, utf32 code_point)
1510 if (num == npos)
1511 throw std::length_error("Code point count can not be 'npos'");
1513 grow(num);
1514 setlen(num);
1515 utf32* p = ptr();
1517 while(num--)
1518 *p++ = code_point;
1520 return *this;
1525 \brief
1526 Assign to this String the given C-string.
1528 \param c_str
1529 Pointer to a valid C style string.
1531 \return
1532 This String after the assignment has happened
1534 \exception std::length_error Thrown if the resulting String would have been too large.
1536 String& operator=(const char* cstr)
1538 return assign(cstr, strlen(cstr));
1543 \brief
1544 Assign to this String the given C-string.
1546 \param c_str
1547 Pointer to a valid C style string.
1549 \return
1550 This String after the assignment has happened
1552 \exception std::length_error Thrown if the resulting String would have been too large.
1554 String& assign(const char* cstr)
1556 return assign(cstr, strlen(cstr));
1561 \brief
1562 Assign to this String a number of chars from a char array.
1564 \param chars
1565 char array.
1567 \param chars_len
1568 Number of chars to be assigned.
1570 \return
1571 This String after the assignment has happened
1573 \exception std::length_error Thrown if the resulting String would have been too large.
1575 String& assign(const char* chars, size_type chars_len)
1577 grow(chars_len);
1578 utf32* pt = ptr();
1580 for (size_type i = 0; i < chars_len; ++i)
1582 *pt++ = static_cast<utf32>(static_cast<unsigned char>(*chars++));
1585 setlen(chars_len);
1586 return *this;
1591 \brief
1592 Swaps the value of this String with the given String \a str
1594 \param str
1595 String object whos value is to be swapped with this String.
1597 \return
1598 Nothing
1600 void swap(String& str)
1602 size_type temp_len = d_cplength;
1603 d_cplength = str.d_cplength;
1604 str.d_cplength = temp_len;
1606 size_type temp_res = d_reserve;
1607 d_reserve = str.d_reserve;
1608 str.d_reserve = temp_res;
1610 utf32* temp_buf = d_buffer;
1611 d_buffer = str.d_buffer;
1612 str.d_buffer = temp_buf;
1614 // see if we need to swap 'quick buffer' data
1615 if (temp_res <= STR_QUICKBUFF_SIZE)
1617 utf32 temp_qbf[STR_QUICKBUFF_SIZE];
1619 memcpy(temp_qbf, d_quickbuff, STR_QUICKBUFF_SIZE * sizeof(utf32));
1620 memcpy(d_quickbuff, str.d_quickbuff, STR_QUICKBUFF_SIZE * sizeof(utf32));
1621 memcpy(str.d_quickbuff, temp_qbf, STR_QUICKBUFF_SIZE * sizeof(utf32));
1626 //////////////////////////////////////////////////////////////////////////
1627 // Appending Functions
1628 //////////////////////////////////////////////////////////////////////////
1630 \brief
1631 Appends the String \a str
1633 \param str
1634 String object that is to be appended
1636 \return
1637 This String after the append operation
1639 \exception std::length_error Thrown if resulting String would be too large.
1641 String& operator+=(const String& str)
1643 return append(str);
1647 \brief
1648 Appends a sub-string of the String \a str
1650 \param str
1651 String object containing data to be appended
1653 \param str_idx
1654 Index of the first code point to be appended
1656 \param str_num
1657 Maximum number of code points to be appended
1659 \return
1660 This String after the append operation
1662 \exception std::out_of_range Thrown if \a str_idx is invalid for \a str.
1663 \exception std::length_error Thrown if resulting String would be too large.
1665 String& append(const String& str, size_type str_idx = 0, size_type str_num = npos)
1667 if (str.d_cplength < str_idx)
1668 throw std::out_of_range("Index is out of range for CEGUI::String");
1670 if ((str_num == npos) || (str_num > str.d_cplength - str_idx))
1671 str_num = str.d_cplength - str_idx;
1673 grow(d_cplength + str_num);
1674 memcpy(&ptr()[d_cplength], &str.ptr()[str_idx], str_num * sizeof(utf32));
1675 setlen(d_cplength + str_num);
1676 return *this;
1681 \brief
1682 Appends the std::string \a std_str
1684 \param std_str
1685 std::string object that is to be appended
1687 \note
1688 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
1689 the provided data will occur.
1691 \return
1692 This String after the append operation
1694 \exception std::length_error Thrown if resulting String would be too large.
1696 String& operator+=(const std::string& std_str)
1698 return append(std_str);
1702 \brief
1703 Appends a sub-string of the std::string \a std_str
1705 \param std_str
1706 std::string object containing data to be appended
1708 \note
1709 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
1710 the provided data will occur.
1712 \param str_idx
1713 Index of the first character to be appended
1715 \param str_num
1716 Maximum number of characters to be appended
1718 \return
1719 This String after the append operation
1721 \exception std::out_of_range Thrown if \a str_idx is invalid for \a std_str.
1722 \exception std::length_error Thrown if resulting String would be too large.
1724 String& append(const std::string& std_str, size_type str_idx = 0, size_type str_num = npos)
1726 if (std_str.size() < str_idx)
1727 throw std::out_of_range("Index is out of range for std::string");
1729 if ((str_num == npos) || (str_num > (size_type)std_str.size() - str_idx))
1730 str_num = (size_type)std_str.size() - str_idx;
1732 size_type newsze = d_cplength + str_num;
1734 grow(newsze);
1735 utf32* pt = &ptr()[newsze-1];
1737 while(str_num--)
1738 *pt-- = static_cast<utf32>(static_cast<unsigned char>(std_str[str_num]));
1740 setlen(newsze);
1741 return *this;
1746 \brief
1747 Appends to the String the null-terminated utf8 encoded data in the buffer utf8_str.
1749 \param utf8_str
1750 buffer holding the null-terminated utf8 encoded data that is to be appended
1752 \note
1753 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
1754 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
1755 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
1756 results.
1758 \return
1759 This String after the append operation
1761 \exception std::length_error Thrown if resulting String would be too large.
1763 String& operator+=(const utf8* utf8_str)
1765 return append(utf8_str, utf_length(utf8_str));
1769 \brief
1770 Appends to the String the null-terminated utf8 encoded data in the buffer utf8_str.
1772 \param utf8_str
1773 Buffer holding the null-terminated utf8 encoded data that is to be appended
1775 \note
1776 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
1777 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
1778 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
1779 results.
1781 \return
1782 This String after the append operation
1784 \exception std::length_error Thrown if resulting String would be too large.
1786 String& append(const utf8* utf8_str)
1788 return append(utf8_str, utf_length(utf8_str));
1793 \brief
1794 Appends to the String the utf8 encoded data in the buffer utf8_str.
1796 \param utf8_str
1797 Buffer holding the utf8 encoded data that is to be appended
1799 \note
1800 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
1801 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
1802 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
1803 results.
1805 \param len
1806 Number of code units (not code points) in the buffer to be appended
1808 \return
1809 This String after the append operation
1811 \exception std::length_error Thrown if resulting String would be too large, or if \a len was 'npos'
1813 String& append(const utf8* utf8_str, size_type len)
1815 if (len == npos)
1816 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
1818 size_type encsz = encoded_size(utf8_str, len);
1819 size_type newsz = d_cplength + encsz;
1821 grow(newsz);
1822 encode(utf8_str, &ptr()[d_cplength], encsz, len);
1823 setlen(newsz);
1825 return *this;
1830 \brief
1831 Appends a single code point to the string
1833 \param code_point
1834 utf32 Unicode code point that is to be appended
1836 \return
1837 This String after the append operation
1839 \exception std::length_error Thrown if resulting String would be too long.
1841 String& operator+=(utf32 code_point)
1843 return append(1, code_point);
1847 \brief
1848 Appends a single code point multiple times to the string
1850 \param num
1851 Number of copies of the code point to be appended
1853 \param code_point
1854 utf32 Unicode code point that is to be appended
1856 \return
1857 This String after the append operation
1859 \exception std::length_error Thrown if resulting String would be too long, or if \a num was 'npos'.
1861 String& append(size_type num, utf32 code_point)
1863 if (num == npos)
1864 throw std::length_error("Code point count can not be 'npos'");
1866 size_type newsz = d_cplength + num;
1867 grow(newsz);
1869 utf32* p = &ptr()[d_cplength];
1871 while(num--)
1872 *p++ = code_point;
1874 setlen(newsz);
1876 return *this;
1880 \brief
1881 Appends a single code point to the string
1883 \param code_point
1884 utf32 Unicode code point that is to be appended
1886 \return
1887 Nothing
1889 \exception std::length_error Thrown if resulting String would be too long.
1891 void push_back(utf32 code_point)
1893 append(1, code_point);
1897 \brief
1898 Appends the code points in the reange [beg, end)
1900 \param beg
1901 Iterator describing the start of the range to be appended
1903 \param end
1904 Iterator describing the (exclusive) end of the range to be appended.
1906 \return
1907 This String after the append operation
1909 \exception std::length_error Thrown if the resulting string would be too large.
1911 String& append(const_iterator iter_beg, const_iterator iter_end)
1913 return replace(end(), end(), iter_beg, iter_end);
1918 \brief
1919 Appends to the String the given c-string.
1921 \param c_str
1922 c-string that is to be appended.
1924 \return
1925 This String after the append operation
1927 \exception std::length_error Thrown if resulting String would be too large.
1929 String& operator+=(const char* cstr)
1931 return append(cstr, strlen(cstr));
1936 \brief
1937 Appends to the String the given c-string.
1939 \param c_str
1940 c-string that is to be appended.
1942 \return
1943 This String after the append operation
1945 \exception std::length_error Thrown if resulting String would be too large.
1947 String& append(const char* cstr)
1949 return append(cstr, strlen(cstr));
1954 \brief
1955 Appends to the String chars from the given char array.
1957 \param chars
1958 char array holding the chars that are to be appended
1960 \param chars_len
1961 Number of chars to be appended
1963 \return
1964 This String after the append operation
1966 \exception std::length_error Thrown if resulting String would be too large, or if \a chars_len was 'npos'
1968 String& append(const char* chars, size_type chars_len)
1970 if (chars_len == npos)
1971 throw std::length_error("Length for char array can not be 'npos'");
1973 size_type newsz = d_cplength + chars_len;
1975 grow(newsz);
1977 utf32* pt = &ptr()[newsz-1];
1979 while(chars_len--)
1980 *pt-- = static_cast<utf32>(static_cast<unsigned char>(chars[chars_len]));
1982 setlen(newsz);
1984 return *this;
1988 //////////////////////////////////////////////////////////////////////////
1989 // Insertion Functions
1990 //////////////////////////////////////////////////////////////////////////
1992 \brief
1993 Inserts the given String object at the specified position.
1995 \param idx
1996 Index where the string is to be inserted.
1998 \param str
1999 String object that is to be inserted.
2001 \return
2002 This String after the insert.
2004 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2005 \exception std::length_error Thrown if resulting String would be too large.
2007 String& insert(size_type idx, const String& str)
2009 return insert(idx, str, 0, npos);
2013 \brief
2014 Inserts a sub-string of the given String object at the specified position.
2016 \param idx
2017 Index where the string is to be inserted.
2019 \param str
2020 String object containing data to be inserted.
2022 \param str_idx
2023 Index of the first code point from \a str to be inserted.
2025 \param str_num
2026 Maximum number of code points from \a str to be inserted.
2028 \return
2029 This String after the insert.
2031 \exception std::out_of_range Thrown if \a idx or \a str_idx are out of range.
2032 \exception std::length_error Thrown if resulting String would be too large.
2034 String& insert(size_type idx, const String& str, size_type str_idx, size_type str_num)
2036 if ((d_cplength < idx) || (str.d_cplength < str_idx))
2037 throw std::out_of_range("Index is out of range for CEGUI::String");
2039 if ((str_num == npos) || (str_num > str.d_cplength - str_idx))
2040 str_num = str.d_cplength - str_idx;
2042 size_type newsz = d_cplength + str_num;
2043 grow(newsz);
2044 memmove(&ptr()[idx + str_num], &ptr()[idx], (d_cplength - idx) * sizeof(utf32));
2045 memcpy(&ptr()[idx], &str.ptr()[str_idx], str_num * sizeof(utf32));
2046 setlen(newsz);
2048 return *this;
2052 \brief
2053 Inserts the given std::string object at the specified position.
2055 \param idx
2056 Index where the std::string is to be inserted.
2058 \param std_str
2059 std::string object that is to be inserted.
2061 \note
2062 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
2063 the provided data will occur.
2065 \return
2066 This String after the insert.
2068 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2069 \exception std::length_error Thrown if resulting String would be too large.
2071 String& insert(size_type idx, const std::string& std_str)
2073 return insert(idx, std_str, 0, npos);
2077 \brief
2078 Inserts a sub-string of the given std::string object at the specified position.
2080 \param idx
2081 Index where the string is to be inserted.
2083 \param std_str
2084 std::string object containing data to be inserted.
2086 \note
2087 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
2088 the provided data will occur.
2090 \param str_idx
2091 Index of the first character from \a std_str to be inserted.
2093 \param str_num
2094 Maximum number of characters from \a str to be inserted.
2096 \return
2097 This String after the insert.
2099 \exception std::out_of_range Thrown if \a idx or \a str_idx are out of range.
2100 \exception std::length_error Thrown if resulting String would be too large.
2102 String& insert(size_type idx, const std::string& std_str, size_type str_idx, size_type str_num)
2104 if (d_cplength < idx)
2105 throw std::out_of_range("Index is out of range for CEGUI::String");
2107 if (std_str.size() < str_idx)
2108 throw std::out_of_range("Index is out of range for std::string");
2110 if ((str_num == npos) || (str_num > (size_type)std_str.size() - str_idx))
2111 str_num = (size_type)std_str.size() - str_idx;
2113 size_type newsz = d_cplength + str_num;
2114 grow(newsz);
2116 memmove(&ptr()[idx + str_num], &ptr()[idx], (d_cplength - idx) * sizeof(utf32));
2118 utf32* pt = &ptr()[idx + str_num - 1];
2120 while(str_num--)
2121 *pt-- = static_cast<utf32>(static_cast<unsigned char>(std_str[str_idx + str_num]));
2123 setlen(newsz);
2125 return *this;
2129 \brief
2130 Inserts the given null-terminated utf8 encoded data at the specified position.
2132 \param idx
2133 Index where the data is to be inserted.
2135 \param utf8_str
2136 Buffer containing the null-terminated utf8 encoded data that is to be inserted.
2138 \note
2139 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
2140 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
2141 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
2142 results.
2144 \return
2145 This String after the insert.
2147 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2148 \exception std::length_error Thrown if resulting String would be too large.
2150 String& insert(size_type idx, const utf8* utf8_str)
2152 return insert(idx, utf8_str, utf_length(utf8_str));
2156 \brief
2157 Inserts the given utf8 encoded data at the specified position.
2159 \param idx
2160 Index where the data is to be inserted.
2162 \param utf8_str
2163 Buffer containing the utf8 encoded data that is to be inserted.
2165 \note
2166 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
2167 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
2168 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
2169 results.
2171 \param len
2172 Length of the data to be inserted in uf8 code units (not code points)
2174 \return
2175 This String after the insert.
2177 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2178 \exception std::length_error Thrown if resulting String would be too large, or if \a len is 'npos'
2180 String& insert(size_type idx, const utf8* utf8_str, size_type len)
2182 if (d_cplength < idx)
2183 throw std::out_of_range("Index is out of range for CEGUI::String");
2185 if (len == npos)
2186 throw std::length_error("Length of utf8 encoded string can not be 'npos'");
2188 size_type encsz = encoded_size(utf8_str, len);
2189 size_type newsz = d_cplength + encsz;
2191 grow(newsz);
2192 memmove(&ptr()[idx + encsz], &ptr()[idx], (d_cplength - idx) * sizeof(utf32));
2193 encode(utf8_str, &ptr()[idx], encsz, len);
2194 setlen(newsz);
2196 return *this;
2200 \brief
2201 Inserts a code point multiple times into the String
2203 \param idx
2204 Index where the code point(s) are to be inserted
2206 \param num
2207 The number of times to insert the code point
2209 \param code_point
2210 The utf32 code point that is to be inserted
2212 \return
2213 This String after the insertion.
2215 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2216 \exception std::length_error Thrown if resulting String would be too large, or if \a num is 'npos'
2218 String& insert(size_type idx, size_type num, utf32 code_point)
2220 if (d_cplength < idx)
2221 throw std::out_of_range("Index is out of range for CEGUI::String");
2223 if (num == npos)
2224 throw std::length_error("Code point count can not be 'npos'");
2226 size_type newsz = d_cplength + num;
2227 grow(newsz);
2229 memmove(&ptr()[idx + num], &ptr()[idx], (d_cplength - idx) * sizeof(utf32));
2231 utf32* pt = &ptr()[idx + num - 1];
2233 while(num--)
2234 *pt-- = code_point;
2236 setlen(newsz);
2238 return *this;
2242 \brief
2243 Inserts a code point multiple times into the String
2245 \param pos
2246 Iterator describing the position where the code point(s) are to be inserted
2248 \param num
2249 The number of times to insert the code point
2251 \param code_point
2252 The utf32 code point that is to be inserted
2254 \return
2255 This String after the insertion.
2257 \exception std::length_error Thrown if resulting String would be too large, or if \a num is 'npos'
2259 void insert(iterator pos, size_type num, utf32 code_point)
2261 insert(safe_iter_dif(pos, begin()), num, code_point);
2265 \brief
2266 Inserts a single code point into the String
2268 \param pos
2269 Iterator describing the position where the code point is to be inserted
2271 \param code_point
2272 The utf32 code point that is to be inserted
2274 \return
2275 This String after the insertion.
2277 \exception std::length_error Thrown if resulting String would be too large.
2279 iterator insert(iterator pos, utf32 code_point)
2281 insert(pos, 1, code_point);
2282 return pos;
2286 \brief
2287 Inserts code points specified by the range [beg, end).
2289 \param pos
2290 Iterator describing the position where the data is to be inserted
2292 \param beg
2293 Iterator describing the begining of the range to be inserted
2295 \param end
2296 Iterator describing the (exclusive) end of the range to be inserted.
2298 \return
2299 Nothing.
2301 \exception std::length_error Thrown if resulting String would be too large.
2303 void insert(iterator iter_pos, const_iterator iter_beg, const_iterator iter_end)
2305 replace(iter_pos, iter_pos, iter_beg, iter_end);
2310 \brief
2311 Inserts the given c-string at the specified position.
2313 \param idx
2314 Index where the c-string is to be inserted.
2316 \param c_str
2317 c-string that is to be inserted.
2319 \return
2320 This String after the insert.
2322 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2323 \exception std::length_error Thrown if resulting String would be too large.
2325 String& insert(size_type idx, const char* cstr)
2327 return insert(idx, cstr, strlen(cstr));
2332 \brief
2333 Inserts chars from the given char array at the specified position.
2335 \param idx
2336 Index where the data is to be inserted.
2338 \param chars
2339 char array containing the chars that are to be inserted.
2341 \param chars_len
2342 Length of the char array to be inserted.
2344 \return
2345 This String after the insert.
2347 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2348 \exception std::length_error Thrown if resulting String would be too large, or if \a chars_len is 'npos'
2350 String& insert(size_type idx, const char* chars, size_type chars_len)
2352 if (d_cplength < idx)
2353 throw std::out_of_range("Index is out of range for CEGUI::String");
2355 if (chars_len == npos)
2356 throw std::length_error("Length of char array can not be 'npos'");
2358 size_type newsz = d_cplength + chars_len;
2360 grow(newsz);
2361 memmove(&ptr()[idx + chars_len], &ptr()[idx], (d_cplength - idx) * sizeof(utf32));
2363 utf32* pt = &ptr()[idx + chars_len - 1];
2365 while(chars_len--)
2366 *pt-- = static_cast<utf32>(static_cast<unsigned char>(chars[chars_len]));
2368 setlen(newsz);
2370 return *this;
2374 //////////////////////////////////////////////////////////////////////////
2375 // Erasing characters
2376 //////////////////////////////////////////////////////////////////////////
2378 \brief
2379 Removes all data from the String
2381 \return
2382 Nothing
2384 void clear(void)
2386 setlen(0);
2387 trim();
2391 \brief
2392 Removes all data from the String
2394 \return
2395 The empty String (*this)
2397 String& erase(void)
2399 clear();
2400 return *this;
2404 \brief
2405 Erase a single code point from the string
2407 \param idx
2408 The index of the code point to be removed.
2410 \return
2411 This String after the erase operation
2413 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2415 String& erase(size_type idx)
2417 return erase(idx, 1);
2421 \brief
2422 Erase a range of code points
2424 \param idx
2425 Index of the first code point to be removed.
2427 \param len
2428 Maximum number of code points to be removed.
2430 \return
2431 This String after the erase operation.
2433 \exception std::out_of_range Thrown if \a idx is invalid for this String.
2435 String& erase(size_type idx, size_type len = npos)
2437 if (d_cplength < idx)
2438 throw std::out_of_range("Index is out of range foe CEGUI::String");
2440 if (len == npos)
2441 len = d_cplength - idx;
2443 size_type newsz = d_cplength - len;
2445 memmove(&ptr()[idx], &ptr()[idx + len], (d_cplength - idx - len) * sizeof(utf32));
2446 setlen(newsz);
2447 return *this;
2451 \brief
2452 Erase the code point described by the given iterator
2454 \param pos
2455 Iterator describing the code point to be erased
2457 \return
2458 This String after the erase operation.
2460 String& erase(iterator pos)
2462 return erase(safe_iter_dif(pos, begin()), 1);
2466 \brief
2467 Erase a range of code points described by the iterators [beg, end).
2469 \param beg
2470 Iterator describing the postion of the beginning of the range to erase
2472 \param end
2473 Iterator describing the postion of the (exclusive) end of the range to erase
2475 \return
2476 This String after the erase operation.
2478 String& erase(iterator iter_beg, iterator iter_end)
2480 return erase(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg));
2483 //////////////////////////////////////////////////////////////////////////
2484 // Resizing
2485 //////////////////////////////////////////////////////////////////////////
2487 \brief
2488 Resizes the String either by inserting default utf32 code points to make it larger, or by truncating to make it smaller
2490 \param num
2491 The length, in code points, that the String is to be made.
2493 \return
2494 Nothing.
2496 \exception std::length_error Thrown if the String would be too large.
2498 void resize(size_type num)
2500 resize(num, utf32());
2504 \brief
2505 Resizes the String either by inserting the given utf32 code point to make it larger, or by truncating to make it smaller
2507 \param num
2508 The length, in code points, that the String is to be made.
2510 \param code_point
2511 The utf32 code point that should be used when majing the String larger
2513 \return
2514 Nothing.
2516 \exception std::length_error Thrown if the String would be too large.
2518 void resize(size_type num, utf32 code_point)
2520 if (num < d_cplength)
2522 setlen(num);
2524 else
2526 append(num - d_cplength, code_point);
2531 //////////////////////////////////////////////////////////////////////////
2532 // Replacing Characters
2533 //////////////////////////////////////////////////////////////////////////
2535 \brief
2536 Replace code points in the String with the specified String object
2538 \param idx
2539 Index of the first code point to be replaced
2541 \param len
2542 Maximum number of code points to be replaced (if this is 0, operation is an insert at position \a idx)
2544 \param str
2545 The String object that is to replace the specified code points
2547 \return
2548 This String after the replace operation
2550 \exception std::out_of_range Thrown if \a idx is invalid for this String
2551 \exception std::length_error Thrown if the resulting String would be too large.
2553 String& replace(size_type idx, size_type len, const String& str)
2555 return replace(idx, len, str, 0, npos);
2559 \brief
2560 Replace the code points in the range [beg, end) with the specified String object
2562 \note
2563 If \a beg == \a end, the operation is a insert at iterator position \a beg
2565 \param beg
2566 Iterator describing the start of the range to be replaced
2568 \param end
2569 Iterator describing the (exclusive) end of the range to be replaced.
2571 \param str
2572 The String object that is to replace the specified range of code points
2574 \return
2575 This String after the replace operation
2577 \exception std::length_error Thrown if the resulting String would be too large.
2579 String& replace(iterator iter_beg, iterator iter_end, const String& str)
2581 return replace(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg), str, 0, npos);
2585 \brief
2586 Replace code points in the String with a specified sub-string of a given String object.
2588 \param idx
2589 Index of the first code point to be replaced
2591 \param len
2592 Maximum number of code points to be replaced. If this is 0, the operation is an insert at position \a idx.
2594 \param str
2595 String object containing the data that will replace the specified range of code points
2597 \param str_idx
2598 Index of the first code point of \a str that is to replace the specified code point range
2600 \param str_num
2601 Maximum number of code points of \a str that are to replace the specified code point range
2603 \return
2604 This String after the replace operation
2606 \exception std::out_of_range Thrown if either \a idx, or \a str_idx are invalid
2607 \exception std::length_error Thrown if the resulting String would have been too large.
2609 String& replace(size_type idx, size_type len, const String& str, size_type str_idx, size_type str_num)
2611 if ((d_cplength < idx) || (str.d_cplength < str_idx))
2612 throw std::out_of_range("Index is out of range for CEGUI::String");
2614 if (((str_idx + str_num) > str.d_cplength) || (str_num == npos))
2615 str_num = str.d_cplength - str_idx;
2617 if (((len + idx) > d_cplength) || (len == npos))
2618 len = d_cplength - idx;
2620 size_type newsz = d_cplength + str_num - len;
2622 grow(newsz);
2624 if ((idx + len) < d_cplength)
2625 memmove(&ptr()[idx + str_num], &ptr()[len + idx], (d_cplength - idx - len) * sizeof(utf32));
2627 memcpy(&ptr()[idx], &str.ptr()[str_idx], str_num * sizeof(utf32));
2628 setlen(newsz);
2630 return *this;
2635 \brief
2636 Replace code points in the String with the specified std::string object
2638 \param idx
2639 Index of the first code point to be replaced
2641 \param len
2642 Maximum number of code points to be replaced (if this is 0, operation is an insert at position \a idx)
2644 \param std_str
2645 The std::string object that is to replace the specified code points
2647 \note
2648 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
2649 the encountered data is performed.
2651 \return
2652 This String after the replace operation
2654 \exception std::out_of_range Thrown if \a idx is invalid for this String
2655 \exception std::length_error Thrown if the resulting String would be too large.
2657 String& replace(size_type idx, size_type len, const std::string& std_str)
2659 return replace(idx, len, std_str, 0, npos);
2663 \brief
2664 Replace the code points in the range [beg, end) with the specified std::string object
2666 \note
2667 If \a beg == \a end, the operation is a insert at iterator position \a beg
2669 \param beg
2670 Iterator describing the start of the range to be replaced
2672 \param end
2673 Iterator describing the (exclusive) end of the range to be replaced.
2675 \param std_str
2676 The std::string object that is to replace the specified range of code points
2678 \note
2679 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
2680 the encountered data is performed.
2682 \return
2683 This String after the replace operation
2685 \exception std::length_error Thrown if the resulting String would be too large.
2687 String& replace(iterator iter_beg, iterator iter_end, const std::string& std_str)
2689 return replace(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg), std_str, 0, npos);
2693 \brief
2694 Replace code points in the String with a specified sub-string of a given std::string object.
2696 \param idx
2697 Index of the first code point to be replaced
2699 \param len
2700 Maximum number of code points to be replaced. If this is 0, the operation is an insert at position \a idx.
2702 \param std_str
2703 std::string object containing the data that will replace the specified range of code points
2705 \note
2706 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
2707 the encountered data is performed.
2709 \param str_idx
2710 Index of the first code point of \a std_str that is to replace the specified code point range
2712 \param str_num
2713 Maximum number of code points of \a std_str that are to replace the specified code point range
2715 \return
2716 This String after the replace operation
2718 \exception std::out_of_range Thrown if either \a idx, or \a str_idx are invalid
2719 \exception std::length_error Thrown if the resulting String would have been too large.
2721 String& replace(size_type idx, size_type len, const std::string& std_str, size_type str_idx, size_type str_num)
2723 if (d_cplength < idx)
2724 throw std::out_of_range("Index is out of range for CEGUI::String");
2726 if (std_str.size() < str_idx)
2727 throw std::out_of_range("Index is out of range for std::string");
2729 if (((str_idx + str_num) > std_str.size()) || (str_num == npos))
2730 str_num = (size_type)std_str.size() - str_idx;
2732 if (((len + idx) > d_cplength) || (len == npos))
2733 len = d_cplength - idx;
2735 size_type newsz = d_cplength + str_num - len;
2737 grow(newsz);
2739 if ((idx + len) < d_cplength)
2740 memmove(&ptr()[idx + str_num], &ptr()[len + idx], (d_cplength - idx - len) * sizeof(utf32));
2742 utf32* pt = &ptr()[idx + str_num - 1];
2744 while (str_num--)
2745 *pt-- = static_cast<utf32>(static_cast<unsigned char>(std_str[str_idx + str_num]));
2747 setlen(newsz);
2749 return *this;
2754 \brief
2755 Replace code points in the String with the specified null-terminated utf8 encoded data.
2757 \param idx
2758 Index of the first code point to be replaced
2760 \param len
2761 Maximum number of code points to be replaced (if this is 0, operation is an insert at position \a idx)
2763 \param utf8_str
2764 Buffer containing the null-terminated utf8 encoded data that is to replace the specified code points
2766 \note
2767 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
2768 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
2769 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
2770 results.
2772 \return
2773 This String after the replace operation
2775 \exception std::out_of_range Thrown if \a idx is invalid for this String
2776 \exception std::length_error Thrown if the resulting String would be too large.
2778 String& replace(size_type idx, size_type len, const utf8* utf8_str)
2780 return replace(idx, len, utf8_str, utf_length(utf8_str));
2784 \brief
2785 Replace the code points in the range [beg, end) with the specified null-terminated utf8 encoded data.
2787 \note
2788 If \a beg == \a end, the operation is a insert at iterator position \a beg
2790 \param beg
2791 Iterator describing the start of the range to be replaced
2793 \param end
2794 Iterator describing the (exclusive) end of the range to be replaced.
2796 \param utf8_str
2797 Buffer containing the null-terminated utf8 encoded data that is to replace the specified range of code points
2799 \note
2800 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
2801 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
2802 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
2803 results.
2805 \return
2806 This String after the replace operation
2808 \exception std::length_error Thrown if the resulting String would be too large.
2810 String& replace(iterator iter_beg, iterator iter_end, const utf8* utf8_str)
2812 return replace(iter_beg, iter_end, utf8_str, utf_length(utf8_str));
2816 \brief
2817 Replace code points in the String with the specified utf8 encoded data.
2819 \param idx
2820 Index of the first code point to be replaced
2822 \param len
2823 Maximum number of code points to be replaced (if this is 0, operation is an insert at position \a idx)
2825 \param utf8_str
2826 Buffer containing the null-terminated utf8 encoded data that is to replace the specified code points
2828 \note
2829 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
2830 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
2831 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
2832 results.
2834 \param str_len
2835 Length of the utf8 encoded data in utf8 code units (not code points).
2837 \return
2838 This String after the replace operation
2840 \exception std::out_of_range Thrown if \a idx is invalid for this String
2841 \exception std::length_error Thrown if the resulting String would be too large, or if \a str_len was 'npos'.
2843 String& replace(size_type idx, size_type len, const utf8* utf8_str, size_type str_len)
2845 if (d_cplength < idx)
2846 throw std::out_of_range("Index is out of range for CEGUI::String");
2848 if (str_len == npos)
2849 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
2851 if (((len + idx) > d_cplength) || (len == npos))
2852 len = d_cplength - idx;
2854 size_type encsz = encoded_size(utf8_str, str_len);
2855 size_type newsz = d_cplength + encsz - len;
2857 grow(newsz);
2859 if ((idx + len) < d_cplength)
2860 memmove(&ptr()[idx + encsz], &ptr()[len + idx], (d_cplength - idx - len) * sizeof(utf32));
2862 encode(utf8_str, &ptr()[idx], encsz, str_len);
2864 setlen(newsz);
2865 return *this;
2869 \brief
2870 Replace the code points in the range [beg, end) with the specified null-terminated utf8 encoded data.
2872 \note
2873 If \a beg == \a end, the operation is a insert at iterator position \a beg
2875 \param beg
2876 Iterator describing the start of the range to be replaced
2878 \param end
2879 Iterator describing the (exclusive) end of the range to be replaced.
2881 \param utf8_str
2882 Buffer containing the null-terminated utf8 encoded data that is to replace the specified range of code points
2884 \note
2885 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
2886 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
2887 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
2888 results.
2890 \param str_len
2891 Length of the utf8 encoded data in utf8 code units (not code points).
2893 \return
2894 This String after the replace operation
2896 \exception std::length_error Thrown if the resulting String would be too large, or if \a str_len was 'npos'.
2898 String& replace(iterator iter_beg, iterator iter_end, const utf8* utf8_str, size_type str_len)
2900 return replace(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg), utf8_str, str_len);
2904 \brief
2905 Replaces a specified range of code points with occurrences of a given code point
2907 \param idx
2908 Index of the first code point to be replaced
2910 \param len
2911 Maximum number of code points to replace. If this is 0 the operation is an insert
2913 \param num
2914 Number of occurrences of \a code_point that are to replace the specified range of code points
2916 \param code_point
2917 Code point that is to be used when replacing the specified range of code points
2919 \return
2920 This String after the replace operation.
2922 \exception std::out_of_range Thrown if \a idx is invalid for this String
2923 \exception std::length_error Thrown if resulting String would have been too long, or if \a num was 'npos'.
2925 String& replace(size_type idx, size_type len, size_type num, utf32 code_point)
2927 if (d_cplength < idx)
2928 throw std::out_of_range("Index is out of range for CEGUI::String");
2930 if (num == npos)
2931 throw std::length_error("Code point count can not be 'npos'");
2933 if (((len + idx) > d_cplength) || (len == npos))
2934 len = d_cplength - idx;
2936 size_type newsz = d_cplength + num - len;
2938 grow(newsz);
2940 if ((idx + len) < d_cplength)
2941 memmove(&ptr()[idx + num], &ptr()[len + idx], (d_cplength - idx - len) * sizeof(utf32));
2943 utf32* pt = &ptr()[idx + num - 1];
2945 while (num--)
2946 *pt-- = code_point;
2948 setlen(newsz);
2950 return *this;
2954 \brief
2955 Replace the code points in the range [beg, end) with occurrences of a given code point
2957 \note
2958 If \a beg == \a end, the operation is an insert at iterator position \a beg
2960 \param beg
2961 Iterator describing the start of the range to be replaced
2963 \param end
2964 Iterator describing the (exclusive) end of the range to be replaced.
2966 \param num
2967 Number of occurrences of \a code_point that are to replace the specified range of code points
2969 \param code_point
2970 Code point that is to be used when replacing the specified range of code points
2972 \return
2973 This String after the replace operation
2975 \exception std::length_error Thrown if resulting String would have been too long, or if \a num was 'npos'.
2977 String& replace(iterator iter_beg, iterator iter_end, size_type num, utf32 code_point)
2979 return replace(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg), num, code_point);
2984 \brief
2985 Replace the code points in the range [beg, end) with code points from the range [newBeg, newEnd).
2987 \note
2988 If \a beg == \a end, the operation is an insert at iterator position \a beg
2990 \param beg
2991 Iterator describing the start of the range to be replaced
2993 \param end
2994 Iterator describing the (exclusive) end of the range to be replaced.
2996 \param newBeg
2997 Iterator describing the beginning of the range to insert.
2999 \param newEnd
3000 Iterator describing the (exclusive) end of the range to insert.
3002 \return
3003 This String after the insert operation.
3005 \exception std::length_error Thrown if the resulting string would be too long.
3007 String& replace(iterator iter_beg, iterator iter_end, const_iterator iter_newBeg, const_iterator iter_newEnd)
3009 if (iter_beg == iter_end)
3011 erase(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg));
3013 else
3015 size_type str_len = safe_iter_dif(iter_newEnd, iter_newBeg);
3016 size_type idx = safe_iter_dif(iter_beg, begin());
3017 size_type len = safe_iter_dif(iter_end, iter_beg);
3019 if ((len + idx) > d_cplength)
3020 len = d_cplength - idx;
3022 size_type newsz = d_cplength + str_len - len;
3024 grow(newsz);
3026 if ((idx + len) < d_cplength)
3027 memmove(&ptr()[idx + str_len], &ptr()[len + idx], (d_cplength - idx - len) * sizeof(utf32));
3029 memcpy(&ptr()[idx], iter_newBeg.d_ptr, str_len * sizeof(utf32));
3030 setlen(newsz);
3033 return *this;
3038 \brief
3039 Replace code points in the String with the specified c-string.
3041 \param idx
3042 Index of the first code point to be replaced
3044 \param len
3045 Maximum number of code points to be replaced (if this is 0, operation is an insert at position \a idx)
3047 \param c_str
3048 c-string that is to replace the specified code points
3050 \return
3051 This String after the replace operation
3053 \exception std::out_of_range Thrown if \a idx is invalid for this String
3054 \exception std::length_error Thrown if the resulting String would be too large.
3056 String& replace(size_type idx, size_type len, const char* cstr)
3058 return replace(idx, len, cstr, strlen(cstr));
3063 \brief
3064 Replace the code points in the range [beg, end) with the specified c-string.
3066 \note
3067 If \a beg == \a end, the operation is a insert at iterator position \a beg
3069 \param beg
3070 Iterator describing the start of the range to be replaced
3072 \param end
3073 Iterator describing the (exclusive) end of the range to be replaced.
3075 \param c_str
3076 c-string that is to replace the specified range of code points
3078 \return
3079 This String after the replace operation
3081 \exception std::length_error Thrown if the resulting String would be too large.
3083 String& replace(iterator iter_beg, iterator iter_end, const char* cstr)
3085 return replace(iter_beg, iter_end, cstr, strlen(cstr));
3090 \brief
3091 Replace code points in the String with chars from the given char array.
3093 \param idx
3094 Index of the first code point to be replaced
3096 \param len
3097 Maximum number of code points to be replaced (if this is 0, operation is an insert at position \a idx)
3099 \param chars
3100 char array containing the cars that are to replace the specified code points
3102 \param chars_len
3103 Number of chars in the char array.
3105 \return
3106 This String after the replace operation
3108 \exception std::out_of_range Thrown if \a idx is invalid for this String
3109 \exception std::length_error Thrown if the resulting String would be too large, or if \a chars_len was 'npos'.
3111 String& replace(size_type idx, size_type len, const char* chars, size_type chars_len)
3113 if (d_cplength < idx)
3114 throw std::out_of_range("Index is out of range for CEGUI::String");
3116 if (chars_len == npos)
3117 throw std::length_error("Length for the char array can not be 'npos'");
3119 if (((len + idx) > d_cplength) || (len == npos))
3120 len = d_cplength - idx;
3122 size_type newsz = d_cplength + chars_len - len;
3124 grow(newsz);
3126 if ((idx + len) < d_cplength)
3127 memmove(&ptr()[idx + chars_len], &ptr()[len + idx], (d_cplength - idx - len) * sizeof(utf32));
3129 utf32* pt = &ptr()[idx + chars_len - 1];
3131 while (chars_len--)
3132 *pt-- = static_cast<utf32>(static_cast<unsigned char>(chars[chars_len]));
3134 setlen(newsz);
3135 return *this;
3140 \brief
3141 Replace the code points in the range [beg, end) with chars from the given char array.
3143 \note
3144 If \a beg == \a end, the operation is a insert at iterator position \a beg
3146 \param beg
3147 Iterator describing the start of the range to be replaced
3149 \param end
3150 Iterator describing the (exclusive) end of the range to be replaced.
3152 \param chars
3153 char array containing the chars that are to replace the specified range of code points
3155 \param chars_len
3156 Number of chars in the char array.
3158 \return
3159 This String after the replace operation
3161 \exception std::length_error Thrown if the resulting String would be too large, or if \a chars_len was 'npos'.
3163 String& replace(iterator iter_beg, iterator iter_end, const char* chars, size_type chars_len)
3165 return replace(safe_iter_dif(iter_beg, begin()), safe_iter_dif(iter_end, iter_beg), chars, chars_len);
3169 //////////////////////////////////////////////////////////////////////////
3170 // Find a code point
3171 //////////////////////////////////////////////////////////////////////////
3173 \brief
3174 Search forwards for a given code point
3176 \param code_point
3177 The utf32 code point to search for
3179 \param idx
3180 Index of the code point where the search is to start.
3182 \return
3183 - Index of the first occurrence of \a code_point travelling forwards from \a idx.
3184 - npos if the code point could not be found
3186 size_type find(utf32 code_point, size_type idx = 0) const
3188 if (idx < d_cplength)
3190 const utf32* pt = &ptr()[idx];
3192 while (idx < d_cplength)
3194 if (*pt++ == code_point)
3195 return idx;
3197 ++idx;
3202 return npos;
3206 \brief
3207 Search backwards for a given code point
3209 \param code_point
3210 The utf32 code point to search for
3212 \param idx
3213 Index of the code point where the search is to start.
3215 \return
3216 - Index of the first occurrence of \a code_point travelling backwards from \a idx.
3217 - npos if the code point could not be found
3219 size_type rfind(utf32 code_point, size_type idx = npos) const
3221 if (idx >= d_cplength)
3222 idx = d_cplength - 1;
3224 if (d_cplength > 0)
3226 const utf32* pt = &ptr()[idx];
3230 if (*pt-- == code_point)
3231 return idx;
3233 } while (idx-- != 0);
3237 return npos;
3240 //////////////////////////////////////////////////////////////////////////
3241 // Find a substring
3242 //////////////////////////////////////////////////////////////////////////
3244 \brief
3245 Search forwards for a sub-string
3247 \param str
3248 String object describing the sub-string to search for
3250 \param idx
3251 Index of the code point where the search is to start
3253 \return
3254 - Index of the first occurrence of sub-string \a str travelling forwards from \a idx.
3255 - npos if the sub-string could not be found
3257 size_type find(const String& str, size_type idx = 0) const
3259 if ((str.d_cplength == 0) && (idx < d_cplength))
3260 return idx;
3262 if (idx < d_cplength)
3264 // loop while search string could fit in to search area
3265 while (d_cplength - idx >= str.d_cplength)
3267 if (0 == compare(idx, str.d_cplength, str))
3268 return idx;
3270 ++idx;
3275 return npos;
3279 \brief
3280 Search backwards for a sub-string
3282 \param str
3283 String object describing the sub-string to search for
3285 \param idx
3286 Index of the code point where the search is to start
3288 \return
3289 - Index of the first occurrence of sub-string \a str travelling backwards from \a idx.
3290 - npos if the sub-string could not be found
3292 size_type rfind(const String& str, size_type idx = npos) const
3294 if (str.d_cplength == 0)
3295 return (idx < d_cplength) ? idx : d_cplength;
3297 if (str.d_cplength <= d_cplength)
3299 if (idx > (d_cplength - str.d_cplength))
3300 idx = d_cplength - str.d_cplength;
3304 if (0 == compare(idx, str.d_cplength, str))
3305 return idx;
3307 } while (idx-- != 0);
3311 return npos;
3315 \brief
3316 Search forwards for a sub-string
3318 \param std_str
3319 std::string object describing the sub-string to search for
3321 \note
3322 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
3323 the encountered data is performed.
3325 \param idx
3326 Index of the code point where the search is to start
3328 \return
3329 - Index of the first occurrence of sub-string \a std_str travelling forwards from \a idx.
3330 - npos if the sub-string could not be found
3332 size_type find(const std::string& std_str, size_type idx = 0) const
3334 std::string::size_type sze = std_str.size();
3336 if ((sze == 0) && (idx < d_cplength))
3337 return idx;
3339 if (idx < d_cplength)
3341 // loop while search string could fit in to search area
3342 while (d_cplength - idx >= sze)
3344 if (0 == compare(idx, (size_type)sze, std_str))
3345 return idx;
3347 ++idx;
3352 return npos;
3356 \brief
3357 Search backwards for a sub-string
3359 \param std_str
3360 std::string object describing the sub-string to search for
3362 \note
3363 Characters from \a std_str are considered to represent Unicode code points in the range 0x00..0xFF. No translation of
3364 the encountered data is performed.
3366 \param idx
3367 Index of the code point where the search is to start
3369 \return
3370 - Index of the first occurrence of sub-string \a std_str travelling backwards from \a idx.
3371 - npos if the sub-string could not be found
3373 size_type rfind(const std::string& std_str, size_type idx = npos) const
3375 std::string::size_type sze = std_str.size();
3377 if (sze == 0)
3378 return (idx < d_cplength) ? idx : d_cplength;
3380 if (sze <= d_cplength)
3382 if (idx > (d_cplength - sze))
3383 idx = d_cplength - sze;
3387 if (0 == compare(idx, (size_type)sze, std_str))
3388 return idx;
3390 } while (idx-- != 0);
3394 return npos;
3398 \brief
3399 Search forwards for a sub-string
3401 \param utf8_str
3402 Buffer containing null-terminated utf8 encoded data describing the sub-string to search for
3404 \note
3405 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3406 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3407 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3408 results.
3410 \param idx
3411 Index of the code point where the search is to start
3413 \return
3414 - Index of the first occurrence of sub-string \a utf8_str travelling forwards from \a idx.
3415 - npos if the sub-string could not be found
3417 \exception std::out_of_range Thrown if \a idx is invalid for this String.
3419 size_type find(const utf8* utf8_str, size_type idx = 0) const
3421 return find(utf8_str, idx, utf_length(utf8_str));
3425 \brief
3426 Search backwards for a sub-string
3428 \param utf8_str
3429 Buffer containing null-terminated utf8 encoded data describing the sub-string to search for
3431 \note
3432 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3433 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3434 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3435 results.
3437 \param idx
3438 Index of the code point where the search is to start
3440 \return
3441 - Index of the first occurrence of sub-string \a utf8_str travelling backwards from \a idx.
3442 - npos if the sub-string could not be found
3444 \exception std::out_of_range Thrown if \a idx is invalid for this String.
3446 size_type rfind(const utf8* utf8_str, size_type idx = npos) const
3448 return rfind(utf8_str, idx, utf_length(utf8_str));
3452 \brief
3453 Search forwards for a sub-string
3455 \param utf8_str
3456 Buffer containing utf8 encoded data describing the sub-string to search for
3458 \note
3459 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3460 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3461 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3462 results.
3464 \param idx
3465 Index of the code point where the search is to start
3467 \param str_len
3468 Length of the utf8 encoded sub-string in utf8 code units (not code points)
3470 \return
3471 - Index of the first occurrence of sub-string \a utf8_str travelling forwards from \a idx.
3472 - npos if the sub-string could not be found
3474 \exception std::length_error Thrown if \a str_len is 'npos'
3476 size_type find(const utf8* utf8_str, size_type idx, size_type str_len) const
3478 if (str_len == npos)
3479 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
3481 size_type sze = encoded_size(utf8_str, str_len);
3483 if ((sze == 0) && (idx < d_cplength))
3484 return idx;
3486 if (idx < d_cplength)
3488 // loop while search string could fit in to search area
3489 while (d_cplength - idx >= sze)
3491 if (0 == compare(idx, sze, utf8_str, sze))
3492 return idx;
3494 ++idx;
3499 return npos;
3503 \brief
3504 Search backwards for a sub-string
3506 \param utf8_str
3507 Buffer containing utf8 encoded data describing the sub-string to search for
3509 \note
3510 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3511 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3512 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3513 results.
3515 \param idx
3516 Index of the code point where the search is to start
3518 \param str_len
3519 Length of the utf8 encoded sub-string in utf8 code units (not code points)
3521 \return
3522 - Index of the first occurrence of sub-string \a utf8_str travelling backwards from \a idx.
3523 - npos if the sub-string could not be found
3525 \exception std::length_error Thrown if \a str_len is 'npos'
3527 size_type rfind(const utf8* utf8_str, size_type idx, size_type str_len) const
3529 if (str_len == npos)
3530 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
3532 size_type sze = encoded_size(utf8_str, str_len);
3534 if (sze == 0)
3535 return (idx < d_cplength) ? idx : d_cplength;
3537 if (sze <= d_cplength)
3539 if (idx > (d_cplength - sze))
3540 idx = d_cplength - sze;
3544 if (0 == compare(idx, sze, utf8_str, sze))
3545 return idx;
3547 } while (idx-- != 0);
3551 return npos;
3556 \brief
3557 Search forwards for a sub-string
3559 \param c_str
3560 c-string describing the sub-string to search for
3562 \param idx
3563 Index of the code point where the search is to start
3565 \return
3566 - Index of the first occurrence of sub-string \a c_str travelling forwards from \a idx.
3567 - npos if the sub-string could not be found
3569 \exception std::out_of_range Thrown if \a idx is invalid for this String.
3571 size_type find(const char* cstr, size_type idx = 0) const
3573 return find(cstr, idx, strlen(cstr));
3578 \brief
3579 Search backwards for a sub-string
3581 \param c_str
3582 c-string describing the sub-string to search for
3584 \param idx
3585 Index of the code point where the search is to start
3587 \return
3588 - Index of the first occurrence of sub-string \a c_str travelling backwards from \a idx.
3589 - npos if the sub-string could not be found
3591 \exception std::out_of_range Thrown if \a idx is invalid for this String.
3593 size_type rfind(const char* cstr, size_type idx = npos) const
3595 return rfind(cstr, idx, strlen(cstr));
3600 \brief
3601 Search forwards for a sub-string
3603 \param chars
3604 char array describing the sub-string to search for
3606 \param idx
3607 Index of the code point where the search is to start
3609 \param chars_len
3610 Number of chars in the char array.
3612 \return
3613 - Index of the first occurrence of sub-string \a chars travelling forwards from \a idx.
3614 - npos if the sub-string could not be found
3616 \exception std::length_error Thrown if \a chars_len is 'npos'
3618 size_type find(const char* chars, size_type idx, size_type chars_len) const
3620 if (chars_len == npos)
3621 throw std::length_error("Length for char array can not be 'npos'");
3623 if ((chars_len == 0) && (idx < d_cplength))
3624 return idx;
3626 if (idx < d_cplength)
3628 // loop while search string could fit in to search area
3629 while (d_cplength - idx >= chars_len)
3631 if (0 == compare(idx, chars_len, chars, chars_len))
3632 return idx;
3634 ++idx;
3639 return npos;
3644 \brief
3645 Search backwards for a sub-string
3647 \param chars
3648 char array describing the sub-string to search for
3650 \param idx
3651 Index of the code point where the search is to start
3653 \param chars_len
3654 Number of chars in the char array.
3656 \return
3657 - Index of the first occurrence of sub-string \a chars travelling backwards from \a idx.
3658 - npos if the sub-string could not be found
3660 \exception std::length_error Thrown if \a chars_len is 'npos'
3662 size_type rfind(const char* chars, size_type idx, size_type chars_len) const
3664 if (chars_len == npos)
3665 throw std::length_error("Length for char array can not be 'npos'");
3667 if (chars_len == 0)
3668 return (idx < d_cplength) ? idx : d_cplength;
3670 if (chars_len <= d_cplength)
3672 if (idx > (d_cplength - chars_len))
3673 idx = d_cplength - chars_len;
3677 if (0 == compare(idx, chars_len, chars, chars_len))
3678 return idx;
3680 } while (idx-- != 0);
3684 return npos;
3688 //////////////////////////////////////////////////////////////////////////
3689 // Find first of different code-points
3690 //////////////////////////////////////////////////////////////////////////
3692 \brief
3693 Find the first occurrence of one of a set of code points.
3695 \param str
3696 String object describing the set of code points.
3698 \param idx
3699 Index of the start point for the search
3701 \return
3702 - Index of the first occurrence of any one of the code points in \a str starting from from \a idx.
3703 - npos if none of the code points in \a str were found.
3705 size_type find_first_of(const String& str, size_type idx = 0) const
3707 if (idx < d_cplength)
3709 const utf32* pt = &ptr()[idx];
3713 if (npos != str.find(*pt++))
3714 return idx;
3716 } while (++idx != d_cplength);
3720 return npos;
3724 \brief
3725 Find the first code point that is not one of a set of code points.
3727 \param str
3728 String object describing the set of code points.
3730 \param idx
3731 Index of the start point for the search
3733 \return
3734 - Index of the first code point that does not match any one of the code points in \a str starting from from \a idx.
3735 - npos if all code points matched one of the code points in \a str.
3737 size_type find_first_not_of(const String& str, size_type idx = 0) const
3739 if (idx < d_cplength)
3741 const utf32* pt = &ptr()[idx];
3745 if (npos == str.find(*pt++))
3746 return idx;
3748 } while (++idx != d_cplength);
3752 return npos;
3757 \brief
3758 Find the first occurrence of one of a set of code points.
3760 \param std_str
3761 std::string object describing the set of code points.
3763 \note
3764 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
3765 the provided data will occur.
3767 \param idx
3768 Index of the start point for the search
3770 \return
3771 - Index of the first occurrence of any one of the code points in \a std_str starting from from \a idx.
3772 - npos if none of the code points in \a std_str were found.
3774 size_type find_first_of(const std::string& std_str, size_type idx = 0) const
3776 if (idx < d_cplength)
3778 const utf32* pt = &ptr()[idx];
3782 if (npos != find_codepoint(std_str, *pt++))
3783 return idx;
3785 } while (++idx != d_cplength);
3789 return npos;
3793 \brief
3794 Find the first code point that is not one of a set of code points.
3796 \param std_str
3797 std::string object describing the set of code points.
3799 \note
3800 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
3801 the provided data will occur.
3803 \param idx
3804 Index of the start point for the search
3806 \return
3807 - Index of the first code point that does not match any one of the code points in \a std_str starting from from \a idx.
3808 - npos if all code points matched one of the code points in \a std_str.
3810 size_type find_first_not_of(const std::string& std_str, size_type idx = 0) const
3812 if (idx < d_cplength)
3814 const utf32* pt = &ptr()[idx];
3818 if (npos == find_codepoint(std_str, *pt++))
3819 return idx;
3821 } while (++idx != d_cplength);
3825 return npos;
3830 \brief
3831 Find the first occurrence of one of a set of code points.
3833 \param utf8_str
3834 Buffer containing null-terminated utf8 encoded data describing the set of code points.
3836 \note
3837 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3838 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3839 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3840 results.
3842 \param idx
3843 Index of the start point for the search
3845 \return
3846 - Index of the first occurrence of any one of the code points in \a utf8_str starting from from \a idx.
3847 - npos if none of the code points in \a utf8_str were found.
3849 \exception std::out_of_range Thrown if \a idx is invalid for this String.
3851 size_type find_first_of(const utf8* utf8_str, size_type idx = 0) const
3853 return find_first_of(utf8_str, idx, utf_length(utf8_str));
3857 \brief
3858 Find the first code point that is not one of a set of code points.
3860 \param utf8_str
3861 Buffer containing null-terminated utf8 encoded data describing the set of code points.
3863 \note
3864 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3865 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3866 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3867 results.
3869 \param idx
3870 Index of the start point for the search
3872 \return
3873 - Index of the first code point that does not match any one of the code points in \a utf8_str starting from from \a idx.
3874 - npos if all code points matched one of the code points in \a utf8_str.
3876 \exception std::out_of_range Thrown if \a idx is invalid for this String.
3878 size_type find_first_not_of(const utf8* utf8_str, size_type idx = 0) const
3880 return find_first_not_of(utf8_str, idx, utf_length(utf8_str));
3884 \brief
3885 Find the first occurrence of one of a set of code points.
3887 \param utf8_str
3888 Buffer containing utf8 encoded data describing the set of code points.
3890 \note
3891 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3892 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3893 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3894 results.
3896 \param idx
3897 Index of the start point for the search
3899 \param str_len
3900 Length of the utf8 encoded data in utf8 code units (not code points).
3902 \return
3903 - Index of the first occurrence of any one of the code points in \a utf8_str starting from from \a idx.
3904 - npos if none of the code points in \a utf8_str were found.
3906 \exception std::length_error Thrown if \a str_len was 'npos'.
3908 size_type find_first_of(const utf8* utf8_str, size_type idx, size_type str_len) const
3910 if (str_len == npos)
3911 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
3913 if (idx < d_cplength)
3915 size_type encsze = encoded_size(utf8_str, str_len);
3917 const utf32* pt = &ptr()[idx];
3921 if (npos != find_codepoint(utf8_str, encsze, *pt++))
3922 return idx;
3924 } while (++idx != d_cplength);
3928 return npos;
3932 \brief
3933 Find the first code point that is not one of a set of code points.
3935 \param utf8_str
3936 Buffer containing utf8 encoded data describing the set of code points.
3938 \note
3939 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
3940 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
3941 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
3942 results.
3944 \param idx
3945 Index of the start point for the search
3947 \param str_len
3948 Length of the utf8 encoded data in utf8 code units (not code points).
3950 \return
3951 - Index of the first code point that does not match any one of the code points in \a utf8_str starting from from \a idx.
3952 - npos if all code points matched one of the code points in \a utf8_str.
3954 \exception std::length_error Thrown if \a str_len was 'npos'.
3956 size_type find_first_not_of(const utf8* utf8_str, size_type idx, size_type str_len) const
3958 if (str_len == npos)
3959 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
3961 if (idx < d_cplength)
3963 size_type encsze = encoded_size(utf8_str, str_len);
3965 const utf32* pt = &ptr()[idx];
3969 if (npos == find_codepoint(utf8_str, encsze, *pt++))
3970 return idx;
3972 } while (++idx != d_cplength);
3976 return npos;
3981 \brief
3982 Search forwards for a given code point
3984 \param code_point
3985 The utf32 code point to search for
3987 \param idx
3988 Index of the code point where the search is to start.
3990 \return
3991 - Index of the first occurrence of \a code_point starting from from \a idx.
3992 - npos if the code point could not be found
3994 size_type find_first_of(utf32 code_point, size_type idx = 0) const
3996 return find(code_point, idx);
4000 \brief
4001 Search forwards for the first code point that does not match a given code point
4003 \param code_point
4004 The utf32 code point to search for
4006 \param idx
4007 Index of the code point where the search is to start.
4009 \return
4010 - Index of the first code point that does not match \a code_point starting from from \a idx.
4011 - npos if all code points matched \a code_point
4013 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4015 size_type find_first_not_of(utf32 code_point, size_type idx = 0) const
4017 if (idx < d_cplength)
4021 if ((*this)[idx] != code_point)
4022 return idx;
4024 } while(idx++ < d_cplength);
4028 return npos;
4033 \brief
4034 Find the first occurrence of one of a set of chars.
4036 \param c_str
4037 c-string describing the set of chars.
4039 \param idx
4040 Index of the start point for the search
4042 \return
4043 - Index of the first occurrence of any one of the chars in \a c_str starting from from \a idx.
4044 - npos if none of the chars in \a c_str were found.
4046 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4048 size_type find_first_of(const char* cstr, size_type idx = 0) const
4050 return find_first_of(cstr, idx, strlen(cstr));
4055 \brief
4056 Find the first code point that is not one of a set of chars.
4058 \param c_str
4059 c-string describing the set of chars.
4061 \param idx
4062 Index of the start point for the search
4064 \return
4065 - Index of the first code point that does not match any one of the chars in \a c_str starting from from \a idx.
4066 - npos if all code points matched any of the chars in \a c_str.
4068 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4070 size_type find_first_not_of(const char* cstr, size_type idx = 0) const
4072 return find_first_not_of(cstr, idx, strlen(cstr));
4077 \brief
4078 Find the first occurrence of one of a set of chars.
4080 \param chars
4081 char array containing the set of chars.
4083 \param idx
4084 Index of the start point for the search
4086 \param chars_len
4087 Number of chars in the char array.
4089 \return
4090 - Index of the first occurrence of any one of the chars in \a chars starting from from \a idx.
4091 - npos if none of the chars in \a chars were found.
4093 \exception std::length_error Thrown if \a chars_len was 'npos'.
4095 size_type find_first_of(const char* chars, size_type idx, size_type chars_len) const
4097 if (chars_len == npos)
4098 throw std::length_error("Length for char array can not be 'npos'");
4100 if (idx < d_cplength)
4102 const utf32* pt = &ptr()[idx];
4106 if (npos != find_codepoint(chars, chars_len, *pt++))
4107 return idx;
4109 } while (++idx != d_cplength);
4113 return npos;
4118 \brief
4119 Find the first code point that is not one of a set of chars.
4121 \param chars
4122 char array containing the set of chars.
4124 \param idx
4125 Index of the start point for the search
4127 \param chars_len
4128 Number of chars in the car array.
4130 \return
4131 - Index of the first code point that does not match any one of the chars in \a chars starting from from \a idx.
4132 - npos if all code points matched any of the chars in \a chars.
4134 \exception std::length_error Thrown if \a chars_len was 'npos'.
4136 size_type find_first_not_of(const char* chars, size_type idx, size_type chars_len) const
4138 if (chars_len == npos)
4139 throw std::length_error("Length for char array can not be 'npos'");
4141 if (idx < d_cplength)
4143 const utf32* pt = &ptr()[idx];
4147 if (npos == find_codepoint(chars, chars_len, *pt++))
4148 return idx;
4150 } while (++idx != d_cplength);
4154 return npos;
4158 //////////////////////////////////////////////////////////////////////////
4159 // Find last of different code-points
4160 //////////////////////////////////////////////////////////////////////////
4162 \brief
4163 Find the last occurrence of one of a set of code points.
4165 \param str
4166 String object describing the set of code points.
4168 \param idx
4169 Index of the start point for the search
4171 \return
4172 - Index of the last occurrence of any one of the code points in \a str starting from \a idx.
4173 - npos if none of the code points in \a str were found.
4175 size_type find_last_of(const String& str, size_type idx = npos) const
4177 if (d_cplength > 0)
4179 if (idx >= d_cplength)
4180 idx = d_cplength - 1;
4182 const utf32* pt = &ptr()[idx];
4186 if (npos != str.find(*pt--))
4187 return idx;
4189 } while (idx-- != 0);
4193 return npos;
4197 \brief
4198 Find the last code point that is not one of a set of code points.
4200 \param str
4201 String object describing the set of code points.
4203 \param idx
4204 Index of the start point for the search
4206 \return
4207 - Index of the last code point that does not match any one of the code points in \a str starting from \a idx.
4208 - npos if all code points matched one of the code points in \a str.
4210 size_type find_last_not_of(const String& str, size_type idx = npos) const
4212 if (d_cplength > 0)
4214 if (idx >= d_cplength)
4215 idx = d_cplength - 1;
4217 const utf32* pt = &ptr()[idx];
4221 if (npos == str.find(*pt--))
4222 return idx;
4224 } while (idx-- != 0);
4228 return npos;
4233 \brief
4234 Find the last occurrence of one of a set of code points.
4236 \param std_str
4237 std::string object describing the set of code points.
4239 \note
4240 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
4241 the provided data will occur.
4243 \param idx
4244 Index of the start point for the search
4246 \return
4247 - Index of the last occurrence of any one of the code points in \a std_str starting from \a idx.
4248 - npos if none of the code points in \a std_str were found.
4250 size_type find_last_of(const std::string& std_str, size_type idx = npos) const
4252 if (d_cplength > 0)
4254 if (idx >= d_cplength)
4255 idx = d_cplength - 1;
4257 const utf32* pt = &ptr()[idx];
4261 if (npos != find_codepoint(std_str, *pt--))
4262 return idx;
4264 } while (idx-- != 0);
4268 return npos;
4272 \brief
4273 Find the last code point that is not one of a set of code points.
4275 \param std_str
4276 std::string object describing the set of code points.
4278 \note
4279 The characters of \a std_str are taken to be unencoded data which represent Unicode code points 0x00..0xFF. No translation of
4280 the provided data will occur.
4282 \param idx
4283 Index of the start point for the search
4285 \return
4286 - Index of the last code point that does not match any one of the code points in \a std_str starting from \a idx.
4287 - npos if all code points matched one of the code points in \a std_str.
4289 size_type find_last_not_of(const std::string& std_str, size_type idx = npos) const
4291 if (d_cplength > 0)
4293 if (idx >= d_cplength)
4294 idx = d_cplength - 1;
4296 const utf32* pt = &ptr()[idx];
4300 if (npos == find_codepoint(std_str, *pt--))
4301 return idx;
4303 } while (idx-- != 0);
4307 return npos;
4312 \brief
4313 Find the last occurrence of one of a set of code points.
4315 \param utf8_str
4316 Buffer containing null-terminated utf8 encoded data describing the set of code points.
4318 \note
4319 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
4320 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
4321 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
4322 results.
4324 \param idx
4325 Index of the start point for the search
4327 \return
4328 - Index of the last occurrence of any one of the code points in \a utf8_str starting from \a idx.
4329 - npos if none of the code points in \a utf8_str were found.
4331 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4333 size_type find_last_of(const utf8* utf8_str, size_type idx = npos) const
4335 return find_last_of(utf8_str, idx, utf_length(utf8_str));
4339 \brief
4340 Find the last code point that is not one of a set of code points.
4342 \param utf8_str
4343 Buffer containing null-terminated utf8 encoded data describing the set of code points.
4345 \note
4346 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
4347 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
4348 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
4349 results.
4351 \param idx
4352 Index of the start point for the search
4354 \return
4355 - Index of the last code point that does not match any one of the code points in \a utf8_str starting from \a idx.
4356 - npos if all code points matched one of the code points in \a utf8_str.
4358 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4360 size_type find_last_not_of(const utf8* utf8_str, size_type idx = npos) const
4362 return find_last_not_of(utf8_str, idx, utf_length(utf8_str));
4366 \brief
4367 Find the last occurrence of one of a set of code points.
4369 \param utf8_str
4370 Buffer containing utf8 encoded data describing the set of code points.
4372 \note
4373 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
4374 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
4375 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
4376 results.
4378 \param idx
4379 Index of the start point for the search
4381 \param str_len
4382 Length of the utf8 encoded data in utf8 code units (not code points).
4384 \return
4385 - Index of the last occurrence of any one of the code points in \a utf8_str starting from from \a idx.
4386 - npos if none of the code points in \a utf8_str were found.
4388 \exception std::length_error Thrown if \a str_len was 'npos'.
4390 size_type find_last_of(const utf8* utf8_str, size_type idx, size_type str_len) const
4392 if (str_len == npos)
4393 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
4395 if (d_cplength > 0)
4397 if (idx >= d_cplength)
4398 idx = d_cplength - 1;
4400 size_type encsze = encoded_size(utf8_str, str_len);
4402 const utf32* pt = &ptr()[idx];
4406 if (npos != find_codepoint(utf8_str, encsze, *pt--))
4407 return idx;
4409 } while (idx-- != 0);
4413 return npos;
4417 \brief
4418 Find the last code point that is not one of a set of code points.
4420 \param utf8_str
4421 Buffer containing utf8 encoded data describing the set of code points.
4423 \note
4424 A basic string literal (cast to utf8*) can be passed to this function, provided that the string is
4425 comprised only of code points 0x00..0x7f. The use of extended ASCII characters (with values >0x7f)
4426 would result in incorrect behaviour as the String will attempt to 'decode' the data, with unpredictable
4427 results.
4429 \param idx
4430 Index of the start point for the search
4432 \param str_len
4433 Length of the utf8 encoded data in utf8 code units (not code points).
4435 \return
4436 - Index of the last code point that does not match any one of the code points in \a utf8_str starting from from \a idx.
4437 - npos if all code points matched one of the code points in \a utf8_str.
4439 \exception std::length_error Thrown if \a str_len was 'npos'.
4441 size_type find_last_not_of(const utf8* utf8_str, size_type idx, size_type str_len) const
4443 if (str_len == npos)
4444 throw std::length_error("Length for utf8 encoded string can not be 'npos'");
4446 if (d_cplength > 0)
4448 if (idx >= d_cplength)
4449 idx = d_cplength - 1;
4451 size_type encsze = encoded_size(utf8_str, str_len);
4453 const utf32* pt = &ptr()[idx];
4457 if (npos == find_codepoint(utf8_str, encsze, *pt--))
4458 return idx;
4460 } while (idx-- != 0);
4464 return npos;
4469 \brief
4470 Search for last occurrence of a given code point
4472 \param code_point
4473 The utf32 code point to search for
4475 \param idx
4476 Index of the code point where the search is to start.
4478 \return
4479 - Index of the last occurrence of \a code_point starting from \a idx.
4480 - npos if the code point could not be found
4482 size_type find_last_of(utf32 code_point, size_type idx = npos) const
4484 return rfind(code_point, idx);
4488 \brief
4489 Search for the last code point that does not match a given code point
4491 \param code_point
4492 The utf32 code point to search for
4494 \param idx
4495 Index of the code point where the search is to start.
4497 \return
4498 - Index of the last code point that does not match \a code_point starting from from \a idx.
4499 - npos if all code points matched \a code_point
4501 size_type find_last_not_of(utf32 code_point, size_type idx = npos) const
4503 if (d_cplength > 0)
4505 if (idx >= d_cplength)
4506 idx = d_cplength - 1;
4510 if ((*this)[idx] != code_point)
4511 return idx;
4513 } while(idx-- != 0);
4517 return npos;
4522 \brief
4523 Find the last occurrence of one of a set of chars.
4525 \param c_str
4526 c-string describing the set of chars.
4528 \param idx
4529 Index of the start point for the search
4531 \return
4532 - Index of the last occurrence of any one of the chars in \a c_str starting from \a idx.
4533 - npos if none of the chars in \a c_str were found.
4535 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4537 size_type find_last_of(const char* cstr, size_type idx = npos) const
4539 return find_last_of(cstr, idx, strlen(cstr));
4544 \brief
4545 Find the last code point that is not one of a set of chars.
4547 \param c_str
4548 c-string describing the set of chars.
4550 \param idx
4551 Index of the start point for the search
4553 \return
4554 - Index of the last code point that does not match any one of the chars in \a c_str starting from \a idx.
4555 - npos if all code points matched any of the chars in \a c_str.
4557 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4559 size_type find_last_not_of(const char* cstr, size_type idx = npos) const
4561 return find_last_not_of(cstr, idx, strlen(cstr));
4566 \brief
4567 Find the last occurrence of one of a set of chars.
4569 \param chars
4570 char array containing the set of chars.
4572 \param idx
4573 Index of the start point for the search
4575 \param chars_len
4576 Number of chars in the char array.
4578 \return
4579 - Index of the last occurrence of any one of the chars in \a chars, starting from from \a idx.
4580 - npos if none of the chars in \a chars were found.
4582 \exception std::length_error Thrown if \a chars_len was 'npos'.
4584 size_type find_last_of(const char* chars, size_type idx, size_type chars_len) const
4586 if (chars_len == npos)
4587 throw std::length_error("Length for char array can not be 'npos'");
4589 if (d_cplength > 0)
4591 if (idx >= d_cplength)
4592 idx = d_cplength - 1;
4594 const utf32* pt = &ptr()[idx];
4598 if (npos != find_codepoint(chars, chars_len, *pt--))
4599 return idx;
4601 } while (idx-- != 0);
4605 return npos;
4610 \brief
4611 Find the last code point that is not one of a set of chars.
4613 \param chars
4614 char array containing the set of chars.
4616 \param idx
4617 Index of the start point for the search
4619 \param chars_len
4620 Number of chars in the char array.
4622 \return
4623 - Index of the last code point that does not match any one of the chars in \a chars, starting from from \a idx.
4624 - npos if all code points matched any of the chars in \a chars.
4626 \exception std::length_error Thrown if \a chars_len was 'npos'.
4628 size_type find_last_not_of(const char* chars, size_type idx, size_type chars_len) const
4630 if (chars_len == npos)
4631 throw std::length_error("Length for char array can not be 'npos'");
4633 if (d_cplength > 0)
4635 if (idx >= d_cplength)
4636 idx = d_cplength - 1;
4638 const utf32* pt = &ptr()[idx];
4642 if (npos == find_codepoint(chars, chars_len, *pt--))
4643 return idx;
4645 } while (idx-- != 0);
4649 return npos;
4653 //////////////////////////////////////////////////////////////////////////
4654 // Substring
4655 //////////////////////////////////////////////////////////////////////////
4657 \brief
4658 Returns a substring of this String.
4660 \param idx
4661 Index of the first code point to use for the sub-string.
4663 \param len
4664 Maximum number of code points to use for the sub-string
4666 \return
4667 A String object containing the specified sub-string.
4669 \exception std::out_of_range Thrown if \a idx is invalid for this String.
4671 String substr(size_type idx = 0, size_type len = npos) const
4673 if (d_cplength < idx)
4674 throw std::out_of_range("Index is out of range for this CEGUI::String");
4676 return String(*this, idx, len);
4679 //////////////////////////////////////////////////////////////////////////
4680 // Iterator creation
4681 //////////////////////////////////////////////////////////////////////////
4683 \brief
4684 Return a forwards iterator that describes the beginning of the String
4686 \return
4687 iterator object that describes the beginning of the String.
4689 iterator begin(void)
4691 return iterator(ptr());
4695 \brief
4696 Return a constant forwards iterator that describes the beginning of the String
4698 \return
4699 const_iterator object that describes the beginning of the String.
4701 const_iterator begin(void) const
4703 return const_iterator(ptr());
4707 \brief
4708 Return a forwards iterator that describes the end of the String
4710 \return
4711 iterator object that describes the end of the String.
4713 iterator end(void)
4715 return iterator(&ptr()[d_cplength]);
4719 \brief
4720 Return a constant forwards iterator that describes the end of the String
4722 \return
4723 const_iterator object that describes the end of the String.
4725 const_iterator end(void) const
4727 return const_iterator(&ptr()[d_cplength]);
4731 \brief
4732 Return a reverse iterator that describes the beginning of the String
4734 \return
4735 reverse_iterator object that describes the beginning of the String (so is actually at the end)
4737 reverse_iterator rbegin(void)
4739 return reverse_iterator(end());
4743 \brief
4744 Return a constant reverse iterator that describes the beginning of the String
4746 \return
4747 const_reverse_iterator object that describes the beginning of the String (so is actually at the end)
4749 const_reverse_iterator rbegin(void) const
4751 return const_reverse_iterator(end());
4755 \brief
4756 Return a reverse iterator that describes the end of the String
4758 \return
4759 reverse_iterator object that describes the end of the String (so is actually at the beginning)
4761 reverse_iterator rend(void)
4763 return reverse_iterator(begin());
4767 \brief
4768 Return a constant reverse iterator that describes the end of the String
4770 \return
4771 const_reverse_iterator object that describes the end of the String (so is actually at the beginning)
4773 const_reverse_iterator rend(void) const
4775 return const_reverse_iterator(begin());
4778 private:
4779 /*************************************************************************
4780 Implementation Functions
4781 *************************************************************************/
4782 // string management
4784 // change size of allocated buffer so it is at least 'new_size'.
4785 // May or may not cause re-allocation and copy of buffer if size is larger
4786 // will never re-allocate to make size smaller. (see trim())
4787 bool grow(size_type new_size);
4789 // perform re-allocation to remove wasted space.
4790 void trim(void);
4792 // set the length of the string, and terminate it, according to the given value (will not re-allocate, use grow() first).
4793 void setlen(size_type len)
4795 d_cplength = len;
4796 ptr()[len] = (utf32)(0);
4799 // initialise string object
4800 void init(void)
4802 d_reserve = STR_QUICKBUFF_SIZE;
4803 d_encodedbuff = 0;
4804 d_encodedbufflen = 0;
4805 d_encodeddatlen = 0;
4806 d_buffer = 0;
4807 setlen(0);
4810 // return true if the given pointer is inside the string data
4811 bool inside(utf32* inptr)
4813 if (inptr < ptr() || ptr() + d_cplength <= inptr)
4814 return false;
4815 else
4816 return true;
4819 // compute distance between two iterators, returning a 'safe' value
4820 size_type safe_iter_dif(const const_iterator& iter1, const const_iterator& iter2) const
4822 return (iter1.d_ptr == 0) ? 0 : (iter1 - iter2);
4825 // encoding functions
4826 // for all:
4827 // src_len is in code units, or 0 for null terminated string.
4828 // dest_len is in code units.
4829 // returns number of code units put into dest buffer.
4830 size_type encode(const utf32* src, utf8* dest, size_type dest_len, size_type src_len = 0) const
4832 // count length for null terminated source...
4833 if (src_len == 0)
4835 src_len = utf_length(src);
4838 size_type destCapacity = dest_len;
4840 // while there is data in the source buffer,
4841 for (uint idx = 0; idx < src_len; ++idx)
4843 utf32 cp = src[idx];
4845 // check there is enough destination buffer to receive this encoded unit (exit loop & return if not)
4846 if (destCapacity < encoded_size(cp))
4848 break;
4851 if (cp < 0x80)
4853 *dest++ = (utf8)cp;
4854 --destCapacity;
4856 else if (cp < 0x0800)
4858 *dest++ = (utf8)((cp >> 6) | 0xC0);
4859 *dest++ = (utf8)((cp & 0x3F) | 0x80);
4860 destCapacity -= 2;
4862 else if (cp < 0x10000)
4864 *dest++ = (utf8)((cp >> 12) | 0xE0);
4865 *dest++ = (utf8)(((cp >> 6) & 0x3F) | 0x80);
4866 *dest++ = (utf8)((cp & 0x3F) | 0x80);
4867 destCapacity -= 3;
4869 else
4871 *dest++ = (utf8)((cp >> 18) | 0xF0);
4872 *dest++ = (utf8)(((cp >> 12) & 0x3F) | 0x80);
4873 *dest++ = (utf8)(((cp >> 6) & 0x3F) | 0x80);
4874 *dest++ = (utf8)((cp & 0x3F) | 0x80);
4875 destCapacity -= 4;
4880 return dest_len - destCapacity;
4883 size_type encode(const utf8* src, utf32* dest, size_type dest_len, size_type src_len = 0) const
4885 // count length for null terminated source...
4886 if (src_len == 0)
4888 src_len = utf_length(src);
4891 size_type destCapacity = dest_len;
4893 // while there is data in the source buffer, and space in the dest buffer
4894 for (uint idx = 0; ((idx < src_len) && (destCapacity > 0));)
4896 utf32 cp;
4897 utf8 cu = src[idx++];
4899 if (cu < 0x80)
4901 cp = (utf32)(cu);
4903 else if (cu < 0xE0)
4905 cp = ((cu & 0x1F) << 6);
4906 cp |= (src[idx++] & 0x3F);
4908 else if (cu < 0xF0)
4910 cp = ((cu & 0x0F) << 12);
4911 cp |= ((src[idx++] & 0x3F) << 6);
4912 cp |= (src[idx++] & 0x3F);
4914 else
4916 cp = ((cu & 0x07) << 18);
4917 cp |= ((src[idx++] & 0x3F) << 12);
4918 cp |= ((src[idx++] & 0x3F) << 6);
4919 cp |= (src[idx++] & 0x3F);
4922 *dest++ = cp;
4923 --destCapacity;
4926 return dest_len - destCapacity;
4929 // return the number of utf8 code units required to encode the given utf32 code point
4930 size_type encoded_size(utf32 code_point) const
4932 if (code_point < 0x80)
4933 return 1;
4934 else if (code_point < 0x0800)
4935 return 2;
4936 else if (code_point < 0x10000)
4937 return 3;
4938 else
4939 return 4;
4942 // return number of code units required to re-encode given null-terminated utf32 data as utf8. return does not include terminating null.
4943 size_type encoded_size(const utf32* buf) const
4945 return encoded_size(buf, utf_length(buf));
4948 // return number of code units required to re-encode given utf32 data as utf8. len is number of code units in 'buf'.
4949 size_type encoded_size(const utf32* buf, size_type len) const
4951 size_type count = 0;
4953 while (len--)
4955 count += encoded_size(*buf++);
4958 return count;
4961 // return number of utf32 code units required to re-encode given utf8 data as utf32. return does not include terminating null.
4962 size_type encoded_size(const utf8* buf) const
4964 return encoded_size(buf, utf_length(buf));
4967 // return number of utf32 code units required to re-encode given utf8 data as utf32. len is number of code units in 'buf'.
4968 size_type encoded_size(const utf8* buf, size_type len) const
4970 utf8 tcp;
4971 size_type count = 0;
4973 while (len--)
4975 tcp = *buf++;
4976 ++count;
4978 if (tcp < 0x80)
4981 else if (tcp < 0xE0)
4983 --len;
4984 ++buf;
4986 else if (tcp < 0xF0)
4988 len -= 2;
4989 buf += 2;
4991 else
4993 len -= 2;
4994 buf += 3;
4999 return count;
5002 // return number of code units in a null terminated string
5003 size_type utf_length(const utf8* utf8_str) const
5005 size_type cnt = 0;
5006 while (*utf8_str++)
5007 cnt++;
5009 return cnt;
5012 // return number of code units in a null terminated string
5013 size_type utf_length(const utf32* utf32_str) const
5015 size_type cnt = 0;
5016 while (*utf32_str++)
5017 cnt++;
5019 return cnt;
5022 // build an internal buffer with the string encoded as utf8 (remains valid until string is modified).
5023 utf8* build_utf8_buff(void) const;
5025 // compare two utf32 buffers
5026 int utf32_comp_utf32(const utf32* buf1, const utf32* buf2, size_type cp_count) const
5028 if (!cp_count)
5029 return 0;
5031 while ((--cp_count) && (*buf1 == *buf2))
5032 buf1++, buf2++;
5034 return *buf1 - *buf2;
5037 // compare utf32 buffer with char buffer (chars are taken to be code-points in the range 0x00-0xFF)
5038 int utf32_comp_char(const utf32* buf1, const char* buf2, size_type cp_count) const
5040 if (!cp_count)
5041 return 0;
5043 while ((--cp_count) && (*buf1 == static_cast<utf32>(static_cast<unsigned char>(*buf2))))
5044 buf1++, buf2++;
5046 return *buf1 - static_cast<utf32>(static_cast<unsigned char>(*buf2));
5049 // compare utf32 buffer with encoded utf8 data
5050 int utf32_comp_utf8(const utf32* buf1, const utf8* buf2, size_type cp_count) const
5052 if (!cp_count)
5053 return 0;
5055 utf32 cp;
5056 utf8 cu;
5060 cu = *buf2++;
5062 if (cu < 0x80)
5064 cp = (utf32)(cu);
5066 else if (cu < 0xE0)
5068 cp = ((cu & 0x1F) << 6);
5069 cp |= (*buf2++ & 0x3F);
5071 else if (cu < 0xF0)
5073 cp = ((cu & 0x0F) << 12);
5074 cp |= ((*buf2++ & 0x3F) << 6);
5075 cp |= (*buf2++ & 0x3F);
5077 else
5079 cp = ((cu & 0x07) << 18);
5080 cp |= ((*buf2++ & 0x3F) << 12);
5081 cp |= ((*buf2++ & 0x3F) << 6);
5082 cp |= (*buf2++ & 0x3F);
5085 } while ((*buf1++ == cp) && (--cp_count));
5087 return (*--buf1) - cp;
5090 // return index of first occurrence of 'code_point' in std::string 'str', or npos if none
5091 size_type find_codepoint(const std::string& str, utf32 code_point) const
5093 size_type idx = 0, sze = (size_type)str.size();
5095 while (idx != sze)
5097 if (code_point == static_cast<utf32>(static_cast<unsigned char>(str[idx])))
5098 return idx;
5100 ++idx;
5103 return npos;
5106 // return index of first occurrence of 'code_point' in utf8 encoded string 'str', or npos if none. len is in code points.
5107 size_type find_codepoint(const utf8* str, size_type len, utf32 code_point) const
5109 size_type idx = 0;
5111 utf32 cp;
5112 utf8 cu;
5114 while (idx != len) {
5115 cu = *str++;
5117 if (cu < 0x80)
5119 cp = (utf32)(cu);
5121 else if (cu < 0xE0)
5123 cp = ((cu & 0x1F) << 6);
5124 cp |= (*str++ & 0x3F);
5126 else if (cu < 0xF0)
5128 cp = ((cu & 0x0F) << 12);
5129 cp |= ((*str++ & 0x3F) << 6);
5130 cp |= (*str++ & 0x3F);
5132 else
5134 cp = ((cu & 0x07) << 18);
5135 cp |= ((*str++ & 0x3F) << 12);
5136 cp |= ((*str++ & 0x3F) << 6);
5137 cp |= (*str++ & 0x3F);
5140 if (code_point == cp)
5141 return idx;
5143 ++idx;
5146 return npos;
5150 // return index of first occurrence of 'code_point' in char array 'chars', or npos if none
5151 size_type find_codepoint(const char* chars, size_type chars_len, utf32 code_point) const
5153 for (size_type idx = 0; idx != chars_len; ++idx)
5155 if (code_point == static_cast<utf32>(static_cast<unsigned char>(chars[idx])))
5156 return idx;
5159 return npos;
5165 //////////////////////////////////////////////////////////////////////////
5166 // Comparison operators
5167 //////////////////////////////////////////////////////////////////////////
5169 \brief
5170 Return true if String \a str1 is equal to String \a str2
5172 bool CEGUIEXPORT operator==(const String& str1, const String& str2);
5175 \brief
5176 Return true if String \a str is equal to std::string \a std_str
5178 bool CEGUIEXPORT operator==(const String& str, const std::string& std_str);
5181 \brief
5182 Return true if String \a str is equal to std::string \a std_str
5184 bool CEGUIEXPORT operator==(const std::string& std_str, const String& str);
5187 \brief
5188 Return true if String \a str is equal to null-terminated utf8 data \a utf8_str
5190 bool CEGUIEXPORT operator==(const String& str, const utf8* utf8_str);
5193 \brief
5194 Return true if String \a str is equal to null-terminated utf8 data \a utf8_str
5196 bool CEGUIEXPORT operator==(const utf8* utf8_str, const String& str);
5199 \brief
5200 Return true if String \a str1 is not equal to String \a str2
5202 bool CEGUIEXPORT operator!=(const String& str1, const String& str2);
5205 \brief
5206 Return true if String \a str is not equal to std::string \a std_str
5208 bool CEGUIEXPORT operator!=(const String& str, const std::string& std_str);
5211 \brief
5212 Return true if String \a str is not equal to std::string \a std_str
5214 bool CEGUIEXPORT operator!=(const std::string& std_str, const String& str);
5217 \brief
5218 Return true if String \a str is not equal to null-terminated utf8 data \a utf8_str
5220 bool CEGUIEXPORT operator!=(const String& str, const utf8* utf8_str);
5223 \brief
5224 Return true if String \a str is not equal to null-terminated utf8 data \a utf8_str
5226 bool CEGUIEXPORT operator!=(const utf8* utf8_str, const String& str);
5229 \brief
5230 Return true if String \a str1 is lexicographically less than String \a str2
5232 bool CEGUIEXPORT operator<(const String& str1, const String& str2);
5235 \brief
5236 Return true if String \a str is lexicographically less than std::string \a std_str
5238 bool CEGUIEXPORT operator<(const String& str, const std::string& std_str);
5241 \brief
5242 Return true if String \a str is lexicographically less than std::string \a std_str
5244 bool CEGUIEXPORT operator<(const std::string& std_str, const String& str);
5247 \brief
5248 Return true if String \a str is lexicographically less than null-terminated utf8 data \a utf8_str
5250 bool CEGUIEXPORT operator<(const String& str, const utf8* utf8_str);
5253 \brief
5254 Return true if String \a str is lexicographically less than null-terminated utf8 data \a utf8_str
5256 bool CEGUIEXPORT operator<(const utf8* utf8_str, const String& str);
5259 \brief
5260 Return true if String \a str1 is lexicographically greater than String \a str2
5262 bool CEGUIEXPORT operator>(const String& str1, const String& str2);
5265 \brief
5266 Return true if String \a str is lexicographically greater than std::string \a std_str
5268 bool CEGUIEXPORT operator>(const String& str, const std::string& std_str);
5271 \brief
5272 Return true if String \a str is lexicographically greater than std::string \a std_str
5274 bool CEGUIEXPORT operator>(const std::string& std_str, const String& str);
5277 \brief
5278 Return true if String \a str is lexicographically greater than null-terminated utf8 data \a utf8_str
5280 bool CEGUIEXPORT operator>(const String& str, const utf8* utf8_str);
5283 \brief
5284 Return true if String \a str is lexicographically greater than null-terminated utf8 data \a utf8_str
5286 bool CEGUIEXPORT operator>(const utf8* utf8_str, const String& str);
5289 \brief
5290 Return true if String \a str1 is lexicographically less than or equal to String \a str2
5292 bool CEGUIEXPORT operator<=(const String& str1, const String& str2);
5295 \brief
5296 Return true if String \a str is lexicographically less than or equal to std::string \a std_str
5298 bool CEGUIEXPORT operator<=(const String& str, const std::string& std_str);
5301 \brief
5302 Return true if String \a str is lexicographically less than or equal to std::string \a std_str
5304 bool CEGUIEXPORT operator<=(const std::string& std_str, const String& str);
5307 \brief
5308 Return true if String \a str is lexicographically less than or equal to null-terminated utf8 data \a utf8_str
5310 bool CEGUIEXPORT operator<=(const String& str, const utf8* utf8_str);
5313 \brief
5314 Return true if String \a str is lexicographically less than or equal to null-terminated utf8 data \a utf8_str
5316 bool CEGUIEXPORT operator<=(const utf8* utf8_str, const String& str);
5319 \brief
5320 Return true if String \a str1 is lexicographically greater than or equal to String \a str2
5322 bool CEGUIEXPORT operator>=(const String& str1, const String& str2);
5325 \brief
5326 Return true if String \a str is lexicographically greater than or equal to std::string \a std_str
5328 bool CEGUIEXPORT operator>=(const String& str, const std::string& std_str);
5331 \brief
5332 Return true if String \a str is lexicographically greater than or equal to std::string \a std_str
5334 bool CEGUIEXPORT operator>=(const std::string& std_str, const String& str);
5337 \brief
5338 Return true if String \a str is lexicographically greater than or equal to null-terminated utf8 data \a utf8_str
5340 bool CEGUIEXPORT operator>=(const String& str, const utf8* utf8_str);
5343 \brief
5344 Return true if String \a str is lexicographically greater than or equal to null-terminated utf8 data \a utf8_str
5346 bool CEGUIEXPORT operator>=(const utf8* utf8_str, const String& str);
5349 \brief
5350 Return true if String \a str is equal to c-string \a c_str
5352 bool CEGUIEXPORT operator==(const String& str, const char* c_str);
5355 \brief
5356 Return true if c-string \a c_str is equal to String \a str
5358 bool CEGUIEXPORT operator==(const char* c_str, const String& str);
5361 \brief
5362 Return true if String \a str is not equal to c-string \a c_str
5364 bool CEGUIEXPORT operator!=(const String& str, const char* c_str);
5367 \brief
5368 Return true if c-string \a c_str is not equal to String \a str
5370 bool CEGUIEXPORT operator!=(const char* c_str, const String& str);
5373 \brief
5374 Return true if String \a str is lexicographically less than c-string \a c_str
5376 bool CEGUIEXPORT operator<(const String& str, const char* c_str);
5379 \brief
5380 Return true if c-string \a c_str is lexicographically less than String \a str
5382 bool CEGUIEXPORT operator<(const char* c_str, const String& str);
5385 \brief
5386 Return true if String \a str is lexicographically greater than c-string \a c_str
5388 bool CEGUIEXPORT operator>(const String& str, const char* c_str);
5391 \brief
5392 Return true if c-string \a c_str is lexicographically greater than String \a str
5394 bool CEGUIEXPORT operator>(const char* c_str, const String& str);
5397 \brief
5398 Return true if String \a str is lexicographically less than or equal to c-string \a c_str
5400 bool CEGUIEXPORT operator<=(const String& str, const char* c_str);
5403 \brief
5404 Return true if c-string \a c_str is lexicographically less than or equal to String \a str
5406 bool CEGUIEXPORT operator<=(const char* c_str, const String& str);
5409 \brief
5410 Return true if String \a str is lexicographically greater than or equal to c-string \a c_str
5412 bool CEGUIEXPORT operator>=(const String& str, const char* c_str);
5415 \brief
5416 Return true if c-string \a c_str is lexicographically greater than or equal to String \a str
5418 bool CEGUIEXPORT operator>=(const char* c_str, const String& str);
5420 //////////////////////////////////////////////////////////////////////////
5421 // Concatenation operator functions
5422 //////////////////////////////////////////////////////////////////////////
5424 \brief
5425 Return String object that is the concatenation of the given inputs
5427 \param str1
5428 String object describing first part of the new string
5430 \param str2
5431 String object describing the second part of the new string
5433 \return
5434 A String object that is the concatenation of \a str1 and \a str2
5436 \exception std::length_error Thrown if the resulting String would be too large.
5438 String CEGUIEXPORT operator+(const String& str1, const String& str2);
5441 \brief
5442 Return String object that is the concatenation of the given inputs
5444 \param str
5445 String object describing first part of the new string
5447 \param std_str
5448 std::string object describing the second part of the new string
5450 \return
5451 A String object that is the concatenation of \a str and \a std_str
5453 \exception std::length_error Thrown if the resulting String would be too large.
5455 String CEGUIEXPORT operator+(const String& str, const std::string& std_str);
5458 \brief
5459 Return String object that is the concatenation of the given inputs
5461 \param std_str
5462 std::string object describing the first part of the new string
5464 \param str
5465 String object describing the second part of the new string
5467 \return
5468 A String object that is the concatenation of \a std_str and \a str
5470 \exception std::length_error Thrown if the resulting String would be too large.
5472 String CEGUIEXPORT operator+(const std::string& std_str, const String& str);
5475 \brief
5476 Return String object that is the concatenation of the given inputs
5478 \param str
5479 String object describing first part of the new string
5481 \param utf8_str
5482 Buffer containing null-terminated utf8 encoded data describing the second part of the new string
5484 \return
5485 A String object that is the concatenation of \a str and \a utf8_str
5487 \exception std::length_error Thrown if the resulting String would be too large.
5489 String CEGUIEXPORT operator+(const String& str, const utf8* utf8_str);
5492 \brief
5493 Return String object that is the concatenation of the given inputs
5495 \param utf8_str
5496 Buffer containing null-terminated utf8 encoded data describing the first part of the new string
5498 \param str
5499 String object describing the second part of the new string
5501 \return
5502 A String object that is the concatenation of \a str and \a utf8_str
5504 \exception std::length_error Thrown if the resulting String would be too large.
5506 String CEGUIEXPORT operator+(const utf8* utf8_str, const String& str);
5509 \brief
5510 Return String object that is the concatenation of the given inputs
5512 \param str
5513 String object describing the first part of the new string
5515 \param code_point
5516 utf32 code point describing the second part of the new string
5518 \return
5519 A String object that is the concatenation of \a str and \a code_point
5521 \exception std::length_error Thrown if the resulting String would be too large.
5523 String CEGUIEXPORT operator+(const String& str, utf32 code_point);
5526 \brief
5527 Return String object that is the concatenation of the given inputs
5529 \param code_point
5530 utf32 code point describing the first part of the new string
5532 \param str
5533 String object describing the second part of the new string
5535 \return
5536 A String object that is the concatenation of \a code_point and \a str
5538 \exception std::length_error Thrown if the resulting String would be too large.
5540 String CEGUIEXPORT operator+(utf32 code_point, const String& str);
5543 \brief
5544 Return String object that is the concatenation of the given inputs
5546 \param str
5547 String object describing first part of the new string
5549 \param c_str
5550 c-string describing the second part of the new string
5552 \return
5553 A String object that is the concatenation of \a str and \a c_str
5555 \exception std::length_error Thrown if the resulting String would be too large.
5557 String CEGUIEXPORT operator+(const String& str, const char* c_str);
5560 \brief
5561 Return String object that is the concatenation of the given inputs
5563 \param c_str
5564 c-string describing the first part of the new string
5566 \param str
5567 String object describing the second part of the new string
5569 \return
5570 A String object that is the concatenation of \a c_str and \a str
5572 \exception std::length_error Thrown if the resulting String would be too large.
5574 String CEGUIEXPORT operator+(const char* c_str, const String& str);
5577 //////////////////////////////////////////////////////////////////////////
5578 // Output (stream) functions
5579 //////////////////////////////////////////////////////////////////////////
5580 CEGUIEXPORT std::ostream& operator<<(std::ostream& s, const String& str);
5583 //////////////////////////////////////////////////////////////////////////
5584 // Modifying operations
5585 //////////////////////////////////////////////////////////////////////////
5587 \brief
5588 Swap the contents for two String objects
5590 \param str1
5591 String object who's contents are to be swapped with \a str2
5593 \param str2
5594 String object who's contents are to be swapped with \a str1
5596 \return
5597 Nothing
5599 void CEGUIEXPORT swap(String& str1, String& str2);
5602 } // End of CEGUI namespace section
5605 #endif // end of guard _CEGUIString_h_