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/. */
|
2014-09-17 10:33:05 +04:00
|
|
|
|
2017-05-10 20:12:36 +03:00
|
|
|
#include "nsASCIIMask.h"
|
2015-07-06 21:27:35 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2014-09-17 10:33:05 +04:00
|
|
|
#include "mozilla/double-conversion.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-12-14 19:32:21 +03:00
|
|
|
#include "mozilla/Printf.h"
|
2017-09-13 04:50:05 +03:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2013-09-24 21:36:55 +04:00
|
|
|
|
|
|
|
using double_conversion::DoubleToStringConverter;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
const typename nsTSubstring<T>::size_type nsTSubstring<T>::kMaxCapacity =
|
|
|
|
(nsTSubstring<T>::size_type(-1) /
|
2017-03-23 22:32:53 +03:00
|
|
|
2 - sizeof(nsStringBuffer)) /
|
2017-08-15 00:22:50 +03:00
|
|
|
sizeof(nsTSubstring<T>::char_type) - 2;
|
2017-03-23 22:32:53 +03:00
|
|
|
|
2010-08-07 04:46:51 +04:00
|
|
|
#ifdef XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
nsTSubstring<T>::nsTSubstring(char_type* aData, size_type aLength,
|
|
|
|
DataFlags aDataFlags,
|
|
|
|
ClassFlags aClassFlags)
|
|
|
|
: ::mozilla::detail::nsTStringRepr<T>(aData, aLength, aDataFlags, aClassFlags)
|
2014-05-05 21:30:46 +04: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-07-10 22:23:10 +03:00
|
|
|
if (aDataFlags & DataFlags::OWNED) {
|
2014-05-05 21:30:46 +04:00
|
|
|
STRING_STAT_INCREMENT(Adopt);
|
2017-08-15 00:22:50 +03:00
|
|
|
MOZ_LOG_CTOR(this->mData, "StringAdopt", 1);
|
2006-11-10 23:05:04 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2010-08-07 04:46:51 +04:00
|
|
|
#endif /* XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE */
|
2008-08-18 19:40:49 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
2017-09-27 13:19:33 +03:00
|
|
|
* helper function for down-casting a nsTSubstring to an nsTAutoString.
|
2014-05-05 21:30:46 +04:00
|
|
|
*/
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-09-27 13:19:33 +03:00
|
|
|
inline const nsTAutoString<T>*
|
|
|
|
AsAutoString(const nsTSubstring<T>* aStr)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-09-27 13:19:33 +03:00
|
|
|
return static_cast<const nsTAutoString<T>*>(aStr);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-20 04:53:23 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* this function is called to prepare mData for writing. the given capacity
|
|
|
|
* indicates the required minimum storage size for mData, in sizeof(char_type)
|
|
|
|
* increments. this function returns true if the operation succeeds. it also
|
|
|
|
* returns the old data and old flags members if mData is newly allocated.
|
|
|
|
* the old data must be released by the caller.
|
|
|
|
*/
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::MutatePrep(size_type aCapacity, char_type** aOldData,
|
|
|
|
DataFlags* aOldDataFlags)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// initialize to no old data
|
2014-05-27 11:15:35 +04:00
|
|
|
*aOldData = nullptr;
|
2017-07-10 22:23:10 +03:00
|
|
|
*aOldDataFlags = DataFlags(0);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
size_type curCapacity = Capacity();
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
// If |aCapacity > kMaxCapacity|, then our doubling algorithm may not be
|
2014-05-05 21:30:46 +04:00
|
|
|
// able to allocate it. Just bail out in cases like that. We don't want
|
|
|
|
// to be allocating 2GB+ strings anyway.
|
2015-04-20 20:56:46 +03:00
|
|
|
static_assert((sizeof(nsStringBuffer) & 0x1) == 0,
|
|
|
|
"bad size for nsStringBuffer");
|
2017-03-23 22:32:53 +03:00
|
|
|
if (!CheckCapacity(aCapacity)) {
|
|
|
|
return false;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// |curCapacity == 0| means that the buffer is immutable or 0-sized, so we
|
|
|
|
// need to allocate a new buffer. We cannot use the existing buffer even
|
|
|
|
// though it might be large enough.
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (curCapacity != 0) {
|
|
|
|
if (aCapacity <= curCapacity) {
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mDataFlags &= ~DataFlags::VOIDED; // mutation clears voided flag
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
2004-10-20 01:46:45 +04:00
|
|
|
}
|
2016-09-08 20:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (curCapacity < aCapacity) {
|
|
|
|
// We increase our capacity so that the allocated buffer grows
|
|
|
|
// exponentially, which gives us amortized O(1) appending. Below the
|
|
|
|
// threshold, we use powers-of-two. Above the threshold, we grow by at
|
|
|
|
// least 1.125, rounding up to the nearest MiB.
|
|
|
|
const size_type slowGrowthThreshold = 8 * 1024 * 1024;
|
2004-10-20 01:46:45 +04:00
|
|
|
|
2016-12-22 01:23:07 +03:00
|
|
|
// nsStringBuffer allocates sizeof(nsStringBuffer) + passed size, and
|
|
|
|
// storageSize below wants extra 1 * sizeof(char_type).
|
|
|
|
const size_type neededExtraSpace =
|
|
|
|
sizeof(nsStringBuffer) / sizeof(char_type) + 1;
|
|
|
|
|
2016-09-08 20:49:49 +03:00
|
|
|
size_type temp;
|
|
|
|
if (aCapacity >= slowGrowthThreshold) {
|
|
|
|
size_type minNewCapacity = curCapacity + (curCapacity >> 3); // multiply by 1.125
|
2016-12-22 01:23:07 +03:00
|
|
|
temp = XPCOM_MAX(aCapacity, minNewCapacity) + neededExtraSpace;
|
2016-09-08 20:49:49 +03:00
|
|
|
|
2016-12-22 01:23:07 +03:00
|
|
|
// Round up to the next multiple of MiB, but ensure the expected
|
|
|
|
// capacity doesn't include the extra space required by nsStringBuffer
|
|
|
|
// and null-termination.
|
2016-09-08 20:49:49 +03:00
|
|
|
const size_t MiB = 1 << 20;
|
2016-12-22 01:23:07 +03:00
|
|
|
temp = (MiB * ((temp + MiB - 1) / MiB)) - neededExtraSpace;
|
2016-09-08 20:49:49 +03:00
|
|
|
} else {
|
|
|
|
// Round up to the next power of two.
|
2016-12-22 01:23:07 +03:00
|
|
|
temp =
|
|
|
|
mozilla::RoundUpPow2(aCapacity + neededExtraSpace) - neededExtraSpace;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2016-09-08 20:49:49 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(XPCOM_MIN(temp, kMaxCapacity) >= aCapacity,
|
|
|
|
"should have hit the early return at the top");
|
2014-05-27 11:15:35 +04:00
|
|
|
aCapacity = XPCOM_MIN(temp, kMaxCapacity);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//
|
|
|
|
// several cases:
|
|
|
|
//
|
2017-08-15 00:22:50 +03:00
|
|
|
// (1) we have a shared buffer (this->mDataFlags & DataFlags::SHARED)
|
|
|
|
// (2) we have an owned buffer (this->mDataFlags & DataFlags::OWNED)
|
2017-09-27 13:19:33 +03:00
|
|
|
// (3) we have an inline buffer (this->mDataFlags & DataFlags::INLINE)
|
2014-05-05 21:30:46 +04:00
|
|
|
// (4) we have a readonly buffer
|
|
|
|
//
|
|
|
|
// requiring that we in some cases preserve the data before creating
|
|
|
|
// a new buffer complicates things just a bit ;-)
|
|
|
|
//
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type storageSize = (aCapacity + 1) * sizeof(char_type);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// case #1
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mDataFlags & DataFlags::SHARED) {
|
|
|
|
nsStringBuffer* hdr = nsStringBuffer::FromData(this->mData);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!hdr->IsReadonly()) {
|
|
|
|
nsStringBuffer* newHdr = nsStringBuffer::Realloc(hdr, storageSize);
|
|
|
|
if (!newHdr) {
|
|
|
|
return false; // out-of-memory (original header left intact)
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
hdr = newHdr;
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mData = (char_type*)hdr->Data();
|
|
|
|
this->mDataFlags &= ~DataFlags::VOIDED; // mutation clears voided flag
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
char_type* newData;
|
2017-07-10 22:23:10 +03:00
|
|
|
DataFlags newDataFlags;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-09-27 13:19:33 +03:00
|
|
|
// If this is an nsTAutoStringN whose inline buffer is sufficiently large,
|
|
|
|
// then use it. This helps avoid heap allocations.
|
|
|
|
if ((this->mClassFlags & ClassFlags::INLINE) &&
|
|
|
|
(aCapacity < AsAutoString(this)->mInlineCapacity)) {
|
|
|
|
newData = (char_type*)AsAutoString(this)->mStorage;
|
|
|
|
newDataFlags = DataFlags::TERMINATED | DataFlags::INLINE;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2014-05-05 21:30:46 +04:00
|
|
|
// if we reach here then, we must allocate a new buffer. we cannot
|
2017-09-27 13:19:33 +03:00
|
|
|
// make use of our DataFlags::OWNED or DataFlags::INLINE buffers because
|
|
|
|
// they are not large enough.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
nsStringBuffer* newHdr =
|
|
|
|
nsStringBuffer::Alloc(storageSize).take();
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!newHdr) {
|
|
|
|
return false; // we are still in a consistent state
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
newData = (char_type*)newHdr->Data();
|
2017-07-10 22:23:10 +03:00
|
|
|
newDataFlags = DataFlags::TERMINATED | DataFlags::SHARED;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// save old data and flags
|
2017-08-15 00:22:50 +03:00
|
|
|
*aOldData = this->mData;
|
|
|
|
*aOldDataFlags = this->mDataFlags;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
// this->mLength does not change
|
|
|
|
SetData(newData, this->mLength, newDataFlags);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
// though we are not necessarily terminated at the moment, now is probably
|
2017-07-10 22:23:10 +03:00
|
|
|
// still the best time to set DataFlags::TERMINATED.
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Finalize()
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
|
|
|
// this->mData, this->mLength, and this->mDataFlags are purposefully left dangling
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2015-07-06 21:27:35 +03:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::ReplacePrep(index_type aCutStart,
|
|
|
|
size_type aCutLength,
|
|
|
|
size_type aNewLength)
|
2015-07-06 21:27:35 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutLength = XPCOM_MIN(aCutLength, this->mLength - aCutStart);
|
2015-07-06 21:27:35 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
mozilla::CheckedInt<size_type> newTotalLen = this->mLength;
|
2015-07-06 21:27:35 +03:00
|
|
|
newTotalLen += aNewLength;
|
|
|
|
newTotalLen -= aCutLength;
|
|
|
|
if (!newTotalLen.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aCutStart == this->mLength && Capacity() > newTotalLen.value()) {
|
|
|
|
this->mDataFlags &= ~DataFlags::VOIDED;
|
|
|
|
this->mData[newTotalLen.value()] = char_type(0);
|
|
|
|
this->mLength = newTotalLen.value();
|
2015-07-06 21:27:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReplacePrepInternal(aCutStart, aCutLength, aNewLength,
|
|
|
|
newTotalLen.value());
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::ReplacePrepInternal(index_type aCutStart, size_type aCutLen,
|
|
|
|
size_type aFragLen, size_type aNewLen)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
char_type* oldData;
|
2017-07-10 22:23:10 +03:00
|
|
|
DataFlags oldFlags;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!MutatePrep(aNewLen, &oldData, &oldFlags)) {
|
|
|
|
return false; // out-of-memory
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (oldData) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// determine whether or not we need to copy part of the old string
|
|
|
|
// over to the new string.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aCutStart > 0) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// copy prefix from old string
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copy(this->mData, oldData, aCutStart);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2005-02-16 10:20:20 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aCutStart + aCutLen < this->mLength) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// copy suffix from old string to new offset
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type from = aCutStart + aCutLen;
|
2017-08-15 00:22:50 +03:00
|
|
|
size_type fromLen = this->mLength - from;
|
2014-05-27 11:15:35 +04:00
|
|
|
uint32_t to = aCutStart + aFragLen;
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copy(this->mData + to, oldData + from, fromLen);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
::ReleaseData(oldData, oldFlags);
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2014-05-05 21:30:46 +04:00
|
|
|
// original data remains intact
|
|
|
|
|
|
|
|
// determine whether or not we need to move part of the existing string
|
|
|
|
// to make room for the requested hole.
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aFragLen != aCutLen && aCutStart + aCutLen < this->mLength) {
|
2014-05-27 11:15:35 +04:00
|
|
|
uint32_t from = aCutStart + aCutLen;
|
2017-08-15 00:22:50 +03:00
|
|
|
uint32_t fromLen = this->mLength - from;
|
2014-05-27 11:15:35 +04:00
|
|
|
uint32_t to = aCutStart + aFragLen;
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::move(this->mData + to, this->mData + from, fromLen);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
// add null terminator (mutable this->mData always has room for the null-
|
2014-05-05 21:30:46 +04:00
|
|
|
// terminator).
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mData[aNewLen] = char_type(0);
|
|
|
|
this->mLength = aNewLen;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
typename nsTSubstring<T>::size_type
|
|
|
|
nsTSubstring<T>::Capacity() const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// return 0 to indicate an immutable or 0-sized buffer
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
size_type capacity;
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mDataFlags & DataFlags::SHARED) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// if the string is readonly, then we pretend that it has no capacity.
|
2017-08-15 00:22:50 +03:00
|
|
|
nsStringBuffer* hdr = nsStringBuffer::FromData(this->mData);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (hdr->IsReadonly()) {
|
2014-05-05 21:30:46 +04:00
|
|
|
capacity = 0;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2014-05-05 21:30:46 +04:00
|
|
|
capacity = (hdr->StorageSize() / sizeof(char_type)) - 1;
|
|
|
|
}
|
2017-09-27 13:19:33 +03:00
|
|
|
} else if (this->mDataFlags & DataFlags::INLINE) {
|
|
|
|
capacity = AsAutoString(this)->mInlineCapacity;
|
2017-08-15 00:22:50 +03:00
|
|
|
} else if (this->mDataFlags & DataFlags::OWNED) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// we don't store the capacity of an adopted buffer because that would
|
|
|
|
// require an additional member field. the best we can do is base the
|
|
|
|
// capacity on our length. remains to be seen if this is the right
|
|
|
|
// trade-off.
|
2017-08-15 00:22:50 +03:00
|
|
|
capacity = this->mLength;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2014-05-05 21:30:46 +04:00
|
|
|
capacity = 0;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::EnsureMutable(size_type aNewLen)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aNewLen == size_type(-1) || aNewLen == this->mLength) {
|
2017-09-27 13:19:33 +03:00
|
|
|
if (this->mDataFlags & (DataFlags::INLINE | DataFlags::OWNED)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
if ((this->mDataFlags & DataFlags::SHARED) &&
|
|
|
|
!nsStringBuffer::FromData(this->mData)->IsReadonly()) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
aNewLen = this->mLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2015-01-28 12:00:40 +03:00
|
|
|
return SetLength(aNewLen, mozilla::fallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// This version of Assign is optimized for single-character assignment.
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2005-10-17 20:28:21 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(char_type aChar)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (!ReplacePrep(0, this->mLength, 1)) {
|
|
|
|
AllocFailed(this->mLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
*this->mData = aChar;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2005-10-17 20:28:21 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(char_type aChar, const fallible_t&)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (!ReplacePrep(0, this->mLength, 1)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
*this->mData = aChar;
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2005-10-17 20:28:21 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2013-08-21 00:32:54 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const char_type* aData)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2016-04-06 10:23:19 +03:00
|
|
|
if (!Assign(aData, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(char_traits::length(aData));
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2013-08-21 00:32:54 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2016-04-06 10:23:19 +03:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const char_type* aData, const fallible_t&)
|
2016-04-06 10:23:19 +03:00
|
|
|
{
|
|
|
|
return Assign(aData, size_type(-1), mozilla::fallible);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const char_type* aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!Assign(aData, aLength, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(aLength == size_type(-1) ? char_traits::length(aData)
|
|
|
|
: aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const char_type* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!aData || aLength == 0) {
|
2014-05-05 21:30:46 +04:00
|
|
|
Truncate();
|
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->IsDependentOn(aData, aData + aLength)) {
|
2015-01-28 12:00:40 +03:00
|
|
|
return Assign(string_type(aData, aLength), aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (!ReplacePrep(0, this->mLength, aLength)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copy(this->mData, aData, aLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-06-06 06:17:00 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AssignASCII(const char* aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!AssignASCII(aData, aLength, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AssignASCII(const char* aData, size_type aLength,
|
2015-01-28 12:00:40 +03:00
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// A Unicode string can't depend on an ASCII string buffer,
|
|
|
|
// so this dependence check only applies to CStrings.
|
2004-06-06 06:17:00 +04:00
|
|
|
#ifdef CharT_is_char
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->IsDependentOn(aData, aData + aLength)) {
|
2015-01-28 12:00:40 +03:00
|
|
|
return Assign(string_type(aData, aLength), aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
#endif
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (!ReplacePrep(0, this->mLength, aLength)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copyASCII(this->mData, aData, aLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2014-01-09 00:51:38 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AssignLiteral(const char_type* aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
2017-07-21 01:46:59 +03:00
|
|
|
SetData(const_cast<char_type*>(aData), aLength,
|
|
|
|
DataFlags::TERMINATED | DataFlags::LITERAL);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const self_type& aStr)
|
2012-05-08 20:42:27 +04:00
|
|
|
{
|
2017-01-19 06:20:15 +03:00
|
|
|
if (!Assign(aStr, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(aStr.Length());
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const self_type& aStr, const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
// |aStr| could be sharable. We need to check its flags to know how to
|
2014-05-05 21:30:46 +04:00
|
|
|
// deal with it.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (&aStr == this) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-01-19 06:20:15 +03:00
|
|
|
if (!aStr.mLength) {
|
2014-05-05 21:30:46 +04:00
|
|
|
Truncate();
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mDataFlags |= aStr.mDataFlags & DataFlags::VOIDED;
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-07-10 22:23:10 +03:00
|
|
|
if (aStr.mDataFlags & DataFlags::SHARED) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// nice! we can avoid a string copy :-)
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
// |aStr| should be null-terminated
|
2017-07-10 22:23:10 +03:00
|
|
|
NS_ASSERTION(aStr.mDataFlags & DataFlags::TERMINATED, "shared, but not terminated");
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-07-21 01:46:59 +03:00
|
|
|
SetData(aStr.mData, aStr.mLength,
|
|
|
|
DataFlags::TERMINATED | DataFlags::SHARED);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
// get an owning reference to the this->mData
|
|
|
|
nsStringBuffer::FromData(this->mData)->AddRef();
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
2017-07-10 22:23:10 +03:00
|
|
|
} else if (aStr.mDataFlags & DataFlags::LITERAL) {
|
|
|
|
MOZ_ASSERT(aStr.mDataFlags & DataFlags::TERMINATED, "Unterminated literal");
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-01-19 06:20:15 +03:00
|
|
|
AssignLiteral(aStr.mData, aStr.mLength);
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// else, treat this like an ordinary assignment.
|
2017-01-19 06:20:15 +03:00
|
|
|
return Assign(aStr.Data(), aStr.Length(), aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:12 +03:00
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
nsTSubstring<T>::Assign(self_type&& aStr)
|
|
|
|
{
|
|
|
|
if (!Assign(mozilla::Move(aStr), mozilla::fallible)) {
|
|
|
|
AllocFailed(aStr.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
nsTSubstring<T>::Assign(self_type&& aStr, const fallible_t& aFallible)
|
|
|
|
{
|
|
|
|
// We're moving |aStr| in this method, so we need to try to steal the data,
|
|
|
|
// and in the fallback perform a copy-assignment followed by a truncation of
|
|
|
|
// the original string.
|
|
|
|
|
|
|
|
if (&aStr == this) {
|
|
|
|
NS_WARNING("Move assigning a string to itself?");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStr.mDataFlags & (DataFlags::SHARED | DataFlags::OWNED)) {
|
|
|
|
// If they have a SHARED or OWNED buffer, we can avoid a copy - so steal
|
|
|
|
// their buffer and reset them to the empty string.
|
|
|
|
|
|
|
|
// |aStr| should be null-terminated
|
|
|
|
NS_ASSERTION(aStr.mDataFlags & DataFlags::TERMINATED,
|
|
|
|
"shared or owned, but not terminated");
|
|
|
|
|
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
|
|
|
|
|
|
|
SetData(aStr.mData, aStr.mLength, aStr.mDataFlags);
|
|
|
|
aStr.SetToEmptyBuffer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise treat this as a normal assignment, and truncate the moved string.
|
|
|
|
// We don't truncate the source string if the allocation failed.
|
|
|
|
if (!Assign(aStr, aFallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aStr.Truncate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:06:43 +03:00
|
|
|
// NOTE(nika): gcc 4.9 workaround. Remove when support is dropped.
|
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
nsTSubstring<T>::Assign(const literalstring_type& aStr)
|
|
|
|
{
|
|
|
|
Assign(aStr.AsString());
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const substring_tuple_type& aTuple)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!Assign(aTuple, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(aTuple.Length());
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Assign(const substring_tuple_type& aTuple,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aTuple.IsDependentOn(this->mData, this->mData + this->mLength)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// take advantage of sharing here...
|
2015-01-28 12:00:40 +03:00
|
|
|
return Assign(string_type(aTuple), aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type length = aTuple.Length();
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// don't use ReplacePrep here because it changes the length
|
|
|
|
char_type* oldData;
|
2017-07-10 22:23:10 +03:00
|
|
|
DataFlags oldFlags;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!MutatePrep(length, &oldData, &oldFlags)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2009-03-04 12:50:22 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (oldData) {
|
2014-05-05 21:30:46 +04:00
|
|
|
::ReleaseData(oldData, oldFlags);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
aTuple.WriteTo(this->mData, length);
|
|
|
|
this->mData[length] = 0;
|
|
|
|
this->mLength = length;
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Adopt(char_type* aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aData) {
|
2017-08-15 00:22:50 +03:00
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-03-23 22:32:53 +03:00
|
|
|
MOZ_RELEASE_ASSERT(CheckCapacity(aLength), "adopting a too-long string");
|
|
|
|
|
2017-07-21 01:46:59 +03:00
|
|
|
SetData(aData, aLength, DataFlags::TERMINATED | DataFlags::OWNED);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
STRING_STAT_INCREMENT(Adopt);
|
|
|
|
// Treat this as construction of a "StringAdopt" object for leak
|
|
|
|
// tracking purposes.
|
2017-08-15 00:22:50 +03:00
|
|
|
MOZ_LOG_CTOR(this->mData, "StringAdopt", 1);
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2014-05-05 21:30:46 +04:00
|
|
|
SetIsVoid(true);
|
|
|
|
}
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// This version of Replace is optimized for single-character replacement.
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2005-10-17 20:28:21 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2005-10-17 20:28:21 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (ReplacePrep(aCutStart, aCutLength, 1)) {
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mData[aCutStart] = aChar;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2005-10-17 20:28:21 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2014-03-19 21:05:02 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar,
|
|
|
|
const fallible_t&)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!ReplacePrep(aCutStart, aCutLength, 1)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mData[aCutStart] = aChar;
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2005-10-17 20:28:21 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!Replace(aCutStart, aCutLength, aData, aLength,
|
2015-01-28 12:00:40 +03:00
|
|
|
mozilla::fallible)) {
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(this->Length() - aCutLength + 1);
|
2014-03-19 21:05:02 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2014-03-19 21:05:02 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// unfortunately, some callers pass null :-(
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!aData) {
|
|
|
|
aLength = 0;
|
|
|
|
} else {
|
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->IsDependentOn(aData, aData + aLength)) {
|
|
|
|
nsTAutoString<T> temp(aData, aLength);
|
2015-01-28 12:00:40 +03:00
|
|
|
return Replace(aCutStart, aCutLength, temp, aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
2004-06-09 02:30:11 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
bool ok = ReplacePrep(aCutStart, aCutLength, aLength);
|
|
|
|
if (!ok) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aLength > 0) {
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copy(this->mData + aCutStart, aData, aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-06-06 06:17:00 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData, size_type aLength)
|
2015-03-17 11:55:22 +03:00
|
|
|
{
|
|
|
|
if (!ReplaceASCII(aCutStart, aCutLength, aData, aLength, mozilla::fallible)) {
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(this->Length() - aCutLength + 1);
|
2015-03-17 11:55:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2015-03-17 11:55:22 +03:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = strlen(aData);
|
|
|
|
}
|
2014-03-19 21:05:02 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// A Unicode string can't depend on an ASCII string buffer,
|
|
|
|
// so this dependence check only applies to CStrings.
|
2004-06-06 06:17:00 +04:00
|
|
|
#ifdef CharT_is_char
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->IsDependentOn(aData, aData + aLength)) {
|
2014-05-27 11:15:35 +04:00
|
|
|
nsTAutoString_CharT temp(aData, aLength);
|
2015-03-17 11:55:22 +03:00
|
|
|
return Replace(aCutStart, aCutLength, temp, aFallible);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
#endif
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2004-06-09 02:30:11 +04:00
|
|
|
|
2015-03-17 11:55:22 +03:00
|
|
|
bool ok = ReplacePrep(aCutStart, aCutLength, aLength);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLength > 0) {
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copyASCII(this->mData + aCutStart, aData, aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2015-03-17 11:55:22 +03:00
|
|
|
|
|
|
|
return true;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const substring_tuple_type& aTuple)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aTuple.IsDependentOn(this->mData, this->mData + this->mLength)) {
|
|
|
|
nsTAutoString<T> temp(aTuple);
|
2014-05-27 11:15:35 +04:00
|
|
|
Replace(aCutStart, aCutLength, temp);
|
2014-05-05 21:30:46 +04:00
|
|
|
return;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type length = aTuple.Length();
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2004-06-09 02:30:11 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (ReplacePrep(aCutStart, aCutLength, length) && length > 0) {
|
2017-08-15 00:22:50 +03:00
|
|
|
aTuple.WriteTo(this->mData + aCutStart, length);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2014-01-09 00:51:38 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::ReplaceLiteral(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
if (!aCutStart && aCutLength == this->Length()) {
|
2014-05-27 11:15:35 +04:00
|
|
|
AssignLiteral(aData, aLength);
|
|
|
|
} else if (ReplacePrep(aCutStart, aCutLength, aLength) && aLength > 0) {
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copy(this->mData + aCutStart, aData, aLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SetCapacity(size_type aCapacity)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!SetCapacity(aCapacity, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(aCapacity);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SetCapacity(size_type aCapacity, const fallible_t&)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// capacity does not include room for the terminating null char
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// if our capacity is reduced to zero, then free our buffer.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aCapacity == 0) {
|
2017-08-15 00:22:50 +03:00
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
2017-07-21 01:46:59 +03:00
|
|
|
SetToEmptyBuffer();
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
char_type* oldData;
|
2017-07-10 22:23:10 +03:00
|
|
|
DataFlags oldFlags;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!MutatePrep(aCapacity, &oldData, &oldFlags)) {
|
|
|
|
return false; // out-of-memory
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// compute new string length
|
2017-08-15 00:22:50 +03:00
|
|
|
size_type newLen = XPCOM_MIN(this->mLength, aCapacity);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (oldData) {
|
2014-05-05 21:30:46 +04:00
|
|
|
// preserve old data
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mLength > 0) {
|
|
|
|
char_traits::copy(this->mData, oldData, newLen);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
::ReleaseData(oldData, oldFlags);
|
|
|
|
}
|
2009-11-19 04:14:29 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
// adjust this->mLength if our buffer shrunk down in size
|
|
|
|
if (newLen < this->mLength) {
|
|
|
|
this->mLength = newLen;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// always null-terminate here, even if the buffer got longer. this is
|
|
|
|
// for backwards compat with the old string implementation.
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mData[aCapacity] = char_type(0);
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SetLength(size_type aLength)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
SetCapacity(aLength);
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mLength = aLength;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-05-08 20:42:27 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SetLength(size_type aLength, const fallible_t& aFallible)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!SetCapacity(aLength, aFallible)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-03-20 22:02:38 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mLength = aLength;
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SetIsVoid(bool aVal)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aVal) {
|
2014-05-05 21:30:46 +04:00
|
|
|
Truncate();
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mDataFlags |= DataFlags::VOIDED;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mDataFlags &= ~DataFlags::VOIDED;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-03-14 00:02:50 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace detail {
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
typename nsTStringRepr<T>::char_type
|
|
|
|
nsTStringRepr<T>::First() const
|
2017-03-14 00:02:50 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(this->mLength > 0, "|First()| called on an empty string");
|
|
|
|
return this->mData[0];
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
typename nsTStringRepr<T>::char_type
|
|
|
|
nsTStringRepr<T>::Last() const
|
2017-03-14 00:02:50 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(this->mLength > 0, "|Last()| called on an empty string");
|
|
|
|
return this->mData[this->mLength - 1];
|
2017-03-14 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::Equals(const self_type& aStr) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == aStr.mLength &&
|
|
|
|
char_traits::compare(this->mData, aStr.mData, this->mLength) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::Equals(const self_type& aStr,
|
|
|
|
const comparator_type& aComp) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == aStr.mLength &&
|
|
|
|
aComp(this->mData, aStr.mData, this->mLength, aStr.mLength) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-03-14 00:04:15 +03:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::Equals(const substring_tuple_type& aTuple) const
|
2017-03-14 00:04:15 +03:00
|
|
|
{
|
|
|
|
return Equals(substring_type(aTuple));
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-03-14 00:04:15 +03:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::Equals(const substring_tuple_type& aTuple,
|
|
|
|
const comparator_type& aComp) const
|
2017-03-14 00:04:15 +03:00
|
|
|
{
|
|
|
|
return Equals(substring_type(aTuple), aComp);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-03-14 00:04:15 +03:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::Equals(const char_type* aData) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// unfortunately, some callers pass null :-(
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!aData) {
|
2014-05-05 21:30:46 +04:00
|
|
|
NS_NOTREACHED("null data pointer");
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == 0;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// XXX avoid length calculation?
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type length = char_traits::length(aData);
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == length &&
|
|
|
|
char_traits::compare(this->mData, aData, this->mLength) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::Equals(const char_type* aData,
|
|
|
|
const comparator_type& aComp) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// unfortunately, some callers pass null :-(
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!aData) {
|
2014-05-05 21:30:46 +04:00
|
|
|
NS_NOTREACHED("null data pointer");
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == 0;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// XXX avoid length calculation?
|
2014-05-27 11:15:35 +04:00
|
|
|
size_type length = char_traits::length(aData);
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == length && aComp(this->mData, aData, this->mLength, length) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::EqualsASCII(const char* aData, size_type aLen) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == aLen &&
|
|
|
|
char_traits::compareASCII(this->mData, aData, aLen) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-04-25 02:02:22 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::EqualsASCII(const char* aData) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return char_traits::compareASCIINullTerminated(this->mData, this->mLength, aData) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-04-30 16:05:14 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData,
|
|
|
|
size_type aLen) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mLength == aLen &&
|
|
|
|
char_traits::compareLowerCaseToASCII(this->mData, aData, aLen) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return char_traits::compareLowerCaseToASCIINullTerminated(this->mData,
|
|
|
|
this->mLength,
|
2014-05-27 11:15:35 +04:00
|
|
|
aData) == 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-06-06 06:17:00 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
typename nsTStringRepr<T>::size_type
|
|
|
|
nsTStringRepr<T>::CountChar(char_type aChar) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
const char_type* start = this->mData;
|
|
|
|
const char_type* end = this->mData + this->mLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
return NS_COUNT(start, end, aChar);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTStringRepr<T>::FindChar(char_type aChar, index_type aOffset) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (aOffset < this->mLength) {
|
|
|
|
const char_type* result = char_traits::find(this->mData + aOffset,
|
|
|
|
this->mLength - aOffset, aChar);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (result) {
|
2017-08-15 00:22:50 +03:00
|
|
|
return result - this->mData;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2004-11-25 10:03:20 +03:00
|
|
|
|
2017-03-14 00:02:50 +03:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2004-11-25 10:03:20 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::StripChar(char_type aChar)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mLength == 0) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-11-25 10:03:20 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!EnsureMutable()) { // XXX do this lazily?
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(this->mLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-11-25 10:03:20 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// XXX(darin): this code should defer writing until necessary.
|
2004-11-25 10:03:20 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
char_type* to = this->mData;
|
|
|
|
char_type* from = this->mData;
|
|
|
|
char_type* end = this->mData + this->mLength;
|
2004-11-25 10:03:20 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
while (from < end) {
|
2014-05-05 21:30:46 +04:00
|
|
|
char_type theChar = *from++;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aChar != theChar) {
|
2014-05-05 21:30:46 +04:00
|
|
|
*to++ = theChar;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-11-25 10:03:20 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
*to = char_type(0); // add the null
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mLength = to - this->mData;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2009-11-05 00:33:23 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2010-07-21 04:11:19 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::StripChars(const char_type* aChars)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mLength == 0) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!EnsureMutable()) { // XXX do this lazily?
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(this->mLength);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// XXX(darin): this code should defer writing until necessary.
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
char_type* to = this->mData;
|
|
|
|
char_type* from = this->mData;
|
|
|
|
char_type* end = this->mData + this->mLength;
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
while (from < end) {
|
2014-05-05 21:30:46 +04:00
|
|
|
char_type theChar = *from++;
|
|
|
|
const char_type* test = aChars;
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
for (; *test && *test != theChar; ++test);
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (!*test) {
|
|
|
|
// Not stripped, copy this char.
|
|
|
|
*to++ = theChar;
|
|
|
|
}
|
2010-07-21 04:11:19 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
*to = char_type(0); // add the null
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mLength = to - this->mData;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2010-07-21 04:11:19 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-05-10 20:12:36 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::StripTaggedASCII(const ASCIIMaskArray& aToStrip)
|
2017-05-10 20:12:36 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mLength == 0) {
|
2017-05-10 20:12:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureMutable()) {
|
2017-08-15 00:22:50 +03:00
|
|
|
AllocFailed(this->mLength);
|
2017-05-10 20:12:36 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
char_type* to = this->mData;
|
|
|
|
char_type* from = this->mData;
|
|
|
|
char_type* end = this->mData + this->mLength;
|
2017-05-10 20:12:36 +03:00
|
|
|
|
2017-07-13 17:24:26 +03:00
|
|
|
while (from < end) {
|
|
|
|
uint32_t theChar = (uint32_t)*from++;
|
2017-05-10 20:12:36 +03:00
|
|
|
// Replacing this with a call to ASCIIMask::IsMasked
|
|
|
|
// regresses performance somewhat, so leaving it inlined.
|
2017-07-13 17:24:26 +03:00
|
|
|
if (!mozilla::ASCIIMask::IsMasked(aToStrip, theChar)) {
|
2017-05-10 20:12:36 +03:00
|
|
|
// Not stripped, copy this char.
|
2017-07-13 17:24:26 +03:00
|
|
|
*to++ = (char_type)theChar;
|
2017-05-10 20:12:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*to = char_type(0); // add the null
|
2017-08-15 00:22:50 +03:00
|
|
|
this->mLength = to - this->mData;
|
2017-05-10 20:12:36 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-05-10 20:12:36 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::StripCRLF()
|
2017-05-10 20:12:36 +03:00
|
|
|
{
|
|
|
|
// Expanding this call to copy the code from StripTaggedASCII
|
|
|
|
// instead of just calling it does somewhat help with performance
|
|
|
|
// but it is not worth it given the duplicated code.
|
2017-05-17 20:49:52 +03:00
|
|
|
StripTaggedASCII(mozilla::ASCIIMask::MaskCRLF());
|
2017-05-10 20:12:36 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
struct MOZ_STACK_CLASS PrintfAppend : public mozilla::PrintfTarget
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
explicit PrintfAppend(nsTSubstring<T>* aString)
|
2016-12-14 19:32:21 +03:00
|
|
|
: mString(aString)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append(const char* aStr, size_t aLen) override {
|
|
|
|
if (aLen == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-13 20:58:59 +04:00
|
|
|
|
2016-12-14 19:32:21 +03:00
|
|
|
mString->AppendASCII(aStr, aLen);
|
|
|
|
return true;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2012-04-13 20:58:59 +04:00
|
|
|
|
2016-12-14 19:32:21 +03:00
|
|
|
private:
|
2012-04-13 20:58:59 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>* mString;
|
2016-12-14 19:32:21 +03:00
|
|
|
};
|
2010-11-30 21:18:15 +03:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AppendPrintf(const char* aFormat, ...)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
PrintfAppend<T> appender(this);
|
2014-05-05 21:30:46 +04:00
|
|
|
va_list ap;
|
2014-05-27 11:15:35 +04:00
|
|
|
va_start(ap, aFormat);
|
2016-12-14 19:32:21 +03:00
|
|
|
bool r = appender.vprint(aFormat, ap);
|
|
|
|
if (!r) {
|
|
|
|
MOZ_CRASH("Allocation or other failure in PrintfTarget::print");
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2011-08-25 18:43:49 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AppendPrintf(const char* aFormat, va_list aAp)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
PrintfAppend<T> appender(this);
|
2016-12-14 19:32:21 +03:00
|
|
|
bool r = appender.vprint(aFormat, aAp);
|
|
|
|
if (!r) {
|
|
|
|
MOZ_CRASH("Allocation or other failure in PrintfTarget::print");
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2010-11-30 21:18:15 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
// Returns the length of the formatted aDouble in aBuf.
|
2013-09-24 21:36:55 +04:00
|
|
|
static int
|
2014-05-27 11:15:35 +04:00
|
|
|
FormatWithoutTrailingZeros(char (&aBuf)[40], double aDouble,
|
|
|
|
int aPrecision)
|
2010-11-30 21:18:15 +03:00
|
|
|
{
|
2013-09-24 21:36:55 +04:00
|
|
|
static const DoubleToStringConverter converter(DoubleToStringConverter::UNIQUE_ZERO |
|
|
|
|
DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN,
|
|
|
|
"Infinity",
|
|
|
|
"NaN",
|
|
|
|
'e',
|
|
|
|
-6, 21,
|
|
|
|
6, 1);
|
2014-05-27 11:15:35 +04:00
|
|
|
double_conversion::StringBuilder builder(aBuf, sizeof(aBuf));
|
2013-09-24 21:36:55 +04:00
|
|
|
bool exponential_notation = false;
|
2014-05-27 11:15:35 +04:00
|
|
|
converter.ToPrecision(aDouble, aPrecision, &exponential_notation, &builder);
|
2013-09-24 21:36:55 +04:00
|
|
|
int length = builder.position();
|
|
|
|
char* formattedDouble = builder.Finalize();
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
// If we have a shorter string than aPrecision, it means we have a special
|
2013-09-24 21:36:55 +04:00
|
|
|
// value (NaN or Infinity). All other numbers will be formatted with at
|
2014-05-27 11:15:35 +04:00
|
|
|
// least aPrecision digits.
|
|
|
|
if (length <= aPrecision) {
|
2013-09-24 21:36:55 +04:00
|
|
|
return length;
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2013-09-24 21:36:55 +04:00
|
|
|
char* end = formattedDouble + length;
|
2014-05-27 11:15:35 +04:00
|
|
|
char* decimalPoint = strchr(aBuf, '.');
|
2013-09-24 21:36:55 +04:00
|
|
|
// No trailing zeros to remove.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!decimalPoint) {
|
2013-09-24 21:36:55 +04:00
|
|
|
return length;
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
|
|
|
|
2013-09-24 21:36:55 +04:00
|
|
|
if (MOZ_UNLIKELY(exponential_notation)) {
|
|
|
|
// We need to check for cases like 1.00000e-10 (yes, this is
|
|
|
|
// disgusting).
|
|
|
|
char* exponent = end - 1;
|
2014-05-27 11:15:35 +04:00
|
|
|
for (; ; --exponent) {
|
2013-09-24 21:36:55 +04:00
|
|
|
if (*exponent == 'e') {
|
|
|
|
break;
|
|
|
|
}
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
2013-09-24 21:36:55 +04:00
|
|
|
char* zerosBeforeExponent = exponent - 1;
|
2014-05-27 11:15:35 +04:00
|
|
|
for (; zerosBeforeExponent != decimalPoint; --zerosBeforeExponent) {
|
2013-09-24 21:36:55 +04:00
|
|
|
if (*zerosBeforeExponent != '0') {
|
|
|
|
break;
|
|
|
|
}
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
2013-09-24 21:36:55 +04:00
|
|
|
if (zerosBeforeExponent == decimalPoint) {
|
|
|
|
--zerosBeforeExponent;
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
2013-09-24 21:36:55 +04:00
|
|
|
// Slide the exponent to the left over the trailing zeros. Don't
|
|
|
|
// worry about copying the trailing NUL character.
|
|
|
|
size_t exponentSize = end - exponent;
|
|
|
|
memmove(zerosBeforeExponent + 1, exponent, exponentSize);
|
|
|
|
length -= exponent - (zerosBeforeExponent + 1);
|
|
|
|
} else {
|
|
|
|
char* trailingZeros = end - 1;
|
2014-05-27 11:15:35 +04:00
|
|
|
for (; trailingZeros != decimalPoint; --trailingZeros) {
|
2013-09-24 21:36:55 +04:00
|
|
|
if (*trailingZeros != '0') {
|
|
|
|
break;
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-24 21:36:55 +04:00
|
|
|
if (trailingZeros == decimalPoint) {
|
|
|
|
--trailingZeros;
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
2013-09-24 21:36:55 +04:00
|
|
|
length -= end - (trailingZeros + 1);
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
|
|
|
|
2013-09-24 21:36:55 +04:00
|
|
|
return length;
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2010-11-30 21:18:15 +03:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AppendFloat(float aFloat)
|
2013-09-24 21:36:55 +04:00
|
|
|
{
|
|
|
|
char buf[40];
|
|
|
|
int length = FormatWithoutTrailingZeros(buf, aFloat, 6);
|
|
|
|
AppendASCII(buf, length);
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2013-09-24 21:36:55 +04:00
|
|
|
void
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::AppendFloat(double aFloat)
|
2010-11-30 21:18:15 +03:00
|
|
|
{
|
|
|
|
char buf[40];
|
2013-09-24 21:36:55 +04:00
|
|
|
int length = FormatWithoutTrailingZeros(buf, aFloat, 15);
|
|
|
|
AppendASCII(buf, length);
|
2010-11-30 21:18:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-02-20 07:16:41 +04:00
|
|
|
size_t
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SizeOfExcludingThisIfUnshared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-20 07:16:41 +04:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mDataFlags & DataFlags::SHARED) {
|
|
|
|
return nsStringBuffer::FromData(this->mData)->
|
2015-12-02 02:36:26 +03:00
|
|
|
SizeOfIncludingThisIfUnshared(aMallocSizeOf);
|
2013-01-18 09:21:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mDataFlags & DataFlags::OWNED) {
|
|
|
|
return aMallocSizeOf(this->mData);
|
2012-02-20 07:16:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, exactly one of the following must be true:
|
2017-08-15 00:22:50 +03:00
|
|
|
// - DataFlags::VOIDED is set, and this->mData points to sEmptyBuffer;
|
2017-09-27 13:19:33 +03:00
|
|
|
// - DataFlags::INLINE is set, and this->mData points to a buffer within a
|
|
|
|
// string object (e.g. nsAutoString);
|
|
|
|
// - None of DataFlags::SHARED, DataFlags::OWNED, DataFlags::INLINE is set,
|
|
|
|
// and this->mData points to a buffer owned by something else.
|
2012-02-20 07:16:41 +04:00
|
|
|
//
|
|
|
|
// In all three cases, we don't measure it.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2013-01-18 09:21:35 +04:00
|
|
|
size_t
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SizeOfExcludingThisEvenIfShared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2013-01-18 09:21:35 +04:00
|
|
|
{
|
2015-12-02 02:36:26 +03:00
|
|
|
// This is identical to SizeOfExcludingThisIfUnshared except for the
|
2017-07-10 22:23:10 +03:00
|
|
|
// DataFlags::SHARED case.
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mDataFlags & DataFlags::SHARED) {
|
|
|
|
return nsStringBuffer::FromData(this->mData)->
|
2014-05-27 11:15:35 +04:00
|
|
|
SizeOfIncludingThisEvenIfShared(aMallocSizeOf);
|
2013-01-18 09:21:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
if (this->mDataFlags & DataFlags::OWNED) {
|
|
|
|
return aMallocSizeOf(this->mData);
|
2013-01-18 09:21:35 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2012-02-20 07:16:41 +04:00
|
|
|
size_t
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SizeOfIncludingThisIfUnshared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-20 07:16:41 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2012-02-20 07:16:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2013-01-18 09:21:35 +04:00
|
|
|
size_t
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstring<T>::SizeOfIncludingThisEvenIfShared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2013-01-18 09:21:35 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThisEvenIfShared(aMallocSizeOf);
|
2013-01-18 09:21:35 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2017-03-13 05:56:23 +03:00
|
|
|
inline
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTSubstringSplitter<T>::nsTSubstringSplitter(
|
|
|
|
const nsTSubstring<T>* aStr, char_type aDelim)
|
2017-03-13 05:56:23 +03:00
|
|
|
: mStr(aStr)
|
|
|
|
, mArray(nullptr)
|
|
|
|
, mDelim(aDelim)
|
|
|
|
{
|
|
|
|
if (mStr->IsEmpty()) {
|
|
|
|
mArraySize = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type delimCount = mStr->CountChar(aDelim);
|
|
|
|
mArraySize = delimCount + 1;
|
2017-08-15 00:22:50 +03:00
|
|
|
mArray.reset(new nsTDependentSubstring<T>[mArraySize]);
|
2017-03-13 05:56:23 +03:00
|
|
|
|
|
|
|
size_t seenParts = 0;
|
|
|
|
size_type start = 0;
|
|
|
|
do {
|
|
|
|
MOZ_ASSERT(seenParts < mArraySize);
|
|
|
|
int32_t offset = mStr->FindChar(aDelim, start);
|
|
|
|
if (offset != -1) {
|
|
|
|
size_type length = static_cast<size_type>(offset) - start;
|
|
|
|
mArray[seenParts++].Rebind(mStr->Data() + start, length);
|
|
|
|
start = static_cast<size_type>(offset) + 1;
|
|
|
|
} else {
|
|
|
|
// Get the remainder
|
|
|
|
mArray[seenParts++].Rebind(mStr->Data() + start, mStr->Length() - start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (start < mStr->Length());
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
nsTSubstringSplitter<T>
|
|
|
|
nsTSubstring<T>::Split(const char_type aChar) const
|
2017-01-30 14:34:35 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return nsTSubstringSplitter<T>(this, aChar);
|
2017-01-30 14:34:35 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
const nsTDependentSubstring<T>&
|
|
|
|
nsTSubstringSplitter<T>::nsTSubstringSplit_Iter::operator* () const
|
2017-01-30 14:34:35 +03:00
|
|
|
{
|
|
|
|
return mObj.Get(mPos);
|
|
|
|
}
|