ULAPI  8.0
ulstring.h
1 
7 #ifndef ULSTRING_H
8 #define ULSTRING_H
9 
10 #include "ultypes.h"
11 #include "ulchar.h"
12 #include "ulcontainers.h"
13 
14 #ifdef UL_USING_ICU
15 #include "unicode/unistr.h"
16 #endif
17 
18 // Utility functions
19 int ulstrcmp(const char *a, const char *b);
20 int ulstrcmpi(const char *a, const char *b);
21 
22 // Forward declarations
23 class ULStringIterator;
25 class ULLanguage;
26 class ULCollator;
27 
40 class ULString
41 {
42  friend class ULStringIterator;
44  friend class ULCollator;
45 #ifdef UL_HAS_IOSTREAMS
46  friend ostream& operator<<(ostream& out, const ULString& s);
47  friend istream& operator>>(istream& in, ULString& s);
48 #endif
49  friend bool operator==(const ULString& a, const ULString& b);
50  friend bool operator<(const ULString& a, const ULString& b);
51  UL_TEST_FRIEND;
52 
53 public:
54  ULString();
55  ULString(const ULString& s);
56  ULString(const char *s);
57  ULString(ulchar ch);
58  virtual ~ULString();
59 
60  void clear();
61  ULString& operator=(const ULString& s);
62  ULString& operator=(const char *s);
63  ULString& operator+=(const ULString& s);
64  ULString& operator+=(const char *s);
65  ULString& operator+=(ulchar ch);
66  ulchar operator[](int n) const;
67  operator const char *();
68  uluint32 length() const;
69  void setAt(uluint32 index, ulchar value);
70 
71  int getEncoding() const;
72  void setEncoding(const char *encodingName);
73  void setEncoding(int newEncoding);
74  enum { UTF8, UTF16BE, UTF16LE };
75  const char *getBuffer();
76 
77  void fromUTF8(const char *utf8Buffer);
78 
79  ULStringIterator begin() const;
81  ULStringIterator end() const;
83 
84  bool contains(ulchar ch) const;
85  bool contains(const char *s) const;
86  bool contains(const ULString& s) const;
87  ULStringIterator find(ulchar ch) const;
88  ULStringIterator find(const char *s) const;
89  ULStringIterator find(const ULString& s) const;
90  ULStringIterator rfind(ulchar ch) const;
91  ULStringIterator rfind(const char *s) const;
92  ULStringIterator rfind(const ULString& s) const;
93  bool startsWith(const ULString& head) const;
94  bool startsWith(const ULString& head, ULCollator *collator, bool ignoreCase, bool ignoreAccents) const;
95  bool endsWith(const ULString& tail) const;
96  bool endsWith(const ULString& tail, ULLanguage language, bool ignoreCase, bool ignoreAccents) const;
97 
98  void replace(const ULString& textToReplace, const ULString& replacementText);
99  void replace(ulchar charToReplace, const ULString& replacementText);
100  void replace(const ULStringIterator& iterator, uluint32 nCharsToReplace, const ULString& replacementText);
101  void replace(uluint32 index, uluint32 nCharsToReplace, const ULString& replacementText);
102  void erase(ULStringIterator& iterator, uluint32 nCharsToErase=1);
103  void erase(uluint32 start, uluint32 nCharsToErase=1);
104  void insert(const ULStringIterator& iterator, const ULString& s);
105  void insert(ulint32 insertionIndex, const ULString& s);
106  ULString substr(ulint32 start, ulint32 nChars) const;
107  ULString substr(const ULStringIterator& start, const ULStringIterator& end) const;
108 
109  ulint32 compare(const ULString& s, const ULLanguage& language) const;
110  ulint32 compare(const ULString& s, const ULLanguage& language, bool ignoreCase, bool ignoreAccents) const;
111 
112  bool equals(const ULString& s, bool ignoreCase, bool ignoreAccents) const;
113 
114  uluint32 hash(uluint32 tableSize) const;
115 
116  ULString& reverse();
117  ULString& toLower();
118  ULString& toLower(const ULLanguage& language);
119  ULString& toUpper();
120  ULString& toUpper(const ULLanguage& language);
121  ULString& toBase();
122  ULString& toBase(const ULLanguage& language);
123  int getIntegerValue() const;
124  void appendInteger(int n, uluint32 base=10);
125  void split(const ULString& s, ULList<ULString>& stringList, bool stripParts=false) const;
126  void strip(const char *charactersToRemove = 0);
127  ULString join(const ULList<ULString>& stringList) const;
128 
129 private:
130  int encoding;
131 
132  char *conversionBuffer;
133  int conversionBufferCapacity;
134  bool conversionBufferValid;
135  void invalidateConversionBuffer();
136  void convert();
137  bool checkIntegrity() const; // for debugging
138  enum { MinimumCapacity = 32 };
139 
140 #ifdef UL_USING_ICU
141  UnicodeString icuString;
142  void initConversionBuffer();
143 #elif defined(UL_USING_COCOA_STRINGS)
144  NSMutableString *nsMutableString;
145 #elif defined(UL_USING_LEGACY_STRINGS)
146  // Legacy strings are the default
147  enum { staticBufferSize = 32 };
148  ulchar staticBuffer[staticBufferSize];
149  ulchar *buffer;
150  int capacity;
151  int charLength; // doesn't include the null
152  bool init();
153  bool reallocate(int nCharactersNeeded, bool copyData);
154 #endif
155 };
156 
157 ULString operator+(const ULString& a, const ULString& b);
158 ULString operator+(const ULString& a, const char *s);
159 ULString operator+(const ULString& a, const char ch);
160 ULString operator+(const ULString& a, uluint32 k);
161 
162 bool operator<(const ULString& a, const char *s);
163 bool operator<=(const ULString& a, const char *s);
164 bool operator>(const ULString& a, const char *s);
165 bool operator>=(const ULString& a, const char *s);
166 bool operator==(const ULString& a, const char *s);
167 bool operator!=(const ULString& a, const char *s);
168 
169 bool operator<(const ULString& a, const ULString& b);
170 bool operator<=(const ULString& a, const ULString& b);
171 bool operator>(const ULString& a, const ULString& b);
172 bool operator>=(const ULString& a, const ULString& b);
173 bool operator==(const ULString& a, const ULString& b);
174 bool operator!=(const ULString& a, const ULString& b);
175 
176 //istream& getline(istream& in, ULString& s, ulchar delimiter='\n');
177 //istream& getstring(istream& in, ULString& s);
178 
179 #ifdef UL_HAS_IOSTREAMS
180 ostream& operator<<(ostream& out, const ULString& s);
181 istream& operator>>(istream& in, ULString& s);
182 #endif
183 
184 #endif // ULSTRING_H
185