xpcom/glue/nsStringAPI.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* vim:set ts=2 sw=2 et cindent: */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /**
michael@0 7 * This header provides wrapper classes around the frozen string API
michael@0 8 * which are roughly equivalent to the internal string classes.
michael@0 9 */
michael@0 10
michael@0 11 #ifdef MOZILLA_INTERNAL_API
michael@0 12 #error nsStringAPI.h is only usable from non-MOZILLA_INTERNAL_API code!
michael@0 13 #endif
michael@0 14
michael@0 15 #ifndef nsStringAPI_h__
michael@0 16 #define nsStringAPI_h__
michael@0 17
michael@0 18 #include "mozilla/Attributes.h"
michael@0 19 #include "mozilla/Char16.h"
michael@0 20
michael@0 21 #include "nsXPCOMStrings.h"
michael@0 22 #include "nsISupportsImpl.h"
michael@0 23 #include "prlog.h"
michael@0 24 #include "nsTArray.h"
michael@0 25
michael@0 26 /**
michael@0 27 * Comparison function for use with nsACString::Equals
michael@0 28 */
michael@0 29 NS_HIDDEN_(int32_t)
michael@0 30 CaseInsensitiveCompare(const char *a, const char *b,
michael@0 31 uint32_t length);
michael@0 32
michael@0 33 class nsAString
michael@0 34 {
michael@0 35 public:
michael@0 36 typedef char16_t char_type;
michael@0 37 typedef nsAString self_type;
michael@0 38 typedef uint32_t size_type;
michael@0 39 typedef uint32_t index_type;
michael@0 40
michael@0 41 /**
michael@0 42 * Returns the length, beginning, and end of a string in one operation.
michael@0 43 */
michael@0 44 NS_HIDDEN_(uint32_t) BeginReading(const char_type **begin,
michael@0 45 const char_type **end = nullptr) const;
michael@0 46
michael@0 47 NS_HIDDEN_(const char_type*) BeginReading() const;
michael@0 48 NS_HIDDEN_(const char_type*) EndReading() const;
michael@0 49
michael@0 50 NS_HIDDEN_(char_type) CharAt(uint32_t aPos) const
michael@0 51 {
michael@0 52 NS_ASSERTION(aPos < Length(), "Out of bounds");
michael@0 53 return BeginReading()[aPos];
michael@0 54 }
michael@0 55 NS_HIDDEN_(char_type) operator [](uint32_t aPos) const
michael@0 56 {
michael@0 57 return CharAt(aPos);
michael@0 58 }
michael@0 59 NS_HIDDEN_(char_type) First() const
michael@0 60 {
michael@0 61 return CharAt(0);
michael@0 62 }
michael@0 63 NS_HIDDEN_(char_type) Last() const
michael@0 64 {
michael@0 65 const char_type* data;
michael@0 66 uint32_t dataLen = NS_StringGetData(*this, &data);
michael@0 67 return data[dataLen - 1];
michael@0 68 }
michael@0 69
michael@0 70 /**
michael@0 71 * Get the length, begin writing, and optionally set the length of a
michael@0 72 * string all in one operation.
michael@0 73 *
michael@0 74 * @param newSize Size the string to this length. Pass UINT32_MAX
michael@0 75 * to leave the length unchanged.
michael@0 76 * @return The new length of the string, or 0 if resizing failed.
michael@0 77 */
michael@0 78 NS_HIDDEN_(uint32_t) BeginWriting(char_type **begin,
michael@0 79 char_type **end = nullptr,
michael@0 80 uint32_t newSize = UINT32_MAX);
michael@0 81
michael@0 82 NS_HIDDEN_(char_type*) BeginWriting(uint32_t = UINT32_MAX);
michael@0 83 NS_HIDDEN_(char_type*) EndWriting();
michael@0 84
michael@0 85 NS_HIDDEN_(bool) SetLength(uint32_t aLen);
michael@0 86
michael@0 87 NS_HIDDEN_(size_type) Length() const
michael@0 88 {
michael@0 89 const char_type* data;
michael@0 90 return NS_StringGetData(*this, &data);
michael@0 91 }
michael@0 92
michael@0 93 NS_HIDDEN_(bool) IsEmpty() const
michael@0 94 {
michael@0 95 return Length() == 0;
michael@0 96 }
michael@0 97
michael@0 98 NS_HIDDEN_(void) SetIsVoid(bool val)
michael@0 99 {
michael@0 100 NS_StringSetIsVoid(*this, val);
michael@0 101 }
michael@0 102 NS_HIDDEN_(bool) IsVoid() const
michael@0 103 {
michael@0 104 return NS_StringGetIsVoid(*this);
michael@0 105 }
michael@0 106
michael@0 107 NS_HIDDEN_(void) Assign(const self_type& aString)
michael@0 108 {
michael@0 109 NS_StringCopy(*this, aString);
michael@0 110 }
michael@0 111 NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 112 {
michael@0 113 NS_StringSetData(*this, aData, aLength);
michael@0 114 }
michael@0 115 NS_HIDDEN_(void) Assign(char_type aChar)
michael@0 116 {
michael@0 117 NS_StringSetData(*this, &aChar, 1);
michael@0 118 }
michael@0 119 #ifdef MOZ_USE_CHAR16_WRAPPER
michael@0 120 NS_HIDDEN_(void) Assign(char16ptr_t aData, size_type aLength = UINT32_MAX)
michael@0 121 {
michael@0 122 NS_StringSetData(*this, aData, aLength);
michael@0 123 }
michael@0 124 #endif
michael@0 125
michael@0 126 NS_HIDDEN_(void) AssignLiteral(const char *aStr);
michael@0 127 NS_HIDDEN_(void) AssignASCII(const char *aStr) { AssignLiteral(aStr); }
michael@0 128
michael@0 129 NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; }
michael@0 130 NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
michael@0 131 NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; }
michael@0 132 #ifdef MOZ_USE_CHAR16_WRAPPER
michael@0 133 NS_HIDDEN_(self_type&) operator=(char16ptr_t aPtr) { Assign(aPtr); return *this; }
michael@0 134 #endif
michael@0 135
michael@0 136 NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
michael@0 137 {
michael@0 138 NS_StringSetDataRange(*this, cutStart, cutLength, data, length);
michael@0 139 }
michael@0 140 NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
michael@0 141 {
michael@0 142 Replace(cutStart, cutLength, &c, 1);
michael@0 143 }
michael@0 144 NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
michael@0 145 {
michael@0 146 const char_type* data;
michael@0 147 uint32_t dataLen = NS_StringGetData(readable, &data);
michael@0 148 NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen);
michael@0 149 }
michael@0 150 NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos )
michael@0 151 { Replace(pos, 1, &c, 1); }
michael@0 152
michael@0 153 NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
michael@0 154 NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
michael@0 155 #ifdef MOZ_USE_CHAR16_WRAPPER
michael@0 156 NS_HIDDEN_(void) Append( char16ptr_t data, size_type length = size_type(-1) ) { Append(static_cast<const char16_t*>(data), length); }
michael@0 157 #endif
michael@0 158 NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
michael@0 159 NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr );
michael@0 160 NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { AppendLiteral(aASCIIStr); }
michael@0 161
michael@0 162 NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; }
michael@0 163 NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; }
michael@0 164 NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; }
michael@0 165
michael@0 166 NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
michael@0 167 NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
michael@0 168 NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); }
michael@0 169
michael@0 170 NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nullptr, 0); }
michael@0 171
michael@0 172 NS_HIDDEN_(void) Truncate() { SetLength(0); }
michael@0 173
michael@0 174 /**
michael@0 175 * Remove all occurences of characters in aSet from the string.
michael@0 176 */
michael@0 177 NS_HIDDEN_(void) StripChars(const char *aSet);
michael@0 178
michael@0 179 /**
michael@0 180 * Strip whitespace characters from the string.
michael@0 181 */
michael@0 182 NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); }
michael@0 183
michael@0 184 NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true,
michael@0 185 bool aTrailing = true);
michael@0 186
michael@0 187 /**
michael@0 188 * Compare strings of characters. Return 0 if the characters are equal,
michael@0 189 */
michael@0 190 typedef int32_t (*ComparatorFunc)(const char_type *a,
michael@0 191 const char_type *b,
michael@0 192 uint32_t length);
michael@0 193
michael@0 194 static NS_HIDDEN_(int32_t) DefaultComparator(const char_type *a,
michael@0 195 const char_type *b,
michael@0 196 uint32_t length);
michael@0 197
michael@0 198 NS_HIDDEN_(int32_t) Compare( const char_type *other,
michael@0 199 ComparatorFunc c = DefaultComparator ) const;
michael@0 200
michael@0 201 NS_HIDDEN_(int32_t) Compare( const self_type &other,
michael@0 202 ComparatorFunc c = DefaultComparator ) const;
michael@0 203
michael@0 204 NS_HIDDEN_(bool) Equals( const char_type *other,
michael@0 205 ComparatorFunc c = DefaultComparator ) const;
michael@0 206
michael@0 207 NS_HIDDEN_(bool) Equals( const self_type &other,
michael@0 208 ComparatorFunc c = DefaultComparator ) const;
michael@0 209
michael@0 210 NS_HIDDEN_(bool) operator < (const self_type &other) const
michael@0 211 {
michael@0 212 return Compare(other) < 0;
michael@0 213 }
michael@0 214 NS_HIDDEN_(bool) operator < (const char_type *other) const
michael@0 215 {
michael@0 216 return Compare(other) < 0;
michael@0 217 }
michael@0 218
michael@0 219 NS_HIDDEN_(bool) operator <= (const self_type &other) const
michael@0 220 {
michael@0 221 return Compare(other) <= 0;
michael@0 222 }
michael@0 223 NS_HIDDEN_(bool) operator <= (const char_type *other) const
michael@0 224 {
michael@0 225 return Compare(other) <= 0;
michael@0 226 }
michael@0 227
michael@0 228 NS_HIDDEN_(bool) operator == (const self_type &other) const
michael@0 229 {
michael@0 230 return Equals(other);
michael@0 231 }
michael@0 232 NS_HIDDEN_(bool) operator == (const char_type *other) const
michael@0 233 {
michael@0 234 return Equals(other);
michael@0 235 }
michael@0 236 #ifdef MOZ_USE_CHAR16_WRAPPER
michael@0 237 NS_HIDDEN_(bool) operator == (char16ptr_t other) const
michael@0 238 {
michael@0 239 return Equals(other);
michael@0 240 }
michael@0 241 #endif
michael@0 242
michael@0 243 NS_HIDDEN_(bool) operator >= (const self_type &other) const
michael@0 244 {
michael@0 245 return Compare(other) >= 0;
michael@0 246 }
michael@0 247 NS_HIDDEN_(bool) operator >= (const char_type *other) const
michael@0 248 {
michael@0 249 return Compare(other) >= 0;
michael@0 250 }
michael@0 251
michael@0 252 NS_HIDDEN_(bool) operator > (const self_type &other) const
michael@0 253 {
michael@0 254 return Compare(other) > 0;
michael@0 255 }
michael@0 256 NS_HIDDEN_(bool) operator > (const char_type *other) const
michael@0 257 {
michael@0 258 return Compare(other) > 0;
michael@0 259 }
michael@0 260
michael@0 261 NS_HIDDEN_(bool) operator != (const self_type &other) const
michael@0 262 {
michael@0 263 return !Equals(other);
michael@0 264 }
michael@0 265 NS_HIDDEN_(bool) operator != (const char_type *other) const
michael@0 266 {
michael@0 267 return !Equals(other);
michael@0 268 }
michael@0 269
michael@0 270 NS_HIDDEN_(bool) EqualsLiteral(const char *aASCIIString) const;
michael@0 271 NS_HIDDEN_(bool) EqualsASCII(const char *aASCIIString) const
michael@0 272 {
michael@0 273 return EqualsLiteral(aASCIIString);
michael@0 274 }
michael@0 275
michael@0 276 /**
michael@0 277 * Case-insensitive match this string to a lowercase ASCII string.
michael@0 278 */
michael@0 279 NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const;
michael@0 280
michael@0 281 /**
michael@0 282 * Find the first occurrence of aStr in this string.
michael@0 283 *
michael@0 284 * @return the offset of aStr, or -1 if not found
michael@0 285 */
michael@0 286 NS_HIDDEN_(int32_t) Find(const self_type& aStr,
michael@0 287 ComparatorFunc c = DefaultComparator) const
michael@0 288 { return Find(aStr, 0, c); }
michael@0 289
michael@0 290 /**
michael@0 291 * Find the first occurrence of aStr in this string, beginning at aOffset.
michael@0 292 *
michael@0 293 * @return the offset of aStr, or -1 if not found
michael@0 294 */
michael@0 295 NS_HIDDEN_(int32_t) Find(const self_type& aStr, uint32_t aOffset,
michael@0 296 ComparatorFunc c = DefaultComparator) const;
michael@0 297
michael@0 298 /**
michael@0 299 * Find an ASCII string within this string.
michael@0 300 *
michael@0 301 * @return the offset of aStr, or -1 if not found.
michael@0 302 */
michael@0 303 NS_HIDDEN_(int32_t) Find(const char *aStr, bool aIgnoreCase = false) const
michael@0 304 { return Find(aStr, 0, aIgnoreCase); }
michael@0 305
michael@0 306 NS_HIDDEN_(int32_t) Find(const char *aStr, uint32_t aOffset, bool aIgnoreCase = false) const;
michael@0 307
michael@0 308 /**
michael@0 309 * Find the last occurrence of aStr in this string.
michael@0 310 *
michael@0 311 * @return The offset of aStr from the beginning of the string,
michael@0 312 * or -1 if not found.
michael@0 313 */
michael@0 314 NS_HIDDEN_(int32_t) RFind(const self_type& aStr,
michael@0 315 ComparatorFunc c = DefaultComparator) const
michael@0 316 { return RFind(aStr, -1, c); }
michael@0 317
michael@0 318 /**
michael@0 319 * Find the last occurrence of aStr in this string, beginning at aOffset.
michael@0 320 *
michael@0 321 * @param aOffset the offset from the beginning of the string to begin
michael@0 322 * searching. If aOffset < 0, search from end of this string.
michael@0 323 * @return The offset of aStr from the beginning of the string,
michael@0 324 * or -1 if not found.
michael@0 325 */
michael@0 326 NS_HIDDEN_(int32_t) RFind(const self_type& aStr, int32_t aOffset,
michael@0 327 ComparatorFunc c = DefaultComparator) const;
michael@0 328
michael@0 329 /**
michael@0 330 * Find the last occurrence of an ASCII string within this string.
michael@0 331 *
michael@0 332 * @return The offset of aStr from the beginning of the string,
michael@0 333 * or -1 if not found.
michael@0 334 */
michael@0 335 NS_HIDDEN_(int32_t) RFind(const char *aStr, bool aIgnoreCase = false) const
michael@0 336 { return RFind(aStr, -1, aIgnoreCase); }
michael@0 337
michael@0 338 /**
michael@0 339 * Find the last occurrence of an ASCII string beginning at aOffset.
michael@0 340 *
michael@0 341 * @param aOffset the offset from the beginning of the string to begin
michael@0 342 * searching. If aOffset < 0, search from end of this string.
michael@0 343 * @return The offset of aStr from the beginning of the string,
michael@0 344 * or -1 if not found.
michael@0 345 */
michael@0 346 NS_HIDDEN_(int32_t) RFind(const char *aStr, int32_t aOffset, bool aIgnoreCase) const;
michael@0 347
michael@0 348 /**
michael@0 349 * Search for the offset of the first occurrence of a character in a
michael@0 350 * string.
michael@0 351 *
michael@0 352 * @param aOffset the offset from the beginning of the string to begin
michael@0 353 * searching
michael@0 354 * @return The offset of the character from the beginning of the string,
michael@0 355 * or -1 if not found.
michael@0 356 */
michael@0 357 NS_HIDDEN_(int32_t) FindChar(char_type aChar, uint32_t aOffset = 0) const;
michael@0 358
michael@0 359 /**
michael@0 360 * Search for the offset of the last occurrence of a character in a
michael@0 361 * string.
michael@0 362 *
michael@0 363 * @return The offset of the character from the beginning of the string,
michael@0 364 * or -1 if not found.
michael@0 365 */
michael@0 366 NS_HIDDEN_(int32_t) RFindChar(char_type aChar) const;
michael@0 367
michael@0 368 /**
michael@0 369 * Append a string representation of a number.
michael@0 370 */
michael@0 371 NS_HIDDEN_(void) AppendInt(int aInt, int32_t aRadix = 10);
michael@0 372
michael@0 373 #ifndef XPCOM_GLUE_AVOID_NSPR
michael@0 374 /**
michael@0 375 * Convert this string to an integer.
michael@0 376 *
michael@0 377 * @param aErrorCode pointer to contain result code.
michael@0 378 * @param aRadix must be 10 or 16
michael@0 379 */
michael@0 380 NS_HIDDEN_(int32_t) ToInteger(nsresult* aErrorCode,
michael@0 381 uint32_t aRadix = 10) const;
michael@0 382 /**
michael@0 383 * Convert this string to a 64-bit integer.
michael@0 384 *
michael@0 385 * @param aErrorCode pointer to contain result code.
michael@0 386 * @param aRadix must be 10 or 16
michael@0 387 */
michael@0 388 NS_HIDDEN_(int64_t) ToInteger64(nsresult* aErrorCode,
michael@0 389 uint32_t aRadix = 10) const;
michael@0 390 #endif // XPCOM_GLUE_AVOID_NSPR
michael@0 391
michael@0 392 protected:
michael@0 393 // Prevent people from allocating a nsAString directly.
michael@0 394 ~nsAString() {}
michael@0 395 };
michael@0 396
michael@0 397 class nsACString
michael@0 398 {
michael@0 399 public:
michael@0 400 typedef char char_type;
michael@0 401 typedef nsACString self_type;
michael@0 402 typedef uint32_t size_type;
michael@0 403 typedef uint32_t index_type;
michael@0 404
michael@0 405 /**
michael@0 406 * Returns the length, beginning, and end of a string in one operation.
michael@0 407 */
michael@0 408 NS_HIDDEN_(uint32_t) BeginReading(const char_type **begin,
michael@0 409 const char_type **end = nullptr) const;
michael@0 410
michael@0 411 NS_HIDDEN_(const char_type*) BeginReading() const;
michael@0 412 NS_HIDDEN_(const char_type*) EndReading() const;
michael@0 413
michael@0 414 NS_HIDDEN_(char_type) CharAt(uint32_t aPos) const
michael@0 415 {
michael@0 416 NS_ASSERTION(aPos < Length(), "Out of bounds");
michael@0 417 return BeginReading()[aPos];
michael@0 418 }
michael@0 419 NS_HIDDEN_(char_type) operator [](uint32_t aPos) const
michael@0 420 {
michael@0 421 return CharAt(aPos);
michael@0 422 }
michael@0 423 NS_HIDDEN_(char_type) First() const
michael@0 424 {
michael@0 425 return CharAt(0);
michael@0 426 }
michael@0 427 NS_HIDDEN_(char_type) Last() const
michael@0 428 {
michael@0 429 const char_type* data;
michael@0 430 uint32_t dataLen = NS_CStringGetData(*this, &data);
michael@0 431 return data[dataLen - 1];
michael@0 432 }
michael@0 433
michael@0 434 /**
michael@0 435 * Get the length, begin writing, and optionally set the length of a
michael@0 436 * string all in one operation.
michael@0 437 *
michael@0 438 * @param newSize Size the string to this length. Pass UINT32_MAX
michael@0 439 * to leave the length unchanged.
michael@0 440 * @return The new length of the string, or 0 if resizing failed.
michael@0 441 */
michael@0 442 NS_HIDDEN_(uint32_t) BeginWriting(char_type **begin,
michael@0 443 char_type **end = nullptr,
michael@0 444 uint32_t newSize = UINT32_MAX);
michael@0 445
michael@0 446 NS_HIDDEN_(char_type*) BeginWriting(uint32_t aLen = UINT32_MAX);
michael@0 447 NS_HIDDEN_(char_type*) EndWriting();
michael@0 448
michael@0 449 NS_HIDDEN_(bool) SetLength(uint32_t aLen);
michael@0 450
michael@0 451 NS_HIDDEN_(size_type) Length() const
michael@0 452 {
michael@0 453 const char_type* data;
michael@0 454 return NS_CStringGetData(*this, &data);
michael@0 455 }
michael@0 456
michael@0 457 NS_HIDDEN_(bool) IsEmpty() const
michael@0 458 {
michael@0 459 return Length() == 0;
michael@0 460 }
michael@0 461
michael@0 462 NS_HIDDEN_(void) SetIsVoid(bool val)
michael@0 463 {
michael@0 464 NS_CStringSetIsVoid(*this, val);
michael@0 465 }
michael@0 466 NS_HIDDEN_(bool) IsVoid() const
michael@0 467 {
michael@0 468 return NS_CStringGetIsVoid(*this);
michael@0 469 }
michael@0 470
michael@0 471 NS_HIDDEN_(void) Assign(const self_type& aString)
michael@0 472 {
michael@0 473 NS_CStringCopy(*this, aString);
michael@0 474 }
michael@0 475 NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 476 {
michael@0 477 NS_CStringSetData(*this, aData, aLength);
michael@0 478 }
michael@0 479 NS_HIDDEN_(void) Assign(char_type aChar)
michael@0 480 {
michael@0 481 NS_CStringSetData(*this, &aChar, 1);
michael@0 482 }
michael@0 483 NS_HIDDEN_(void) AssignLiteral(const char_type *aData)
michael@0 484 {
michael@0 485 Assign(aData);
michael@0 486 }
michael@0 487 NS_HIDDEN_(void) AssignASCII(const char_type *aData)
michael@0 488 {
michael@0 489 Assign(aData);
michael@0 490 }
michael@0 491
michael@0 492 NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; }
michael@0 493 NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
michael@0 494 NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; }
michael@0 495
michael@0 496 NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
michael@0 497 {
michael@0 498 NS_CStringSetDataRange(*this, cutStart, cutLength, data, length);
michael@0 499 }
michael@0 500 NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
michael@0 501 {
michael@0 502 Replace(cutStart, cutLength, &c, 1);
michael@0 503 }
michael@0 504 NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
michael@0 505 {
michael@0 506 const char_type* data;
michael@0 507 uint32_t dataLen = NS_CStringGetData(readable, &data);
michael@0 508 NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen);
michael@0 509 }
michael@0 510 NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos )
michael@0 511 { Replace(pos, 1, &c, 1); }
michael@0 512
michael@0 513 NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
michael@0 514 NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
michael@0 515 NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
michael@0 516 NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr ) { Append(aASCIIStr); }
michael@0 517 NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { Append(aASCIIStr); }
michael@0 518
michael@0 519 NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; }
michael@0 520 NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; }
michael@0 521 NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; }
michael@0 522
michael@0 523 NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
michael@0 524 NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
michael@0 525 NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); }
michael@0 526
michael@0 527 NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nullptr, 0); }
michael@0 528
michael@0 529 NS_HIDDEN_(void) Truncate() { SetLength(0); }
michael@0 530
michael@0 531 /**
michael@0 532 * Remove all occurences of characters in aSet from the string.
michael@0 533 */
michael@0 534 NS_HIDDEN_(void) StripChars(const char *aSet);
michael@0 535
michael@0 536 /**
michael@0 537 * Strip whitespace characters from the string.
michael@0 538 */
michael@0 539 NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); }
michael@0 540
michael@0 541 NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true,
michael@0 542 bool aTrailing = true);
michael@0 543
michael@0 544 /**
michael@0 545 * Compare strings of characters. Return 0 if the characters are equal,
michael@0 546 */
michael@0 547 typedef int32_t (*ComparatorFunc)(const char_type *a,
michael@0 548 const char_type *b,
michael@0 549 uint32_t length);
michael@0 550
michael@0 551 static NS_HIDDEN_(int32_t) DefaultComparator(const char_type *a,
michael@0 552 const char_type *b,
michael@0 553 uint32_t length);
michael@0 554
michael@0 555 NS_HIDDEN_(int32_t) Compare( const char_type *other,
michael@0 556 ComparatorFunc c = DefaultComparator ) const;
michael@0 557
michael@0 558 NS_HIDDEN_(int32_t) Compare( const self_type &other,
michael@0 559 ComparatorFunc c = DefaultComparator ) const;
michael@0 560
michael@0 561 NS_HIDDEN_(bool) Equals( const char_type *other,
michael@0 562 ComparatorFunc c = DefaultComparator ) const;
michael@0 563
michael@0 564 NS_HIDDEN_(bool) Equals( const self_type &other,
michael@0 565 ComparatorFunc c = DefaultComparator ) const;
michael@0 566
michael@0 567 NS_HIDDEN_(bool) operator < (const self_type &other) const
michael@0 568 {
michael@0 569 return Compare(other) < 0;
michael@0 570 }
michael@0 571 NS_HIDDEN_(bool) operator < (const char_type *other) const
michael@0 572 {
michael@0 573 return Compare(other) < 0;
michael@0 574 }
michael@0 575
michael@0 576 NS_HIDDEN_(bool) operator <= (const self_type &other) const
michael@0 577 {
michael@0 578 return Compare(other) <= 0;
michael@0 579 }
michael@0 580 NS_HIDDEN_(bool) operator <= (const char_type *other) const
michael@0 581 {
michael@0 582 return Compare(other) <= 0;
michael@0 583 }
michael@0 584
michael@0 585 NS_HIDDEN_(bool) operator == (const self_type &other) const
michael@0 586 {
michael@0 587 return Equals(other);
michael@0 588 }
michael@0 589 NS_HIDDEN_(bool) operator == (const char_type *other) const
michael@0 590 {
michael@0 591 return Equals(other);
michael@0 592 }
michael@0 593
michael@0 594 NS_HIDDEN_(bool) operator >= (const self_type &other) const
michael@0 595 {
michael@0 596 return Compare(other) >= 0;
michael@0 597 }
michael@0 598 NS_HIDDEN_(bool) operator >= (const char_type *other) const
michael@0 599 {
michael@0 600 return Compare(other) >= 0;
michael@0 601 }
michael@0 602
michael@0 603 NS_HIDDEN_(bool) operator > (const self_type &other) const
michael@0 604 {
michael@0 605 return Compare(other) > 0;
michael@0 606 }
michael@0 607 NS_HIDDEN_(bool) operator > (const char_type *other) const
michael@0 608 {
michael@0 609 return Compare(other) > 0;
michael@0 610 }
michael@0 611
michael@0 612 NS_HIDDEN_(bool) operator != (const self_type &other) const
michael@0 613 {
michael@0 614 return !Equals(other);
michael@0 615 }
michael@0 616 NS_HIDDEN_(bool) operator != (const char_type *other) const
michael@0 617 {
michael@0 618 return !Equals(other);
michael@0 619 }
michael@0 620
michael@0 621 NS_HIDDEN_(bool) EqualsLiteral( const char_type *other ) const
michael@0 622 {
michael@0 623 return Equals(other);
michael@0 624 }
michael@0 625 NS_HIDDEN_(bool) EqualsASCII( const char_type *other ) const
michael@0 626 {
michael@0 627 return Equals(other);
michael@0 628 }
michael@0 629
michael@0 630 /**
michael@0 631 * Case-insensitive match this string to a lowercase ASCII string.
michael@0 632 */
michael@0 633 NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const
michael@0 634 {
michael@0 635 return Equals(aASCIIString, CaseInsensitiveCompare);
michael@0 636 }
michael@0 637
michael@0 638 /**
michael@0 639 * Find the first occurrence of aStr in this string.
michael@0 640 *
michael@0 641 * @return the offset of aStr, or -1 if not found
michael@0 642 */
michael@0 643 NS_HIDDEN_(int32_t) Find(const self_type& aStr,
michael@0 644 ComparatorFunc c = DefaultComparator) const
michael@0 645 { return Find(aStr, 0, c); }
michael@0 646
michael@0 647 /**
michael@0 648 * Find the first occurrence of aStr in this string, beginning at aOffset.
michael@0 649 *
michael@0 650 * @return the offset of aStr, or -1 if not found
michael@0 651 */
michael@0 652 NS_HIDDEN_(int32_t) Find(const self_type& aStr, uint32_t aOffset,
michael@0 653 ComparatorFunc c = DefaultComparator) const;
michael@0 654
michael@0 655 /**
michael@0 656 * Find the first occurrence of aStr in this string.
michael@0 657 *
michael@0 658 * @return the offset of aStr, or -1 if not found
michael@0 659 */
michael@0 660 NS_HIDDEN_(int32_t) Find(const char_type *aStr,
michael@0 661 ComparatorFunc c = DefaultComparator) const;
michael@0 662
michael@0 663 NS_HIDDEN_(int32_t) Find(const char_type *aStr, uint32_t aLen,
michael@0 664 ComparatorFunc c = DefaultComparator) const;
michael@0 665
michael@0 666 /**
michael@0 667 * Find the last occurrence of aStr in this string.
michael@0 668 *
michael@0 669 * @return The offset of the character from the beginning of the string,
michael@0 670 * or -1 if not found.
michael@0 671 */
michael@0 672 NS_HIDDEN_(int32_t) RFind(const self_type& aStr,
michael@0 673 ComparatorFunc c = DefaultComparator) const
michael@0 674 { return RFind(aStr, -1, c); }
michael@0 675
michael@0 676 /**
michael@0 677 * Find the last occurrence of aStr in this string, beginning at aOffset.
michael@0 678 *
michael@0 679 * @param aOffset the offset from the beginning of the string to begin
michael@0 680 * searching. If aOffset < 0, search from end of this string.
michael@0 681 * @return The offset of aStr from the beginning of the string,
michael@0 682 * or -1 if not found.
michael@0 683 */
michael@0 684 NS_HIDDEN_(int32_t) RFind(const self_type& aStr, int32_t aOffset,
michael@0 685 ComparatorFunc c = DefaultComparator) const;
michael@0 686
michael@0 687 /**
michael@0 688 * Find the last occurrence of aStr in this string.
michael@0 689 *
michael@0 690 * @return The offset of aStr from the beginning of the string,
michael@0 691 * or -1 if not found.
michael@0 692 */
michael@0 693 NS_HIDDEN_(int32_t) RFind(const char_type *aStr,
michael@0 694 ComparatorFunc c = DefaultComparator) const;
michael@0 695
michael@0 696 /**
michael@0 697 * Find the last occurrence of an ASCII string in this string,
michael@0 698 * beginning at aOffset.
michael@0 699 *
michael@0 700 * @param aLen is the length of aStr
michael@0 701 * @return The offset of aStr from the beginning of the string,
michael@0 702 * or -1 if not found.
michael@0 703 */
michael@0 704 NS_HIDDEN_(int32_t) RFind(const char_type *aStr, int32_t aLen,
michael@0 705 ComparatorFunc c = DefaultComparator) const;
michael@0 706
michael@0 707 /**
michael@0 708 * Search for the offset of the first occurrence of a character in a
michael@0 709 * string.
michael@0 710 *
michael@0 711 * @param aOffset the offset from the beginning of the string to begin
michael@0 712 * searching
michael@0 713 * @return The offset of the character from the beginning of the string,
michael@0 714 * or -1 if not found.
michael@0 715 */
michael@0 716 NS_HIDDEN_(int32_t) FindChar(char_type aChar, uint32_t aOffset = 0) const;
michael@0 717
michael@0 718 /**
michael@0 719 * Search for the offset of the last occurrence of a character in a
michael@0 720 * string.
michael@0 721 *
michael@0 722 * @return The offset of the character from the beginning of the string,
michael@0 723 * or -1 if not found.
michael@0 724 */
michael@0 725 NS_HIDDEN_(int32_t) RFindChar(char_type aChar) const;
michael@0 726
michael@0 727 /**
michael@0 728 * Append a string representation of a number.
michael@0 729 */
michael@0 730 NS_HIDDEN_(void) AppendInt(int aInt, int32_t aRadix = 10);
michael@0 731
michael@0 732 #ifndef XPCOM_GLUE_AVOID_NSPR
michael@0 733 /**
michael@0 734 * Convert this string to an integer.
michael@0 735 *
michael@0 736 * @param aErrorCode pointer to contain result code.
michael@0 737 * @param aRadix must be 10 or 16
michael@0 738 */
michael@0 739 NS_HIDDEN_(int32_t) ToInteger(nsresult* aErrorCode,
michael@0 740 uint32_t aRadix = 10) const;
michael@0 741 /**
michael@0 742 * Convert this string to a 64-bit integer.
michael@0 743 *
michael@0 744 * @param aErrorCode pointer to contain result code.
michael@0 745 * @param aRadix must be 10 or 16
michael@0 746 */
michael@0 747 NS_HIDDEN_(int64_t) ToInteger64(nsresult* aErrorCode,
michael@0 748 uint32_t aRadix = 10) const;
michael@0 749 #endif // XPCOM_GLUE_AVOID_NSPR
michael@0 750
michael@0 751 protected:
michael@0 752 // Prevent people from allocating a nsAString directly.
michael@0 753 ~nsACString() {}
michael@0 754 };
michael@0 755
michael@0 756 /* ------------------------------------------------------------------------- */
michael@0 757
michael@0 758 /**
michael@0 759 * Below we define nsStringContainer and nsCStringContainer. These classes
michael@0 760 * have unspecified structure. In most cases, your code should use
michael@0 761 * nsString/nsCString instead of these classes; if you prefer C-style
michael@0 762 * programming, then look no further.
michael@0 763 */
michael@0 764
michael@0 765 class nsStringContainer : public nsAString,
michael@0 766 private nsStringContainer_base
michael@0 767 {
michael@0 768 };
michael@0 769
michael@0 770 class nsCStringContainer : public nsACString,
michael@0 771 private nsStringContainer_base
michael@0 772 {
michael@0 773 };
michael@0 774
michael@0 775 /**
michael@0 776 * The following classes are C++ helper classes that make the frozen string
michael@0 777 * API easier to use.
michael@0 778 */
michael@0 779
michael@0 780 /**
michael@0 781 * Rename symbols to avoid conflicting with internal versions.
michael@0 782 */
michael@0 783 #define nsString nsString_external
michael@0 784 #define nsCString nsCString_external
michael@0 785 #define nsDependentString nsDependentString_external
michael@0 786 #define nsDependentCString nsDependentCString_external
michael@0 787 #define NS_ConvertASCIItoUTF16 NS_ConvertASCIItoUTF16_external
michael@0 788 #define NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUTF16_external
michael@0 789 #define NS_ConvertUTF16toUTF8 NS_ConvertUTF16toUTF8_external
michael@0 790 #define NS_LossyConvertUTF16toASCII NS_LossyConvertUTF16toASCII_external
michael@0 791 #define nsGetterCopies nsGetterCopies_external
michael@0 792 #define nsCGetterCopies nsCGetterCopies_external
michael@0 793 #define nsDependentSubstring nsDependentSubstring_external
michael@0 794 #define nsDependentCSubstring nsDependentCSubstring_external
michael@0 795
michael@0 796 /**
michael@0 797 * basic strings
michael@0 798 */
michael@0 799
michael@0 800 class nsString : public nsStringContainer
michael@0 801 {
michael@0 802 public:
michael@0 803 typedef nsString self_type;
michael@0 804 typedef nsAString abstract_string_type;
michael@0 805
michael@0 806 nsString()
michael@0 807 {
michael@0 808 NS_StringContainerInit(*this);
michael@0 809 }
michael@0 810
michael@0 811 nsString(const self_type& aString)
michael@0 812 {
michael@0 813 NS_StringContainerInit(*this);
michael@0 814 NS_StringCopy(*this, aString);
michael@0 815 }
michael@0 816
michael@0 817 explicit
michael@0 818 nsString(const abstract_string_type& aReadable)
michael@0 819 {
michael@0 820 NS_StringContainerInit(*this);
michael@0 821 NS_StringCopy(*this, aReadable);
michael@0 822 }
michael@0 823
michael@0 824 explicit
michael@0 825 nsString(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 826 {
michael@0 827 NS_StringContainerInit2(*this, aData, aLength, 0);
michael@0 828 }
michael@0 829
michael@0 830 #ifdef MOZ_USE_CHAR16_WRAPPER
michael@0 831 explicit
michael@0 832 nsString(char16ptr_t aData, size_type aLength = UINT32_MAX)
michael@0 833 : nsString(static_cast<const char16_t*>(aData), aLength) {}
michael@0 834 #endif
michael@0 835
michael@0 836 ~nsString()
michael@0 837 {
michael@0 838 NS_StringContainerFinish(*this);
michael@0 839 }
michael@0 840
michael@0 841 char16ptr_t get() const
michael@0 842 {
michael@0 843 return char16ptr_t(BeginReading());
michael@0 844 }
michael@0 845
michael@0 846 self_type& operator=(const self_type& aString) { Assign(aString); return *this; }
michael@0 847 self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
michael@0 848 self_type& operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
michael@0 849 self_type& operator=(char_type aChar) { Assign(aChar); return *this; }
michael@0 850
michael@0 851 void Adopt(const char_type *aData, size_type aLength = UINT32_MAX)
michael@0 852 {
michael@0 853 NS_StringContainerFinish(*this);
michael@0 854 NS_StringContainerInit2(*this, aData, aLength,
michael@0 855 NS_STRING_CONTAINER_INIT_ADOPT);
michael@0 856 }
michael@0 857
michael@0 858 protected:
michael@0 859
michael@0 860 nsString(const char_type* aData, size_type aLength, uint32_t aFlags)
michael@0 861 {
michael@0 862 NS_StringContainerInit2(*this, aData, aLength, aFlags);
michael@0 863 }
michael@0 864 };
michael@0 865
michael@0 866 class nsCString : public nsCStringContainer
michael@0 867 {
michael@0 868 public:
michael@0 869 typedef nsCString self_type;
michael@0 870 typedef nsACString abstract_string_type;
michael@0 871
michael@0 872 nsCString()
michael@0 873 {
michael@0 874 NS_CStringContainerInit(*this);
michael@0 875 }
michael@0 876
michael@0 877 nsCString(const self_type& aString)
michael@0 878 {
michael@0 879 NS_CStringContainerInit(*this);
michael@0 880 NS_CStringCopy(*this, aString);
michael@0 881 }
michael@0 882
michael@0 883 explicit
michael@0 884 nsCString(const abstract_string_type& aReadable)
michael@0 885 {
michael@0 886 NS_CStringContainerInit(*this);
michael@0 887 NS_CStringCopy(*this, aReadable);
michael@0 888 }
michael@0 889
michael@0 890 explicit
michael@0 891 nsCString(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 892 {
michael@0 893 NS_CStringContainerInit(*this);
michael@0 894 NS_CStringSetData(*this, aData, aLength);
michael@0 895 }
michael@0 896
michael@0 897 ~nsCString()
michael@0 898 {
michael@0 899 NS_CStringContainerFinish(*this);
michael@0 900 }
michael@0 901
michael@0 902 const char_type* get() const
michael@0 903 {
michael@0 904 return BeginReading();
michael@0 905 }
michael@0 906
michael@0 907 self_type& operator=(const self_type& aString) { Assign(aString); return *this; }
michael@0 908 self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
michael@0 909 self_type& operator=(const char_type* aPtr) { Assign(aPtr); return *this; }
michael@0 910 self_type& operator=(char_type aChar) { Assign(aChar); return *this; }
michael@0 911
michael@0 912 void Adopt(const char_type *aData, size_type aLength = UINT32_MAX)
michael@0 913 {
michael@0 914 NS_CStringContainerFinish(*this);
michael@0 915 NS_CStringContainerInit2(*this, aData, aLength,
michael@0 916 NS_CSTRING_CONTAINER_INIT_ADOPT);
michael@0 917 }
michael@0 918
michael@0 919 protected:
michael@0 920
michael@0 921 nsCString(const char_type* aData, size_type aLength, uint32_t aFlags)
michael@0 922 {
michael@0 923 NS_CStringContainerInit2(*this, aData, aLength, aFlags);
michael@0 924 }
michael@0 925 };
michael@0 926
michael@0 927
michael@0 928 /**
michael@0 929 * dependent strings
michael@0 930 */
michael@0 931
michael@0 932 class nsDependentString : public nsString
michael@0 933 {
michael@0 934 public:
michael@0 935 typedef nsDependentString self_type;
michael@0 936
michael@0 937 nsDependentString() {}
michael@0 938
michael@0 939 explicit
michael@0 940 nsDependentString(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 941 : nsString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
michael@0 942 {}
michael@0 943
michael@0 944 #ifdef MOZ_USE_CHAR16_WRAPPER
michael@0 945 explicit
michael@0 946 nsDependentString(char16ptr_t aData, size_type aLength = UINT32_MAX)
michael@0 947 : nsDependentString(static_cast<const char16_t*>(aData), aLength)
michael@0 948 {}
michael@0 949 #endif
michael@0 950
michael@0 951 void Rebind(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 952 {
michael@0 953 NS_StringContainerFinish(*this);
michael@0 954 NS_StringContainerInit2(*this, aData, aLength,
michael@0 955 NS_STRING_CONTAINER_INIT_DEPEND);
michael@0 956 }
michael@0 957
michael@0 958 private:
michael@0 959 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 960 };
michael@0 961
michael@0 962 class nsDependentCString : public nsCString
michael@0 963 {
michael@0 964 public:
michael@0 965 typedef nsDependentCString self_type;
michael@0 966
michael@0 967 nsDependentCString() {}
michael@0 968
michael@0 969 explicit
michael@0 970 nsDependentCString(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 971 : nsCString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
michael@0 972 {}
michael@0 973
michael@0 974 void Rebind(const char_type* aData, size_type aLength = UINT32_MAX)
michael@0 975 {
michael@0 976 NS_CStringContainerFinish(*this);
michael@0 977 NS_CStringContainerInit2(*this, aData, aLength,
michael@0 978 NS_CSTRING_CONTAINER_INIT_DEPEND);
michael@0 979 }
michael@0 980
michael@0 981 private:
michael@0 982 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 983 };
michael@0 984
michael@0 985
michael@0 986 /**
michael@0 987 * conversion classes
michael@0 988 */
michael@0 989
michael@0 990 inline void
michael@0 991 CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest)
michael@0 992 {
michael@0 993 NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_UTF8, aDest);
michael@0 994 }
michael@0 995
michael@0 996 inline void
michael@0 997 CopyUTF8toUTF16(const nsACString& aSource, nsAString& aDest)
michael@0 998 {
michael@0 999 NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_UTF8, aDest);
michael@0 1000 }
michael@0 1001
michael@0 1002 inline void
michael@0 1003 LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest)
michael@0 1004 {
michael@0 1005 NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_ASCII, aDest);
michael@0 1006 }
michael@0 1007
michael@0 1008 inline void
michael@0 1009 CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest)
michael@0 1010 {
michael@0 1011 NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_ASCII, aDest);
michael@0 1012 }
michael@0 1013
michael@0 1014 NS_COM_GLUE char*
michael@0 1015 ToNewUTF8String(const nsAString& aSource);
michael@0 1016
michael@0 1017 class NS_ConvertASCIItoUTF16 : public nsString
michael@0 1018 {
michael@0 1019 public:
michael@0 1020 typedef NS_ConvertASCIItoUTF16 self_type;
michael@0 1021
michael@0 1022 explicit
michael@0 1023 NS_ConvertASCIItoUTF16(const nsACString& aStr)
michael@0 1024 {
michael@0 1025 NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_ASCII, *this);
michael@0 1026 }
michael@0 1027
michael@0 1028 explicit
michael@0 1029 NS_ConvertASCIItoUTF16(const char* aData, uint32_t aLength = UINT32_MAX)
michael@0 1030 {
michael@0 1031 NS_CStringToUTF16(nsDependentCString(aData, aLength),
michael@0 1032 NS_CSTRING_ENCODING_ASCII, *this);
michael@0 1033 }
michael@0 1034
michael@0 1035 private:
michael@0 1036 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 1037 };
michael@0 1038
michael@0 1039 class NS_ConvertUTF8toUTF16 : public nsString
michael@0 1040 {
michael@0 1041 public:
michael@0 1042 typedef NS_ConvertUTF8toUTF16 self_type;
michael@0 1043
michael@0 1044 explicit
michael@0 1045 NS_ConvertUTF8toUTF16(const nsACString& aStr)
michael@0 1046 {
michael@0 1047 NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_UTF8, *this);
michael@0 1048 }
michael@0 1049
michael@0 1050 explicit
michael@0 1051 NS_ConvertUTF8toUTF16(const char* aData, uint32_t aLength = UINT32_MAX)
michael@0 1052 {
michael@0 1053 NS_CStringToUTF16(nsDependentCString(aData, aLength),
michael@0 1054 NS_CSTRING_ENCODING_UTF8, *this);
michael@0 1055 }
michael@0 1056
michael@0 1057 private:
michael@0 1058 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 1059 };
michael@0 1060
michael@0 1061 class NS_ConvertUTF16toUTF8 : public nsCString
michael@0 1062 {
michael@0 1063 public:
michael@0 1064 typedef NS_ConvertUTF16toUTF8 self_type;
michael@0 1065
michael@0 1066 explicit
michael@0 1067 NS_ConvertUTF16toUTF8(const nsAString& aStr)
michael@0 1068 {
michael@0 1069 NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_UTF8, *this);
michael@0 1070 }
michael@0 1071
michael@0 1072 explicit
michael@0 1073 NS_ConvertUTF16toUTF8(const char16_t* aData, uint32_t aLength = UINT32_MAX)
michael@0 1074 {
michael@0 1075 NS_UTF16ToCString(nsDependentString(aData, aLength),
michael@0 1076 NS_CSTRING_ENCODING_UTF8, *this);
michael@0 1077 }
michael@0 1078
michael@0 1079 private:
michael@0 1080 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 1081 };
michael@0 1082
michael@0 1083 class NS_LossyConvertUTF16toASCII : public nsCString
michael@0 1084 {
michael@0 1085 public:
michael@0 1086 typedef NS_LossyConvertUTF16toASCII self_type;
michael@0 1087
michael@0 1088 explicit
michael@0 1089 NS_LossyConvertUTF16toASCII(const nsAString& aStr)
michael@0 1090 {
michael@0 1091 NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_ASCII, *this);
michael@0 1092 }
michael@0 1093
michael@0 1094 explicit
michael@0 1095 NS_LossyConvertUTF16toASCII(const char16_t* aData, uint32_t aLength = UINT32_MAX)
michael@0 1096 {
michael@0 1097 NS_UTF16ToCString(nsDependentString(aData, aLength),
michael@0 1098 NS_CSTRING_ENCODING_ASCII, *this);
michael@0 1099 }
michael@0 1100
michael@0 1101 private:
michael@0 1102 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 1103 };
michael@0 1104
michael@0 1105
michael@0 1106 /**
michael@0 1107 * literal strings
michael@0 1108 */
michael@0 1109 static_assert(sizeof(char16_t) == 2, "size of char16_t must be 2");
michael@0 1110 static_assert(char16_t(-1) > char16_t(0), "char16_t must be unsigned");
michael@0 1111
michael@0 1112 #define NS_MULTILINE_LITERAL_STRING(s) nsDependentString(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1))
michael@0 1113 #define NS_MULTILINE_LITERAL_STRING_INIT(n,s) n(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1))
michael@0 1114 #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) const nsDependentString n(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1))
michael@0 1115 typedef nsDependentString nsLiteralString;
michael@0 1116
michael@0 1117 /* Check that char16_t is unsigned */
michael@0 1118 static_assert(char16_t(-1) > char16_t(0), "char16_t is by definition an unsigned type");
michael@0 1119
michael@0 1120 #define NS_LITERAL_STRING(s) static_cast<const nsString&>(NS_MULTILINE_LITERAL_STRING(MOZ_UTF16(s)))
michael@0 1121 #define NS_LITERAL_STRING_INIT(n,s) NS_MULTILINE_LITERAL_STRING_INIT(n, MOZ_UTF16(s))
michael@0 1122 #define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n, MOZ_UTF16(s))
michael@0 1123
michael@0 1124 #define NS_LITERAL_CSTRING(s) static_cast<const nsDependentCString&>(nsDependentCString(s, uint32_t(sizeof(s)-1)))
michael@0 1125 #define NS_LITERAL_CSTRING_INIT(n,s) n(s, uint32_t(sizeof(s)-1))
michael@0 1126 #define NS_NAMED_LITERAL_CSTRING(n,s) const nsDependentCString n(s, uint32_t(sizeof(s)-1))
michael@0 1127
michael@0 1128 typedef nsDependentCString nsLiteralCString;
michael@0 1129
michael@0 1130
michael@0 1131 /**
michael@0 1132 * getter_Copies support
michael@0 1133 *
michael@0 1134 * NS_IMETHOD GetBlah(char16_t**);
michael@0 1135 *
michael@0 1136 * void some_function()
michael@0 1137 * {
michael@0 1138 * nsString blah;
michael@0 1139 * GetBlah(getter_Copies(blah));
michael@0 1140 * // ...
michael@0 1141 * }
michael@0 1142 */
michael@0 1143
michael@0 1144 class nsGetterCopies
michael@0 1145 {
michael@0 1146 public:
michael@0 1147 typedef char16_t char_type;
michael@0 1148
michael@0 1149 nsGetterCopies(nsString& aStr)
michael@0 1150 : mString(aStr), mData(nullptr)
michael@0 1151 {}
michael@0 1152
michael@0 1153 ~nsGetterCopies()
michael@0 1154 {
michael@0 1155 mString.Adopt(mData);
michael@0 1156 }
michael@0 1157
michael@0 1158 operator char_type**()
michael@0 1159 {
michael@0 1160 return &mData;
michael@0 1161 }
michael@0 1162
michael@0 1163 private:
michael@0 1164 nsString& mString;
michael@0 1165 char_type* mData;
michael@0 1166 };
michael@0 1167
michael@0 1168 inline nsGetterCopies
michael@0 1169 getter_Copies(nsString& aString)
michael@0 1170 {
michael@0 1171 return nsGetterCopies(aString);
michael@0 1172 }
michael@0 1173
michael@0 1174 class nsCGetterCopies
michael@0 1175 {
michael@0 1176 public:
michael@0 1177 typedef char char_type;
michael@0 1178
michael@0 1179 nsCGetterCopies(nsCString& aStr)
michael@0 1180 : mString(aStr), mData(nullptr)
michael@0 1181 {}
michael@0 1182
michael@0 1183 ~nsCGetterCopies()
michael@0 1184 {
michael@0 1185 mString.Adopt(mData);
michael@0 1186 }
michael@0 1187
michael@0 1188 operator char_type**()
michael@0 1189 {
michael@0 1190 return &mData;
michael@0 1191 }
michael@0 1192
michael@0 1193 private:
michael@0 1194 nsCString& mString;
michael@0 1195 char_type* mData;
michael@0 1196 };
michael@0 1197
michael@0 1198 inline nsCGetterCopies
michael@0 1199 getter_Copies(nsCString& aString)
michael@0 1200 {
michael@0 1201 return nsCGetterCopies(aString);
michael@0 1202 }
michael@0 1203
michael@0 1204
michael@0 1205 /**
michael@0 1206 * substrings
michael@0 1207 */
michael@0 1208
michael@0 1209 class NS_COM_GLUE nsDependentSubstring : public nsStringContainer
michael@0 1210 {
michael@0 1211 public:
michael@0 1212 typedef nsDependentSubstring self_type;
michael@0 1213 typedef nsAString abstract_string_type;
michael@0 1214
michael@0 1215 ~nsDependentSubstring()
michael@0 1216 {
michael@0 1217 NS_StringContainerFinish(*this);
michael@0 1218 }
michael@0 1219
michael@0 1220 nsDependentSubstring()
michael@0 1221 {
michael@0 1222 NS_StringContainerInit(*this);
michael@0 1223 }
michael@0 1224
michael@0 1225 nsDependentSubstring(const char_type *aStart, uint32_t aLength)
michael@0 1226 {
michael@0 1227 NS_StringContainerInit2(*this, aStart, aLength,
michael@0 1228 NS_STRING_CONTAINER_INIT_DEPEND |
michael@0 1229 NS_STRING_CONTAINER_INIT_SUBSTRING);
michael@0 1230 }
michael@0 1231
michael@0 1232 nsDependentSubstring(const abstract_string_type& aStr,
michael@0 1233 uint32_t aStartPos);
michael@0 1234 nsDependentSubstring(const abstract_string_type& aStr,
michael@0 1235 uint32_t aStartPos, uint32_t aLength);
michael@0 1236
michael@0 1237 void Rebind(const char_type *aStart, uint32_t aLength)
michael@0 1238 {
michael@0 1239 NS_StringContainerFinish(*this);
michael@0 1240 NS_StringContainerInit2(*this, aStart, aLength,
michael@0 1241 NS_STRING_CONTAINER_INIT_DEPEND |
michael@0 1242 NS_STRING_CONTAINER_INIT_SUBSTRING);
michael@0 1243 }
michael@0 1244
michael@0 1245 private:
michael@0 1246 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 1247 };
michael@0 1248
michael@0 1249 class NS_COM_GLUE nsDependentCSubstring : public nsCStringContainer
michael@0 1250 {
michael@0 1251 public:
michael@0 1252 typedef nsDependentCSubstring self_type;
michael@0 1253 typedef nsACString abstract_string_type;
michael@0 1254
michael@0 1255 ~nsDependentCSubstring()
michael@0 1256 {
michael@0 1257 NS_CStringContainerFinish(*this);
michael@0 1258 }
michael@0 1259
michael@0 1260 nsDependentCSubstring()
michael@0 1261 {
michael@0 1262 NS_CStringContainerInit(*this);
michael@0 1263 }
michael@0 1264
michael@0 1265 nsDependentCSubstring(const char_type *aStart, uint32_t aLength)
michael@0 1266 {
michael@0 1267 NS_CStringContainerInit2(*this, aStart, aLength,
michael@0 1268 NS_CSTRING_CONTAINER_INIT_DEPEND |
michael@0 1269 NS_CSTRING_CONTAINER_INIT_SUBSTRING);
michael@0 1270 }
michael@0 1271
michael@0 1272 nsDependentCSubstring(const abstract_string_type& aStr,
michael@0 1273 uint32_t aStartPos);
michael@0 1274 nsDependentCSubstring(const abstract_string_type& aStr,
michael@0 1275 uint32_t aStartPos, uint32_t aLength);
michael@0 1276
michael@0 1277 void Rebind(const char_type *aStart, uint32_t aLength)
michael@0 1278 {
michael@0 1279 NS_CStringContainerFinish(*this);
michael@0 1280 NS_CStringContainerInit2(*this, aStart, aLength,
michael@0 1281 NS_CSTRING_CONTAINER_INIT_DEPEND |
michael@0 1282 NS_CSTRING_CONTAINER_INIT_SUBSTRING);
michael@0 1283 }
michael@0 1284
michael@0 1285 private:
michael@0 1286 self_type& operator=(const self_type& aString) MOZ_DELETE;
michael@0 1287 };
michael@0 1288
michael@0 1289
michael@0 1290 /**
michael@0 1291 * Various nsDependentC?Substring constructor functions
michael@0 1292 */
michael@0 1293
michael@0 1294 // char16_t
michael@0 1295 inline const nsDependentSubstring
michael@0 1296 Substring( const nsAString& str, uint32_t startPos )
michael@0 1297 {
michael@0 1298 return nsDependentSubstring(str, startPos);
michael@0 1299 }
michael@0 1300
michael@0 1301 inline const nsDependentSubstring
michael@0 1302 Substring( const nsAString& str, uint32_t startPos, uint32_t length )
michael@0 1303 {
michael@0 1304 return nsDependentSubstring(str, startPos, length);
michael@0 1305 }
michael@0 1306
michael@0 1307 inline const nsDependentSubstring
michael@0 1308 Substring( const char16_t* start, const char16_t* end )
michael@0 1309 {
michael@0 1310 NS_ABORT_IF_FALSE(uint32_t(end - start) == uintptr_t(end - start), "string too long");
michael@0 1311 return nsDependentSubstring(start, uint32_t(end - start));
michael@0 1312 }
michael@0 1313
michael@0 1314 inline const nsDependentSubstring
michael@0 1315 Substring( const char16_t* start, uint32_t length )
michael@0 1316 {
michael@0 1317 return nsDependentSubstring(start, length);
michael@0 1318 }
michael@0 1319
michael@0 1320 inline const nsDependentSubstring
michael@0 1321 StringHead( const nsAString& str, uint32_t count )
michael@0 1322 {
michael@0 1323 return nsDependentSubstring(str, 0, count);
michael@0 1324 }
michael@0 1325
michael@0 1326 inline const nsDependentSubstring
michael@0 1327 StringTail( const nsAString& str, uint32_t count )
michael@0 1328 {
michael@0 1329 return nsDependentSubstring(str, str.Length() - count, count);
michael@0 1330 }
michael@0 1331
michael@0 1332 // char
michael@0 1333 inline const nsDependentCSubstring
michael@0 1334 Substring( const nsACString& str, uint32_t startPos )
michael@0 1335 {
michael@0 1336 return nsDependentCSubstring(str, startPos);
michael@0 1337 }
michael@0 1338
michael@0 1339 inline const nsDependentCSubstring
michael@0 1340 Substring( const nsACString& str, uint32_t startPos, uint32_t length )
michael@0 1341 {
michael@0 1342 return nsDependentCSubstring(str, startPos, length);
michael@0 1343 }
michael@0 1344
michael@0 1345 inline
michael@0 1346 const nsDependentCSubstring
michael@0 1347 Substring( const char* start, const char* end )
michael@0 1348 {
michael@0 1349 NS_ABORT_IF_FALSE(uint32_t(end - start) == uintptr_t(end - start), "string too long");
michael@0 1350 return nsDependentCSubstring(start, uint32_t(end - start));
michael@0 1351 }
michael@0 1352
michael@0 1353 inline
michael@0 1354 const nsDependentCSubstring
michael@0 1355 Substring( const char* start, uint32_t length )
michael@0 1356 {
michael@0 1357 return nsDependentCSubstring(start, length);
michael@0 1358 }
michael@0 1359
michael@0 1360 inline const nsDependentCSubstring
michael@0 1361 StringHead( const nsACString& str, uint32_t count )
michael@0 1362 {
michael@0 1363 return nsDependentCSubstring(str, 0, count);
michael@0 1364 }
michael@0 1365
michael@0 1366 inline const nsDependentCSubstring
michael@0 1367 StringTail( const nsACString& str, uint32_t count )
michael@0 1368 {
michael@0 1369 return nsDependentCSubstring(str, str.Length() - count, count);
michael@0 1370 }
michael@0 1371
michael@0 1372
michael@0 1373 inline bool
michael@0 1374 StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring,
michael@0 1375 nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator)
michael@0 1376 {
michael@0 1377 return aSubstring.Length() <= aSource.Length() &&
michael@0 1378 StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
michael@0 1379 }
michael@0 1380
michael@0 1381 inline bool
michael@0 1382 StringEndsWith(const nsAString& aSource, const nsAString& aSubstring,
michael@0 1383 nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator)
michael@0 1384 {
michael@0 1385 return aSubstring.Length() <= aSource.Length() &&
michael@0 1386 StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
michael@0 1387 }
michael@0 1388
michael@0 1389 inline bool
michael@0 1390 StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring,
michael@0 1391 nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator)
michael@0 1392 {
michael@0 1393 return aSubstring.Length() <= aSource.Length() &&
michael@0 1394 StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
michael@0 1395 }
michael@0 1396
michael@0 1397 inline bool
michael@0 1398 StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
michael@0 1399 nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator)
michael@0 1400 {
michael@0 1401 return aSubstring.Length() <= aSource.Length() &&
michael@0 1402 StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator);
michael@0 1403 }
michael@0 1404
michael@0 1405 /**
michael@0 1406 * Trim whitespace from the beginning and end of a string; then compress
michael@0 1407 * remaining runs of whitespace characters to a single space.
michael@0 1408 */
michael@0 1409 NS_HIDDEN_(void)
michael@0 1410 CompressWhitespace(nsAString& aString);
michael@0 1411
michael@0 1412 #define EmptyCString() nsCString()
michael@0 1413 #define EmptyString() nsString()
michael@0 1414
michael@0 1415 /**
michael@0 1416 * Convert an ASCII string to all upper/lowercase (a-z,A-Z only). As a bonus,
michael@0 1417 * returns the string length.
michael@0 1418 */
michael@0 1419 NS_HIDDEN_(uint32_t)
michael@0 1420 ToLowerCase(nsACString& aStr);
michael@0 1421
michael@0 1422 NS_HIDDEN_(uint32_t)
michael@0 1423 ToUpperCase(nsACString& aStr);
michael@0 1424
michael@0 1425 NS_HIDDEN_(uint32_t)
michael@0 1426 ToLowerCase(const nsACString& aSrc, nsACString& aDest);
michael@0 1427
michael@0 1428 NS_HIDDEN_(uint32_t)
michael@0 1429 ToUpperCase(const nsACString& aSrc, nsACString& aDest);
michael@0 1430
michael@0 1431 /**
michael@0 1432 * The following declarations are *deprecated*, and are included here only
michael@0 1433 * to make porting from existing code that doesn't use the frozen string API
michael@0 1434 * easier. They may disappear in the future.
michael@0 1435 */
michael@0 1436
michael@0 1437 inline char*
michael@0 1438 ToNewCString(const nsACString& aStr)
michael@0 1439 {
michael@0 1440 return NS_CStringCloneData(aStr);
michael@0 1441 }
michael@0 1442
michael@0 1443 inline char16_t*
michael@0 1444 ToNewUnicode(const nsAString& aStr)
michael@0 1445 {
michael@0 1446 return NS_StringCloneData(aStr);
michael@0 1447 }
michael@0 1448
michael@0 1449 typedef nsString PromiseFlatString;
michael@0 1450 typedef nsCString PromiseFlatCString;
michael@0 1451
michael@0 1452 typedef nsCString nsAutoCString;
michael@0 1453 typedef nsString nsAutoString;
michael@0 1454
michael@0 1455 NS_HIDDEN_(bool) ParseString(const nsACString& aAstring, char aDelimiter,
michael@0 1456 nsTArray<nsCString>& aArray);
michael@0 1457
michael@0 1458 #endif // nsStringAPI_h__

mercurial