1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsStringAPI.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1458 @@ 1.4 +/* vim:set ts=2 sw=2 et cindent: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/** 1.10 + * This header provides wrapper classes around the frozen string API 1.11 + * which are roughly equivalent to the internal string classes. 1.12 + */ 1.13 + 1.14 +#ifdef MOZILLA_INTERNAL_API 1.15 +#error nsStringAPI.h is only usable from non-MOZILLA_INTERNAL_API code! 1.16 +#endif 1.17 + 1.18 +#ifndef nsStringAPI_h__ 1.19 +#define nsStringAPI_h__ 1.20 + 1.21 +#include "mozilla/Attributes.h" 1.22 +#include "mozilla/Char16.h" 1.23 + 1.24 +#include "nsXPCOMStrings.h" 1.25 +#include "nsISupportsImpl.h" 1.26 +#include "prlog.h" 1.27 +#include "nsTArray.h" 1.28 + 1.29 +/** 1.30 + * Comparison function for use with nsACString::Equals 1.31 + */ 1.32 +NS_HIDDEN_(int32_t) 1.33 +CaseInsensitiveCompare(const char *a, const char *b, 1.34 + uint32_t length); 1.35 + 1.36 +class nsAString 1.37 +{ 1.38 +public: 1.39 + typedef char16_t char_type; 1.40 + typedef nsAString self_type; 1.41 + typedef uint32_t size_type; 1.42 + typedef uint32_t index_type; 1.43 + 1.44 + /** 1.45 + * Returns the length, beginning, and end of a string in one operation. 1.46 + */ 1.47 + NS_HIDDEN_(uint32_t) BeginReading(const char_type **begin, 1.48 + const char_type **end = nullptr) const; 1.49 + 1.50 + NS_HIDDEN_(const char_type*) BeginReading() const; 1.51 + NS_HIDDEN_(const char_type*) EndReading() const; 1.52 + 1.53 + NS_HIDDEN_(char_type) CharAt(uint32_t aPos) const 1.54 + { 1.55 + NS_ASSERTION(aPos < Length(), "Out of bounds"); 1.56 + return BeginReading()[aPos]; 1.57 + } 1.58 + NS_HIDDEN_(char_type) operator [](uint32_t aPos) const 1.59 + { 1.60 + return CharAt(aPos); 1.61 + } 1.62 + NS_HIDDEN_(char_type) First() const 1.63 + { 1.64 + return CharAt(0); 1.65 + } 1.66 + NS_HIDDEN_(char_type) Last() const 1.67 + { 1.68 + const char_type* data; 1.69 + uint32_t dataLen = NS_StringGetData(*this, &data); 1.70 + return data[dataLen - 1]; 1.71 + } 1.72 + 1.73 + /** 1.74 + * Get the length, begin writing, and optionally set the length of a 1.75 + * string all in one operation. 1.76 + * 1.77 + * @param newSize Size the string to this length. Pass UINT32_MAX 1.78 + * to leave the length unchanged. 1.79 + * @return The new length of the string, or 0 if resizing failed. 1.80 + */ 1.81 + NS_HIDDEN_(uint32_t) BeginWriting(char_type **begin, 1.82 + char_type **end = nullptr, 1.83 + uint32_t newSize = UINT32_MAX); 1.84 + 1.85 + NS_HIDDEN_(char_type*) BeginWriting(uint32_t = UINT32_MAX); 1.86 + NS_HIDDEN_(char_type*) EndWriting(); 1.87 + 1.88 + NS_HIDDEN_(bool) SetLength(uint32_t aLen); 1.89 + 1.90 + NS_HIDDEN_(size_type) Length() const 1.91 + { 1.92 + const char_type* data; 1.93 + return NS_StringGetData(*this, &data); 1.94 + } 1.95 + 1.96 + NS_HIDDEN_(bool) IsEmpty() const 1.97 + { 1.98 + return Length() == 0; 1.99 + } 1.100 + 1.101 + NS_HIDDEN_(void) SetIsVoid(bool val) 1.102 + { 1.103 + NS_StringSetIsVoid(*this, val); 1.104 + } 1.105 + NS_HIDDEN_(bool) IsVoid() const 1.106 + { 1.107 + return NS_StringGetIsVoid(*this); 1.108 + } 1.109 + 1.110 + NS_HIDDEN_(void) Assign(const self_type& aString) 1.111 + { 1.112 + NS_StringCopy(*this, aString); 1.113 + } 1.114 + NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = UINT32_MAX) 1.115 + { 1.116 + NS_StringSetData(*this, aData, aLength); 1.117 + } 1.118 + NS_HIDDEN_(void) Assign(char_type aChar) 1.119 + { 1.120 + NS_StringSetData(*this, &aChar, 1); 1.121 + } 1.122 +#ifdef MOZ_USE_CHAR16_WRAPPER 1.123 + NS_HIDDEN_(void) Assign(char16ptr_t aData, size_type aLength = UINT32_MAX) 1.124 + { 1.125 + NS_StringSetData(*this, aData, aLength); 1.126 + } 1.127 +#endif 1.128 + 1.129 + NS_HIDDEN_(void) AssignLiteral(const char *aStr); 1.130 + NS_HIDDEN_(void) AssignASCII(const char *aStr) { AssignLiteral(aStr); } 1.131 + 1.132 + NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; } 1.133 + NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; } 1.134 + NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; } 1.135 +#ifdef MOZ_USE_CHAR16_WRAPPER 1.136 + NS_HIDDEN_(self_type&) operator=(char16ptr_t aPtr) { Assign(aPtr); return *this; } 1.137 +#endif 1.138 + 1.139 + NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) ) 1.140 + { 1.141 + NS_StringSetDataRange(*this, cutStart, cutLength, data, length); 1.142 + } 1.143 + NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c ) 1.144 + { 1.145 + Replace(cutStart, cutLength, &c, 1); 1.146 + } 1.147 + NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable ) 1.148 + { 1.149 + const char_type* data; 1.150 + uint32_t dataLen = NS_StringGetData(readable, &data); 1.151 + NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen); 1.152 + } 1.153 + NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos ) 1.154 + { Replace(pos, 1, &c, 1); } 1.155 + 1.156 + NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); } 1.157 + NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); } 1.158 +#ifdef MOZ_USE_CHAR16_WRAPPER 1.159 + NS_HIDDEN_(void) Append( char16ptr_t data, size_type length = size_type(-1) ) { Append(static_cast<const char16_t*>(data), length); } 1.160 +#endif 1.161 + NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); } 1.162 + NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr ); 1.163 + NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { AppendLiteral(aASCIIStr); } 1.164 + 1.165 + NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; } 1.166 + NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; } 1.167 + NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; } 1.168 + 1.169 + NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); } 1.170 + NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); } 1.171 + NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); } 1.172 + 1.173 + NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nullptr, 0); } 1.174 + 1.175 + NS_HIDDEN_(void) Truncate() { SetLength(0); } 1.176 + 1.177 + /** 1.178 + * Remove all occurences of characters in aSet from the string. 1.179 + */ 1.180 + NS_HIDDEN_(void) StripChars(const char *aSet); 1.181 + 1.182 + /** 1.183 + * Strip whitespace characters from the string. 1.184 + */ 1.185 + NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); } 1.186 + 1.187 + NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true, 1.188 + bool aTrailing = true); 1.189 + 1.190 + /** 1.191 + * Compare strings of characters. Return 0 if the characters are equal, 1.192 + */ 1.193 + typedef int32_t (*ComparatorFunc)(const char_type *a, 1.194 + const char_type *b, 1.195 + uint32_t length); 1.196 + 1.197 + static NS_HIDDEN_(int32_t) DefaultComparator(const char_type *a, 1.198 + const char_type *b, 1.199 + uint32_t length); 1.200 + 1.201 + NS_HIDDEN_(int32_t) Compare( const char_type *other, 1.202 + ComparatorFunc c = DefaultComparator ) const; 1.203 + 1.204 + NS_HIDDEN_(int32_t) Compare( const self_type &other, 1.205 + ComparatorFunc c = DefaultComparator ) const; 1.206 + 1.207 + NS_HIDDEN_(bool) Equals( const char_type *other, 1.208 + ComparatorFunc c = DefaultComparator ) const; 1.209 + 1.210 + NS_HIDDEN_(bool) Equals( const self_type &other, 1.211 + ComparatorFunc c = DefaultComparator ) const; 1.212 + 1.213 + NS_HIDDEN_(bool) operator < (const self_type &other) const 1.214 + { 1.215 + return Compare(other) < 0; 1.216 + } 1.217 + NS_HIDDEN_(bool) operator < (const char_type *other) const 1.218 + { 1.219 + return Compare(other) < 0; 1.220 + } 1.221 + 1.222 + NS_HIDDEN_(bool) operator <= (const self_type &other) const 1.223 + { 1.224 + return Compare(other) <= 0; 1.225 + } 1.226 + NS_HIDDEN_(bool) operator <= (const char_type *other) const 1.227 + { 1.228 + return Compare(other) <= 0; 1.229 + } 1.230 + 1.231 + NS_HIDDEN_(bool) operator == (const self_type &other) const 1.232 + { 1.233 + return Equals(other); 1.234 + } 1.235 + NS_HIDDEN_(bool) operator == (const char_type *other) const 1.236 + { 1.237 + return Equals(other); 1.238 + } 1.239 +#ifdef MOZ_USE_CHAR16_WRAPPER 1.240 + NS_HIDDEN_(bool) operator == (char16ptr_t other) const 1.241 + { 1.242 + return Equals(other); 1.243 + } 1.244 +#endif 1.245 + 1.246 + NS_HIDDEN_(bool) operator >= (const self_type &other) const 1.247 + { 1.248 + return Compare(other) >= 0; 1.249 + } 1.250 + NS_HIDDEN_(bool) operator >= (const char_type *other) const 1.251 + { 1.252 + return Compare(other) >= 0; 1.253 + } 1.254 + 1.255 + NS_HIDDEN_(bool) operator > (const self_type &other) const 1.256 + { 1.257 + return Compare(other) > 0; 1.258 + } 1.259 + NS_HIDDEN_(bool) operator > (const char_type *other) const 1.260 + { 1.261 + return Compare(other) > 0; 1.262 + } 1.263 + 1.264 + NS_HIDDEN_(bool) operator != (const self_type &other) const 1.265 + { 1.266 + return !Equals(other); 1.267 + } 1.268 + NS_HIDDEN_(bool) operator != (const char_type *other) const 1.269 + { 1.270 + return !Equals(other); 1.271 + } 1.272 + 1.273 + NS_HIDDEN_(bool) EqualsLiteral(const char *aASCIIString) const; 1.274 + NS_HIDDEN_(bool) EqualsASCII(const char *aASCIIString) const 1.275 + { 1.276 + return EqualsLiteral(aASCIIString); 1.277 + } 1.278 + 1.279 + /** 1.280 + * Case-insensitive match this string to a lowercase ASCII string. 1.281 + */ 1.282 + NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const; 1.283 + 1.284 + /** 1.285 + * Find the first occurrence of aStr in this string. 1.286 + * 1.287 + * @return the offset of aStr, or -1 if not found 1.288 + */ 1.289 + NS_HIDDEN_(int32_t) Find(const self_type& aStr, 1.290 + ComparatorFunc c = DefaultComparator) const 1.291 + { return Find(aStr, 0, c); } 1.292 + 1.293 + /** 1.294 + * Find the first occurrence of aStr in this string, beginning at aOffset. 1.295 + * 1.296 + * @return the offset of aStr, or -1 if not found 1.297 + */ 1.298 + NS_HIDDEN_(int32_t) Find(const self_type& aStr, uint32_t aOffset, 1.299 + ComparatorFunc c = DefaultComparator) const; 1.300 + 1.301 + /** 1.302 + * Find an ASCII string within this string. 1.303 + * 1.304 + * @return the offset of aStr, or -1 if not found. 1.305 + */ 1.306 + NS_HIDDEN_(int32_t) Find(const char *aStr, bool aIgnoreCase = false) const 1.307 + { return Find(aStr, 0, aIgnoreCase); } 1.308 + 1.309 + NS_HIDDEN_(int32_t) Find(const char *aStr, uint32_t aOffset, bool aIgnoreCase = false) const; 1.310 + 1.311 + /** 1.312 + * Find the last occurrence of aStr in this string. 1.313 + * 1.314 + * @return The offset of aStr from the beginning of the string, 1.315 + * or -1 if not found. 1.316 + */ 1.317 + NS_HIDDEN_(int32_t) RFind(const self_type& aStr, 1.318 + ComparatorFunc c = DefaultComparator) const 1.319 + { return RFind(aStr, -1, c); } 1.320 + 1.321 + /** 1.322 + * Find the last occurrence of aStr in this string, beginning at aOffset. 1.323 + * 1.324 + * @param aOffset the offset from the beginning of the string to begin 1.325 + * searching. If aOffset < 0, search from end of this string. 1.326 + * @return The offset of aStr from the beginning of the string, 1.327 + * or -1 if not found. 1.328 + */ 1.329 + NS_HIDDEN_(int32_t) RFind(const self_type& aStr, int32_t aOffset, 1.330 + ComparatorFunc c = DefaultComparator) const; 1.331 + 1.332 + /** 1.333 + * Find the last occurrence of an ASCII string within this string. 1.334 + * 1.335 + * @return The offset of aStr from the beginning of the string, 1.336 + * or -1 if not found. 1.337 + */ 1.338 + NS_HIDDEN_(int32_t) RFind(const char *aStr, bool aIgnoreCase = false) const 1.339 + { return RFind(aStr, -1, aIgnoreCase); } 1.340 + 1.341 + /** 1.342 + * Find the last occurrence of an ASCII string beginning at aOffset. 1.343 + * 1.344 + * @param aOffset the offset from the beginning of the string to begin 1.345 + * searching. If aOffset < 0, search from end of this string. 1.346 + * @return The offset of aStr from the beginning of the string, 1.347 + * or -1 if not found. 1.348 + */ 1.349 + NS_HIDDEN_(int32_t) RFind(const char *aStr, int32_t aOffset, bool aIgnoreCase) const; 1.350 + 1.351 + /** 1.352 + * Search for the offset of the first occurrence of a character in a 1.353 + * string. 1.354 + * 1.355 + * @param aOffset the offset from the beginning of the string to begin 1.356 + * searching 1.357 + * @return The offset of the character from the beginning of the string, 1.358 + * or -1 if not found. 1.359 + */ 1.360 + NS_HIDDEN_(int32_t) FindChar(char_type aChar, uint32_t aOffset = 0) const; 1.361 + 1.362 + /** 1.363 + * Search for the offset of the last occurrence of a character in a 1.364 + * string. 1.365 + * 1.366 + * @return The offset of the character from the beginning of the string, 1.367 + * or -1 if not found. 1.368 + */ 1.369 + NS_HIDDEN_(int32_t) RFindChar(char_type aChar) const; 1.370 + 1.371 + /** 1.372 + * Append a string representation of a number. 1.373 + */ 1.374 + NS_HIDDEN_(void) AppendInt(int aInt, int32_t aRadix = 10); 1.375 + 1.376 +#ifndef XPCOM_GLUE_AVOID_NSPR 1.377 + /** 1.378 + * Convert this string to an integer. 1.379 + * 1.380 + * @param aErrorCode pointer to contain result code. 1.381 + * @param aRadix must be 10 or 16 1.382 + */ 1.383 + NS_HIDDEN_(int32_t) ToInteger(nsresult* aErrorCode, 1.384 + uint32_t aRadix = 10) const; 1.385 + /** 1.386 + * Convert this string to a 64-bit integer. 1.387 + * 1.388 + * @param aErrorCode pointer to contain result code. 1.389 + * @param aRadix must be 10 or 16 1.390 + */ 1.391 + NS_HIDDEN_(int64_t) ToInteger64(nsresult* aErrorCode, 1.392 + uint32_t aRadix = 10) const; 1.393 +#endif // XPCOM_GLUE_AVOID_NSPR 1.394 + 1.395 +protected: 1.396 + // Prevent people from allocating a nsAString directly. 1.397 + ~nsAString() {} 1.398 +}; 1.399 + 1.400 +class nsACString 1.401 +{ 1.402 +public: 1.403 + typedef char char_type; 1.404 + typedef nsACString self_type; 1.405 + typedef uint32_t size_type; 1.406 + typedef uint32_t index_type; 1.407 + 1.408 + /** 1.409 + * Returns the length, beginning, and end of a string in one operation. 1.410 + */ 1.411 + NS_HIDDEN_(uint32_t) BeginReading(const char_type **begin, 1.412 + const char_type **end = nullptr) const; 1.413 + 1.414 + NS_HIDDEN_(const char_type*) BeginReading() const; 1.415 + NS_HIDDEN_(const char_type*) EndReading() const; 1.416 + 1.417 + NS_HIDDEN_(char_type) CharAt(uint32_t aPos) const 1.418 + { 1.419 + NS_ASSERTION(aPos < Length(), "Out of bounds"); 1.420 + return BeginReading()[aPos]; 1.421 + } 1.422 + NS_HIDDEN_(char_type) operator [](uint32_t aPos) const 1.423 + { 1.424 + return CharAt(aPos); 1.425 + } 1.426 + NS_HIDDEN_(char_type) First() const 1.427 + { 1.428 + return CharAt(0); 1.429 + } 1.430 + NS_HIDDEN_(char_type) Last() const 1.431 + { 1.432 + const char_type* data; 1.433 + uint32_t dataLen = NS_CStringGetData(*this, &data); 1.434 + return data[dataLen - 1]; 1.435 + } 1.436 + 1.437 + /** 1.438 + * Get the length, begin writing, and optionally set the length of a 1.439 + * string all in one operation. 1.440 + * 1.441 + * @param newSize Size the string to this length. Pass UINT32_MAX 1.442 + * to leave the length unchanged. 1.443 + * @return The new length of the string, or 0 if resizing failed. 1.444 + */ 1.445 + NS_HIDDEN_(uint32_t) BeginWriting(char_type **begin, 1.446 + char_type **end = nullptr, 1.447 + uint32_t newSize = UINT32_MAX); 1.448 + 1.449 + NS_HIDDEN_(char_type*) BeginWriting(uint32_t aLen = UINT32_MAX); 1.450 + NS_HIDDEN_(char_type*) EndWriting(); 1.451 + 1.452 + NS_HIDDEN_(bool) SetLength(uint32_t aLen); 1.453 + 1.454 + NS_HIDDEN_(size_type) Length() const 1.455 + { 1.456 + const char_type* data; 1.457 + return NS_CStringGetData(*this, &data); 1.458 + } 1.459 + 1.460 + NS_HIDDEN_(bool) IsEmpty() const 1.461 + { 1.462 + return Length() == 0; 1.463 + } 1.464 + 1.465 + NS_HIDDEN_(void) SetIsVoid(bool val) 1.466 + { 1.467 + NS_CStringSetIsVoid(*this, val); 1.468 + } 1.469 + NS_HIDDEN_(bool) IsVoid() const 1.470 + { 1.471 + return NS_CStringGetIsVoid(*this); 1.472 + } 1.473 + 1.474 + NS_HIDDEN_(void) Assign(const self_type& aString) 1.475 + { 1.476 + NS_CStringCopy(*this, aString); 1.477 + } 1.478 + NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = UINT32_MAX) 1.479 + { 1.480 + NS_CStringSetData(*this, aData, aLength); 1.481 + } 1.482 + NS_HIDDEN_(void) Assign(char_type aChar) 1.483 + { 1.484 + NS_CStringSetData(*this, &aChar, 1); 1.485 + } 1.486 + NS_HIDDEN_(void) AssignLiteral(const char_type *aData) 1.487 + { 1.488 + Assign(aData); 1.489 + } 1.490 + NS_HIDDEN_(void) AssignASCII(const char_type *aData) 1.491 + { 1.492 + Assign(aData); 1.493 + } 1.494 + 1.495 + NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; } 1.496 + NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; } 1.497 + NS_HIDDEN_(self_type&) operator=(char_type aChar) { Assign(aChar); return *this; } 1.498 + 1.499 + NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) ) 1.500 + { 1.501 + NS_CStringSetDataRange(*this, cutStart, cutLength, data, length); 1.502 + } 1.503 + NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c ) 1.504 + { 1.505 + Replace(cutStart, cutLength, &c, 1); 1.506 + } 1.507 + NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable ) 1.508 + { 1.509 + const char_type* data; 1.510 + uint32_t dataLen = NS_CStringGetData(readable, &data); 1.511 + NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen); 1.512 + } 1.513 + NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos ) 1.514 + { Replace(pos, 1, &c, 1); } 1.515 + 1.516 + NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); } 1.517 + NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); } 1.518 + NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); } 1.519 + NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr ) { Append(aASCIIStr); } 1.520 + NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { Append(aASCIIStr); } 1.521 + 1.522 + NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; } 1.523 + NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; } 1.524 + NS_HIDDEN_(self_type&) operator+=( const self_type& readable ) { Append(readable); return *this; } 1.525 + 1.526 + NS_HIDDEN_(void) Insert( char_type c, index_type pos ) { Replace(pos, 0, c); } 1.527 + NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); } 1.528 + NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos ) { Replace(pos, 0, readable); } 1.529 + 1.530 + NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, nullptr, 0); } 1.531 + 1.532 + NS_HIDDEN_(void) Truncate() { SetLength(0); } 1.533 + 1.534 + /** 1.535 + * Remove all occurences of characters in aSet from the string. 1.536 + */ 1.537 + NS_HIDDEN_(void) StripChars(const char *aSet); 1.538 + 1.539 + /** 1.540 + * Strip whitespace characters from the string. 1.541 + */ 1.542 + NS_HIDDEN_(void) StripWhitespace() { StripChars("\b\t\r\n "); } 1.543 + 1.544 + NS_HIDDEN_(void) Trim(const char *aSet, bool aLeading = true, 1.545 + bool aTrailing = true); 1.546 + 1.547 + /** 1.548 + * Compare strings of characters. Return 0 if the characters are equal, 1.549 + */ 1.550 + typedef int32_t (*ComparatorFunc)(const char_type *a, 1.551 + const char_type *b, 1.552 + uint32_t length); 1.553 + 1.554 + static NS_HIDDEN_(int32_t) DefaultComparator(const char_type *a, 1.555 + const char_type *b, 1.556 + uint32_t length); 1.557 + 1.558 + NS_HIDDEN_(int32_t) Compare( const char_type *other, 1.559 + ComparatorFunc c = DefaultComparator ) const; 1.560 + 1.561 + NS_HIDDEN_(int32_t) Compare( const self_type &other, 1.562 + ComparatorFunc c = DefaultComparator ) const; 1.563 + 1.564 + NS_HIDDEN_(bool) Equals( const char_type *other, 1.565 + ComparatorFunc c = DefaultComparator ) const; 1.566 + 1.567 + NS_HIDDEN_(bool) Equals( const self_type &other, 1.568 + ComparatorFunc c = DefaultComparator ) const; 1.569 + 1.570 + NS_HIDDEN_(bool) operator < (const self_type &other) const 1.571 + { 1.572 + return Compare(other) < 0; 1.573 + } 1.574 + NS_HIDDEN_(bool) operator < (const char_type *other) const 1.575 + { 1.576 + return Compare(other) < 0; 1.577 + } 1.578 + 1.579 + NS_HIDDEN_(bool) operator <= (const self_type &other) const 1.580 + { 1.581 + return Compare(other) <= 0; 1.582 + } 1.583 + NS_HIDDEN_(bool) operator <= (const char_type *other) const 1.584 + { 1.585 + return Compare(other) <= 0; 1.586 + } 1.587 + 1.588 + NS_HIDDEN_(bool) operator == (const self_type &other) const 1.589 + { 1.590 + return Equals(other); 1.591 + } 1.592 + NS_HIDDEN_(bool) operator == (const char_type *other) const 1.593 + { 1.594 + return Equals(other); 1.595 + } 1.596 + 1.597 + NS_HIDDEN_(bool) operator >= (const self_type &other) const 1.598 + { 1.599 + return Compare(other) >= 0; 1.600 + } 1.601 + NS_HIDDEN_(bool) operator >= (const char_type *other) const 1.602 + { 1.603 + return Compare(other) >= 0; 1.604 + } 1.605 + 1.606 + NS_HIDDEN_(bool) operator > (const self_type &other) const 1.607 + { 1.608 + return Compare(other) > 0; 1.609 + } 1.610 + NS_HIDDEN_(bool) operator > (const char_type *other) const 1.611 + { 1.612 + return Compare(other) > 0; 1.613 + } 1.614 + 1.615 + NS_HIDDEN_(bool) operator != (const self_type &other) const 1.616 + { 1.617 + return !Equals(other); 1.618 + } 1.619 + NS_HIDDEN_(bool) operator != (const char_type *other) const 1.620 + { 1.621 + return !Equals(other); 1.622 + } 1.623 + 1.624 + NS_HIDDEN_(bool) EqualsLiteral( const char_type *other ) const 1.625 + { 1.626 + return Equals(other); 1.627 + } 1.628 + NS_HIDDEN_(bool) EqualsASCII( const char_type *other ) const 1.629 + { 1.630 + return Equals(other); 1.631 + } 1.632 + 1.633 + /** 1.634 + * Case-insensitive match this string to a lowercase ASCII string. 1.635 + */ 1.636 + NS_HIDDEN_(bool) LowerCaseEqualsLiteral(const char *aASCIIString) const 1.637 + { 1.638 + return Equals(aASCIIString, CaseInsensitiveCompare); 1.639 + } 1.640 + 1.641 + /** 1.642 + * Find the first occurrence of aStr in this string. 1.643 + * 1.644 + * @return the offset of aStr, or -1 if not found 1.645 + */ 1.646 + NS_HIDDEN_(int32_t) Find(const self_type& aStr, 1.647 + ComparatorFunc c = DefaultComparator) const 1.648 + { return Find(aStr, 0, c); } 1.649 + 1.650 + /** 1.651 + * Find the first occurrence of aStr in this string, beginning at aOffset. 1.652 + * 1.653 + * @return the offset of aStr, or -1 if not found 1.654 + */ 1.655 + NS_HIDDEN_(int32_t) Find(const self_type& aStr, uint32_t aOffset, 1.656 + ComparatorFunc c = DefaultComparator) const; 1.657 + 1.658 + /** 1.659 + * Find the first occurrence of aStr in this string. 1.660 + * 1.661 + * @return the offset of aStr, or -1 if not found 1.662 + */ 1.663 + NS_HIDDEN_(int32_t) Find(const char_type *aStr, 1.664 + ComparatorFunc c = DefaultComparator) const; 1.665 + 1.666 + NS_HIDDEN_(int32_t) Find(const char_type *aStr, uint32_t aLen, 1.667 + ComparatorFunc c = DefaultComparator) const; 1.668 + 1.669 + /** 1.670 + * Find the last occurrence of aStr in this string. 1.671 + * 1.672 + * @return The offset of the character from the beginning of the string, 1.673 + * or -1 if not found. 1.674 + */ 1.675 + NS_HIDDEN_(int32_t) RFind(const self_type& aStr, 1.676 + ComparatorFunc c = DefaultComparator) const 1.677 + { return RFind(aStr, -1, c); } 1.678 + 1.679 + /** 1.680 + * Find the last occurrence of aStr in this string, beginning at aOffset. 1.681 + * 1.682 + * @param aOffset the offset from the beginning of the string to begin 1.683 + * searching. If aOffset < 0, search from end of this string. 1.684 + * @return The offset of aStr from the beginning of the string, 1.685 + * or -1 if not found. 1.686 + */ 1.687 + NS_HIDDEN_(int32_t) RFind(const self_type& aStr, int32_t aOffset, 1.688 + ComparatorFunc c = DefaultComparator) const; 1.689 + 1.690 + /** 1.691 + * Find the last occurrence of aStr in this string. 1.692 + * 1.693 + * @return The offset of aStr from the beginning of the string, 1.694 + * or -1 if not found. 1.695 + */ 1.696 + NS_HIDDEN_(int32_t) RFind(const char_type *aStr, 1.697 + ComparatorFunc c = DefaultComparator) const; 1.698 + 1.699 + /** 1.700 + * Find the last occurrence of an ASCII string in this string, 1.701 + * beginning at aOffset. 1.702 + * 1.703 + * @param aLen is the length of aStr 1.704 + * @return The offset of aStr from the beginning of the string, 1.705 + * or -1 if not found. 1.706 + */ 1.707 + NS_HIDDEN_(int32_t) RFind(const char_type *aStr, int32_t aLen, 1.708 + ComparatorFunc c = DefaultComparator) const; 1.709 + 1.710 + /** 1.711 + * Search for the offset of the first occurrence of a character in a 1.712 + * string. 1.713 + * 1.714 + * @param aOffset the offset from the beginning of the string to begin 1.715 + * searching 1.716 + * @return The offset of the character from the beginning of the string, 1.717 + * or -1 if not found. 1.718 + */ 1.719 + NS_HIDDEN_(int32_t) FindChar(char_type aChar, uint32_t aOffset = 0) const; 1.720 + 1.721 + /** 1.722 + * Search for the offset of the last occurrence of a character in a 1.723 + * string. 1.724 + * 1.725 + * @return The offset of the character from the beginning of the string, 1.726 + * or -1 if not found. 1.727 + */ 1.728 + NS_HIDDEN_(int32_t) RFindChar(char_type aChar) const; 1.729 + 1.730 + /** 1.731 + * Append a string representation of a number. 1.732 + */ 1.733 + NS_HIDDEN_(void) AppendInt(int aInt, int32_t aRadix = 10); 1.734 + 1.735 +#ifndef XPCOM_GLUE_AVOID_NSPR 1.736 + /** 1.737 + * Convert this string to an integer. 1.738 + * 1.739 + * @param aErrorCode pointer to contain result code. 1.740 + * @param aRadix must be 10 or 16 1.741 + */ 1.742 + NS_HIDDEN_(int32_t) ToInteger(nsresult* aErrorCode, 1.743 + uint32_t aRadix = 10) const; 1.744 + /** 1.745 + * Convert this string to a 64-bit integer. 1.746 + * 1.747 + * @param aErrorCode pointer to contain result code. 1.748 + * @param aRadix must be 10 or 16 1.749 + */ 1.750 + NS_HIDDEN_(int64_t) ToInteger64(nsresult* aErrorCode, 1.751 + uint32_t aRadix = 10) const; 1.752 +#endif // XPCOM_GLUE_AVOID_NSPR 1.753 + 1.754 +protected: 1.755 + // Prevent people from allocating a nsAString directly. 1.756 + ~nsACString() {} 1.757 +}; 1.758 + 1.759 +/* ------------------------------------------------------------------------- */ 1.760 + 1.761 +/** 1.762 + * Below we define nsStringContainer and nsCStringContainer. These classes 1.763 + * have unspecified structure. In most cases, your code should use 1.764 + * nsString/nsCString instead of these classes; if you prefer C-style 1.765 + * programming, then look no further. 1.766 + */ 1.767 + 1.768 +class nsStringContainer : public nsAString, 1.769 + private nsStringContainer_base 1.770 +{ 1.771 +}; 1.772 + 1.773 +class nsCStringContainer : public nsACString, 1.774 + private nsStringContainer_base 1.775 +{ 1.776 +}; 1.777 + 1.778 +/** 1.779 + * The following classes are C++ helper classes that make the frozen string 1.780 + * API easier to use. 1.781 + */ 1.782 + 1.783 +/** 1.784 + * Rename symbols to avoid conflicting with internal versions. 1.785 + */ 1.786 +#define nsString nsString_external 1.787 +#define nsCString nsCString_external 1.788 +#define nsDependentString nsDependentString_external 1.789 +#define nsDependentCString nsDependentCString_external 1.790 +#define NS_ConvertASCIItoUTF16 NS_ConvertASCIItoUTF16_external 1.791 +#define NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUTF16_external 1.792 +#define NS_ConvertUTF16toUTF8 NS_ConvertUTF16toUTF8_external 1.793 +#define NS_LossyConvertUTF16toASCII NS_LossyConvertUTF16toASCII_external 1.794 +#define nsGetterCopies nsGetterCopies_external 1.795 +#define nsCGetterCopies nsCGetterCopies_external 1.796 +#define nsDependentSubstring nsDependentSubstring_external 1.797 +#define nsDependentCSubstring nsDependentCSubstring_external 1.798 + 1.799 +/** 1.800 + * basic strings 1.801 + */ 1.802 + 1.803 +class nsString : public nsStringContainer 1.804 +{ 1.805 +public: 1.806 + typedef nsString self_type; 1.807 + typedef nsAString abstract_string_type; 1.808 + 1.809 + nsString() 1.810 + { 1.811 + NS_StringContainerInit(*this); 1.812 + } 1.813 + 1.814 + nsString(const self_type& aString) 1.815 + { 1.816 + NS_StringContainerInit(*this); 1.817 + NS_StringCopy(*this, aString); 1.818 + } 1.819 + 1.820 + explicit 1.821 + nsString(const abstract_string_type& aReadable) 1.822 + { 1.823 + NS_StringContainerInit(*this); 1.824 + NS_StringCopy(*this, aReadable); 1.825 + } 1.826 + 1.827 + explicit 1.828 + nsString(const char_type* aData, size_type aLength = UINT32_MAX) 1.829 + { 1.830 + NS_StringContainerInit2(*this, aData, aLength, 0); 1.831 + } 1.832 + 1.833 +#ifdef MOZ_USE_CHAR16_WRAPPER 1.834 + explicit 1.835 + nsString(char16ptr_t aData, size_type aLength = UINT32_MAX) 1.836 + : nsString(static_cast<const char16_t*>(aData), aLength) {} 1.837 +#endif 1.838 + 1.839 + ~nsString() 1.840 + { 1.841 + NS_StringContainerFinish(*this); 1.842 + } 1.843 + 1.844 + char16ptr_t get() const 1.845 + { 1.846 + return char16ptr_t(BeginReading()); 1.847 + } 1.848 + 1.849 + self_type& operator=(const self_type& aString) { Assign(aString); return *this; } 1.850 + self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; } 1.851 + self_type& operator=(const char_type* aPtr) { Assign(aPtr); return *this; } 1.852 + self_type& operator=(char_type aChar) { Assign(aChar); return *this; } 1.853 + 1.854 + void Adopt(const char_type *aData, size_type aLength = UINT32_MAX) 1.855 + { 1.856 + NS_StringContainerFinish(*this); 1.857 + NS_StringContainerInit2(*this, aData, aLength, 1.858 + NS_STRING_CONTAINER_INIT_ADOPT); 1.859 + } 1.860 + 1.861 +protected: 1.862 + 1.863 + nsString(const char_type* aData, size_type aLength, uint32_t aFlags) 1.864 + { 1.865 + NS_StringContainerInit2(*this, aData, aLength, aFlags); 1.866 + } 1.867 +}; 1.868 + 1.869 +class nsCString : public nsCStringContainer 1.870 +{ 1.871 +public: 1.872 + typedef nsCString self_type; 1.873 + typedef nsACString abstract_string_type; 1.874 + 1.875 + nsCString() 1.876 + { 1.877 + NS_CStringContainerInit(*this); 1.878 + } 1.879 + 1.880 + nsCString(const self_type& aString) 1.881 + { 1.882 + NS_CStringContainerInit(*this); 1.883 + NS_CStringCopy(*this, aString); 1.884 + } 1.885 + 1.886 + explicit 1.887 + nsCString(const abstract_string_type& aReadable) 1.888 + { 1.889 + NS_CStringContainerInit(*this); 1.890 + NS_CStringCopy(*this, aReadable); 1.891 + } 1.892 + 1.893 + explicit 1.894 + nsCString(const char_type* aData, size_type aLength = UINT32_MAX) 1.895 + { 1.896 + NS_CStringContainerInit(*this); 1.897 + NS_CStringSetData(*this, aData, aLength); 1.898 + } 1.899 + 1.900 + ~nsCString() 1.901 + { 1.902 + NS_CStringContainerFinish(*this); 1.903 + } 1.904 + 1.905 + const char_type* get() const 1.906 + { 1.907 + return BeginReading(); 1.908 + } 1.909 + 1.910 + self_type& operator=(const self_type& aString) { Assign(aString); return *this; } 1.911 + self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; } 1.912 + self_type& operator=(const char_type* aPtr) { Assign(aPtr); return *this; } 1.913 + self_type& operator=(char_type aChar) { Assign(aChar); return *this; } 1.914 + 1.915 + void Adopt(const char_type *aData, size_type aLength = UINT32_MAX) 1.916 + { 1.917 + NS_CStringContainerFinish(*this); 1.918 + NS_CStringContainerInit2(*this, aData, aLength, 1.919 + NS_CSTRING_CONTAINER_INIT_ADOPT); 1.920 + } 1.921 + 1.922 +protected: 1.923 + 1.924 + nsCString(const char_type* aData, size_type aLength, uint32_t aFlags) 1.925 + { 1.926 + NS_CStringContainerInit2(*this, aData, aLength, aFlags); 1.927 + } 1.928 +}; 1.929 + 1.930 + 1.931 +/** 1.932 + * dependent strings 1.933 + */ 1.934 + 1.935 +class nsDependentString : public nsString 1.936 +{ 1.937 +public: 1.938 + typedef nsDependentString self_type; 1.939 + 1.940 + nsDependentString() {} 1.941 + 1.942 + explicit 1.943 + nsDependentString(const char_type* aData, size_type aLength = UINT32_MAX) 1.944 + : nsString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND) 1.945 + {} 1.946 + 1.947 +#ifdef MOZ_USE_CHAR16_WRAPPER 1.948 + explicit 1.949 + nsDependentString(char16ptr_t aData, size_type aLength = UINT32_MAX) 1.950 + : nsDependentString(static_cast<const char16_t*>(aData), aLength) 1.951 + {} 1.952 +#endif 1.953 + 1.954 + void Rebind(const char_type* aData, size_type aLength = UINT32_MAX) 1.955 + { 1.956 + NS_StringContainerFinish(*this); 1.957 + NS_StringContainerInit2(*this, aData, aLength, 1.958 + NS_STRING_CONTAINER_INIT_DEPEND); 1.959 + } 1.960 + 1.961 +private: 1.962 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.963 +}; 1.964 + 1.965 +class nsDependentCString : public nsCString 1.966 +{ 1.967 +public: 1.968 + typedef nsDependentCString self_type; 1.969 + 1.970 + nsDependentCString() {} 1.971 + 1.972 + explicit 1.973 + nsDependentCString(const char_type* aData, size_type aLength = UINT32_MAX) 1.974 + : nsCString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND) 1.975 + {} 1.976 + 1.977 + void Rebind(const char_type* aData, size_type aLength = UINT32_MAX) 1.978 + { 1.979 + NS_CStringContainerFinish(*this); 1.980 + NS_CStringContainerInit2(*this, aData, aLength, 1.981 + NS_CSTRING_CONTAINER_INIT_DEPEND); 1.982 + } 1.983 + 1.984 +private: 1.985 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.986 +}; 1.987 + 1.988 + 1.989 +/** 1.990 + * conversion classes 1.991 + */ 1.992 + 1.993 +inline void 1.994 +CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest) 1.995 +{ 1.996 + NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_UTF8, aDest); 1.997 +} 1.998 + 1.999 +inline void 1.1000 +CopyUTF8toUTF16(const nsACString& aSource, nsAString& aDest) 1.1001 +{ 1.1002 + NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_UTF8, aDest); 1.1003 +} 1.1004 + 1.1005 +inline void 1.1006 +LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest) 1.1007 +{ 1.1008 + NS_UTF16ToCString(aSource, NS_CSTRING_ENCODING_ASCII, aDest); 1.1009 +} 1.1010 + 1.1011 +inline void 1.1012 +CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest) 1.1013 +{ 1.1014 + NS_CStringToUTF16(aSource, NS_CSTRING_ENCODING_ASCII, aDest); 1.1015 +} 1.1016 + 1.1017 +NS_COM_GLUE char* 1.1018 +ToNewUTF8String(const nsAString& aSource); 1.1019 + 1.1020 +class NS_ConvertASCIItoUTF16 : public nsString 1.1021 +{ 1.1022 +public: 1.1023 + typedef NS_ConvertASCIItoUTF16 self_type; 1.1024 + 1.1025 + explicit 1.1026 + NS_ConvertASCIItoUTF16(const nsACString& aStr) 1.1027 + { 1.1028 + NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_ASCII, *this); 1.1029 + } 1.1030 + 1.1031 + explicit 1.1032 + NS_ConvertASCIItoUTF16(const char* aData, uint32_t aLength = UINT32_MAX) 1.1033 + { 1.1034 + NS_CStringToUTF16(nsDependentCString(aData, aLength), 1.1035 + NS_CSTRING_ENCODING_ASCII, *this); 1.1036 + } 1.1037 + 1.1038 +private: 1.1039 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.1040 +}; 1.1041 + 1.1042 +class NS_ConvertUTF8toUTF16 : public nsString 1.1043 +{ 1.1044 +public: 1.1045 + typedef NS_ConvertUTF8toUTF16 self_type; 1.1046 + 1.1047 + explicit 1.1048 + NS_ConvertUTF8toUTF16(const nsACString& aStr) 1.1049 + { 1.1050 + NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_UTF8, *this); 1.1051 + } 1.1052 + 1.1053 + explicit 1.1054 + NS_ConvertUTF8toUTF16(const char* aData, uint32_t aLength = UINT32_MAX) 1.1055 + { 1.1056 + NS_CStringToUTF16(nsDependentCString(aData, aLength), 1.1057 + NS_CSTRING_ENCODING_UTF8, *this); 1.1058 + } 1.1059 + 1.1060 +private: 1.1061 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.1062 +}; 1.1063 + 1.1064 +class NS_ConvertUTF16toUTF8 : public nsCString 1.1065 +{ 1.1066 +public: 1.1067 + typedef NS_ConvertUTF16toUTF8 self_type; 1.1068 + 1.1069 + explicit 1.1070 + NS_ConvertUTF16toUTF8(const nsAString& aStr) 1.1071 + { 1.1072 + NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_UTF8, *this); 1.1073 + } 1.1074 + 1.1075 + explicit 1.1076 + NS_ConvertUTF16toUTF8(const char16_t* aData, uint32_t aLength = UINT32_MAX) 1.1077 + { 1.1078 + NS_UTF16ToCString(nsDependentString(aData, aLength), 1.1079 + NS_CSTRING_ENCODING_UTF8, *this); 1.1080 + } 1.1081 + 1.1082 +private: 1.1083 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.1084 +}; 1.1085 + 1.1086 +class NS_LossyConvertUTF16toASCII : public nsCString 1.1087 +{ 1.1088 +public: 1.1089 + typedef NS_LossyConvertUTF16toASCII self_type; 1.1090 + 1.1091 + explicit 1.1092 + NS_LossyConvertUTF16toASCII(const nsAString& aStr) 1.1093 + { 1.1094 + NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_ASCII, *this); 1.1095 + } 1.1096 + 1.1097 + explicit 1.1098 + NS_LossyConvertUTF16toASCII(const char16_t* aData, uint32_t aLength = UINT32_MAX) 1.1099 + { 1.1100 + NS_UTF16ToCString(nsDependentString(aData, aLength), 1.1101 + NS_CSTRING_ENCODING_ASCII, *this); 1.1102 + } 1.1103 + 1.1104 +private: 1.1105 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.1106 +}; 1.1107 + 1.1108 + 1.1109 +/** 1.1110 + * literal strings 1.1111 + */ 1.1112 +static_assert(sizeof(char16_t) == 2, "size of char16_t must be 2"); 1.1113 +static_assert(char16_t(-1) > char16_t(0), "char16_t must be unsigned"); 1.1114 + 1.1115 +#define NS_MULTILINE_LITERAL_STRING(s) nsDependentString(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1)) 1.1116 +#define NS_MULTILINE_LITERAL_STRING_INIT(n,s) n(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1)) 1.1117 +#define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) const nsDependentString n(reinterpret_cast<const nsAString::char_type*>(s), uint32_t((sizeof(s)/2)-1)) 1.1118 +typedef nsDependentString nsLiteralString; 1.1119 + 1.1120 +/* Check that char16_t is unsigned */ 1.1121 +static_assert(char16_t(-1) > char16_t(0), "char16_t is by definition an unsigned type"); 1.1122 + 1.1123 +#define NS_LITERAL_STRING(s) static_cast<const nsString&>(NS_MULTILINE_LITERAL_STRING(MOZ_UTF16(s))) 1.1124 +#define NS_LITERAL_STRING_INIT(n,s) NS_MULTILINE_LITERAL_STRING_INIT(n, MOZ_UTF16(s)) 1.1125 +#define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n, MOZ_UTF16(s)) 1.1126 + 1.1127 +#define NS_LITERAL_CSTRING(s) static_cast<const nsDependentCString&>(nsDependentCString(s, uint32_t(sizeof(s)-1))) 1.1128 +#define NS_LITERAL_CSTRING_INIT(n,s) n(s, uint32_t(sizeof(s)-1)) 1.1129 +#define NS_NAMED_LITERAL_CSTRING(n,s) const nsDependentCString n(s, uint32_t(sizeof(s)-1)) 1.1130 + 1.1131 +typedef nsDependentCString nsLiteralCString; 1.1132 + 1.1133 + 1.1134 +/** 1.1135 + * getter_Copies support 1.1136 + * 1.1137 + * NS_IMETHOD GetBlah(char16_t**); 1.1138 + * 1.1139 + * void some_function() 1.1140 + * { 1.1141 + * nsString blah; 1.1142 + * GetBlah(getter_Copies(blah)); 1.1143 + * // ... 1.1144 + * } 1.1145 + */ 1.1146 + 1.1147 +class nsGetterCopies 1.1148 +{ 1.1149 +public: 1.1150 + typedef char16_t char_type; 1.1151 + 1.1152 + nsGetterCopies(nsString& aStr) 1.1153 + : mString(aStr), mData(nullptr) 1.1154 + {} 1.1155 + 1.1156 + ~nsGetterCopies() 1.1157 + { 1.1158 + mString.Adopt(mData); 1.1159 + } 1.1160 + 1.1161 + operator char_type**() 1.1162 + { 1.1163 + return &mData; 1.1164 + } 1.1165 + 1.1166 +private: 1.1167 + nsString& mString; 1.1168 + char_type* mData; 1.1169 +}; 1.1170 + 1.1171 +inline nsGetterCopies 1.1172 +getter_Copies(nsString& aString) 1.1173 +{ 1.1174 + return nsGetterCopies(aString); 1.1175 +} 1.1176 + 1.1177 +class nsCGetterCopies 1.1178 +{ 1.1179 +public: 1.1180 + typedef char char_type; 1.1181 + 1.1182 + nsCGetterCopies(nsCString& aStr) 1.1183 + : mString(aStr), mData(nullptr) 1.1184 + {} 1.1185 + 1.1186 + ~nsCGetterCopies() 1.1187 + { 1.1188 + mString.Adopt(mData); 1.1189 + } 1.1190 + 1.1191 + operator char_type**() 1.1192 + { 1.1193 + return &mData; 1.1194 + } 1.1195 + 1.1196 +private: 1.1197 + nsCString& mString; 1.1198 + char_type* mData; 1.1199 +}; 1.1200 + 1.1201 +inline nsCGetterCopies 1.1202 +getter_Copies(nsCString& aString) 1.1203 +{ 1.1204 + return nsCGetterCopies(aString); 1.1205 +} 1.1206 + 1.1207 + 1.1208 +/** 1.1209 +* substrings 1.1210 +*/ 1.1211 + 1.1212 +class NS_COM_GLUE nsDependentSubstring : public nsStringContainer 1.1213 +{ 1.1214 +public: 1.1215 + typedef nsDependentSubstring self_type; 1.1216 + typedef nsAString abstract_string_type; 1.1217 + 1.1218 + ~nsDependentSubstring() 1.1219 + { 1.1220 + NS_StringContainerFinish(*this); 1.1221 + } 1.1222 + 1.1223 + nsDependentSubstring() 1.1224 + { 1.1225 + NS_StringContainerInit(*this); 1.1226 + } 1.1227 + 1.1228 + nsDependentSubstring(const char_type *aStart, uint32_t aLength) 1.1229 + { 1.1230 + NS_StringContainerInit2(*this, aStart, aLength, 1.1231 + NS_STRING_CONTAINER_INIT_DEPEND | 1.1232 + NS_STRING_CONTAINER_INIT_SUBSTRING); 1.1233 + } 1.1234 + 1.1235 + nsDependentSubstring(const abstract_string_type& aStr, 1.1236 + uint32_t aStartPos); 1.1237 + nsDependentSubstring(const abstract_string_type& aStr, 1.1238 + uint32_t aStartPos, uint32_t aLength); 1.1239 + 1.1240 + void Rebind(const char_type *aStart, uint32_t aLength) 1.1241 + { 1.1242 + NS_StringContainerFinish(*this); 1.1243 + NS_StringContainerInit2(*this, aStart, aLength, 1.1244 + NS_STRING_CONTAINER_INIT_DEPEND | 1.1245 + NS_STRING_CONTAINER_INIT_SUBSTRING); 1.1246 + } 1.1247 + 1.1248 +private: 1.1249 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.1250 +}; 1.1251 + 1.1252 +class NS_COM_GLUE nsDependentCSubstring : public nsCStringContainer 1.1253 +{ 1.1254 +public: 1.1255 + typedef nsDependentCSubstring self_type; 1.1256 + typedef nsACString abstract_string_type; 1.1257 + 1.1258 + ~nsDependentCSubstring() 1.1259 + { 1.1260 + NS_CStringContainerFinish(*this); 1.1261 + } 1.1262 + 1.1263 + nsDependentCSubstring() 1.1264 + { 1.1265 + NS_CStringContainerInit(*this); 1.1266 + } 1.1267 + 1.1268 + nsDependentCSubstring(const char_type *aStart, uint32_t aLength) 1.1269 + { 1.1270 + NS_CStringContainerInit2(*this, aStart, aLength, 1.1271 + NS_CSTRING_CONTAINER_INIT_DEPEND | 1.1272 + NS_CSTRING_CONTAINER_INIT_SUBSTRING); 1.1273 + } 1.1274 + 1.1275 + nsDependentCSubstring(const abstract_string_type& aStr, 1.1276 + uint32_t aStartPos); 1.1277 + nsDependentCSubstring(const abstract_string_type& aStr, 1.1278 + uint32_t aStartPos, uint32_t aLength); 1.1279 + 1.1280 + void Rebind(const char_type *aStart, uint32_t aLength) 1.1281 + { 1.1282 + NS_CStringContainerFinish(*this); 1.1283 + NS_CStringContainerInit2(*this, aStart, aLength, 1.1284 + NS_CSTRING_CONTAINER_INIT_DEPEND | 1.1285 + NS_CSTRING_CONTAINER_INIT_SUBSTRING); 1.1286 + } 1.1287 + 1.1288 +private: 1.1289 + self_type& operator=(const self_type& aString) MOZ_DELETE; 1.1290 +}; 1.1291 + 1.1292 + 1.1293 +/** 1.1294 + * Various nsDependentC?Substring constructor functions 1.1295 + */ 1.1296 + 1.1297 +// char16_t 1.1298 +inline const nsDependentSubstring 1.1299 +Substring( const nsAString& str, uint32_t startPos ) 1.1300 +{ 1.1301 + return nsDependentSubstring(str, startPos); 1.1302 +} 1.1303 + 1.1304 +inline const nsDependentSubstring 1.1305 +Substring( const nsAString& str, uint32_t startPos, uint32_t length ) 1.1306 +{ 1.1307 + return nsDependentSubstring(str, startPos, length); 1.1308 +} 1.1309 + 1.1310 +inline const nsDependentSubstring 1.1311 +Substring( const char16_t* start, const char16_t* end ) 1.1312 +{ 1.1313 + NS_ABORT_IF_FALSE(uint32_t(end - start) == uintptr_t(end - start), "string too long"); 1.1314 + return nsDependentSubstring(start, uint32_t(end - start)); 1.1315 +} 1.1316 + 1.1317 +inline const nsDependentSubstring 1.1318 +Substring( const char16_t* start, uint32_t length ) 1.1319 +{ 1.1320 + return nsDependentSubstring(start, length); 1.1321 +} 1.1322 + 1.1323 +inline const nsDependentSubstring 1.1324 +StringHead( const nsAString& str, uint32_t count ) 1.1325 +{ 1.1326 + return nsDependentSubstring(str, 0, count); 1.1327 +} 1.1328 + 1.1329 +inline const nsDependentSubstring 1.1330 +StringTail( const nsAString& str, uint32_t count ) 1.1331 +{ 1.1332 + return nsDependentSubstring(str, str.Length() - count, count); 1.1333 +} 1.1334 + 1.1335 +// char 1.1336 +inline const nsDependentCSubstring 1.1337 +Substring( const nsACString& str, uint32_t startPos ) 1.1338 +{ 1.1339 + return nsDependentCSubstring(str, startPos); 1.1340 +} 1.1341 + 1.1342 +inline const nsDependentCSubstring 1.1343 +Substring( const nsACString& str, uint32_t startPos, uint32_t length ) 1.1344 +{ 1.1345 + return nsDependentCSubstring(str, startPos, length); 1.1346 +} 1.1347 + 1.1348 +inline 1.1349 +const nsDependentCSubstring 1.1350 +Substring( const char* start, const char* end ) 1.1351 +{ 1.1352 + NS_ABORT_IF_FALSE(uint32_t(end - start) == uintptr_t(end - start), "string too long"); 1.1353 + return nsDependentCSubstring(start, uint32_t(end - start)); 1.1354 +} 1.1355 + 1.1356 +inline 1.1357 +const nsDependentCSubstring 1.1358 +Substring( const char* start, uint32_t length ) 1.1359 +{ 1.1360 + return nsDependentCSubstring(start, length); 1.1361 +} 1.1362 + 1.1363 +inline const nsDependentCSubstring 1.1364 +StringHead( const nsACString& str, uint32_t count ) 1.1365 +{ 1.1366 + return nsDependentCSubstring(str, 0, count); 1.1367 +} 1.1368 + 1.1369 +inline const nsDependentCSubstring 1.1370 +StringTail( const nsACString& str, uint32_t count ) 1.1371 +{ 1.1372 + return nsDependentCSubstring(str, str.Length() - count, count); 1.1373 +} 1.1374 + 1.1375 + 1.1376 +inline bool 1.1377 +StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring, 1.1378 + nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator) 1.1379 +{ 1.1380 + return aSubstring.Length() <= aSource.Length() && 1.1381 + StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator); 1.1382 +} 1.1383 + 1.1384 +inline bool 1.1385 +StringEndsWith(const nsAString& aSource, const nsAString& aSubstring, 1.1386 + nsAString::ComparatorFunc aComparator = nsAString::DefaultComparator) 1.1387 +{ 1.1388 + return aSubstring.Length() <= aSource.Length() && 1.1389 + StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator); 1.1390 +} 1.1391 + 1.1392 +inline bool 1.1393 +StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring, 1.1394 + nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator) 1.1395 +{ 1.1396 + return aSubstring.Length() <= aSource.Length() && 1.1397 + StringHead(aSource, aSubstring.Length()).Equals(aSubstring, aComparator); 1.1398 +} 1.1399 + 1.1400 +inline bool 1.1401 +StringEndsWith(const nsACString& aSource, const nsACString& aSubstring, 1.1402 + nsACString::ComparatorFunc aComparator = nsACString::DefaultComparator) 1.1403 +{ 1.1404 + return aSubstring.Length() <= aSource.Length() && 1.1405 + StringTail(aSource, aSubstring.Length()).Equals(aSubstring, aComparator); 1.1406 +} 1.1407 + 1.1408 +/** 1.1409 + * Trim whitespace from the beginning and end of a string; then compress 1.1410 + * remaining runs of whitespace characters to a single space. 1.1411 + */ 1.1412 +NS_HIDDEN_(void) 1.1413 +CompressWhitespace(nsAString& aString); 1.1414 + 1.1415 +#define EmptyCString() nsCString() 1.1416 +#define EmptyString() nsString() 1.1417 + 1.1418 +/** 1.1419 + * Convert an ASCII string to all upper/lowercase (a-z,A-Z only). As a bonus, 1.1420 + * returns the string length. 1.1421 + */ 1.1422 +NS_HIDDEN_(uint32_t) 1.1423 +ToLowerCase(nsACString& aStr); 1.1424 + 1.1425 +NS_HIDDEN_(uint32_t) 1.1426 +ToUpperCase(nsACString& aStr); 1.1427 + 1.1428 +NS_HIDDEN_(uint32_t) 1.1429 +ToLowerCase(const nsACString& aSrc, nsACString& aDest); 1.1430 + 1.1431 +NS_HIDDEN_(uint32_t) 1.1432 +ToUpperCase(const nsACString& aSrc, nsACString& aDest); 1.1433 + 1.1434 +/** 1.1435 + * The following declarations are *deprecated*, and are included here only 1.1436 + * to make porting from existing code that doesn't use the frozen string API 1.1437 + * easier. They may disappear in the future. 1.1438 + */ 1.1439 + 1.1440 +inline char* 1.1441 +ToNewCString(const nsACString& aStr) 1.1442 +{ 1.1443 + return NS_CStringCloneData(aStr); 1.1444 +} 1.1445 + 1.1446 +inline char16_t* 1.1447 +ToNewUnicode(const nsAString& aStr) 1.1448 +{ 1.1449 + return NS_StringCloneData(aStr); 1.1450 +} 1.1451 + 1.1452 +typedef nsString PromiseFlatString; 1.1453 +typedef nsCString PromiseFlatCString; 1.1454 + 1.1455 +typedef nsCString nsAutoCString; 1.1456 +typedef nsString nsAutoString; 1.1457 + 1.1458 +NS_HIDDEN_(bool) ParseString(const nsACString& aAstring, char aDelimiter, 1.1459 + nsTArray<nsCString>& aArray); 1.1460 + 1.1461 +#endif // nsStringAPI_h__