2014-05-05 10:30:46 -07: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 04:12:37 -07: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-16 23:33:05 -07:00
|
|
|
|
|
|
|
#include "mozilla/double-conversion.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-09-24 10:36:55 -07:00
|
|
|
|
|
|
|
using double_conversion::DoubleToStringConverter;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-06 17:46:51 -07:00
|
|
|
#ifdef XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::nsTSubstring_CharT(char_type* aData, size_type aLength,
|
|
|
|
uint32_t aFlags)
|
|
|
|
: mData(aData),
|
|
|
|
mLength(aLength),
|
|
|
|
mFlags(aFlags)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aFlags & F_OWNED) {
|
2014-05-05 10:30:46 -07:00
|
|
|
STRING_STAT_INCREMENT(Adopt);
|
2015-04-03 06:52:00 -07:00
|
|
|
MOZ_LOG_CTOR(mData, "StringAdopt", 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2010-08-06 17:46:51 -07:00
|
|
|
#endif /* XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE */
|
2008-08-18 08:40:49 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
/**
|
|
|
|
* helper function for down-casting a nsTSubstring to a nsTFixedString.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
inline const nsTFixedString_CharT*
|
2014-05-27 00:15:35 -07:00
|
|
|
AsFixedString(const nsTSubstring_CharT* aStr)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return static_cast<const nsTFixedString_CharT*>(aStr);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-05 10:30:46 -07: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.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::MutatePrep(size_type aCapacity, char_type** aOldData,
|
|
|
|
uint32_t* aOldFlags)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// initialize to no old data
|
2014-05-27 00:15:35 -07:00
|
|
|
*aOldData = nullptr;
|
|
|
|
*aOldFlags = 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
size_type curCapacity = Capacity();
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
// If |aCapacity > kMaxCapacity|, then our doubling algorithm may not be
|
2014-05-05 10:30:46 -07: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 10:56:46 -07:00
|
|
|
static_assert((sizeof(nsStringBuffer) & 0x1) == 0,
|
|
|
|
"bad size for nsStringBuffer");
|
2014-05-05 10:30:46 -07:00
|
|
|
const size_type kMaxCapacity =
|
2014-05-27 00:15:35 -07:00
|
|
|
(size_type(-1) / 2 - sizeof(nsStringBuffer)) / sizeof(char_type) - 2;
|
|
|
|
if (aCapacity > kMaxCapacity) {
|
|
|
|
// Also assert for |aCapacity| equal to |size_type(-1)|, since we used to
|
2014-05-05 10:30:46 -07:00
|
|
|
// use that value to flag immutability.
|
2014-05-27 00:15:35 -07:00
|
|
|
NS_ASSERTION(aCapacity != size_type(-1), "Bogus capacity");
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |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 00:15:35 -07:00
|
|
|
if (curCapacity != 0) {
|
|
|
|
if (aCapacity <= curCapacity) {
|
2014-05-05 10:30:46 -07:00
|
|
|
mFlags &= ~F_VOIDED; // mutation clears voided flag
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// Use doubling algorithm when forced to increase available capacity.
|
|
|
|
size_type temp = curCapacity;
|
2014-05-27 00:15:35 -07:00
|
|
|
while (temp < aCapacity) {
|
2014-05-05 10:30:46 -07:00
|
|
|
temp <<= 1;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
|
|
|
NS_ASSERTION(XPCOM_MIN(temp, kMaxCapacity) >= aCapacity,
|
2014-05-05 10:30:46 -07:00
|
|
|
"should have hit the early return at the top");
|
2014-05-27 00:15:35 -07:00
|
|
|
aCapacity = XPCOM_MIN(temp, kMaxCapacity);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
//
|
|
|
|
// several cases:
|
|
|
|
//
|
|
|
|
// (1) we have a shared buffer (mFlags & F_SHARED)
|
|
|
|
// (2) we have an owned buffer (mFlags & F_OWNED)
|
|
|
|
// (3) we have a fixed buffer (mFlags & F_FIXED)
|
|
|
|
// (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 ;-)
|
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type storageSize = (aCapacity + 1) * sizeof(char_type);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// case #1
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mFlags & F_SHARED) {
|
2014-05-05 10:30:46 -07:00
|
|
|
nsStringBuffer* hdr = nsStringBuffer::FromData(mData);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!hdr->IsReadonly()) {
|
|
|
|
nsStringBuffer* newHdr = nsStringBuffer::Realloc(hdr, storageSize);
|
|
|
|
if (!newHdr) {
|
|
|
|
return false; // out-of-memory (original header left intact)
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
hdr = newHdr;
|
2014-05-27 00:15:35 -07:00
|
|
|
mData = (char_type*)hdr->Data();
|
2014-05-05 10:30:46 -07:00
|
|
|
mFlags &= ~F_VOIDED; // mutation clears voided flag
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
char_type* newData;
|
|
|
|
uint32_t newDataFlags;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// if we have a fixed buffer of sufficient size, then use it. this helps
|
|
|
|
// avoid heap allocations.
|
2014-05-27 00:15:35 -07:00
|
|
|
if ((mFlags & F_CLASS_FIXED) &&
|
|
|
|
(aCapacity < AsFixedString(this)->mFixedCapacity)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
newData = AsFixedString(this)->mFixedBuf;
|
|
|
|
newDataFlags = F_TERMINATED | F_FIXED;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
// if we reach here then, we must allocate a new buffer. we cannot
|
|
|
|
// make use of our F_OWNED or F_FIXED buffers because they are not
|
|
|
|
// large enough.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsStringBuffer* newHdr =
|
|
|
|
nsStringBuffer::Alloc(storageSize).take();
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!newHdr) {
|
|
|
|
return false; // we are still in a consistent state
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
newData = (char_type*)newHdr->Data();
|
2014-05-05 10:30:46 -07:00
|
|
|
newDataFlags = F_TERMINATED | F_SHARED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// save old data and flags
|
2014-05-27 00:15:35 -07:00
|
|
|
*aOldData = mData;
|
|
|
|
*aOldFlags = mFlags;
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
mData = newData;
|
|
|
|
SetDataFlags(newDataFlags);
|
|
|
|
|
|
|
|
// mLength does not change
|
|
|
|
|
|
|
|
// though we are not necessarily terminated at the moment, now is probably
|
|
|
|
// still the best time to set F_TERMINATED.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsTSubstring_CharT::Finalize()
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
::ReleaseData(mData, mFlags);
|
|
|
|
// mData, mLength, and mFlags are purposefully left dangling
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::ReplacePrepInternal(index_type aCutStart, size_type aCutLen,
|
|
|
|
size_type aFragLen, size_type aNewLen)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
char_type* oldData;
|
|
|
|
uint32_t oldFlags;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!MutatePrep(aNewLen, &oldData, &oldFlags)) {
|
|
|
|
return false; // out-of-memory
|
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (oldData) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// determine whether or not we need to copy part of the old string
|
|
|
|
// over to the new string.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aCutStart > 0) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// copy prefix from old string
|
2014-05-27 00:15:35 -07:00
|
|
|
char_traits::copy(mData, oldData, aCutStart);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aCutStart + aCutLen < mLength) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// copy suffix from old string to new offset
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type from = aCutStart + aCutLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
size_type fromLen = mLength - from;
|
2014-05-27 00:15:35 -07:00
|
|
|
uint32_t to = aCutStart + aFragLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
char_traits::copy(mData + to, oldData + from, fromLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
::ReleaseData(oldData, oldFlags);
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07: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.
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aFragLen != aCutLen && aCutStart + aCutLen < mLength) {
|
|
|
|
uint32_t from = aCutStart + aCutLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t fromLen = mLength - from;
|
2014-05-27 00:15:35 -07:00
|
|
|
uint32_t to = aCutStart + aFragLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
char_traits::move(mData + to, mData + from, fromLen);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// add null terminator (mutable mData always has room for the null-
|
|
|
|
// terminator).
|
2014-05-27 00:15:35 -07:00
|
|
|
mData[aNewLen] = char_type(0);
|
|
|
|
mLength = aNewLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTSubstring_CharT::size_type
|
|
|
|
nsTSubstring_CharT::Capacity() const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// return 0 to indicate an immutable or 0-sized buffer
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
size_type capacity;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mFlags & F_SHARED) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// if the string is readonly, then we pretend that it has no capacity.
|
|
|
|
nsStringBuffer* hdr = nsStringBuffer::FromData(mData);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (hdr->IsReadonly()) {
|
2014-05-05 10:30:46 -07:00
|
|
|
capacity = 0;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
capacity = (hdr->StorageSize() / sizeof(char_type)) - 1;
|
|
|
|
}
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (mFlags & F_FIXED) {
|
2014-05-05 10:30:46 -07:00
|
|
|
capacity = AsFixedString(this)->mFixedCapacity;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (mFlags & F_OWNED) {
|
2014-05-05 10:30:46 -07: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.
|
|
|
|
capacity = mLength;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
capacity = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::EnsureMutable(size_type aNewLen)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aNewLen == size_type(-1) || aNewLen == mLength) {
|
|
|
|
if (mFlags & (F_FIXED | F_OWNED)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
|
|
|
if ((mFlags & F_SHARED) &&
|
|
|
|
!nsStringBuffer::FromData(mData)->IsReadonly()) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
aNewLen = mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2015-01-28 01:00:40 -08:00
|
|
|
return SetLength(aNewLen, mozilla::fallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// This version of Assign is optimized for single-character assignment.
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(char_type aChar)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!ReplacePrep(0, mLength, 1)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(mLength);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
*mData = aChar;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-08 09:42:27 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(char_type aChar, const fallible_t&)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!ReplacePrep(0, mLength, 1)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
*mData = aChar;
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-20 13:32:54 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(const char_type* aData)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!Assign(aData, size_type(-1), mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(char_traits::length(aData));
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2013-08-20 13:32:54 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(const char_type* aData, size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!Assign(aData, aLength, mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(aLength == size_type(-1) ? char_traits::length(aData)
|
|
|
|
: aLength);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(const char_type* aData, size_type aLength,
|
2015-01-28 01:00:40 -08:00
|
|
|
const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!aData || aLength == 0) {
|
2014-05-05 10:30:46 -07:00
|
|
|
Truncate();
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (IsDependentOn(aData, aData + aLength)) {
|
2015-01-28 01:00:40 -08:00
|
|
|
return Assign(string_type(aData, aLength), aFallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!ReplacePrep(0, mLength, aLength)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
char_traits::copy(mData, aData, aLength);
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::AssignASCII(const char* aData, size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!AssignASCII(aData, aLength, mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(aLength);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::AssignASCII(const char* aData, size_type aLength,
|
2015-01-28 01:00:40 -08:00
|
|
|
const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// A Unicode string can't depend on an ASCII string buffer,
|
|
|
|
// so this dependence check only applies to CStrings.
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef CharT_is_char
|
2014-05-27 00:15:35 -07:00
|
|
|
if (IsDependentOn(aData, aData + aLength)) {
|
2015-01-28 01:00:40 -08:00
|
|
|
return Assign(string_type(aData, aLength), aFallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!ReplacePrep(0, mLength, aLength)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
char_traits::copyASCII(mData, aData, aLength);
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-08 12:51:38 -08:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::AssignLiteral(const char_type* aData, size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
::ReleaseData(mData, mFlags);
|
2014-05-27 00:15:35 -07:00
|
|
|
mData = const_cast<char_type*>(aData);
|
|
|
|
mLength = aLength;
|
2014-05-05 10:30:46 -07:00
|
|
|
SetDataFlags(F_TERMINATED | F_LITERAL);
|
|
|
|
}
|
2014-01-08 12:51:38 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(const self_type& aStr)
|
2012-05-08 09:42:27 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!Assign(aStr, mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(aStr.Length());
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-28 01:00:40 -08:00
|
|
|
nsTSubstring_CharT::Assign(const self_type& aStr, const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
// |aStr| could be sharable. We need to check its flags to know how to
|
2014-05-05 10:30:46 -07:00
|
|
|
// deal with it.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (&aStr == this) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!aStr.mLength) {
|
2014-05-05 10:30:46 -07:00
|
|
|
Truncate();
|
2014-05-27 00:15:35 -07:00
|
|
|
mFlags |= aStr.mFlags & F_VOIDED;
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aStr.mFlags & F_SHARED) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// nice! we can avoid a string copy :-)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
// |aStr| should be null-terminated
|
|
|
|
NS_ASSERTION(aStr.mFlags & F_TERMINATED, "shared, but not terminated");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
::ReleaseData(mData, mFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
mData = aStr.mData;
|
|
|
|
mLength = aStr.mLength;
|
2014-05-05 10:30:46 -07:00
|
|
|
SetDataFlags(F_TERMINATED | F_SHARED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// get an owning reference to the mData
|
|
|
|
nsStringBuffer::FromData(mData)->AddRef();
|
|
|
|
return true;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (aStr.mFlags & F_LITERAL) {
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(aStr.mFlags & F_TERMINATED, "Unterminated literal");
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
AssignLiteral(aStr.mData, aStr.mLength);
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// else, treat this like an ordinary assignment.
|
2015-01-28 01:00:40 -08:00
|
|
|
return Assign(aStr.Data(), aStr.Length(), aFallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(const substring_tuple_type& aTuple)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!Assign(aTuple, mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(aTuple.Length());
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Assign(const substring_tuple_type& aTuple,
|
2015-01-28 01:00:40 -08:00
|
|
|
const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aTuple.IsDependentOn(mData, mData + mLength)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// take advantage of sharing here...
|
2015-01-28 01:00:40 -08:00
|
|
|
return Assign(string_type(aTuple), aFallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type length = aTuple.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// don't use ReplacePrep here because it changes the length
|
|
|
|
char_type* oldData;
|
|
|
|
uint32_t oldFlags;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!MutatePrep(length, &oldData, &oldFlags)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2009-03-04 01:50:22 -08:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (oldData) {
|
2014-05-05 10:30:46 -07:00
|
|
|
::ReleaseData(oldData, oldFlags);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
aTuple.WriteTo(mData, length);
|
2014-05-05 10:30:46 -07:00
|
|
|
mData[length] = 0;
|
|
|
|
mLength = length;
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Adopt(char_type* aData, size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aData) {
|
2014-05-05 10:30:46 -07:00
|
|
|
::ReleaseData(mData, mFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
mData = aData;
|
|
|
|
mLength = aLength;
|
2014-05-05 10:30:46 -07:00
|
|
|
SetDataFlags(F_TERMINATED | F_OWNED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
STRING_STAT_INCREMENT(Adopt);
|
|
|
|
// Treat this as construction of a "StringAdopt" object for leak
|
|
|
|
// tracking purposes.
|
2015-04-03 06:52:00 -07:00
|
|
|
MOZ_LOG_CTOR(mData, "StringAdopt", 1);
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
SetIsVoid(true);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// This version of Replace is optimized for single-character replacement.
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (ReplacePrep(aCutStart, aCutLength, 1)) {
|
|
|
|
mData[aCutStart] = aChar;
|
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-19 10:05:02 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
char_type aChar,
|
2015-01-28 01:00:40 -08:00
|
|
|
const fallible_t&)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, Length());
|
2014-03-19 10:05:02 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!ReplacePrep(aCutStart, aCutLength, 1)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-03-19 10:05:02 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
mData[aCutStart] = aChar;
|
2014-03-19 10:05:02 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!Replace(aCutStart, aCutLength, aData, aLength,
|
2015-01-28 01:00:40 -08:00
|
|
|
mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(Length() - aCutLength + 1);
|
2014-03-19 10:05:02 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2014-03-19 10:05:02 -07:00
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength,
|
2015-01-28 01:00:40 -08:00
|
|
|
const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// unfortunately, some callers pass null :-(
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!aData) {
|
|
|
|
aLength = 0;
|
|
|
|
} else {
|
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = char_traits::length(aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsDependentOn(aData, aData + aLength)) {
|
|
|
|
nsTAutoString_CharT temp(aData, aLength);
|
2015-01-28 01:00:40 -08:00
|
|
|
return Replace(aCutStart, aCutLength, temp, aFallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, Length());
|
2014-03-19 10:05:02 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
bool ok = ReplacePrep(aCutStart, aCutLength, aLength);
|
|
|
|
if (!ok) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-03-19 10:05:02 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aLength > 0) {
|
|
|
|
char_traits::copy(mData + aCutStart, aData, aLength);
|
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData, size_type aLength)
|
2015-03-17 01:55:22 -07:00
|
|
|
{
|
|
|
|
if (!ReplaceASCII(aCutStart, aCutLength, aData, aLength, mozilla::fallible)) {
|
|
|
|
AllocFailed(Length() - aCutLength + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsTSubstring_CharT::ReplaceASCII(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char* aData, size_type aLength,
|
|
|
|
const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aLength == size_type(-1)) {
|
|
|
|
aLength = strlen(aData);
|
|
|
|
}
|
2014-03-19 10:05:02 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// A Unicode string can't depend on an ASCII string buffer,
|
|
|
|
// so this dependence check only applies to CStrings.
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef CharT_is_char
|
2014-05-27 00:15:35 -07:00
|
|
|
if (IsDependentOn(aData, aData + aLength)) {
|
|
|
|
nsTAutoString_CharT temp(aData, aLength);
|
2015-03-17 01:55:22 -07:00
|
|
|
return Replace(aCutStart, aCutLength, temp, aFallible);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
bool ok = ReplacePrep(aCutStart, aCutLength, aLength);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLength > 0) {
|
2014-05-27 00:15:35 -07:00
|
|
|
char_traits::copyASCII(mData + aCutStart, aData, aLength);
|
|
|
|
}
|
2015-03-17 01:55:22 -07:00
|
|
|
|
|
|
|
return true;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Replace(index_type aCutStart, size_type aCutLength,
|
|
|
|
const substring_tuple_type& aTuple)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aTuple.IsDependentOn(mData, mData + mLength)) {
|
|
|
|
nsTAutoString_CharT temp(aTuple);
|
|
|
|
Replace(aCutStart, aCutLength, temp);
|
2014-05-05 10:30:46 -07:00
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type length = aTuple.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (ReplacePrep(aCutStart, aCutLength, length) && length > 0) {
|
|
|
|
aTuple.WriteTo(mData + aCutStart, length);
|
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-08 12:51:38 -08:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::ReplaceLiteral(index_type aCutStart, size_type aCutLength,
|
|
|
|
const char_type* aData, size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
aCutStart = XPCOM_MIN(aCutStart, Length());
|
2014-01-08 12:51:38 -08:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!aCutStart && aCutLength == Length()) {
|
|
|
|
AssignLiteral(aData, aLength);
|
|
|
|
} else if (ReplacePrep(aCutStart, aCutLength, aLength) && aLength > 0) {
|
|
|
|
char_traits::copy(mData + aCutStart, aData, aLength);
|
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2014-01-08 12:51:38 -08:00
|
|
|
|
2012-05-08 09:42:27 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::SetCapacity(size_type aCapacity)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!SetCapacity(aCapacity, mozilla::fallible)) {
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(aCapacity);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::SetCapacity(size_type aCapacity, const fallible_t&)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// capacity does not include room for the terminating null char
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// if our capacity is reduced to zero, then free our buffer.
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aCapacity == 0) {
|
2014-05-05 10:30:46 -07:00
|
|
|
::ReleaseData(mData, mFlags);
|
|
|
|
mData = char_traits::sEmptyBuffer;
|
|
|
|
mLength = 0;
|
|
|
|
SetDataFlags(F_TERMINATED);
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
char_type* oldData;
|
|
|
|
uint32_t oldFlags;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!MutatePrep(aCapacity, &oldData, &oldFlags)) {
|
|
|
|
return false; // out-of-memory
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// compute new string length
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type newLen = XPCOM_MIN(mLength, aCapacity);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (oldData) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// preserve old data
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mLength > 0) {
|
2014-05-05 10:30:46 -07:00
|
|
|
char_traits::copy(mData, oldData, newLen);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
::ReleaseData(oldData, oldFlags);
|
|
|
|
}
|
2009-11-18 17:14:29 -08:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// adjust mLength if our buffer shrunk down in size
|
2014-05-27 00:15:35 -07:00
|
|
|
if (newLen < mLength) {
|
2014-05-05 10:30:46 -07:00
|
|
|
mLength = newLen;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// always null-terminate here, even if the buffer got longer. this is
|
|
|
|
// for backwards compat with the old string implementation.
|
2014-05-27 00:15:35 -07:00
|
|
|
mData[aCapacity] = char_type(0);
|
2012-05-08 09:42:27 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-08 09:42:27 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::SetLength(size_type aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
SetCapacity(aLength);
|
|
|
|
mLength = aLength;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2012-05-08 09:42:27 -07:00
|
|
|
|
|
|
|
bool
|
2015-01-28 01:00:40 -08:00
|
|
|
nsTSubstring_CharT::SetLength(size_type aLength, const fallible_t& aFallible)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-01-28 01:00:40 -08:00
|
|
|
if (!SetCapacity(aLength, aFallible)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-03-20 11:02:38 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
mLength = aLength;
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::SetIsVoid(bool aVal)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aVal) {
|
2014-05-05 10:30:46 -07:00
|
|
|
Truncate();
|
|
|
|
mFlags |= F_VOIDED;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
mFlags &= ~F_VOIDED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Equals(const self_type& aStr) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return mLength == aStr.mLength &&
|
|
|
|
char_traits::compare(mData, aStr.mData, mLength) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Equals(const self_type& aStr,
|
|
|
|
const comparator_type& aComp) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return mLength == aStr.mLength &&
|
|
|
|
aComp(mData, aStr.mData, mLength, aStr.mLength) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Equals(const char_type* aData) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// unfortunately, some callers pass null :-(
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!aData) {
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_NOTREACHED("null data pointer");
|
|
|
|
return mLength == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// XXX avoid length calculation?
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type length = char_traits::length(aData);
|
|
|
|
return mLength == length &&
|
|
|
|
char_traits::compare(mData, aData, mLength) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::Equals(const char_type* aData,
|
|
|
|
const comparator_type& aComp) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// unfortunately, some callers pass null :-(
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!aData) {
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_NOTREACHED("null data pointer");
|
|
|
|
return mLength == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// XXX avoid length calculation?
|
2014-05-27 00:15:35 -07:00
|
|
|
size_type length = char_traits::length(aData);
|
|
|
|
return mLength == length && aComp(mData, aData, mLength, length) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::EqualsASCII(const char* aData, size_type aLen) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return mLength == aLen &&
|
|
|
|
char_traits::compareASCII(mData, aData, aLen) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::EqualsASCII(const char* aData) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return char_traits::compareASCIINullTerminated(mData, mLength, aData) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::LowerCaseEqualsASCII(const char* aData,
|
|
|
|
size_type aLen) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return mLength == aLen &&
|
|
|
|
char_traits::compareLowerCaseToASCII(mData, aData, aLen) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::LowerCaseEqualsASCII(const char* aData) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return char_traits::compareLowerCaseToASCIINullTerminated(mData,
|
|
|
|
mLength,
|
|
|
|
aData) == 0;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsTSubstring_CharT::size_type
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::CountChar(char_type aChar) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
const char_type* start = mData;
|
|
|
|
const char_type* end = mData + mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
return NS_COUNT(start, end, aChar);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::FindChar(char_type aChar, index_type aOffset) const
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aOffset < mLength) {
|
|
|
|
const char_type* result = char_traits::find(mData + aOffset,
|
|
|
|
mLength - aOffset, aChar);
|
|
|
|
if (result) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return result - mData;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return -1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::StripChar(char_type aChar, int32_t aOffset)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mLength == 0 || aOffset >= int32_t(mLength)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!EnsureMutable()) { // XXX do this lazily?
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(mLength);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// XXX(darin): this code should defer writing until necessary.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
char_type* to = mData + aOffset;
|
|
|
|
char_type* from = mData + aOffset;
|
|
|
|
char_type* end = mData + mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (from < end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
char_type theChar = *from++;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aChar != theChar) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*to++ = theChar;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
*to = char_type(0); // add the null
|
|
|
|
mLength = to - mData;
|
|
|
|
}
|
2009-11-04 13:33:23 -08:00
|
|
|
|
2010-07-20 17:11:19 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::StripChars(const char_type* aChars, uint32_t aOffset)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aOffset >= uint32_t(mLength)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!EnsureMutable()) { // XXX do this lazily?
|
2014-11-12 00:13:44 -08:00
|
|
|
AllocFailed(mLength);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// XXX(darin): this code should defer writing until necessary.
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
char_type* to = mData + aOffset;
|
|
|
|
char_type* from = mData + aOffset;
|
|
|
|
char_type* end = mData + mLength;
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (from < end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
char_type theChar = *from++;
|
|
|
|
const char_type* test = aChars;
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
for (; *test && *test != theChar; ++test);
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
if (!*test) {
|
|
|
|
// Not stripped, copy this char.
|
|
|
|
*to++ = theChar;
|
|
|
|
}
|
2010-07-20 17:11:19 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
*to = char_type(0); // add the null
|
|
|
|
mLength = to - mData;
|
|
|
|
}
|
2010-07-20 17:11:19 -07:00
|
|
|
|
2012-08-09 00:09:40 -07:00
|
|
|
int
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::AppendFunc(void* aArg, const char* aStr, uint32_t aLen)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
self_type* self = static_cast<self_type*>(aArg);
|
2012-04-13 09:58:59 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// NSPR sends us the final null terminator even though we don't want it
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aLen && aStr[aLen - 1] == '\0') {
|
|
|
|
--aLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2012-04-13 09:58:59 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
self->AppendASCII(aStr, aLen);
|
2012-04-13 09:58:59 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
return aLen;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2010-11-30 10:18:15 -08:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
void
|
|
|
|
nsTSubstring_CharT::AppendPrintf(const char* aFormat, ...)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
va_list ap;
|
2014-05-27 00:15:35 -07:00
|
|
|
va_start(ap, aFormat);
|
|
|
|
uint32_t r = PR_vsxprintf(AppendFunc, this, aFormat, ap);
|
|
|
|
if (r == (uint32_t)-1) {
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_RUNTIMEABORT("Allocation or other failure in PR_vsxprintf");
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2011-08-25 07:43:49 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
void
|
|
|
|
nsTSubstring_CharT::AppendPrintf(const char* aFormat, va_list aAp)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
uint32_t r = PR_vsxprintf(AppendFunc, this, aFormat, aAp);
|
|
|
|
if (r == (uint32_t)-1) {
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_RUNTIMEABORT("Allocation or other failure in PR_vsxprintf");
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2010-11-30 10:18:15 -08:00
|
|
|
|
2013-09-24 10:36:55 -07:00
|
|
|
/* hack to make sure we define FormatWithoutTrailingZeros only once */
|
2010-11-30 10:18:15 -08:00
|
|
|
#ifdef CharT_is_PRUnichar
|
2014-05-27 00:15:35 -07:00
|
|
|
// Returns the length of the formatted aDouble in aBuf.
|
2013-09-24 10:36:55 -07:00
|
|
|
static int
|
2014-05-27 00:15:35 -07:00
|
|
|
FormatWithoutTrailingZeros(char (&aBuf)[40], double aDouble,
|
|
|
|
int aPrecision)
|
2010-11-30 10:18:15 -08:00
|
|
|
{
|
2013-09-24 10:36:55 -07:00
|
|
|
static const DoubleToStringConverter converter(DoubleToStringConverter::UNIQUE_ZERO |
|
|
|
|
DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN,
|
|
|
|
"Infinity",
|
|
|
|
"NaN",
|
|
|
|
'e',
|
|
|
|
-6, 21,
|
|
|
|
6, 1);
|
2014-05-27 00:15:35 -07:00
|
|
|
double_conversion::StringBuilder builder(aBuf, sizeof(aBuf));
|
2013-09-24 10:36:55 -07:00
|
|
|
bool exponential_notation = false;
|
2014-05-27 00:15:35 -07:00
|
|
|
converter.ToPrecision(aDouble, aPrecision, &exponential_notation, &builder);
|
2013-09-24 10:36:55 -07:00
|
|
|
int length = builder.position();
|
|
|
|
char* formattedDouble = builder.Finalize();
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
// If we have a shorter string than aPrecision, it means we have a special
|
2013-09-24 10:36:55 -07:00
|
|
|
// value (NaN or Infinity). All other numbers will be formatted with at
|
2014-05-27 00:15:35 -07:00
|
|
|
// least aPrecision digits.
|
|
|
|
if (length <= aPrecision) {
|
2013-09-24 10:36:55 -07:00
|
|
|
return length;
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2013-09-24 10:36:55 -07:00
|
|
|
char* end = formattedDouble + length;
|
2014-05-27 00:15:35 -07:00
|
|
|
char* decimalPoint = strchr(aBuf, '.');
|
2013-09-24 10:36:55 -07:00
|
|
|
// No trailing zeros to remove.
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!decimalPoint) {
|
2013-09-24 10:36:55 -07:00
|
|
|
return length;
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
|
|
|
|
2013-09-24 10:36:55 -07: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 00:15:35 -07:00
|
|
|
for (; ; --exponent) {
|
2013-09-24 10:36:55 -07:00
|
|
|
if (*exponent == 'e') {
|
|
|
|
break;
|
|
|
|
}
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
2013-09-24 10:36:55 -07:00
|
|
|
char* zerosBeforeExponent = exponent - 1;
|
2014-05-27 00:15:35 -07:00
|
|
|
for (; zerosBeforeExponent != decimalPoint; --zerosBeforeExponent) {
|
2013-09-24 10:36:55 -07:00
|
|
|
if (*zerosBeforeExponent != '0') {
|
|
|
|
break;
|
|
|
|
}
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
2013-09-24 10:36:55 -07:00
|
|
|
if (zerosBeforeExponent == decimalPoint) {
|
|
|
|
--zerosBeforeExponent;
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
2013-09-24 10:36:55 -07: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 00:15:35 -07:00
|
|
|
for (; trailingZeros != decimalPoint; --trailingZeros) {
|
2013-09-24 10:36:55 -07:00
|
|
|
if (*trailingZeros != '0') {
|
|
|
|
break;
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
|
|
|
}
|
2013-09-24 10:36:55 -07:00
|
|
|
if (trailingZeros == decimalPoint) {
|
|
|
|
--trailingZeros;
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
2013-09-24 10:36:55 -07:00
|
|
|
length -= end - (trailingZeros + 1);
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
|
|
|
|
2013-09-24 10:36:55 -07:00
|
|
|
return length;
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
|
|
|
#endif /* CharT_is_PRUnichar */
|
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::AppendFloat(float aFloat)
|
2013-09-24 10:36:55 -07:00
|
|
|
{
|
|
|
|
char buf[40];
|
|
|
|
int length = FormatWithoutTrailingZeros(buf, aFloat, 6);
|
|
|
|
AppendASCII(buf, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsTSubstring_CharT::AppendFloat(double aFloat)
|
2010-11-30 10:18:15 -08:00
|
|
|
{
|
|
|
|
char buf[40];
|
2013-09-24 10:36:55 -07:00
|
|
|
int length = FormatWithoutTrailingZeros(buf, aFloat, 15);
|
|
|
|
AppendASCII(buf, length);
|
2010-11-30 10:18:15 -08:00
|
|
|
}
|
|
|
|
|
2012-02-19 19:16:41 -08:00
|
|
|
size_t
|
|
|
|
nsTSubstring_CharT::SizeOfExcludingThisMustBeUnshared(
|
2014-05-27 00:15:35 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-19 19:16:41 -08:00
|
|
|
{
|
|
|
|
if (mFlags & F_SHARED) {
|
|
|
|
return nsStringBuffer::FromData(mData)->
|
2014-05-27 00:15:35 -07:00
|
|
|
SizeOfIncludingThisMustBeUnshared(aMallocSizeOf);
|
2013-01-17 21:21:35 -08:00
|
|
|
}
|
2012-02-19 19:16:41 -08:00
|
|
|
if (mFlags & F_OWNED) {
|
2014-05-27 00:15:35 -07:00
|
|
|
return aMallocSizeOf(mData);
|
2012-02-19 19:16:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, exactly one of the following must be true:
|
|
|
|
// - F_VOIDED is set, and mData points to sEmptyBuffer;
|
|
|
|
// - F_FIXED is set, and mData points to a buffer within a string
|
|
|
|
// object (e.g. nsAutoString);
|
|
|
|
// - None of F_SHARED, F_OWNED, F_FIXED is set, and mData points to a buffer
|
|
|
|
// owned by something else.
|
|
|
|
//
|
|
|
|
// In all three cases, we don't measure it.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsTSubstring_CharT::SizeOfExcludingThisIfUnshared(
|
2014-05-27 00:15:35 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-19 19:16:41 -08:00
|
|
|
{
|
|
|
|
// This is identical to SizeOfExcludingThisMustBeUnshared except for the
|
|
|
|
// F_SHARED case.
|
|
|
|
if (mFlags & F_SHARED) {
|
|
|
|
return nsStringBuffer::FromData(mData)->
|
2014-05-27 00:15:35 -07:00
|
|
|
SizeOfIncludingThisIfUnshared(aMallocSizeOf);
|
2012-02-19 19:16:41 -08:00
|
|
|
}
|
|
|
|
if (mFlags & F_OWNED) {
|
2014-05-27 00:15:35 -07:00
|
|
|
return aMallocSizeOf(mData);
|
2012-02-19 19:16:41 -08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-17 21:21:35 -08:00
|
|
|
size_t
|
|
|
|
nsTSubstring_CharT::SizeOfExcludingThisEvenIfShared(
|
2014-05-27 00:15:35 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2013-01-17 21:21:35 -08:00
|
|
|
{
|
|
|
|
// This is identical to SizeOfExcludingThisMustBeUnshared except for the
|
|
|
|
// F_SHARED case.
|
|
|
|
if (mFlags & F_SHARED) {
|
|
|
|
return nsStringBuffer::FromData(mData)->
|
2014-05-27 00:15:35 -07:00
|
|
|
SizeOfIncludingThisEvenIfShared(aMallocSizeOf);
|
2013-01-17 21:21:35 -08:00
|
|
|
}
|
|
|
|
if (mFlags & F_OWNED) {
|
2014-05-27 00:15:35 -07:00
|
|
|
return aMallocSizeOf(mData);
|
2013-01-17 21:21:35 -08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-19 19:16:41 -08:00
|
|
|
size_t
|
|
|
|
nsTSubstring_CharT::SizeOfIncludingThisMustBeUnshared(
|
2014-05-27 00:15:35 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-19 19:16:41 -08:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return aMallocSizeOf(this) +
|
|
|
|
SizeOfExcludingThisMustBeUnshared(aMallocSizeOf);
|
2012-02-19 19:16:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsTSubstring_CharT::SizeOfIncludingThisIfUnshared(
|
2014-05-27 00:15:35 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-19 19:16:41 -08:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2012-02-19 19:16:41 -08:00
|
|
|
}
|
|
|
|
|
2013-01-17 21:21:35 -08:00
|
|
|
size_t
|
|
|
|
nsTSubstring_CharT::SizeOfIncludingThisEvenIfShared(
|
2014-05-27 00:15:35 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const
|
2013-01-17 21:21:35 -08:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThisEvenIfShared(aMallocSizeOf);
|
2013-01-17 21:21:35 -08:00
|
|
|
}
|
|
|
|
|