1998-04-14 00:24:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-04-14 00:24:54 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-04-14 00:24:54 +04:00
|
|
|
*/
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsCRT.h"
|
1998-06-06 01:38:36 +04:00
|
|
|
#include "nsISizeOfHandler.h"
|
1999-01-23 09:58:34 +03:00
|
|
|
#include "nsString.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
static const PRInt32 kGrowArrayBy = 4;
|
|
|
|
static const PRInt32 kLinearThreshold = 16;
|
|
|
|
static const PRInt32 kGrowthFactor = 1;
|
1999-12-01 03:37:11 +03:00
|
|
|
|
|
|
|
inline PRInt32
|
|
|
|
nsVoidArray::GetArraySize() const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
return mImpl ? PRInt32(mImpl->mBits & kArraySizeMask) : 0;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
nsVoidArray::SetArraySize(PRInt32 aSize)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
NS_PRECONDITION(mImpl, "can't set size");
|
|
|
|
mImpl->mBits &= ~kArraySizeMask;
|
|
|
|
mImpl->mBits |= PRUint32(aSize & kArraySizeMask);
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline PRBool
|
|
|
|
nsVoidArray::IsArrayOwner() const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
return mImpl ? PRBool(mImpl->mBits & kArrayOwnerMask) : PR_FALSE;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
nsVoidArray::SetArrayOwner(PRBool aOwner)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
NS_PRECONDITION(mImpl, "can't set owner");
|
|
|
|
if (aOwner)
|
|
|
|
mImpl->mBits |= kArrayOwnerMask;
|
|
|
|
else
|
|
|
|
mImpl->mBits &= ~kArrayOwnerMask;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
nsVoidArray::nsVoidArray()
|
2000-06-13 12:33:19 +04:00
|
|
|
: mImpl(nsnull)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-06-12 02:48:25 +04:00
|
|
|
nsVoidArray::nsVoidArray(PRInt32 aCount)
|
2000-06-13 12:33:19 +04:00
|
|
|
: mImpl(nsnull)
|
1998-06-12 02:48:25 +04:00
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
2000-06-13 12:33:19 +04:00
|
|
|
if (aCount) {
|
|
|
|
char* bytes = new char[sizeof(Impl) + sizeof(void*) * (aCount - 1)];
|
|
|
|
mImpl = NS_REINTERPRET_CAST(Impl*, bytes);
|
|
|
|
if (mImpl) {
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mBits = 0;
|
2000-01-19 04:18:27 +03:00
|
|
|
SetArraySize(aCount);
|
2000-06-13 12:33:19 +04:00
|
|
|
mImpl->mCount = aCount;
|
|
|
|
nsCRT::memset(mImpl->mArray, 0, mImpl->mCount * sizeof(void*));
|
|
|
|
SetArrayOwner(PR_TRUE);
|
2000-01-19 04:18:27 +03:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
}
|
1998-06-12 02:48:25 +04:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl && IsArrayOwner())
|
|
|
|
delete[] NS_REINTERPRET_CAST(char*, mImpl);
|
|
|
|
|
|
|
|
PRInt32 otherCount = other.Count();
|
|
|
|
if (otherCount) {
|
|
|
|
char* bytes = new char[sizeof(Impl) + sizeof(void*) * (otherCount - 1)];
|
|
|
|
mImpl = NS_REINTERPRET_CAST(Impl*, bytes);
|
|
|
|
if (mImpl) {
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mBits = 0;
|
2000-06-13 12:33:19 +04:00
|
|
|
SetArraySize(otherCount);
|
|
|
|
mImpl->mCount = otherCount;
|
|
|
|
SetArrayOwner(PR_TRUE);
|
2000-08-24 13:26:58 +04:00
|
|
|
|
|
|
|
nsCRT::memcpy(mImpl->mArray, other.mImpl->mArray, mImpl->mCount * sizeof(void*));
|
2000-06-13 12:33:19 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
else {
|
|
|
|
mImpl = nsnull;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsVoidArray::~nsVoidArray()
|
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_DTOR(nsVoidArray);
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl && IsArrayOwner())
|
|
|
|
delete[] NS_REINTERPRET_CAST(char*, mImpl);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1999-08-31 07:04:37 +04:00
|
|
|
|
1998-06-06 01:38:36 +04:00
|
|
|
void
|
1999-08-31 07:04:37 +04:00
|
|
|
nsVoidArray::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const
|
1998-06-06 01:38:36 +04:00
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
if (aResult) {
|
2000-06-13 12:33:19 +04:00
|
|
|
*aResult = sizeof(*this) + (mImpl ? sizeof(Impl) + (sizeof(void*) * (GetArraySize() - 1)) : 0);
|
1999-08-31 07:04:37 +04:00
|
|
|
}
|
1998-06-06 01:38:36 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
void* nsVoidArray::ElementAt(PRInt32 aIndex) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (aIndex < 0 || aIndex >= Count()) {
|
1998-04-14 00:24:54 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
return mImpl->mArray[aIndex];
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nsVoidArray::IndexOf(void* aPossibleElement) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end) {
|
|
|
|
if (*ap == aPossibleElement) {
|
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 oldCount = Count();
|
1998-04-14 00:24:54 +04:00
|
|
|
if (PRUint32(aIndex) > PRUint32(oldCount)) {
|
|
|
|
// An invalid index causes the insertion to fail
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-12-01 03:37:11 +03:00
|
|
|
if (oldCount >= GetArraySize()) {
|
2000-06-13 12:33:19 +04:00
|
|
|
// We have to grow the array. Grow by 4 slots if we're smaller
|
|
|
|
// than 16 elements, or a power of two if we're larger.
|
|
|
|
PRInt32 newCount = oldCount + ((oldCount >= kLinearThreshold) ? (oldCount / kGrowthFactor) : kGrowArrayBy);
|
|
|
|
|
|
|
|
char* bytes = new char[sizeof(Impl) + sizeof(void*) * (newCount - 1)];
|
|
|
|
Impl* newImpl = NS_REINTERPRET_CAST(Impl*, bytes);
|
|
|
|
|
|
|
|
if (newImpl) {
|
|
|
|
if (aIndex != 0)
|
|
|
|
nsCRT::memcpy(newImpl->mArray, mImpl->mArray, aIndex * sizeof(void*));
|
|
|
|
|
|
|
|
PRInt32 slide = oldCount - aIndex;
|
|
|
|
if (0 != slide) {
|
|
|
|
// Slide data over to make room for the insertion
|
|
|
|
nsCRT::memcpy(newImpl->mArray + aIndex + 1, mImpl->mArray + aIndex,
|
|
|
|
slide * sizeof(void*));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsArrayOwner())
|
|
|
|
delete[] NS_REINTERPRET_CAST(char*, mImpl);
|
|
|
|
|
|
|
|
mImpl = newImpl;
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mBits = 0;
|
2000-06-13 12:33:19 +04:00
|
|
|
SetArraySize(newCount);
|
|
|
|
mImpl->mCount = oldCount;
|
|
|
|
SetArrayOwner(PR_TRUE);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
}
|
|
|
|
else {
|
1998-04-14 00:24:54 +04:00
|
|
|
// The array is already large enough
|
|
|
|
PRInt32 slide = oldCount - aIndex;
|
|
|
|
if (0 != slide) {
|
|
|
|
// Slide data over to make room for the insertion
|
2000-06-13 12:33:19 +04:00
|
|
|
nsCRT::memmove(mImpl->mArray + aIndex + 1, mImpl->mArray + aIndex,
|
1998-04-14 00:24:54 +04:00
|
|
|
slide * sizeof(void*));
|
|
|
|
}
|
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
|
|
|
|
mImpl->mArray[aIndex] = aElement;
|
|
|
|
mImpl->mCount++;
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::ReplaceElementAt(void* aElement, PRInt32 aIndex)
|
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
if (PRUint32(aIndex) >= PRUint32(GetArraySize())) {
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 oldCount = Count();
|
1998-07-02 04:28:56 +04:00
|
|
|
PRInt32 requestedCount = aIndex + 1;
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 growDelta = requestedCount - oldCount;
|
|
|
|
PRInt32 newCount = oldCount + (growDelta > kGrowArrayBy ? growDelta : kGrowArrayBy);
|
|
|
|
|
|
|
|
char* bytes = new char[sizeof(Impl) + sizeof(void*) * (newCount - 1)];
|
|
|
|
Impl* newImpl = NS_REINTERPRET_CAST(Impl*, bytes);
|
|
|
|
|
|
|
|
if (newImpl) {
|
|
|
|
nsCRT::memset(newImpl->mArray, 0, newCount * sizeof(void*));
|
|
|
|
|
|
|
|
if (mImpl != nsnull && aIndex != 0)
|
|
|
|
nsCRT::memcpy(newImpl->mArray, mImpl->mArray, mImpl->mCount * sizeof(void*));
|
|
|
|
|
1999-12-01 03:37:11 +03:00
|
|
|
if (IsArrayOwner())
|
2000-06-13 12:33:19 +04:00
|
|
|
delete[] NS_REINTERPRET_CAST(char*, mImpl);
|
|
|
|
|
|
|
|
mImpl = newImpl;
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mBits = 0;
|
2000-06-13 12:33:19 +04:00
|
|
|
SetArraySize(newCount);
|
|
|
|
mImpl->mCount = oldCount;
|
|
|
|
SetArrayOwner(PR_TRUE);
|
1998-07-02 04:28:56 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
|
|
|
|
mImpl->mArray[aIndex] = aElement;
|
|
|
|
if (aIndex >= mImpl->mCount)
|
|
|
|
mImpl->mCount = aIndex + 1;
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::RemoveElementAt(PRInt32 aIndex)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 oldCount = Count();
|
1998-04-14 00:24:54 +04:00
|
|
|
if (PRUint32(aIndex) >= PRUint32(oldCount)) {
|
|
|
|
// An invalid index causes the replace to fail
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to move any elements if we're removing the
|
|
|
|
// last element in the array
|
|
|
|
if (aIndex < (oldCount - 1)) {
|
2000-06-13 12:33:19 +04:00
|
|
|
nsCRT::memmove(mImpl->mArray + aIndex, mImpl->mArray + aIndex + 1,
|
1998-04-14 00:24:54 +04:00
|
|
|
(oldCount - 1 - aIndex) * sizeof(void*));
|
|
|
|
}
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
mImpl->mCount--;
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::RemoveElement(void* aElement)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
void** ep = mImpl->mArray;
|
|
|
|
void** end = ep + mImpl->mCount;
|
|
|
|
while (ep < end) {
|
|
|
|
void* e = *ep++;
|
|
|
|
if (e == aElement) {
|
|
|
|
ep--;
|
|
|
|
return RemoveElementAt(PRInt32(ep - mImpl->mArray));
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Clear()
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
mImpl->mCount = 0;
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Compact()
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
PRInt32 count = mImpl->mCount;
|
|
|
|
if (IsArrayOwner() && GetArraySize() > count) {
|
|
|
|
Impl* doomedImpl = mImpl;
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
char* bytes = new char[sizeof(Impl) + sizeof(void*) * (count - 1)];
|
|
|
|
Impl* newImpl = NS_REINTERPRET_CAST(Impl*, bytes);
|
|
|
|
if (newImpl)
|
|
|
|
nsCRT::memcpy(newImpl->mArray, mImpl->mArray, count * sizeof(void*));
|
|
|
|
|
|
|
|
mImpl = newImpl;
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mBits = 0;
|
2000-06-13 12:33:19 +04:00
|
|
|
SetArraySize(count);
|
|
|
|
mImpl->mCount = count;
|
2000-06-14 11:27:08 +04:00
|
|
|
SetArrayOwner(PR_TRUE);
|
2000-06-13 12:33:19 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mImpl = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] NS_REINTERPRET_CAST(char*, doomedImpl);
|
1998-06-06 01:38:36 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
1998-05-21 00:31:58 +04:00
|
|
|
|
|
|
|
PRBool nsVoidArray::EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRInt32 index = -1;
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
while (running && (++index < mImpl->mCount)) {
|
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
1998-05-21 00:31:58 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
PRInt32 index = Count();
|
|
|
|
while (running && (0 <= --index)) {
|
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
1998-05-21 00:31:58 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
1999-12-01 03:37:11 +03:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// nsAutoVoidArray
|
|
|
|
|
|
|
|
nsAutoVoidArray::nsAutoVoidArray()
|
|
|
|
: nsVoidArray()
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
mImpl = NS_REINTERPRET_CAST(Impl*, mAutoBuf);
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mBits = 0;
|
2000-06-13 12:33:19 +04:00
|
|
|
SetArraySize(kAutoBufSize);
|
2000-06-15 10:42:27 +04:00
|
|
|
mImpl->mCount = 0;
|
1999-12-01 03:37:11 +03:00
|
|
|
SetArrayOwner(PR_FALSE);
|
2000-06-15 10:42:27 +04:00
|
|
|
::memset(mImpl->mArray, 0, mImpl->mCount * sizeof(void*));
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
1999-01-23 09:58:34 +03:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// nsStringArray
|
|
|
|
|
|
|
|
nsStringArray::nsStringArray(void)
|
|
|
|
: nsVoidArray()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringArray::~nsStringArray(void)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringArray&
|
|
|
|
nsStringArray::operator=(const nsStringArray& other)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
// Copy the pointers
|
|
|
|
nsVoidArray::operator=(other);
|
|
|
|
|
|
|
|
// Now copy the strings
|
|
|
|
for (PRInt32 i = Count() - 1; i >= 0; --i) {
|
2000-06-28 18:37:25 +04:00
|
|
|
nsString* oldString = NS_STATIC_CAST(nsString*, other.ElementAt(i));
|
2000-06-13 12:33:19 +04:00
|
|
|
mImpl->mArray[i] = new nsString(*oldString);
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
|
1999-01-23 09:58:34 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-08-31 07:04:37 +04:00
|
|
|
nsStringArray::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
PRUint32 sum = 0;
|
|
|
|
nsVoidArray::SizeOf(aHandler, &sum);
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
while (0 <= --index) {
|
2000-06-13 12:33:19 +04:00
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, ElementAt(index));
|
1999-08-31 07:04:37 +04:00
|
|
|
PRUint32 size;
|
|
|
|
string->SizeOf(aHandler, &size);
|
|
|
|
sum += size;
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::StringAt(PRInt32 aIndex, nsAWritableString& aString) const
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, nsVoidArray::ElementAt(aIndex));
|
1999-01-23 09:58:34 +03:00
|
|
|
if (nsnull != string) {
|
2000-08-23 21:27:06 +04:00
|
|
|
aString.Assign(*string);
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
aString.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString*
|
|
|
|
nsStringArray::StringAt(PRInt32 aIndex) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
return NS_STATIC_CAST(nsString*, nsVoidArray::ElementAt(aIndex));
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::IndexOf(const nsAReadableString& aPossibleString) const
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end) {
|
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, *ap);
|
|
|
|
if (string->Equals(aPossibleString)) {
|
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::IndexOfIgnoreCase(const nsAReadableString& aPossibleString) const
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2000-08-23 21:27:06 +04:00
|
|
|
// XXX
|
|
|
|
// nsString::EqualsIgnoreCase() doesn't take a nsAReadableString& so we
|
|
|
|
// construct a stack based string here and do a copy...
|
|
|
|
nsAutoString possible_string(aPossibleString);
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end) {
|
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, *ap);
|
2000-08-23 21:27:06 +04:00
|
|
|
if (string->EqualsIgnoreCase(possible_string)) {
|
2000-06-13 12:33:19 +04:00
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::InsertStringAt(const nsAReadableString& aString, PRInt32 aIndex)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
|
|
|
nsString* string = new nsString(aString);
|
|
|
|
if (nsVoidArray::InsertElementAt(string, aIndex)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
delete string;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::ReplaceStringAt(const nsAReadableString& aString,
|
|
|
|
PRInt32 aIndex)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, nsVoidArray::ElementAt(aIndex));
|
1999-01-23 09:58:34 +03:00
|
|
|
if (nsnull != string) {
|
|
|
|
*string = aString;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::RemoveString(const nsAReadableString& aString)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
|
|
|
PRInt32 index = IndexOf(aString);
|
|
|
|
if (-1 < index) {
|
|
|
|
return RemoveStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringArray::RemoveStringIgnoreCase(const nsAReadableString& aString)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
|
|
|
PRInt32 index = IndexOfIgnoreCase(aString);
|
|
|
|
if (-1 < index) {
|
|
|
|
return RemoveStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsStringArray::RemoveStringAt(PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
nsString* string = StringAt(aIndex);
|
|
|
|
if (nsnull != string) {
|
|
|
|
nsVoidArray::RemoveElementAt(aIndex);
|
|
|
|
delete string;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStringArray::Clear(void)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
while (0 <= --index) {
|
2000-06-13 12:33:19 +04:00
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, mImpl->mArray[index]);
|
1999-01-23 09:58:34 +03:00
|
|
|
delete string;
|
|
|
|
}
|
|
|
|
nsVoidArray::Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::EnumerateForwards(nsStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRInt32 index = -1;
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
while (running && (++index < mImpl->mCount)) {
|
|
|
|
running = (*aFunc)(*NS_STATIC_CAST(nsString*, mImpl->mArray[index]), aData);
|
|
|
|
}
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::EnumerateBackwards(nsStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
while (running && (0 <= --index)) {
|
|
|
|
running = (*aFunc)(*NS_STATIC_CAST(nsString*, mImpl->mArray[index]), aData);
|
|
|
|
}
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
1999-10-30 00:47:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// nsCStringArray
|
|
|
|
|
|
|
|
nsCStringArray::nsCStringArray(void)
|
|
|
|
: nsVoidArray()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringArray::~nsCStringArray(void)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringArray&
|
|
|
|
nsCStringArray::operator=(const nsCStringArray& other)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
// Copy the pointers
|
|
|
|
nsVoidArray::operator=(other);
|
|
|
|
|
|
|
|
// Now copy the strings
|
|
|
|
for (PRInt32 i = Count() - 1; i >= 0; --i) {
|
2000-06-28 18:37:25 +04:00
|
|
|
nsCString* oldString = NS_STATIC_CAST(nsCString*, other.ElementAt(i));
|
2000-06-13 12:33:19 +04:00
|
|
|
mImpl->mArray[i] = new nsCString(*oldString);
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
2000-06-13 12:33:19 +04:00
|
|
|
|
1999-10-30 00:47:17 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCStringArray::SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const
|
|
|
|
{
|
|
|
|
PRUint32 sum = 0;
|
|
|
|
nsVoidArray::SizeOf(aHandler, &sum);
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 index = Count();
|
1999-10-30 00:47:17 +04:00
|
|
|
while (0 <= --index) {
|
2000-06-13 12:33:19 +04:00
|
|
|
nsCString* string = NS_STATIC_CAST(nsCString*, mImpl->mArray[index]);
|
1999-10-30 00:47:17 +04:00
|
|
|
PRUint32 size;
|
|
|
|
string->SizeOf(aHandler, &size);
|
|
|
|
sum += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCStringArray::CStringAt(PRInt32 aIndex, nsCString& aCString) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
nsCString* string = NS_STATIC_CAST(nsCString*, nsVoidArray::ElementAt(aIndex));
|
1999-10-30 00:47:17 +04:00
|
|
|
if (nsnull != string) {
|
|
|
|
aCString = *string;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aCString.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString*
|
|
|
|
nsCStringArray::CStringAt(PRInt32 aIndex) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
return NS_STATIC_CAST(nsCString*, nsVoidArray::ElementAt(aIndex));
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsCStringArray::IndexOf(const nsCString& aPossibleString) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end) {
|
|
|
|
nsCString* string = NS_STATIC_CAST(nsCString*, *ap);
|
|
|
|
if (string->Equals(aPossibleString)) {
|
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsCStringArray::IndexOfIgnoreCase(const nsCString& aPossibleString) const
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end) {
|
|
|
|
nsCString* string = NS_STATIC_CAST(nsCString*, *ap);
|
|
|
|
if (string->EqualsIgnoreCase(aPossibleString)) {
|
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::InsertCStringAt(const nsCString& aCString, PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
nsCString* string = new nsCString(aCString);
|
|
|
|
if (nsVoidArray::InsertElementAt(string, aIndex)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
delete string;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::ReplaceCStringAt(const nsCString& aCString, PRInt32 aIndex)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
nsCString* string = NS_STATIC_CAST(nsCString*, nsVoidArray::ElementAt(aIndex));
|
1999-10-30 00:47:17 +04:00
|
|
|
if (nsnull != string) {
|
|
|
|
*string = aCString;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::RemoveCString(const nsCString& aCString)
|
|
|
|
{
|
|
|
|
PRInt32 index = IndexOf(aCString);
|
|
|
|
if (-1 < index) {
|
|
|
|
return RemoveCStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::RemoveCStringIgnoreCase(const nsCString& aCString)
|
|
|
|
{
|
|
|
|
PRInt32 index = IndexOfIgnoreCase(aCString);
|
|
|
|
if (-1 < index) {
|
|
|
|
return RemoveCStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsCStringArray::RemoveCStringAt(PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
nsCString* string = CStringAt(aIndex);
|
|
|
|
if (nsnull != string) {
|
|
|
|
nsVoidArray::RemoveElementAt(aIndex);
|
|
|
|
delete string;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCStringArray::Clear(void)
|
|
|
|
{
|
2000-06-13 12:33:19 +04:00
|
|
|
PRInt32 index = Count();
|
1999-10-30 00:47:17 +04:00
|
|
|
while (0 <= --index) {
|
2000-06-13 12:33:19 +04:00
|
|
|
nsCString* string = NS_STATIC_CAST(nsCString*, mImpl->mArray[index]);
|
1999-10-30 00:47:17 +04:00
|
|
|
delete string;
|
|
|
|
}
|
|
|
|
nsVoidArray::Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::EnumerateForwards(nsCStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
PRInt32 index = -1;
|
|
|
|
while (running && (++index < mImpl->mCount)) {
|
|
|
|
running = (*aFunc)(*NS_STATIC_CAST(nsCString*, mImpl->mArray[index]), aData);
|
|
|
|
}
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::EnumerateBackwards(nsCStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2000-06-13 12:33:19 +04:00
|
|
|
if (mImpl) {
|
|
|
|
PRInt32 index = Count();
|
|
|
|
while (running && (0 <= --index)) {
|
|
|
|
running = (*aFunc)(*NS_STATIC_CAST(nsCString*, mImpl->mArray[index]), aData);
|
|
|
|
}
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|