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
|
|
|
|
2018-02-21 11:15:43 +03:00
|
|
|
#include "double-conversion/double-conversion.h"
|
2015-07-06 21:27:35 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2018-02-21 11:15:43 +03:00
|
|
|
#include "mozilla/MathAlgorithms.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"
|
2018-02-21 11:15:43 +03:00
|
|
|
|
|
|
|
#include "nsASCIIMask.h"
|
2013-09-24 21:36:55 +04:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
// It's not worthwhile to reallocate the buffer and memcpy the
|
|
|
|
// contents over when the size difference isn't large. With
|
|
|
|
// power-of-two allocation buckets and 64 as the typical inline
|
|
|
|
// capacity, considering that above 1000 there performance aspects
|
|
|
|
// of realloc and memcpy seem to be absorbed, relative to the old
|
|
|
|
// code, by the performance benefits of the new code being exact,
|
|
|
|
// we need to choose which transitions of 256 to 128, 512 to 256
|
|
|
|
// and 1024 to 512 to allow. As a guess, let's pick the middle
|
|
|
|
// one as the the largest potential transition that we forgo. So
|
|
|
|
// we'll shrink from 1024 bucket to 512 bucket but not from 512
|
|
|
|
// bucket to 256 bucket. We'll decide by comparing the difference
|
|
|
|
// of capacities. As bucket differences, the differences are 256
|
|
|
|
// and 512. Since the capacities have various overheads, we
|
|
|
|
// can't compare with 256 or 512 exactly but it's easier to
|
|
|
|
// compare to some number that's between the two, so it's
|
|
|
|
// far away from either to ignore the overheads.
|
|
|
|
const uint32_t kNsStringBufferShrinkingThreshold = 384;
|
|
|
|
|
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) / 2 - sizeof(nsStringBuffer)) /
|
|
|
|
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) {
|
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) {
|
|
|
|
return static_cast<const nsTAutoString<T>*>(aStr);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-20 04:53:23 +03:00
|
|
|
|
2018-08-28 19:42:26 +03:00
|
|
|
template <typename T>
|
|
|
|
mozilla::BulkWriteHandle<T> nsTSubstring<T>::BulkWrite(
|
|
|
|
size_type aCapacity, size_type aPrefixToPreserve, bool aAllowShrinking,
|
|
|
|
nsresult& aRv) {
|
|
|
|
auto r = StartBulkWriteImpl(aCapacity, aPrefixToPreserve, aAllowShrinking);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
|
|
|
aRv = r.unwrapErr();
|
|
|
|
return mozilla::BulkWriteHandle<T>(nullptr, 0);
|
|
|
|
}
|
|
|
|
aRv = NS_OK;
|
|
|
|
return mozilla::BulkWriteHandle<T>(this, r.unwrap());
|
|
|
|
}
|
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
template <typename T>
|
2018-08-28 19:42:26 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> nsTSubstring<T>::StartBulkWriteImpl(
|
|
|
|
size_type aCapacity, size_type aPrefixToPreserve, bool aAllowShrinking,
|
|
|
|
size_type aSuffixLength, size_type aOldSuffixStart,
|
|
|
|
size_type aNewSuffixStart) {
|
2018-07-06 10:44:43 +03:00
|
|
|
// Note! Capacity does not include room for the terminating null char.
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
MOZ_ASSERT(aPrefixToPreserve <= aCapacity,
|
|
|
|
"Requested preservation of an overlong prefix.");
|
|
|
|
MOZ_ASSERT(aNewSuffixStart + aSuffixLength <= aCapacity,
|
|
|
|
"Requesed move of suffix to out-of-bounds location.");
|
|
|
|
// Can't assert aOldSuffixStart, because mLength may not be valid anymore,
|
|
|
|
// since this method allows itself to be called more than once.
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
// If zero capacity is requested, set the string to the special empty
|
|
|
|
// string.
|
|
|
|
if (MOZ_UNLIKELY(!aCapacity)) {
|
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
|
|
|
SetToEmptyBuffer();
|
|
|
|
return 0;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
// Note! Capacity() returns 0 when the string is immutable.
|
2018-08-27 10:03:59 +03:00
|
|
|
const size_type curCapacity = Capacity();
|
|
|
|
|
|
|
|
bool shrinking = false;
|
2018-07-06 10:44:43 +03:00
|
|
|
|
|
|
|
// We've established that aCapacity > 0.
|
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.
|
|
|
|
|
2018-08-27 10:03:59 +03:00
|
|
|
if (aCapacity <= curCapacity) {
|
|
|
|
if (aAllowShrinking) {
|
|
|
|
shrinking = true;
|
|
|
|
} else {
|
|
|
|
char_traits::move(this->mData + aNewSuffixStart,
|
|
|
|
this->mData + aOldSuffixStart, aSuffixLength);
|
2018-09-07 08:47:57 +03:00
|
|
|
if (aSuffixLength) {
|
2018-09-14 21:58:15 +03:00
|
|
|
char_traits::uninitialize(
|
|
|
|
this->mData + aPrefixToPreserve,
|
|
|
|
XPCOM_MIN(size_t(aNewSuffixStart - aPrefixToPreserve),
|
|
|
|
kNsStringBufferMaxPoison));
|
|
|
|
char_traits::uninitialize(
|
|
|
|
this->mData + aNewSuffixStart + aSuffixLength,
|
|
|
|
XPCOM_MIN(size_t(curCapacity + 1 - aNewSuffixStart - aSuffixLength),
|
|
|
|
kNsStringBufferMaxPoison));
|
2018-09-07 08:47:57 +03:00
|
|
|
} else {
|
2018-09-14 21:58:15 +03:00
|
|
|
char_traits::uninitialize(
|
|
|
|
this->mData + aPrefixToPreserve,
|
|
|
|
XPCOM_MIN(size_t(curCapacity + 1 - aPrefixToPreserve),
|
|
|
|
kNsStringBufferMaxPoison));
|
2018-09-07 08:47:57 +03:00
|
|
|
}
|
2018-08-27 10:03:59 +03:00
|
|
|
return curCapacity;
|
|
|
|
}
|
2016-09-08 20:49:49 +03:00
|
|
|
}
|
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
char_type* oldData = this->mData;
|
|
|
|
DataFlags oldFlags = this->mDataFlags;
|
|
|
|
|
|
|
|
char_type* newData;
|
|
|
|
DataFlags newDataFlags;
|
|
|
|
size_type newCapacity;
|
|
|
|
|
|
|
|
// If this is an nsTAutoStringN, it's possible that we can use the inline
|
|
|
|
// buffer.
|
|
|
|
if ((this->mClassFlags & ClassFlags::INLINE) &&
|
|
|
|
(aCapacity <= AsAutoString(this)->mInlineCapacity)) {
|
|
|
|
newCapacity = AsAutoString(this)->mInlineCapacity;
|
|
|
|
newData = (char_type*)AsAutoString(this)->mStorage;
|
|
|
|
newDataFlags = DataFlags::TERMINATED | DataFlags::INLINE;
|
|
|
|
} else {
|
|
|
|
// If |aCapacity > kMaxCapacity|, then our doubling algorithm may not be
|
|
|
|
// able to allocate it. Just bail out in cases like that. We don't want
|
|
|
|
// to be allocating 2GB+ strings anyway.
|
|
|
|
static_assert((sizeof(nsStringBuffer) & 0x1) == 0,
|
|
|
|
"bad size for nsStringBuffer");
|
|
|
|
if (MOZ_UNLIKELY(!CheckCapacity(aCapacity))) {
|
|
|
|
return mozilla::Err(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:49:49 +03:00
|
|
|
// 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
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
newCapacity = XPCOM_MIN(temp, kMaxCapacity);
|
|
|
|
MOZ_ASSERT(newCapacity >= aCapacity,
|
2016-09-08 20:49:49 +03:00
|
|
|
"should have hit the early return at the top");
|
2018-07-06 10:44:43 +03:00
|
|
|
// Avoid shrinking if the new buffer size is close to the old. Note that
|
|
|
|
// unsigned underflow is defined behavior.
|
|
|
|
if ((curCapacity - newCapacity) <= kNsStringBufferShrinkingThreshold &&
|
|
|
|
(this->mDataFlags & DataFlags::REFCOUNTED)) {
|
|
|
|
MOZ_ASSERT(aAllowShrinking, "How come we didn't return earlier?");
|
|
|
|
// We're already close enough to the right size.
|
|
|
|
newData = oldData;
|
2018-08-27 10:03:59 +03:00
|
|
|
newCapacity = curCapacity;
|
2018-07-06 10:44:43 +03:00
|
|
|
} else {
|
|
|
|
size_type storageSize = (newCapacity + 1) * sizeof(char_type);
|
|
|
|
// Since we allocate only by powers of 2 we always fit into a full
|
|
|
|
// mozjemalloc bucket, it's not useful to use realloc, which may spend
|
|
|
|
// time uselessly copying too much.
|
|
|
|
nsStringBuffer* newHdr = nsStringBuffer::Alloc(storageSize).take();
|
2018-08-28 17:49:20 +03:00
|
|
|
if (newHdr) {
|
|
|
|
newData = (char_type*)newHdr->Data();
|
|
|
|
} else if (shrinking) {
|
|
|
|
// We're still in a consistent state.
|
|
|
|
//
|
|
|
|
// Since shrinking is just a memory footprint optimization, we
|
|
|
|
// don't propagate OOM if we tried to shrink in order to avoid
|
|
|
|
// OOM crashes from infallible callers. If we're lucky, soon enough
|
|
|
|
// a fallible caller reaches OOM and is able to deal or we end up
|
|
|
|
// disposing of this string before reaching OOM again.
|
|
|
|
newData = oldData;
|
|
|
|
newCapacity = curCapacity;
|
|
|
|
} else {
|
|
|
|
return mozilla::Err(NS_ERROR_OUT_OF_MEMORY);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2018-07-06 10:44:43 +03:00
|
|
|
newDataFlags = DataFlags::TERMINATED | DataFlags::REFCOUNTED;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
this->mData = newData;
|
|
|
|
this->mDataFlags = newDataFlags;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
if (oldData == newData) {
|
|
|
|
char_traits::move(newData + aNewSuffixStart, oldData + aOldSuffixStart,
|
|
|
|
aSuffixLength);
|
2018-09-07 08:47:57 +03:00
|
|
|
if (aSuffixLength) {
|
2018-09-14 21:58:15 +03:00
|
|
|
char_traits::uninitialize(
|
|
|
|
this->mData + aPrefixToPreserve,
|
|
|
|
XPCOM_MIN(size_t(aNewSuffixStart - aPrefixToPreserve),
|
|
|
|
kNsStringBufferMaxPoison));
|
|
|
|
char_traits::uninitialize(
|
|
|
|
this->mData + aNewSuffixStart + aSuffixLength,
|
|
|
|
XPCOM_MIN(size_t(newCapacity + 1 - aNewSuffixStart - aSuffixLength),
|
|
|
|
kNsStringBufferMaxPoison));
|
2018-09-07 08:47:57 +03:00
|
|
|
} else {
|
2018-09-14 21:58:15 +03:00
|
|
|
char_traits::uninitialize(
|
|
|
|
this->mData + aPrefixToPreserve,
|
|
|
|
XPCOM_MIN(size_t(newCapacity + 1 - aPrefixToPreserve),
|
|
|
|
kNsStringBufferMaxPoison));
|
2018-09-07 08:47:57 +03:00
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2018-07-06 10:44:43 +03:00
|
|
|
char_traits::copy(newData, oldData, aPrefixToPreserve);
|
|
|
|
char_traits::copy(newData + aNewSuffixStart, oldData + aOldSuffixStart,
|
|
|
|
aSuffixLength);
|
|
|
|
::ReleaseData(oldData, oldFlags);
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
return newCapacity;
|
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
template <typename T>
|
2018-08-28 19:42:26 +03:00
|
|
|
void nsTSubstring<T>::FinishBulkWriteImpl(size_type aLength) {
|
2018-07-06 10:44:43 +03:00
|
|
|
MOZ_ASSERT(aLength != UINT32_MAX, "OOM magic value passed as length.");
|
|
|
|
if (aLength) {
|
2018-09-17 12:45:02 +03:00
|
|
|
FinishBulkWriteImplImpl(aLength);
|
2018-07-06 10:44:43 +03:00
|
|
|
} else {
|
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
|
|
|
SetToEmptyBuffer();
|
|
|
|
}
|
|
|
|
AssertValid();
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Finalize() {
|
|
|
|
::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>
|
|
|
|
bool nsTSubstring<T>::ReplacePrep(index_type aCutStart, size_type aCutLength,
|
|
|
|
size_type aNewLength) {
|
|
|
|
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>
|
|
|
|
bool nsTSubstring<T>::ReplacePrepInternal(index_type aCutStart,
|
|
|
|
size_type aCutLen, size_type aFragLen,
|
|
|
|
size_type aNewLen) {
|
2018-07-06 10:44:43 +03:00
|
|
|
size_type newSuffixStart = aCutStart + aFragLen;
|
|
|
|
size_type oldSuffixStart = aCutStart + aCutLen;
|
|
|
|
size_type suffixLength = this->mLength - oldSuffixStart;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-08-28 19:42:26 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> r = StartBulkWriteImpl(
|
2018-07-06 10:44:43 +03:00
|
|
|
aNewLen, aCutStart, false, suffixLength, oldSuffixStart, newSuffixStart);
|
|
|
|
if (r.isErr()) {
|
|
|
|
return false;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2018-08-28 19:42:26 +03:00
|
|
|
FinishBulkWriteImpl(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;
|
2018-03-24 03:31:37 +03:00
|
|
|
if (this->mDataFlags & DataFlags::REFCOUNTED) {
|
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>
|
|
|
|
bool nsTSubstring<T>::EnsureMutable(size_type aNewLen) {
|
|
|
|
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
|
|
|
}
|
2018-03-24 03:31:37 +03:00
|
|
|
if ((this->mDataFlags & DataFlags::REFCOUNTED) &&
|
2017-08-15 00:22:50 +03:00
|
|
|
!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>
|
|
|
|
void nsTSubstring<T>::Assign(char_type aChar) {
|
2018-09-24 17:23:35 +03:00
|
|
|
if (MOZ_UNLIKELY(!Assign(aChar, mozilla::fallible))) {
|
|
|
|
AllocFailed(1);
|
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>
|
|
|
|
bool nsTSubstring<T>::Assign(char_type aChar, const fallible_t&) {
|
2018-09-24 17:23:35 +03:00
|
|
|
auto r = StartBulkWriteImpl(1, 0, true);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
*this->mData = aChar;
|
2018-09-24 17:23:35 +03:00
|
|
|
FinishBulkWriteImpl(1);
|
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>
|
2018-09-24 17:23:35 +03:00
|
|
|
void nsTSubstring<T>::Assign(const char_type* aData, size_type aLength) {
|
|
|
|
if (MOZ_UNLIKELY(!Assign(aData, aLength, mozilla::fallible))) {
|
|
|
|
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
|
|
|
}
|
2013-08-21 00:32:54 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
2018-09-24 17:23:35 +03:00
|
|
|
bool nsTSubstring<T>::Assign(const char_type* aData,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
return Assign(aData, size_type(-1), aFallible);
|
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>
|
|
|
|
bool nsTSubstring<T>::Assign(const char_type* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible) {
|
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
|
|
|
|
2018-09-24 17:23:35 +03:00
|
|
|
if (MOZ_UNLIKELY(aLength == size_type(-1))) {
|
2014-05-27 11:15:35 +04:00
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-09-24 17:23:35 +03:00
|
|
|
if (MOZ_UNLIKELY(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
|
|
|
|
2018-09-24 17:23:35 +03:00
|
|
|
auto r = StartBulkWriteImpl(aLength, 0, true);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copy(this->mData, aData, aLength);
|
2018-09-24 17:23:35 +03:00
|
|
|
FinishBulkWriteImpl(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>
|
|
|
|
void nsTSubstring<T>::AssignASCII(const char* aData, size_type aLength) {
|
2018-09-24 17:23:35 +03:00
|
|
|
if (MOZ_UNLIKELY(!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>
|
|
|
|
bool nsTSubstring<T>::AssignASCII(const char* aData, size_type aLength,
|
2015-01-28 12:00:40 +03:00
|
|
|
const fallible_t& aFallible) {
|
2018-09-24 17:23:35 +03:00
|
|
|
MOZ_ASSERT(aLength != size_type(-1));
|
|
|
|
|
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
|
|
|
|
|
2018-09-24 17:23:35 +03:00
|
|
|
auto r = StartBulkWriteImpl(aLength, 0, true);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
2014-05-05 21:30:46 +04:00
|
|
|
return false;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2017-08-15 00:22:50 +03:00
|
|
|
char_traits::copyASCII(this->mData, aData, aLength);
|
2018-09-24 17:23:35 +03:00
|
|
|
FinishBulkWriteImpl(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>
|
|
|
|
void nsTSubstring<T>::AssignLiteral(const char_type* aData, size_type aLength) {
|
|
|
|
::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>
|
|
|
|
void nsTSubstring<T>::Assign(const self_type& aStr) {
|
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>
|
|
|
|
bool nsTSubstring<T>::Assign(const self_type& aStr,
|
|
|
|
const fallible_t& aFallible) {
|
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
|
|
|
|
2018-03-24 03:31:37 +03:00
|
|
|
if (aStr.mDataFlags & DataFlags::REFCOUNTED) {
|
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,
|
2018-03-24 03:31:37 +03:00
|
|
|
DataFlags::TERMINATED | DataFlags::REFCOUNTED);
|
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) {
|
2018-05-30 22:15:35 +03:00
|
|
|
if (!Assign(std::move(aStr), mozilla::fallible)) {
|
2017-10-02 17:58:12 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-24 03:31:37 +03:00
|
|
|
if (aStr.mDataFlags & (DataFlags::REFCOUNTED | DataFlags::OWNED)) {
|
|
|
|
// If they have a REFCOUNTED or OWNED buffer, we can avoid a copy - so steal
|
2017-10-02 17:58:12 +03:00
|
|
|
// 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-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Assign(const substring_tuple_type& aTuple) {
|
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>
|
|
|
|
bool nsTSubstring<T>::Assign(const substring_tuple_type& aTuple,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
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
|
|
|
|
2018-08-28 19:42:26 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> r = StartBulkWriteImpl(length);
|
2018-07-06 10:44:43 +03:00
|
|
|
if (r.isErr()) {
|
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
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
aTuple.WriteTo(this->mData, length);
|
2018-07-06 10:44:43 +03:00
|
|
|
|
2018-08-28 19:42:26 +03:00
|
|
|
FinishBulkWriteImpl(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>
|
|
|
|
void nsTSubstring<T>::Adopt(char_type* aData, size_type aLength) {
|
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>
|
|
|
|
void nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar) {
|
|
|
|
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>
|
|
|
|
bool nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar, const fallible_t&) {
|
|
|
|
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>
|
|
|
|
void nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength) {
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!Replace(aCutStart, aCutLength, aData, aLength, 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>
|
|
|
|
bool 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>
|
|
|
|
void 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>
|
|
|
|
bool nsTSubstring<T>::ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible) {
|
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>
|
|
|
|
void nsTSubstring<T>::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const substring_tuple_type& aTuple) {
|
|
|
|
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>
|
|
|
|
void nsTSubstring<T>::ReplaceLiteral(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData,
|
|
|
|
size_type aLength) {
|
|
|
|
aCutStart = XPCOM_MIN(aCutStart, this->Length());
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2018-08-31 17:57:26 +03:00
|
|
|
if (!aCutStart && aCutLength == this->Length() &&
|
|
|
|
!(this->mDataFlags & DataFlags::REFCOUNTED)) {
|
|
|
|
// Check for REFCOUNTED above to avoid undoing the effect of
|
|
|
|
// SetCapacity().
|
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
|
|
|
|
2018-09-24 17:23:35 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Append(char_type aChar) {
|
|
|
|
if (MOZ_UNLIKELY(!Append(aChar, mozilla::fallible))) {
|
|
|
|
AllocFailed(this->mLength + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool nsTSubstring<T>::Append(char_type aChar, const fallible_t& aFallible) {
|
|
|
|
size_type oldLen = this->mLength;
|
|
|
|
size_type newLen = oldLen + 1; // Can't overflow
|
|
|
|
auto r = StartBulkWriteImpl(newLen, oldLen, false);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->mData[oldLen] = aChar;
|
|
|
|
FinishBulkWriteImpl(newLen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Append(const char_type* aData, size_type aLength) {
|
|
|
|
if (MOZ_UNLIKELY(!Append(aData, aLength, mozilla::fallible))) {
|
|
|
|
AllocFailed(this->mLength + (aLength == size_type(-1)
|
|
|
|
? char_traits::length(aData)
|
|
|
|
: aLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool nsTSubstring<T>::Append(const char_type* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
if (MOZ_UNLIKELY(aLength == size_type(-1))) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(!aLength)) {
|
|
|
|
// Avoid undoing the effect of SetCapacity() if both
|
|
|
|
// mLength and aLength are zero.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(this->IsDependentOn(aData, aData + aLength))) {
|
|
|
|
return Append(string_type(aData, aLength), mozilla::fallible);
|
|
|
|
}
|
|
|
|
size_type oldLen = this->mLength;
|
|
|
|
mozilla::CheckedInt<size_type> newLen(oldLen);
|
|
|
|
newLen += aLength;
|
|
|
|
if (MOZ_UNLIKELY(!newLen.isValid())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto r = StartBulkWriteImpl(newLen.value(), oldLen, false);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char_traits::copy(this->mData + oldLen, aData, aLength);
|
|
|
|
FinishBulkWriteImpl(newLen.value());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendASCII(const char* aData, size_type aLength) {
|
|
|
|
if (MOZ_UNLIKELY(!AppendASCII(aData, aLength, mozilla::fallible))) {
|
|
|
|
AllocFailed(this->mLength +
|
|
|
|
(aLength == size_type(-1) ? strlen(aData) : aLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool nsTSubstring<T>::AppendASCII(const char* aData,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
return AppendASCII(aData, size_type(-1), aFallible);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool nsTSubstring<T>::AppendASCII(const char* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
if (MOZ_UNLIKELY(aLength == size_type(-1))) {
|
|
|
|
aLength = strlen(aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(!aLength)) {
|
|
|
|
// Avoid undoing the effect of SetCapacity() if both
|
|
|
|
// mLength and aLength are zero.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CharT_is_char
|
|
|
|
// 16-bit string can't depend on an 8-bit buffer
|
|
|
|
if (MOZ_UNLIKELY(this->IsDependentOn(aData, aData + aLength))) {
|
|
|
|
return Append(string_type(aData, aLength), mozilla::fallible);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
size_type oldLen = this->mLength;
|
|
|
|
mozilla::CheckedInt<size_type> newLen(oldLen);
|
|
|
|
newLen += aLength;
|
|
|
|
if (MOZ_UNLIKELY(!newLen.isValid())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto r = StartBulkWriteImpl(newLen.value(), oldLen, false);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char_traits::copyASCII(this->mData + oldLen, aData, aLength);
|
|
|
|
FinishBulkWriteImpl(newLen.value());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Append(const self_type& aStr) {
|
|
|
|
if (MOZ_UNLIKELY(!Append(aStr, mozilla::fallible))) {
|
|
|
|
AllocFailed(this->mLength + aStr.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool nsTSubstring<T>::Append(const self_type& aStr,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
// Check refcounted to avoid undoing the effects of SetCapacity().
|
|
|
|
if (MOZ_UNLIKELY(!this->mLength &&
|
|
|
|
!(this->mDataFlags & DataFlags::REFCOUNTED))) {
|
|
|
|
return Assign(aStr, mozilla::fallible);
|
|
|
|
}
|
|
|
|
return Append(aStr.BeginReading(), aStr.Length(), mozilla::fallible);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Append(const substring_tuple_type& aTuple) {
|
|
|
|
if (MOZ_UNLIKELY(!Append(aTuple, mozilla::fallible))) {
|
|
|
|
AllocFailed(this->mLength + aTuple.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool nsTSubstring<T>::Append(const substring_tuple_type& aTuple,
|
|
|
|
const fallible_t& aFallible) {
|
|
|
|
size_type tupleLength = aTuple.Length();
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(!tupleLength)) {
|
|
|
|
// Avoid undoing the effect of SetCapacity() if both
|
|
|
|
// mLength and tupleLength are zero.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(
|
|
|
|
aTuple.IsDependentOn(this->mData, this->mData + this->mLength))) {
|
|
|
|
return Append(string_type(aTuple), aFallible);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type oldLen = this->mLength;
|
|
|
|
mozilla::CheckedInt<size_type> newLen(oldLen);
|
|
|
|
newLen += tupleLength;
|
|
|
|
if (MOZ_UNLIKELY(!newLen.isValid())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto r = StartBulkWriteImpl(newLen.value(), oldLen, false);
|
|
|
|
if (MOZ_UNLIKELY(r.isErr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aTuple.WriteTo(this->mData + oldLen, tupleLength);
|
|
|
|
FinishBulkWriteImpl(newLen.value());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::SetCapacity(size_type aCapacity) {
|
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>
|
|
|
|
bool nsTSubstring<T>::SetCapacity(size_type aCapacity, const fallible_t&) {
|
2018-09-17 12:45:02 +03:00
|
|
|
size_type length = this->mLength;
|
|
|
|
// This method can no longer be used to shorten the
|
|
|
|
// logical length.
|
|
|
|
size_type capacity = XPCOM_MAX(aCapacity, length);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-09-17 12:45:02 +03:00
|
|
|
mozilla::Result<uint32_t, nsresult> r =
|
|
|
|
StartBulkWriteImpl(capacity, length, true);
|
2018-07-06 10:44:43 +03:00
|
|
|
if (r.isErr()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-17 12:45:02 +03:00
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(!capacity)) {
|
|
|
|
// Zero capacity was requested on a zero-length
|
|
|
|
// string. In this special case, we are pointing
|
|
|
|
// to the special empty buffer, which is already
|
|
|
|
// zero-terminated and not writable, so we must
|
|
|
|
// not attempt to zero-terminate it.
|
|
|
|
AssertValid();
|
|
|
|
return true;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2018-09-17 12:45:02 +03:00
|
|
|
|
|
|
|
// FinishBulkWriteImpl with argument zero releases
|
|
|
|
// the heap-allocated buffer. However, SetCapacity()
|
|
|
|
// is a special case that allows mLength to be zero
|
|
|
|
// while a heap-allocated buffer exists.
|
|
|
|
// By calling FinishBulkWriteImplImpl, we skip the
|
|
|
|
// zero case handling that's inappropriate in the
|
|
|
|
// SetCapacity() case.
|
|
|
|
FinishBulkWriteImplImpl(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>
|
|
|
|
void nsTSubstring<T>::SetLength(size_type aLength) {
|
2018-09-17 12:45:02 +03:00
|
|
|
if (!SetLength(aLength, mozilla::fallible)) {
|
|
|
|
AllocFailed(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>
|
|
|
|
bool nsTSubstring<T>::SetLength(size_type aLength,
|
|
|
|
const fallible_t& aFallible) {
|
2018-09-17 12:45:02 +03:00
|
|
|
size_type preserve = XPCOM_MIN(aLength, this->mLength);
|
|
|
|
mozilla::Result<uint32_t, nsresult> r =
|
|
|
|
StartBulkWriteImpl(aLength, preserve, true);
|
|
|
|
if (r.isErr()) {
|
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
|
|
|
|
2018-09-17 12:45:02 +03:00
|
|
|
FinishBulkWriteImpl(aLength);
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-09-17 12:45:02 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::Truncate() {
|
|
|
|
::ReleaseData(this->mData, this->mDataFlags);
|
|
|
|
SetToEmptyBuffer();
|
|
|
|
AssertValid();
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::SetIsVoid(bool aVal) {
|
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 {
|
|
|
|
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 {
|
|
|
|
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>
|
|
|
|
bool nsTStringRepr<T>::Equals(const self_type& aStr) const {
|
|
|
|
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>
|
|
|
|
bool nsTStringRepr<T>::Equals(const self_type& aStr,
|
|
|
|
const comparator_type& aComp) const {
|
|
|
|
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>
|
|
|
|
bool 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>
|
|
|
|
bool 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>
|
|
|
|
bool 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) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("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>
|
|
|
|
bool 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) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("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>
|
|
|
|
bool nsTStringRepr<T>::EqualsASCII(const char* aData, size_type aLen) const {
|
|
|
|
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>
|
|
|
|
bool nsTStringRepr<T>::EqualsASCII(const char* aData) const {
|
|
|
|
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>
|
|
|
|
bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData,
|
|
|
|
size_type aLen) const {
|
|
|
|
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>
|
|
|
|
bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData) const {
|
|
|
|
return char_traits::compareLowerCaseToASCIINullTerminated(
|
|
|
|
this->mData, this->mLength, 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 {
|
|
|
|
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>
|
|
|
|
int32_t nsTStringRepr<T>::FindChar(char_type aChar, index_type aOffset) const {
|
|
|
|
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>
|
|
|
|
void nsTSubstring<T>::StripChar(char_type aChar) {
|
|
|
|
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>
|
|
|
|
void nsTSubstring<T>::StripChars(const char_type* aChars) {
|
|
|
|
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>
|
|
|
|
void nsTSubstring<T>::StripTaggedASCII(const ASCIIMaskArray& aToStrip) {
|
|
|
|
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>
|
|
|
|
void 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 {
|
|
|
|
explicit PrintfAppend(nsTSubstring<T>* aString) : mString(aString) {}
|
2016-12-14 19:32:21 +03:00
|
|
|
|
|
|
|
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:
|
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>
|
|
|
|
void nsTSubstring<T>::AppendPrintf(const char* aFormat, ...) {
|
|
|
|
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>
|
|
|
|
void nsTSubstring<T>::AppendPrintf(const char* aFormat, va_list aAp) {
|
|
|
|
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
|
|
|
|
2019-03-29 22:38:53 +03:00
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntDec(int32_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntDec(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntDec(uint32_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntDec(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntOct(uint32_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntOct(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntHex(uint32_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntHex(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntDec(int64_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntDec(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntDec(uint64_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntDec(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntOct(uint64_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntOct(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void nsTSubstring<T>::AppendIntHex(uint64_t aInteger) {
|
|
|
|
PrintfAppend<T> appender(this);
|
|
|
|
bool r = appender.appendIntHex(aInteger);
|
|
|
|
if (MOZ_UNLIKELY(!r)) {
|
|
|
|
MOZ_CRASH("Allocation or other failure while appending integers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
// Returns the length of the formatted aDouble in aBuf.
|
|
|
|
static int FormatWithoutTrailingZeros(char (&aBuf)[40], double aDouble,
|
|
|
|
int aPrecision) {
|
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>
|
|
|
|
void 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>
|
|
|
|
void 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>
|
|
|
|
size_t nsTSubstring<T>::SizeOfExcludingThisIfUnshared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
2018-03-24 03:31:37 +03:00
|
|
|
if (this->mDataFlags & DataFlags::REFCOUNTED) {
|
2017-08-15 00:22:50 +03:00
|
|
|
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);
|
2018-11-29 13:30:46 +03:00
|
|
|
// - None of DataFlags::REFCOUNTED, 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>
|
|
|
|
size_t nsTSubstring<T>::SizeOfExcludingThisEvenIfShared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
2015-12-02 02:36:26 +03:00
|
|
|
// This is identical to SizeOfExcludingThisIfUnshared except for the
|
2018-03-24 03:31:37 +03:00
|
|
|
// DataFlags::REFCOUNTED case.
|
|
|
|
if (this->mDataFlags & DataFlags::REFCOUNTED) {
|
2017-08-15 00:22:50 +03:00
|
|
|
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>
|
|
|
|
size_t nsTSubstring<T>::SizeOfIncludingThisIfUnshared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2012-02-20 07:16:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
size_t nsTSubstring<T>::SizeOfIncludingThisEvenIfShared(
|
2014-05-27 11:15:35 +04:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThisEvenIfShared(aMallocSizeOf);
|
2013-01-18 09:21:35 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
template <typename T>
|
|
|
|
inline 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 {
|
|
|
|
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);
|
|
|
|
}
|
2018-01-18 17:32:35 +03:00
|
|
|
|
|
|
|
// Common logic for nsTSubstring<T>::ToInteger and nsTSubstring<T>::ToInteger64.
|
|
|
|
template <typename T, typename int_type>
|
|
|
|
int_type ToIntegerCommon(const nsTSubstring<T>& aSrc, nsresult* aErrorCode,
|
|
|
|
uint32_t aRadix) {
|
|
|
|
MOZ_ASSERT(aRadix == 10 || aRadix == 16);
|
|
|
|
|
|
|
|
// Initial value, override if we find an integer.
|
|
|
|
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
// Begin by skipping over leading chars that shouldn't be part of the number.
|
|
|
|
auto cp = aSrc.BeginReading();
|
|
|
|
auto endcp = aSrc.EndReading();
|
|
|
|
bool negate = false;
|
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
// NB: For backwards compatibility I'm not going to change this logic but
|
|
|
|
// it seems really odd. Previously there was logic to auto-detect the
|
|
|
|
// radix if kAutoDetect was passed in. In practice this value was never
|
|
|
|
// used, so it pretended to auto detect and skipped some preceding
|
|
|
|
// letters (excluding valid hex digits) but never used the result.
|
|
|
|
//
|
|
|
|
// For example if you pass in "Get the number: 10", aRadix = 10 we'd
|
|
|
|
// skip the 'G', and then fail to parse "et the number: 10". If aRadix =
|
|
|
|
// 16 we'd skip the 'G', and parse just 'e' returning 14.
|
|
|
|
while ((cp < endcp) && (!done)) {
|
|
|
|
switch (*cp++) {
|
|
|
|
// clang-format off
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
// clang-format on
|
|
|
|
case '-':
|
|
|
|
negate = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
// No base 16 or base 10 digits were found.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step back.
|
|
|
|
cp--;
|
|
|
|
|
|
|
|
mozilla::CheckedInt<int_type> result;
|
|
|
|
|
|
|
|
// Now iterate the numeric chars and build our result.
|
|
|
|
while (cp < endcp) {
|
|
|
|
auto theChar = *cp++;
|
|
|
|
if (('0' <= theChar) && (theChar <= '9')) {
|
|
|
|
result = (aRadix * result) + (theChar - '0');
|
|
|
|
} else if ((theChar >= 'A') && (theChar <= 'F')) {
|
|
|
|
if (10 == aRadix) {
|
|
|
|
// Invalid base 10 digit, error out.
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
result = (aRadix * result) + ((theChar - 'A') + 10);
|
|
|
|
}
|
|
|
|
} else if ((theChar >= 'a') && (theChar <= 'f')) {
|
|
|
|
if (10 == aRadix) {
|
|
|
|
// Invalid base 10 digit, error out.
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
result = (aRadix * result) + ((theChar - 'a') + 10);
|
|
|
|
}
|
|
|
|
} else if ((('X' == theChar) || ('x' == theChar)) && result == 0) {
|
|
|
|
// For some reason we support a leading 'x' regardless of radix. For
|
|
|
|
// example: "000000x500", aRadix = 10 would be parsed as 500 rather
|
|
|
|
// than 0.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// We've encountered a char that's not a legal number or sign and we can
|
|
|
|
// terminate processing.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.isValid()) {
|
|
|
|
// Overflow!
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integer found.
|
|
|
|
*aErrorCode = NS_OK;
|
|
|
|
|
|
|
|
if (negate) {
|
|
|
|
result = -result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int32_t nsTSubstring<T>::ToInteger(nsresult* aErrorCode,
|
|
|
|
uint32_t aRadix) const {
|
|
|
|
return ToIntegerCommon<T, int32_t>(*this, aErrorCode, aRadix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsTSubstring::ToInteger64
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
int64_t nsTSubstring<T>::ToInteger64(nsresult* aErrorCode,
|
|
|
|
uint32_t aRadix) const {
|
|
|
|
return ToIntegerCommon<T, int64_t>(*this, aErrorCode, aRadix);
|
|
|
|
}
|