2014-05-05 21:30:46 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2013-09-23 21:23:56 +04:00
|
|
|
// IWYU pragma: private, include "nsString.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
#ifndef nsTSubstring_h
|
|
|
|
#define nsTSubstring_h
|
|
|
|
|
2014-04-27 08:29:25 +04:00
|
|
|
#include "mozilla/Casting.h"
|
2016-12-14 19:32:21 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2017-01-30 14:34:35 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2017-02-16 12:43:50 +03:00
|
|
|
#include "mozilla/IntegerTypeTraits.h"
|
|
|
|
#include "mozilla/Span.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
#include "nsTStringRepr.h"
|
|
|
|
|
2008-09-19 19:07:22 +04:00
|
|
|
#ifndef MOZILLA_INTERNAL_API
|
2017-03-02 17:23:08 +03:00
|
|
|
#error "Using XPCOM strings is limited to code linked into libxul."
|
2008-09-19 19:07:22 +04:00
|
|
|
#endif
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T> class nsTSubstringSplitter;
|
|
|
|
template <typename T> class nsTString;
|
2017-03-14 00:01:35 +03:00
|
|
|
|
|
|
|
/**
|
2017-08-15 00:22:50 +03:00
|
|
|
* nsTSubstring is an abstract string class. From an API perspective, this
|
|
|
|
* class is the root of the string class hierarchy. It represents a single
|
|
|
|
* contiguous array of characters, which may or may not be null-terminated.
|
|
|
|
* This type is not instantiated directly. A sub-class is instantiated
|
|
|
|
* instead. For example, see nsTString.
|
2017-03-14 00:01:35 +03:00
|
|
|
*
|
|
|
|
* NAMES:
|
2017-08-15 00:22:50 +03:00
|
|
|
* nsAString for wide characters
|
|
|
|
* nsACString for narrow characters
|
2017-03-14 00:01:35 +03:00
|
|
|
*
|
|
|
|
*/
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
class nsTSubstring : public mozilla::detail::nsTStringRepr<T>
|
2017-03-14 00:01:35 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
public:
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef nsTSubstring<T> self_type;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef nsTString<T> string_type;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename mozilla::detail::nsTStringRepr<T> base_string_type;
|
|
|
|
typedef typename base_string_type::substring_type substring_type;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::fallible_t fallible_t;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::char_type char_type;
|
|
|
|
typedef typename base_string_type::char_traits char_traits;
|
|
|
|
typedef typename base_string_type::incompatible_char_type incompatible_char_type;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::substring_tuple_type substring_tuple_type;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::const_iterator const_iterator;
|
|
|
|
typedef typename base_string_type::iterator iterator;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::comparator_type comparator_type;
|
2017-03-14 00:02:50 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::char_iterator char_iterator;
|
|
|
|
typedef typename base_string_type::const_char_iterator const_char_iterator;
|
2017-03-14 00:02:50 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename base_string_type::index_type index_type;
|
|
|
|
typedef typename base_string_type::size_type size_type;
|
2017-03-14 00:02:50 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
// These are only for internal use within the string classes:
|
|
|
|
typedef typename base_string_type::DataFlags DataFlags;
|
|
|
|
typedef typename base_string_type::ClassFlags ClassFlags;
|
2017-03-14 00:02:50 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
using typename base_string_type::IsChar;
|
|
|
|
using typename base_string_type::IsChar16;
|
2017-03-14 00:02:50 +03:00
|
|
|
|
|
|
|
// this acts like a virtual destructor
|
2017-08-15 00:22:50 +03:00
|
|
|
~nsTSubstring()
|
2017-03-14 00:02:50 +03:00
|
|
|
{
|
|
|
|
Finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* writing iterators
|
|
|
|
*/
|
|
|
|
|
|
|
|
char_iterator BeginWriting()
|
|
|
|
{
|
|
|
|
if (!EnsureMutable()) {
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(base_string_type::mLength);
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
return base_string_type::mData;
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator BeginWriting(const fallible_t&)
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return EnsureMutable() ? base_string_type::mData : char_iterator(0);
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator EndWriting()
|
|
|
|
{
|
|
|
|
if (!EnsureMutable()) {
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(base_string_type::mLength);
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
return base_string_type::mData + base_string_type::mLength;
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator EndWriting(const fallible_t&)
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return EnsureMutable() ? (base_string_type::mData + base_string_type::mLength) : char_iterator(0);
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator& BeginWriting(char_iterator& aIter)
|
|
|
|
{
|
|
|
|
return aIter = BeginWriting();
|
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator& BeginWriting(char_iterator& aIter, const fallible_t& aFallible)
|
|
|
|
{
|
|
|
|
return aIter = BeginWriting(aFallible);
|
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator& EndWriting(char_iterator& aIter)
|
|
|
|
{
|
|
|
|
return aIter = EndWriting();
|
|
|
|
}
|
|
|
|
|
|
|
|
char_iterator& EndWriting(char_iterator& aIter, const fallible_t& aFallible)
|
|
|
|
{
|
|
|
|
return aIter = EndWriting(aFallible);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deprecated writing iterators
|
|
|
|
*/
|
|
|
|
|
|
|
|
iterator& BeginWriting(iterator& aIter)
|
|
|
|
{
|
|
|
|
char_type* data = BeginWriting();
|
|
|
|
aIter.mStart = data;
|
2017-08-15 00:22:50 +03:00
|
|
|
aIter.mEnd = data + base_string_type::mLength;
|
2017-03-14 00:02:50 +03:00
|
|
|
aIter.mPosition = aIter.mStart;
|
|
|
|
return aIter;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator& EndWriting(iterator& aIter)
|
|
|
|
{
|
|
|
|
char_type* data = BeginWriting();
|
|
|
|
aIter.mStart = data;
|
2017-08-15 00:22:50 +03:00
|
|
|
aIter.mEnd = data + base_string_type::mLength;
|
2017-03-14 00:02:50 +03:00
|
|
|
aIter.mPosition = aIter.mEnd;
|
|
|
|
return aIter;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* assignment
|
|
|
|
*/
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Assign(char_type aChar);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Assign(char_type aChar, const fallible_t&);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Assign(const char_type* aData);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Assign(const char_type* aData,
|
|
|
|
const fallible_t&);
|
2016-04-06 10:23:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Assign(const char_type* aData, size_type aLength);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Assign(const char_type* aData,
|
|
|
|
size_type aLength, const fallible_t&);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Assign(const self_type&);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Assign(const self_type&, const fallible_t&);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Assign(const substring_tuple_type&);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Assign(const substring_tuple_type&,
|
|
|
|
const fallible_t&);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
void Assign(char16ptr_t aData)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
Assign(static_cast<const char16_t*>(aData));
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
void Assign(char16ptr_t aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
Assign(static_cast<const char16_t*>(aData), aLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool Assign(char16ptr_t aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
return Assign(static_cast<const char16_t*>(aData), aLength,
|
|
|
|
aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL AssignASCII(const char* aData, size_type aLength);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL AssignASCII(const char* aData,
|
|
|
|
size_type aLength,
|
|
|
|
const fallible_t&);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL AssignASCII(const char* aData)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-08-25 23:17:32 +04:00
|
|
|
AssignASCII(aData, mozilla::AssertedCast<size_type, size_t>(strlen(aData)));
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL AssignASCII(const char* aData,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return AssignASCII(aData,
|
2014-08-25 23:17:32 +04:00
|
|
|
mozilla::AssertedCast<size_type, size_t>(strlen(aData)),
|
2015-01-28 12:00:40 +03:00
|
|
|
aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AssignLiteral must ONLY be applied to an actual literal string, or
|
|
|
|
// a char array *constant* declared without an explicit size.
|
|
|
|
// Do not attempt to use it with a regular char* pointer, or with a
|
|
|
|
// non-constant char array variable. Use AssignASCII for those.
|
|
|
|
// There are not fallible version of these methods because they only really
|
|
|
|
// apply to small allocations that we wouldn't want to check anyway.
|
|
|
|
template<int N>
|
2014-05-27 11:15:35 +04:00
|
|
|
void AssignLiteral(const char_type (&aStr)[N])
|
|
|
|
{
|
|
|
|
AssignLiteral(aStr, N - 1);
|
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
|
|
|
|
template<int N, typename EnableIfChar16 = IsChar16>
|
|
|
|
void AssignLiteral(const incompatible_char_type (&aStr)[N])
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
AssignASCII(aStr, N - 1);
|
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
self_type& operator=(char_type aChar)
|
|
|
|
{
|
|
|
|
Assign(aChar);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
self_type& operator=(const char_type* aData)
|
|
|
|
{
|
|
|
|
Assign(aData);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
self_type& operator=(char16ptr_t aData)
|
|
|
|
{
|
|
|
|
Assign(aData);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
#endif
|
2014-05-27 11:15:35 +04:00
|
|
|
self_type& operator=(const self_type& aStr)
|
|
|
|
{
|
|
|
|
Assign(aStr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
self_type& operator=(const substring_tuple_type& aTuple)
|
|
|
|
{
|
|
|
|
Assign(aTuple);
|
|
|
|
return *this;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-07-31 23:06:36 +03:00
|
|
|
// Adopt a heap-allocated char sequence for this string; is Voided if aData
|
|
|
|
// is null. Useful for e.g. converting an strdup'd C string into an
|
|
|
|
// nsCString. See also getter_Copies(), which is a useful wrapper.
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Adopt(char_type* aData, size_type aLength = size_type(-1));
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* buffer manipulation
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Replace(index_type aCutStart,
|
|
|
|
size_type aCutLength,
|
|
|
|
char_type aChar,
|
|
|
|
const fallible_t&);
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData,
|
|
|
|
size_type aLength = size_type(-1));
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL Replace(index_type aCutStart,
|
|
|
|
size_type aCutLength,
|
|
|
|
const char_type* aData,
|
|
|
|
size_type aLength,
|
|
|
|
const fallible_t&);
|
2014-05-27 11:15:35 +04:00
|
|
|
void Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const self_type& aStr)
|
|
|
|
{
|
|
|
|
Replace(aCutStart, aCutLength, aStr.Data(), aStr.Length());
|
|
|
|
}
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool Replace(index_type aCutStart,
|
|
|
|
size_type aCutLength,
|
|
|
|
const self_type& aStr,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
return Replace(aCutStart, aCutLength, aStr.Data(), aStr.Length(),
|
2015-01-28 12:00:40 +03:00
|
|
|
aFallible);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
void NS_FASTCALL Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const substring_tuple_type& aTuple);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData,
|
|
|
|
size_type aLength = size_type(-1));
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData,
|
|
|
|
size_type aLength,
|
|
|
|
const fallible_t&);
|
2015-03-17 11:55:22 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// ReplaceLiteral must ONLY be applied to an actual literal string.
|
|
|
|
// Do not attempt to use it with a regular char* pointer, or with a char
|
|
|
|
// array variable. Use Replace or ReplaceASCII for those.
|
|
|
|
template<int N>
|
2014-05-27 11:15:35 +04:00
|
|
|
void ReplaceLiteral(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type (&aStr)[N])
|
|
|
|
{
|
|
|
|
ReplaceLiteral(aCutStart, aCutLength, aStr, N - 1);
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void Append(char_type aChar)
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
Replace(base_string_type::mLength, 0, aChar);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool Append(char_type aChar, const fallible_t& aFallible)
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return Replace(base_string_type::mLength, 0, aChar, aFallible);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
void Append(const char_type* aData, size_type aLength = size_type(-1))
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
Replace(base_string_type::mLength, 0, aData, aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool Append(const char_type* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return Replace(base_string_type::mLength, 0, aData, aLength, aFallible);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
void Append(char16ptr_t aData, size_type aLength = size_type(-1))
|
|
|
|
{
|
|
|
|
Append(static_cast<const char16_t*>(aData), aLength);
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
#endif
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void Append(const self_type& aStr)
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
Replace(base_string_type::mLength, 0, aStr);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool Append(const self_type& aStr, const fallible_t& aFallible)
|
2015-03-17 11:55:22 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return Replace(base_string_type::mLength, 0, aStr, aFallible);
|
2015-03-17 11:55:22 +03:00
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
void Append(const substring_tuple_type& aTuple)
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
Replace(base_string_type::mLength, 0, aTuple);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void AppendASCII(const char* aData, size_type aLength = size_type(-1))
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
ReplaceASCII(base_string_type::mLength, 0, aData, aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool AppendASCII(const char* aData, const fallible_t& aFallible)
|
2015-03-17 11:55:22 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return ReplaceASCII(base_string_type::mLength, 0, aData, size_type(-1), aFallible);
|
2015-03-17 11:55:22 +03:00
|
|
|
}
|
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool AppendASCII(const char* aData, size_type aLength, const fallible_t& aFallible)
|
2015-03-17 11:55:22 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return ReplaceASCII(base_string_type::mLength, 0, aData, aLength, aFallible);
|
2015-03-17 11:55:22 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
2016-12-14 19:32:21 +03:00
|
|
|
* Append a formatted string to the current string. Uses the
|
2017-03-15 06:16:32 +03:00
|
|
|
* standard printf format codes. This uses NSPR formatting, which will be
|
|
|
|
* locale-aware for floating-point values. You probably don't want to use
|
|
|
|
* this with floating-point values as a result.
|
2014-05-05 21:30:46 +04:00
|
|
|
*/
|
2016-12-14 19:32:21 +03:00
|
|
|
void AppendPrintf(const char* aFormat, ...) MOZ_FORMAT_PRINTF(2, 3);
|
2017-05-04 21:10:19 +03:00
|
|
|
void AppendPrintf(const char* aFormat, va_list aAp) MOZ_FORMAT_PRINTF(2, 0);
|
2014-05-27 11:15:35 +04:00
|
|
|
void AppendInt(int32_t aInteger)
|
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
AppendPrintf("%" PRId32, aInteger);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
void AppendInt(int32_t aInteger, int aRadix)
|
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
if (aRadix == 10) {
|
|
|
|
AppendPrintf("%" PRId32, aInteger);
|
|
|
|
} else {
|
|
|
|
AppendPrintf(aRadix == 8 ? "%" PRIo32 : "%" PRIx32,
|
|
|
|
static_cast<uint32_t>(aInteger));
|
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
void AppendInt(uint32_t aInteger)
|
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
AppendPrintf("%" PRIu32, aInteger);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
void AppendInt(uint32_t aInteger, int aRadix)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
AppendPrintf(aRadix == 10 ? "%" PRIu32 : aRadix == 8 ? "%" PRIo32 : "%" PRIx32,
|
|
|
|
aInteger);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
void AppendInt(int64_t aInteger)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
AppendPrintf("%" PRId64, aInteger);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
void AppendInt(int64_t aInteger, int aRadix)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
if (aRadix == 10) {
|
|
|
|
AppendPrintf("%" PRId64, aInteger);
|
|
|
|
} else {
|
|
|
|
AppendPrintf(aRadix == 8 ? "%" PRIo64 : "%" PRIx64,
|
|
|
|
static_cast<uint64_t>(aInteger));
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
void AppendInt(uint64_t aInteger)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
AppendPrintf("%" PRIu64, aInteger);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
void AppendInt(uint64_t aInteger, int aRadix)
|
|
|
|
{
|
2016-12-14 19:32:21 +03:00
|
|
|
AppendPrintf(aRadix == 10 ? "%" PRIu64 : aRadix == 8 ? "%" PRIo64 : "%" PRIx64,
|
|
|
|
aInteger);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* Append the given float to this string
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL AppendFloat(float aFloat);
|
|
|
|
void NS_FASTCALL AppendFloat(double aFloat);
|
2014-05-05 21:30:46 +04:00
|
|
|
public:
|
|
|
|
|
|
|
|
// AppendLiteral must ONLY be applied to an actual literal string.
|
|
|
|
// Do not attempt to use it with a regular char* pointer, or with a char
|
|
|
|
// array variable. Use Append or AppendASCII for those.
|
|
|
|
template<int N>
|
2014-05-27 11:15:35 +04:00
|
|
|
void AppendLiteral(const char_type (&aStr)[N])
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
ReplaceLiteral(base_string_type::mLength, 0, aStr, N - 1);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
|
|
|
|
// Only enable for T = char16_t
|
|
|
|
template<int N, typename EnableIfChar16 = IsChar16>
|
|
|
|
void AppendLiteral(const incompatible_char_type (&aStr)[N])
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
AppendASCII(aStr, N - 1);
|
|
|
|
}
|
2015-03-17 11:55:22 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
// Only enable for T = char16_t
|
|
|
|
template<int N, typename EnableIfChar16 = IsChar16>
|
|
|
|
MOZ_MUST_USE bool
|
|
|
|
AppendLiteral(const incompatible_char_type (&aStr)[N], const fallible_t& aFallible)
|
2015-03-17 11:55:22 +03:00
|
|
|
{
|
|
|
|
return AppendASCII(aStr, N - 1, aFallible);
|
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
self_type& operator+=(char_type aChar)
|
|
|
|
{
|
|
|
|
Append(aChar);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
self_type& operator+=(const char_type* aData)
|
|
|
|
{
|
|
|
|
Append(aData);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
self_type& operator+=(char16ptr_t aData)
|
|
|
|
{
|
|
|
|
Append(aData);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
#endif
|
2014-05-27 11:15:35 +04:00
|
|
|
self_type& operator+=(const self_type& aStr)
|
|
|
|
{
|
|
|
|
Append(aStr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
self_type& operator+=(const substring_tuple_type& aTuple)
|
|
|
|
{
|
|
|
|
Append(aTuple);
|
|
|
|
return *this;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void Insert(char_type aChar, index_type aPos)
|
|
|
|
{
|
|
|
|
Replace(aPos, 0, aChar);
|
|
|
|
}
|
|
|
|
void Insert(const char_type* aData, index_type aPos,
|
|
|
|
size_type aLength = size_type(-1))
|
|
|
|
{
|
|
|
|
Replace(aPos, 0, aData, aLength);
|
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
void Insert(char16ptr_t aData, index_type aPos,
|
|
|
|
size_type aLength = size_type(-1))
|
|
|
|
{
|
|
|
|
Insert(static_cast<const char16_t*>(aData), aPos, aLength);
|
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
#endif
|
2014-05-27 11:15:35 +04:00
|
|
|
void Insert(const self_type& aStr, index_type aPos)
|
|
|
|
{
|
|
|
|
Replace(aPos, 0, aStr);
|
|
|
|
}
|
|
|
|
void Insert(const substring_tuple_type& aTuple, index_type aPos)
|
|
|
|
{
|
|
|
|
Replace(aPos, 0, aTuple);
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
// InsertLiteral must ONLY be applied to an actual literal string.
|
|
|
|
// Do not attempt to use it with a regular char* pointer, or with a char
|
|
|
|
// array variable. Use Insert for those.
|
|
|
|
template<int N>
|
2014-05-27 11:15:35 +04:00
|
|
|
void InsertLiteral(const char_type (&aStr)[N], index_type aPos)
|
|
|
|
{
|
|
|
|
ReplaceLiteral(aPos, 0, aStr, N - 1);
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void Cut(index_type aCutStart, size_type aCutLength)
|
|
|
|
{
|
|
|
|
Replace(aCutStart, aCutLength, char_traits::sEmptyBuffer, 0);
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstringSplitter<T> Split(const char_type aChar) const;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buffer sizing
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to set the capacity to the given size in number of
|
|
|
|
* characters, without affecting the length of the string.
|
|
|
|
* There is no need to include room for the null terminator: it is
|
|
|
|
* the job of the string class.
|
|
|
|
* Also ensures that the buffer is mutable.
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL SetCapacity(size_type aNewCapacity);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL SetCapacity(size_type aNewCapacity,
|
|
|
|
const fallible_t&);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL SetLength(size_type aNewLength);
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL SetLength(size_type aNewLength,
|
|
|
|
const fallible_t&);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void Truncate(size_type aNewLength = 0)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
NS_ASSERTION(aNewLength <= base_string_type::mLength, "Truncate cannot make string longer");
|
2014-05-27 11:15:35 +04:00
|
|
|
SetLength(aNewLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* buffer access
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a const pointer to the string's internal buffer. The caller
|
|
|
|
* MUST NOT modify the characters at the returned address.
|
|
|
|
*
|
|
|
|
* @returns The length of the buffer in characters.
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
inline size_type GetData(const char_type** aData) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
*aData = base_string_type::mData;
|
|
|
|
return base_string_type::mLength;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a pointer to the string's internal buffer, optionally resizing
|
|
|
|
* the buffer first. If size_type(-1) is passed for newLen, then the
|
|
|
|
* current length of the string is used. The caller MAY modify the
|
|
|
|
* characters at the returned address (up to but not exceeding the
|
|
|
|
* length of the string).
|
|
|
|
*
|
|
|
|
* @returns The length of the buffer in characters or 0 if unable to
|
|
|
|
* satisfy the request due to low-memory conditions.
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type GetMutableData(char_type** aData, size_type aNewLen = size_type(-1))
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!EnsureMutable(aNewLen)) {
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(aNewLen == size_type(-1) ? base_string_type::mLength : aNewLen);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
*aData = base_string_type::mData;
|
|
|
|
return base_string_type::mLength;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type GetMutableData(char_type** aData, size_type aNewLen, const fallible_t&)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!EnsureMutable(aNewLen)) {
|
|
|
|
*aData = nullptr;
|
2014-05-05 21:30:46 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
*aData = base_string_type::mData;
|
|
|
|
return base_string_type::mLength;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2006-06-29 02:11:39 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
#if defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type GetMutableData(wchar_t** aData, size_type aNewLen = size_type(-1))
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return GetMutableData(reinterpret_cast<char16_t**>(aData), aNewLen);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar16 = IsChar16>
|
2015-01-28 12:00:40 +03:00
|
|
|
size_type GetMutableData(wchar_t** aData, size_type aNewLen,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-08-25 23:17:24 +04:00
|
|
|
return GetMutableData(reinterpret_cast<char16_t**>(aData), aNewLen,
|
2015-01-28 12:00:40 +03:00
|
|
|
aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2013-11-27 17:40:54 +04:00
|
|
|
#endif
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
|
|
|
* Span integration
|
|
|
|
*/
|
|
|
|
|
|
|
|
operator mozilla::Span<char_type>()
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return mozilla::MakeSpan(BeginWriting(), base_string_type::Length());
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
operator mozilla::Span<const char_type>() const
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return mozilla::MakeSpan(base_string_type::BeginReading(), base_string_type::Length());
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Append(mozilla::Span<const char_type> aSpan)
|
|
|
|
{
|
|
|
|
auto len = aSpan.Length();
|
|
|
|
MOZ_RELEASE_ASSERT(len <= mozilla::MaxValue<size_type>::value);
|
|
|
|
Append(aSpan.Elements(), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MUST_USE bool Append(mozilla::Span<const char_type> aSpan,
|
|
|
|
const fallible_t& aFallible)
|
|
|
|
{
|
|
|
|
auto len = aSpan.Length();
|
|
|
|
if (len > mozilla::MaxValue<size_type>::value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Append(aSpan.Elements(), len, aFallible);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar = IsChar>
|
2017-02-16 12:43:50 +03:00
|
|
|
operator mozilla::Span<uint8_t>()
|
|
|
|
{
|
|
|
|
return mozilla::MakeSpan(reinterpret_cast<uint8_t*>(BeginWriting()),
|
2017-08-15 00:22:50 +03:00
|
|
|
base_string_type::Length());
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar = IsChar>
|
2017-02-16 12:43:50 +03:00
|
|
|
operator mozilla::Span<const uint8_t>() const
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return mozilla::MakeSpan(reinterpret_cast<const uint8_t*>(base_string_type::BeginReading()),
|
|
|
|
base_string_type::Length());
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar = IsChar>
|
2017-02-16 12:43:50 +03:00
|
|
|
void Append(mozilla::Span<const uint8_t> aSpan)
|
|
|
|
{
|
|
|
|
auto len = aSpan.Length();
|
|
|
|
MOZ_RELEASE_ASSERT(len <= mozilla::MaxValue<size_type>::value);
|
|
|
|
Append(reinterpret_cast<const char*>(aSpan.Elements()), len);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename EnableIfChar = IsChar>
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_MUST_USE bool Append(mozilla::Span<const uint8_t> aSpan,
|
|
|
|
const fallible_t& aFallible)
|
|
|
|
{
|
|
|
|
auto len = aSpan.Length();
|
|
|
|
if (len > mozilla::MaxValue<size_type>::value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Append(
|
|
|
|
reinterpret_cast<const char*>(aSpan.Elements()), len, aFallible);
|
|
|
|
}
|
2006-06-29 02:11:39 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* string data is never null, but can be marked void. if true, the
|
|
|
|
* string will be truncated. @see nsTSubstring::IsVoid
|
|
|
|
*/
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL SetIsVoid(bool);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to remove all occurrences of aChar from this
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* @param aChar -- char to be stripped
|
|
|
|
*/
|
|
|
|
|
2017-05-17 20:49:52 +03:00
|
|
|
void StripChar(char_type aChar);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to remove all occurrences of aChars from this
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* @param aChars -- chars to be stripped
|
|
|
|
*/
|
|
|
|
|
2017-05-17 20:49:52 +03:00
|
|
|
void StripChars(const char_type* aChars);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-05-10 20:12:36 +03:00
|
|
|
/**
|
|
|
|
* This method is used to remove all occurrences of some characters this
|
|
|
|
* from this string. The characters removed have the corresponding
|
|
|
|
* entries in the bool array set to true; we retain all characters
|
|
|
|
* with code beyond 127.
|
|
|
|
* THE CALLER IS RESPONSIBLE for making sure the complete boolean
|
|
|
|
* array, 128 entries, is properly initialized.
|
|
|
|
*
|
|
|
|
* See also: ASCIIMask class.
|
|
|
|
*
|
|
|
|
* @param aToStrip -- Array where each entry is true if the
|
|
|
|
* corresponding ASCII character is to be stripped. All
|
|
|
|
* characters beyond code 127 are retained. Note that this
|
|
|
|
* parameter is of ASCIIMaskArray type, but we expand the typedef
|
|
|
|
* to avoid having to include nsASCIIMask.h in this include file
|
|
|
|
* as it brings other includes.
|
|
|
|
*/
|
2017-05-17 20:49:52 +03:00
|
|
|
void StripTaggedASCII(const std::array<bool, 128>& aToStrip);
|
2017-05-10 20:12:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A shortcut to strip \r and \n.
|
|
|
|
*/
|
2017-05-17 20:49:52 +03:00
|
|
|
void StripCRLF();
|
2017-05-10 20:12:36 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* If the string uses a shared buffer, this method
|
|
|
|
* clears the pointer without releasing the buffer.
|
|
|
|
*/
|
|
|
|
void ForgetSharedBuffer()
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (base_string_type::mDataFlags & DataFlags::SHARED) {
|
2017-07-21 01:46:59 +03:00
|
|
|
SetToEmptyBuffer();
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 01:46:59 +03:00
|
|
|
protected:
|
|
|
|
void AssertValid()
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
MOZ_ASSERT(!(this->mClassFlags & ClassFlags::NULL_TERMINATED) ||
|
|
|
|
(this->mDataFlags & DataFlags::TERMINATED),
|
2017-07-21 01:46:59 +03:00
|
|
|
"String classes whose static type guarantees a null-terminated "
|
|
|
|
"buffer must not be assigned a non-null-terminated buffer.");
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this is public to support automatic conversion of tuple to string
|
|
|
|
* base type, which helps avoid converting to nsTAString.
|
|
|
|
*/
|
2017-08-15 00:22:50 +03:00
|
|
|
MOZ_IMPLICIT nsTSubstring(const substring_tuple_type& aTuple)
|
|
|
|
: base_string_type(nullptr, 0, DataFlags(0), ClassFlags(0))
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2014-05-27 11:15:35 +04:00
|
|
|
Assign(aTuple);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
size_t SizeOfExcludingThisIfUnshared(mozilla::MallocSizeOf aMallocSizeOf)
|
2014-05-05 21:30:46 +04:00
|
|
|
const;
|
2014-05-27 11:15:35 +04:00
|
|
|
size_t SizeOfIncludingThisIfUnshared(mozilla::MallocSizeOf aMallocSizeOf)
|
2014-05-05 21:30:46 +04:00
|
|
|
const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WARNING: Only use these functions if you really know what you are
|
|
|
|
* doing, because they can easily lead to double-counting strings. If
|
|
|
|
* you do use them, please explain clearly in a comment why it's safe
|
|
|
|
* and won't lead to double-counting.
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
size_t SizeOfExcludingThisEvenIfShared(mozilla::MallocSizeOf aMallocSizeOf)
|
2014-05-05 21:30:46 +04:00
|
|
|
const;
|
2014-05-27 11:15:35 +04:00
|
|
|
size_t SizeOfIncludingThisEvenIfShared(mozilla::MallocSizeOf aMallocSizeOf)
|
2014-05-05 21:30:46 +04:00
|
|
|
const;
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template<class N>
|
2014-11-12 11:13:44 +03:00
|
|
|
void NS_ABORT_OOM(T)
|
|
|
|
{
|
2014-11-12 22:57:09 +03:00
|
|
|
struct never {}; // a compiler-friendly way to do static_assert(false)
|
2017-08-15 00:22:50 +03:00
|
|
|
static_assert(mozilla::IsSame<N, never>::value,
|
2014-11-12 11:13:44 +03:00
|
|
|
"In string classes, use AllocFailed to account for sizeof(char_type). "
|
|
|
|
"Use the global ::NS_ABORT_OOM if you really have a count of bytes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE void AllocFailed(size_t aLength)
|
|
|
|
{
|
|
|
|
::NS_ABORT_OOM(aLength * sizeof(char_type));
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// default initialization
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring()
|
|
|
|
: base_string_type(char_traits::sEmptyBuffer, 0, DataFlags::TERMINATED,
|
|
|
|
ClassFlags(0))
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-03-14 06:52:24 +03:00
|
|
|
// copy-constructor, constructs as dependent on given object
|
|
|
|
// (NOTE: this is for internal use only)
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring(const self_type& aStr)
|
|
|
|
: base_string_type(aStr.base_string_type::mData, aStr.base_string_type::mLength,
|
|
|
|
aStr.base_string_type::mDataFlags & (DataFlags::TERMINATED | DataFlags::VOIDED),
|
|
|
|
ClassFlags(0))
|
2017-03-13 23:28:49 +03:00
|
|
|
{
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2017-03-13 23:28:49 +03:00
|
|
|
}
|
|
|
|
|
2017-07-21 01:46:58 +03:00
|
|
|
// initialization with ClassFlags
|
2017-08-15 00:22:50 +03:00
|
|
|
explicit nsTSubstring(ClassFlags aClassFlags)
|
|
|
|
: base_string_type(char_traits::sEmptyBuffer, 0, DataFlags::TERMINATED,
|
|
|
|
aClassFlags)
|
2017-07-21 01:46:58 +03:00
|
|
|
{
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2017-07-21 01:46:58 +03:00
|
|
|
}
|
|
|
|
|
2017-03-13 23:28:49 +03:00
|
|
|
/**
|
|
|
|
* allows for direct initialization of a nsTSubstring object.
|
|
|
|
*/
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring(char_type* aData, size_type aLength,
|
|
|
|
DataFlags aDataFlags, ClassFlags aClassFlags)
|
2017-07-10 22:23:10 +03:00
|
|
|
// XXXbz or can I just include nscore.h and use NS_BUILD_REFCNT_LOGGING?
|
2017-03-13 23:28:49 +03:00
|
|
|
#if defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING)
|
|
|
|
#define XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE
|
2017-07-10 22:23:10 +03:00
|
|
|
;
|
2017-03-13 23:28:49 +03:00
|
|
|
#else
|
|
|
|
#undef XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE
|
2017-08-15 00:22:50 +03:00
|
|
|
: base_string_type(aData, aLength, aDataFlags, aClassFlags)
|
2017-03-13 23:28:49 +03:00
|
|
|
{
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2017-05-16 02:52:43 +03:00
|
|
|
MOZ_RELEASE_ASSERT(CheckCapacity(aLength), "String is too large.");
|
2017-03-13 23:28:49 +03:00
|
|
|
}
|
|
|
|
#endif /* DEBUG || FORCE_BUILD_REFCNT_LOGGING */
|
|
|
|
|
2017-07-21 01:46:59 +03:00
|
|
|
void SetToEmptyBuffer()
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
base_string_type::mData = char_traits::sEmptyBuffer;
|
|
|
|
base_string_type::mLength = 0;
|
|
|
|
base_string_type::mDataFlags = DataFlags::TERMINATED;
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2017-07-21 01:46:59 +03:00
|
|
|
}
|
|
|
|
|
2017-07-21 01:46:59 +03:00
|
|
|
void SetData(char_type* aData, size_type aLength, DataFlags aDataFlags)
|
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
base_string_type::mData = aData;
|
|
|
|
base_string_type::mLength = aLength;
|
|
|
|
base_string_type::mDataFlags = aDataFlags;
|
2017-07-21 01:46:59 +03:00
|
|
|
AssertValid();
|
2017-07-21 01:46:59 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* this function releases mData and does not change the value of
|
|
|
|
* any of its member variables. in other words, this function acts
|
|
|
|
* like a destructor.
|
|
|
|
*/
|
2016-04-07 19:50:01 +03:00
|
|
|
void NS_FASTCALL Finalize();
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this function prepares mData to be mutated.
|
|
|
|
*
|
2014-05-27 11:15:35 +04:00
|
|
|
* @param aCapacity specifies the required capacity of mData
|
|
|
|
* @param aOldData returns null or the old value of mData
|
2017-07-10 22:23:10 +03:00
|
|
|
* @param aOldFlags returns 0 or the old value of mDataFlags
|
2014-05-05 21:30:46 +04:00
|
|
|
*
|
|
|
|
* if mData is already mutable and of sufficient capacity, then this
|
|
|
|
* function will return immediately. otherwise, it will either resize
|
|
|
|
* mData or allocate a new shared buffer. if it needs to allocate a
|
|
|
|
* new buffer, then it will return the old buffer and the corresponding
|
|
|
|
* flags. this allows the caller to decide when to free the old data.
|
|
|
|
*
|
|
|
|
* this function returns false if is unable to allocate sufficient
|
|
|
|
* memory.
|
|
|
|
*
|
|
|
|
* XXX we should expose a way for subclasses to free old_data.
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
bool NS_FASTCALL MutatePrep(size_type aCapacity,
|
2017-07-10 22:23:10 +03:00
|
|
|
char_type** aOldData, DataFlags* aOldDataFlags);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this function prepares a section of mData to be modified. if
|
|
|
|
* necessary, this function will reallocate mData and possibly move
|
|
|
|
* existing data to open up the specified section.
|
|
|
|
*
|
2014-05-27 11:15:35 +04:00
|
|
|
* @param aCutStart specifies the starting offset of the section
|
|
|
|
* @param aCutLength specifies the length of the section to be replaced
|
|
|
|
* @param aNewLength specifies the length of the new section
|
2014-05-05 21:30:46 +04:00
|
|
|
*
|
|
|
|
* for example, suppose mData contains the string "abcdef" then
|
|
|
|
*
|
|
|
|
* ReplacePrep(2, 3, 4);
|
|
|
|
*
|
|
|
|
* would cause mData to look like "ab____f" where the characters
|
|
|
|
* indicated by '_' have an unspecified value and can be freely
|
|
|
|
* modified. this function will null-terminate mData upon return.
|
|
|
|
*
|
|
|
|
* this function returns false if is unable to allocate sufficient
|
|
|
|
* memory.
|
|
|
|
*/
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool ReplacePrep(index_type aCutStart,
|
|
|
|
size_type aCutLength,
|
|
|
|
size_type aNewLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL ReplacePrepInternal(
|
2014-05-27 11:15:35 +04:00
|
|
|
index_type aCutStart,
|
|
|
|
size_type aCutLength,
|
|
|
|
size_type aNewFragLength,
|
|
|
|
size_type aNewTotalLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the number of writable storage units starting at mData.
|
|
|
|
* the value does not include space for the null-terminator character.
|
|
|
|
*
|
|
|
|
* NOTE: this function returns 0 if mData is immutable (or the buffer
|
|
|
|
* is 0-sized).
|
|
|
|
*/
|
|
|
|
size_type NS_FASTCALL Capacity() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this helper function can be called prior to directly manipulating
|
|
|
|
* the contents of mData. see, for example, BeginWriting.
|
|
|
|
*/
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool NS_FASTCALL EnsureMutable(
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type aNewLen = size_type(-1));
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-03-23 22:32:53 +03:00
|
|
|
/**
|
|
|
|
* Checks if the given capacity is valid for this string type.
|
|
|
|
*/
|
|
|
|
static MOZ_MUST_USE bool CheckCapacity(size_type aCapacity) {
|
|
|
|
if (aCapacity > kMaxCapacity) {
|
|
|
|
// Also assert for |aCapacity| equal to |size_type(-1)|, since we used to
|
|
|
|
// use that value to flag immutability.
|
|
|
|
NS_ASSERTION(aCapacity != size_type(-1), "Bogus capacity");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL ReplaceLiteral(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-03-23 22:32:53 +03:00
|
|
|
static const size_type kMaxCapacity;
|
2014-05-05 21:30:46 +04:00
|
|
|
public:
|
|
|
|
|
|
|
|
// NOTE: this method is declared public _only_ for convenience for
|
|
|
|
// callers who don't have access to the original nsLiteralString_CharT.
|
2014-05-27 11:15:35 +04:00
|
|
|
void NS_FASTCALL AssignLiteral(const char_type* aData, size_type aLength);
|
2017-03-14 06:52:24 +03:00
|
|
|
};
|
2017-03-14 00:01:35 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
extern template class nsTSubstring<char>;
|
|
|
|
extern template class nsTSubstring<char16_t>;
|
2005-01-24 19:44:41 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
static_assert(sizeof(nsTSubstring<char>) ==
|
|
|
|
sizeof(mozilla::detail::nsTStringRepr<char>),
|
|
|
|
"Don't add new data fields to nsTSubstring_CharT. "
|
|
|
|
"Add to nsTStringRepr<T> instead.");
|
2005-01-24 19:44:41 +03:00
|
|
|
|
2017-01-30 14:34:35 +03:00
|
|
|
|
|
|
|
// You should not need to instantiate this class directly.
|
|
|
|
// Use nsTSubstring::Split instead.
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
class nsTSubstringSplitter
|
2017-01-30 14:34:35 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef typename nsTSubstring<T>::size_type size_type;
|
|
|
|
typedef typename nsTSubstring<T>::char_type char_type;
|
2017-03-13 05:56:23 +03:00
|
|
|
|
2017-01-30 14:34:35 +03:00
|
|
|
class nsTSubstringSplit_Iter
|
|
|
|
{
|
|
|
|
public:
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstringSplit_Iter(const nsTSubstringSplitter<T>& aObj,
|
2017-03-13 05:56:23 +03:00
|
|
|
size_type aPos)
|
2017-01-30 14:34:35 +03:00
|
|
|
: mObj(aObj)
|
|
|
|
, mPos(aPos)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const nsTSubstringSplit_Iter& other) const
|
|
|
|
{
|
|
|
|
return mPos != other.mPos;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
const nsTDependentSubstring<T>& operator*() const;
|
2017-01-30 14:34:35 +03:00
|
|
|
|
|
|
|
const nsTSubstringSplit_Iter& operator++()
|
|
|
|
{
|
|
|
|
++mPos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-15 00:22:50 +03:00
|
|
|
const nsTSubstringSplitter<T>& mObj;
|
2017-03-13 05:56:23 +03:00
|
|
|
size_type mPos;
|
2017-01-30 14:34:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2017-08-15 00:22:50 +03:00
|
|
|
const nsTSubstring<T>* const mStr;
|
|
|
|
mozilla::UniquePtr<nsTDependentSubstring<T>[]> mArray;
|
2017-03-13 05:56:23 +03:00
|
|
|
size_type mArraySize;
|
|
|
|
const char_type mDelim;
|
2017-01-30 14:34:35 +03:00
|
|
|
|
|
|
|
public:
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstringSplitter(const nsTSubstring<T>* aStr, char_type aDelim);
|
2017-01-30 14:34:35 +03:00
|
|
|
|
|
|
|
nsTSubstringSplit_Iter begin() const
|
|
|
|
{
|
|
|
|
return nsTSubstringSplit_Iter(*this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTSubstringSplit_Iter end() const
|
|
|
|
{
|
|
|
|
return nsTSubstringSplit_Iter(*this, mArraySize);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
const nsTDependentSubstring<T>& Get(const size_type index) const
|
2017-01-30 14:34:35 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(index < mArraySize);
|
|
|
|
return mArray[index];
|
|
|
|
}
|
|
|
|
};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
extern template class nsTSubstringSplitter<char>;
|
|
|
|
extern template class nsTSubstringSplitter<char16_t>;
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
|
|
|
* Span integration
|
|
|
|
*/
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
inline Span<char>
|
|
|
|
MakeSpan(nsTSubstring<char>& aString)
|
|
|
|
{
|
|
|
|
return aString;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Span<const char>
|
|
|
|
MakeSpan(const nsTSubstring<char>& aString)
|
|
|
|
{
|
|
|
|
return aString;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Span<char16_t>
|
|
|
|
MakeSpan(nsTSubstring<char16_t>& aString)
|
2017-02-16 12:43:50 +03:00
|
|
|
{
|
|
|
|
return aString;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
inline Span<const char16_t>
|
|
|
|
MakeSpan(const nsTSubstring<char16_t>& aString)
|
2017-02-16 12:43:50 +03:00
|
|
|
{
|
|
|
|
return aString;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
} // namespace mozilla
|
2017-08-15 00:22:50 +03:00
|
|
|
|
|
|
|
#endif
|