1 /* 2 * Copyright 2001-2007, Haiku Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef __BSTRING__ 6 #define __BSTRING__ 7 8 9 #include <BeBuild.h> 10 #include <SupportDefs.h> 11 #include <string.h> 12 13 14 class BString { 15 public: 16 BString(); 17 BString(const char* string); 18 BString(const BString& string); 19 BString(const char* string, int32 maxLength); 20 ~BString(); 21 22 // Access 23 const char* String() const; 24 25 int32 Length() const; 26 // length of corresponding string 27 int32 CountChars() const; 28 // returns number of UTF8 characters in string 29 30 // Assignment 31 BString& operator=(const BString& string); 32 BString& operator=(const char* string); 33 BString& operator=(char c); 34 35 BString& SetTo(const char* string); 36 BString& SetTo(const char* string, int32 maxLength); 37 38 BString& SetTo(const BString& string); 39 BString& Adopt(BString& from); 40 41 BString& SetTo(const BString& string, int32 maxLength); 42 BString& Adopt(BString& from, int32 maxLength); 43 44 BString& SetTo(char c, int32 count); 45 46 // Substring copying 47 BString& CopyInto(BString& into, int32 fromOffset, 48 int32 length) const; 49 void CopyInto(char* into, int32 fromOffset, 50 int32 length) const; 51 52 // Appending 53 BString& operator+=(const BString& string); 54 BString& operator+=(const char* string); 55 BString& operator+=(char c); 56 57 BString& Append(const BString& string); 58 BString& Append(const char* string); 59 60 BString& Append(const BString& string, int32 length); 61 BString& Append(const char* string, int32 length); 62 BString& Append(char c, int32 count); 63 64 // Prepending 65 BString& Prepend(const char* string); 66 BString& Prepend(const BString& string); 67 BString& Prepend(const char* string, int32 length); 68 BString& Prepend(const BString& string, int32 length); 69 BString& Prepend(char c, int32 count); 70 71 // Inserting 72 BString& Insert(const char* string, int32 pos); 73 BString& Insert(const char* string, int32 length, int32 pos); 74 BString& Insert(const char* string, int32 fromOffset, 75 int32 length, int32 pos); 76 77 BString& Insert(const BString& string, int32 pos); 78 BString& Insert(const BString& string, int32 length, int32 pos); 79 BString& Insert(const BString& string, int32 fromOffset, 80 int32 length, int32 pos); 81 BString& Insert(char, int32 count, int32 pos); 82 83 // Removing 84 BString& Truncate(int32 newLength, bool lazy = true); 85 BString& Remove(int32 from, int32 length); 86 87 BString& RemoveFirst(const BString& string); 88 BString& RemoveLast(const BString& string); 89 BString& RemoveAll(const BString& string); 90 91 BString& RemoveFirst(const char* string); 92 BString& RemoveLast(const char* string); 93 BString& RemoveAll(const char* string); 94 95 BString& RemoveSet(const char* setOfCharsToRemove); 96 97 BString& MoveInto(BString& into, int32 from, int32 length); 98 void MoveInto(char* into, int32 from, int32 length); 99 100 // Compare functions 101 bool operator<(const BString& string) const; 102 bool operator<=(const BString& string) const; 103 bool operator==(const BString& string) const; 104 bool operator>=(const BString& string) const; 105 bool operator>(const BString& string) const; 106 bool operator!=(const BString& string) const; 107 108 bool operator<(const char* string) const; 109 bool operator<=(const char* string) const; 110 bool operator==(const char* string) const; 111 bool operator>=(const char* string) const; 112 bool operator>(const char* string) const; 113 bool operator!=(const char* string) const; 114 115 // strcmp()-style compare functions 116 int Compare(const BString& string) const; 117 int Compare(const char* string) const; 118 int Compare(const BString& string, int32 length) const; 119 int Compare(const char* string, int32 length) const; 120 int ICompare(const BString& string) const; 121 int ICompare(const char* string) const; 122 int ICompare(const BString& string, int32 length) const; 123 int ICompare(const char* string, int32 length) const; 124 125 // Searching 126 int32 FindFirst(const BString& string) const; 127 int32 FindFirst(const char* string) const; 128 int32 FindFirst(const BString& string, int32 fromOffset) const; 129 int32 FindFirst(const char* string, int32 fromOffset) const; 130 int32 FindFirst(char c) const; 131 int32 FindFirst(char c, int32 fromOffset) const; 132 133 int32 FindLast(const BString& string) const; 134 int32 FindLast(const char* string) const; 135 int32 FindLast(const BString& string, int32 beforeOffset) const; 136 int32 FindLast(const char* string, int32 beforeOffset) const; 137 int32 FindLast(char c) const; 138 int32 FindLast(char c, int32 beforeOffset) const; 139 140 int32 IFindFirst(const BString& string) const; 141 int32 IFindFirst(const char* string) const; 142 int32 IFindFirst(const BString& string, int32 fromOffset) const; 143 int32 IFindFirst(const char* string, int32 fromOffset) const; 144 145 int32 IFindLast(const BString& string) const; 146 int32 IFindLast(const char* string) const; 147 int32 IFindLast(const BString& string, int32 beforeOffset) const; 148 int32 IFindLast(const char* string, int32 beforeOffset) const; 149 150 // Replacing 151 BString& ReplaceFirst(char replaceThis, char withThis); 152 BString& ReplaceLast(char replaceThis, char withThis); 153 BString& ReplaceAll(char replaceThis, char withThis, 154 int32 fromOffset = 0); 155 BString& Replace(char replaceThis, char withThis, 156 int32 maxReplaceCount, int32 fromOffset = 0); 157 BString& ReplaceFirst(const char* replaceThis, 158 const char* withThis); 159 BString& ReplaceLast(const char* replaceThis, 160 const char* withThis); 161 BString& ReplaceAll(const char* replaceThis, 162 const char* withThis, int32 fromOffset = 0); 163 BString& Replace(const char* replaceThis, const char* withThis, 164 int32 maxReplaceCount, int32 fromOffset = 0); 165 166 BString& IReplaceFirst(char replaceThis, char withThis); 167 BString& IReplaceLast(char replaceThis, char withThis); 168 BString& IReplaceAll(char replaceThis, char withThis, 169 int32 fromOffset = 0); 170 BString& IReplace(char replaceThis, char withThis, 171 int32 maxReplaceCount, int32 fromOffset = 0); 172 BString& IReplaceFirst(const char* replaceThis, 173 const char* withThis); 174 BString& IReplaceLast(const char* replaceThis, 175 const char* withThis); 176 BString& IReplaceAll(const char* replaceThis, 177 const char* withThis, int32 fromOffset = 0); 178 BString& IReplace(const char* replaceThis, const char* withThis, 179 int32 maxReplaceCount, int32 fromOffset = 0); 180 181 BString& ReplaceSet(const char* setOfChars, char with); 182 BString& ReplaceSet(const char* setOfChars, const char *with); 183 184 // Unchecked char access 185 char operator[](int32 index) const; 186 char& operator[](int32 index); 187 188 // Checked char access 189 char ByteAt(int32 index) const; 190 191 // Fast low-level manipulation 192 char* LockBuffer(int32 maxLength); 193 BString& UnlockBuffer(int32 length = -1); 194 195 // Upercase <-> Lowercase 196 BString& ToLower(); 197 BString& ToUpper(); 198 199 BString& Capitalize(); 200 BString& CapitalizeEachWord(); 201 202 // Escaping and De-escaping 203 BString& CharacterEscape(const char* original, 204 const char* setOfCharsToEscape, char escapeWith); 205 BString& CharacterEscape(const char* setOfCharsToEscape, 206 char escapeWith); 207 208 BString& CharacterDeescape(const char* original, char escapeChar); 209 BString& CharacterDeescape(char escapeChar); 210 211 // Slower than sprintf() but type and overflow safe 212 BString& operator<<(const char* string); 213 BString& operator<<(const BString& string); 214 BString& operator<<(char c); 215 BString& operator<<(int value); 216 BString& operator<<(unsigned int value); 217 BString& operator<<(uint32 value); 218 BString& operator<<(int32 value); 219 BString& operator<<(uint64 value); 220 BString& operator<<(int64 value); 221 BString& operator<<(float value); 222 // float output hardcodes %.2f style formatting 223 224 private: 225 void _Init(const char *, int32); 226 void _DoAssign(const char *, int32); 227 void _DoAppend(const char *, int32); 228 char* _GrowBy(int32); 229 char* _OpenAtBy(int32, int32); 230 char* _ShrinkAtBy(int32, int32); 231 void _DoPrepend(const char *, int32); 232 233 int32 _FindAfter(const char *, int32, int32) const; 234 int32 _IFindAfter(const char *, int32, int32) const; 235 int32 _ShortFindAfter(const char *, int32) const; 236 int32 _FindBefore(const char *, int32, int32) const; 237 int32 _IFindBefore(const char *, int32, int32) const; 238 BString& _DoReplace(const char *, const char *, int32, int32, 239 bool); 240 void _SetLength(int32); 241 242 #if DEBUG 243 void _SetUsingAsCString(bool); 244 void _AssertNotUsingAsCString() const; 245 #else 246 void _SetUsingAsCString(bool) {} 247 void _AssertNotUsingAsCString() const {} 248 #endif 249 250 char* _Alloc(int32 size); 251 252 class PosVect; 253 void _ReplaceAtPositions(const PosVect* positions, 254 int32 searchLength, const char* with, int32 withLen); 255 256 protected: 257 char* fPrivateData; 258 }; 259 260 // Commutative compare operators 261 bool operator<(const char* a, const BString& b); 262 bool operator<=(const char* a, const BString& b); 263 bool operator==(const char* a, const BString& b); 264 bool operator>(const char* a, const BString& b); 265 bool operator>=(const char* a, const BString& b); 266 bool operator!=(const char* a, const BString& b); 267 268 // Non-member compare for sorting, etc. 269 int Compare(const BString& a, const BString& b); 270 int ICompare(const BString& a, const BString& b); 271 int Compare(const BString* a, const BString* b); 272 int ICompare(const BString* a, const BString* b); 273 274 275 inline int32 276 BString::Length() const 277 { 278 return fPrivateData ? (*((int32 *)fPrivateData - 1) & 0x7fffffff) : 0; 279 /* the most significant bit is reserved; accessing 280 * it in any way will cause the computer to explode 281 */ 282 } 283 284 inline const char * 285 BString::String() const 286 { 287 if (!fPrivateData) 288 return ""; 289 return fPrivateData; 290 } 291 292 inline BString & 293 BString::SetTo(const char *string) 294 { 295 return operator=(string); 296 } 297 298 inline char 299 BString::operator[](int32 index) const 300 { 301 return fPrivateData[index]; 302 } 303 304 inline char 305 BString::ByteAt(int32 index) const 306 { 307 if (!fPrivateData || index < 0 || index > Length()) 308 return 0; 309 return fPrivateData[index]; 310 } 311 312 inline BString & 313 BString::operator+=(const BString &string) 314 { 315 _DoAppend(string.String(), string.Length()); 316 return *this; 317 } 318 319 inline BString & 320 BString::Append(const BString &string) 321 { 322 _DoAppend(string.String(), string.Length()); 323 return *this; 324 } 325 326 inline BString & 327 BString::Append(const char *str) 328 { 329 return operator+=(str); 330 } 331 332 inline bool 333 BString::operator==(const BString &string) const 334 { 335 return strcmp(String(), string.String()) == 0; 336 } 337 338 inline bool 339 BString::operator<(const BString &string) const 340 { 341 return strcmp(String(), string.String()) < 0; 342 } 343 344 inline bool 345 BString::operator<=(const BString &string) const 346 { 347 return strcmp(String(), string.String()) <= 0; 348 } 349 350 inline bool 351 BString::operator>=(const BString &string) const 352 { 353 return strcmp(String(), string.String()) >= 0; 354 } 355 356 inline bool 357 BString::operator>(const BString &string) const 358 { 359 return strcmp(String(), string.String()) > 0; 360 } 361 362 inline bool 363 BString::operator!=(const BString &string) const 364 { 365 return strcmp(String(), string.String()) != 0; 366 } 367 368 inline bool 369 BString::operator!=(const char *str) const 370 { 371 return !operator==(str); 372 } 373 374 inline bool 375 operator<(const char *str, const BString &string) 376 { 377 return string > str; 378 } 379 380 inline bool 381 operator<=(const char *str, const BString &string) 382 { 383 return string >= str; 384 } 385 386 inline bool 387 operator==(const char *str, const BString &string) 388 { 389 return string == str; 390 } 391 392 inline bool 393 operator>(const char *str, const BString &string) 394 { 395 return string < str; 396 } 397 398 inline bool 399 operator>=(const char *str, const BString &string) 400 { 401 return string <= str; 402 } 403 404 inline bool 405 operator!=(const char *str, const BString &string) 406 { 407 return string != str; 408 } 409 410 #endif /* __BSTRING__ */ 411