1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// IWYU pragma: private, include "nsString.h"
#ifndef nsTString_h
#define nsTString_h
#include "nsTSubstring.h"
/**
* This is the canonical null-terminated string class. All subclasses
* promise null-terminated storage. Instances of this class allocate
* strings on the heap.
*
* NAMES:
* nsString for wide characters
* nsCString for narrow characters
*
* This class is also known as nsAFlat[C]String, where "flat" is used
* to denote a null-terminated string.
*/
template <typename T>
class MOZ_GSL_OWNER nsTString : public nsTSubstring<T> {
public:
typedef nsTString<T> self_type;
using repr_type = mozilla::detail::nsTStringRepr<T>;
#ifdef __clang__
// bindgen w/ clang 3.9 at least chokes on a typedef, but using is okay.
using typename nsTSubstring<T>::substring_type;
#else
// On the other hand msvc chokes on the using statement. It seems others
// don't care either way so we lump them in here.
typedef typename nsTSubstring<T>::substring_type substring_type;
#endif
typedef typename substring_type::fallible_t fallible_t;
typedef typename substring_type::char_type char_type;
typedef typename substring_type::char_traits char_traits;
typedef
typename substring_type::incompatible_char_type incompatible_char_type;
typedef typename substring_type::substring_tuple_type substring_tuple_type;
typedef typename substring_type::const_iterator const_iterator;
typedef typename substring_type::iterator iterator;
typedef typename substring_type::comparator_type comparator_type;
typedef typename substring_type::const_char_iterator const_char_iterator;
typedef typename substring_type::string_view string_view;
typedef typename substring_type::index_type index_type;
typedef typename substring_type::size_type size_type;
// These are only for internal use within the string classes:
typedef typename substring_type::DataFlags DataFlags;
typedef typename substring_type::ClassFlags ClassFlags;
public:
/**
* constructors
*/
constexpr nsTString() : substring_type(ClassFlags::NULL_TERMINATED) {}
explicit nsTString(const char_type* aData, size_type aLength = size_type(-1))
: substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(aData, aLength);
}
explicit nsTString(mozilla::Span<const char_type> aData)
: nsTString(aData.Elements(), aData.Length()) {}
#if defined(MOZ_USE_CHAR16_WRAPPER)
template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
explicit nsTString(char16ptr_t aStr, size_type aLength = size_type(-1))
: substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(static_cast<const char16_t*>(aStr), aLength);
}
#endif
nsTString(const self_type& aStr)
: substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(aStr);
}
nsTString(self_type&& aStr) : substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(std::move(aStr));
}
MOZ_IMPLICIT nsTString(const substring_tuple_type& aTuple)
: substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(aTuple);
}
explicit nsTString(const substring_type& aReadable)
: substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(aReadable);
}
explicit nsTString(substring_type&& aReadable)
: substring_type(ClassFlags::NULL_TERMINATED) {
this->Assign(std::move(aReadable));
}
// |operator=| does not inherit, so we must define our own
self_type& operator=(char_type aChar) {
this->Assign(aChar);
return *this;
}
self_type& operator=(const char_type* aData) {
this->Assign(aData);
return *this;
}
self_type& operator=(const self_type& aStr) {
this->Assign(aStr);
return *this;
}
self_type& operator=(self_type&& aStr) {
this->Assign(std::move(aStr));
return *this;
}
#if defined(MOZ_USE_CHAR16_WRAPPER)
template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
self_type& operator=(const char16ptr_t aStr) {
this->Assign(static_cast<const char16_t*>(aStr));
return *this;
}
#endif
self_type& operator=(const substring_type& aStr) {
this->Assign(aStr);
return *this;
}
self_type& operator=(substring_type&& aStr) {
this->Assign(std::move(aStr));
return *this;
}
self_type& operator=(const substring_tuple_type& aTuple) {
this->Assign(aTuple);
return *this;
}
/**
* returns the null-terminated string
*/
template <typename U, typename Dummy>
struct raw_type {
typedef const U* type;
};
#if defined(MOZ_USE_CHAR16_WRAPPER)
template <typename Dummy>
struct raw_type<char16_t, Dummy> {
typedef char16ptr_t type;
};
#endif
MOZ_NO_DANGLING_ON_TEMPORARIES typename raw_type<T, int>::type get() const {
return this->mData;
}
#ifdef XP_WIN
/**
* Returns the string as a wchar_t
*/
template <typename U = T>
typename std::enable_if<std::is_same<U, char16_t>::value,
const wchar_t*>::type
getW() const {
return reinterpret_cast<const wchar_t*>(this->mData);
}
#endif
/**
* returns character at specified index.
*
* NOTE: unlike nsTSubstring::CharAt, this function allows you to index
* the null terminator character.
*/
char_type CharAt(index_type aIndex) const {
MOZ_ASSERT(aIndex <= this->Length(), "index exceeds allowable range");
return this->mData[aIndex];
}
char_type operator[](index_type aIndex) const { return CharAt(aIndex); }
/**
* Set a char inside this string at given index
*
* @param aChar is the char you want to write into this string
* @param anIndex is the ofs where you want to write the given char
* @return TRUE if successful
*/
bool SetCharAt(char16_t aChar, index_type aIndex);
/**
* Allow this string to be bound to a character buffer
* until the string is rebound or mutated; the caller
* must ensure that the buffer outlives the string.
*/
void Rebind(const char_type* aData, size_type aLength);
/**
* verify restrictions for dependent strings
*/
void AssertValidDependentString() {
MOZ_ASSERT(this->mData, "nsTDependentString must wrap a non-NULL buffer");
MOZ_ASSERT(this->mData[substring_type::mLength] == 0,
"nsTDependentString must wrap only null-terminated strings. "
"You are probably looking for nsTDependentSubstring.");
}
protected:
// allow subclasses to initialize fields directly
nsTString(char_type* aData, size_type aLength, DataFlags aDataFlags,
ClassFlags aClassFlags)
: substring_type(aData, aLength, aDataFlags,
aClassFlags | ClassFlags::NULL_TERMINATED) {}
friend const nsTString<char>& VoidCString();
friend const nsTString<char16_t>& VoidString();
// Used by Null[C]String.
explicit nsTString(DataFlags aDataFlags)
: substring_type(char_traits::sEmptyBuffer, 0,
aDataFlags | DataFlags::TERMINATED,
ClassFlags::NULL_TERMINATED) {}
};
extern template class nsTString<char>;
extern template class nsTString<char16_t>;
template <typename Char>
struct fmt::formatter<nsTString<Char>, Char>
: fmt::formatter<nsTSubstring<Char>, Char> {};
/**
* nsTAutoStringN
*
* Subclass of nsTString that adds support for stack-based string
* allocation. It is normally not a good idea to use this class on the
* heap, because it will allocate space which may be wasted if the string
* it contains is significantly smaller or any larger than 64 characters.
*
* NAMES:
* nsAutoStringN / nsTAutoString for wide characters
* nsAutoCStringN / nsTAutoCString for narrow characters
*/
template <typename T, size_t N>
class MOZ_NON_MEMMOVABLE MOZ_GSL_OWNER nsTAutoStringN : public nsTString<T> {
public:
typedef nsTAutoStringN<T, N> self_type;
typedef nsTString<T> base_string_type;
typedef typename base_string_type::string_type string_type;
typedef typename base_string_type::char_type char_type;
typedef typename base_string_type::char_traits char_traits;
typedef typename base_string_type::substring_type substring_type;
typedef typename base_string_type::size_type size_type;
typedef typename base_string_type::substring_tuple_type substring_tuple_type;
// These are only for internal use within the string classes:
typedef typename base_string_type::DataFlags DataFlags;
typedef typename base_string_type::ClassFlags ClassFlags;
typedef typename base_string_type::LengthStorage LengthStorage;
public:
/**
* constructors
*/
nsTAutoStringN()
: string_type(mStorage, 0, DataFlags::TERMINATED | DataFlags::INLINE,
ClassFlags::INLINE),
mInlineCapacity(N - 1) {
// null-terminate
mStorage[0] = char_type(0);
}
explicit nsTAutoStringN(char_type aChar) : self_type() {
this->Assign(aChar);
}
explicit nsTAutoStringN(const char_type* aData,
size_type aLength = size_type(-1))
: self_type() {
this->Assign(aData, aLength);
}
#if defined(MOZ_USE_CHAR16_WRAPPER)
template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
explicit nsTAutoStringN(char16ptr_t aData, size_type aLength = size_type(-1))
: self_type(static_cast<const char16_t*>(aData), aLength) {}
#endif
nsTAutoStringN(const self_type& aStr) : self_type() { this->Assign(aStr); }
nsTAutoStringN(self_type&& aStr) : self_type() {
this->Assign(std::move(aStr));
}
explicit nsTAutoStringN(const substring_type& aStr) : self_type() {
this->Assign(aStr);
}
explicit nsTAutoStringN(substring_type&& aStr) : self_type() {
this->Assign(std::move(aStr));
}
MOZ_IMPLICIT nsTAutoStringN(const substring_tuple_type& aTuple)
: self_type() {
this->Assign(aTuple);
}
// |operator=| does not inherit, so we must define our own
self_type& operator=(char_type aChar) {
this->Assign(aChar);
return *this;
}
self_type& operator=(const char_type* aData) {
this->Assign(aData);
return *this;
}
#if defined(MOZ_USE_CHAR16_WRAPPER)
template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
self_type& operator=(char16ptr_t aStr) {
this->Assign(aStr);
return *this;
}
#endif
self_type& operator=(const self_type& aStr) {
this->Assign(aStr);
return *this;
}
self_type& operator=(self_type&& aStr) {
this->Assign(std::move(aStr));
return *this;
}
self_type& operator=(const substring_type& aStr) {
this->Assign(aStr);
return *this;
}
self_type& operator=(substring_type&& aStr) {
this->Assign(std::move(aStr));
return *this;
}
self_type& operator=(const substring_tuple_type& aTuple) {
this->Assign(aTuple);
return *this;
}
static const size_t kStorageSize = N;
protected:
friend class nsTSubstring<T>;
const LengthStorage mInlineCapacity;
private:
char_type mStorage[N];
};
// Externs for the most common nsTAutoStringN variations.
extern template class nsTAutoStringN<char, 64>;
extern template class nsTAutoStringN<char16_t, 64>;
template <typename Char, size_t N>
struct fmt::formatter<nsTAutoStringN<Char, N>, Char>
: fmt::formatter<nsTString<Char>, Char> {};
//
// nsAutoString stores pointers into itself which are invalidated when an
// nsTArray is resized, so nsTArray must not be instantiated with nsAutoString
// elements!
//
template <class E>
class nsTArrayElementTraits;
template <typename T>
class nsTArrayElementTraits<nsTAutoString<T>> {
public:
template <class A>
struct Dont_Instantiate_nsTArray_of;
template <class A>
struct Instead_Use_nsTArray_of;
static Dont_Instantiate_nsTArray_of<nsTAutoString<T>>* Construct(
Instead_Use_nsTArray_of<nsTString<T>>* aE) {
return 0;
}
template <class A>
static Dont_Instantiate_nsTArray_of<nsTAutoString<T>>* Construct(
Instead_Use_nsTArray_of<nsTString<T>>* aE, const A& aArg) {
return 0;
}
template <class... Args>
static Dont_Instantiate_nsTArray_of<nsTAutoString<T>>* Construct(
Instead_Use_nsTArray_of<nsTString<T>>* aE, Args&&... aArgs) {
return 0;
}
static Dont_Instantiate_nsTArray_of<nsTAutoString<T>>* Destruct(
Instead_Use_nsTArray_of<nsTString<T>>* aE) {
return 0;
}
};
/**
* getter_Copies support for adopting raw string out params that are
* heap-allocated, e.g.:
*
* char* gStr;
* void GetBlah(char** aStr)
* {
* *aStr = strdup(gStr);
* }
*
* // This works, but is clumsy.
* void Inelegant()
* {
* char* buf;
* GetBlah(&buf);
* nsCString str;
* str.Adopt(buf);
* // ...
* }
*
* // This is nicer.
* void Elegant()
* {
* nsCString str;
* GetBlah(getter_Copies(str));
* // ...
* }
*/
template <typename T>
class MOZ_STACK_CLASS nsTGetterCopies {
public:
typedef T char_type;
explicit nsTGetterCopies(nsTSubstring<T>& aStr)
: mString(aStr), mData(nullptr) {}
~nsTGetterCopies() {
mString.Adopt(mData); // OK if mData is null
}
operator char_type**() { return &mData; }
private:
nsTSubstring<T>& mString;
char_type* mData;
};
// See the comment above nsTGetterCopies_CharT for how to use this.
template <typename T>
inline nsTGetterCopies<T> getter_Copies(nsTSubstring<T>& aString) {
return nsTGetterCopies<T>(aString);
}
#endif
|