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
|
|
|
|
2005-11-02 19:04:22 +03: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
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return mImpl ? PRInt32(mImpl->mBits & kArraySizeMask) : 0;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
nsVoidArray::SetArraySize(PRInt32 aSize)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return mImpl ? PRBool(mImpl->mBits & kArrayOwnerMask) : PR_FALSE;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
nsVoidArray::SetArrayOwner(PRBool aOwner)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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()
|
2005-11-02 19:04:22 +03: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)
|
2005-11-02 19:04:22 +03:00
|
|
|
: mImpl(nsnull)
|
1998-06-12 02:48:25 +04:00
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
2005-11-02 19:04:22 +03:00
|
|
|
if (aCount) {
|
|
|
|
char* bytes = new char[sizeof(Impl) + sizeof(void*) * (aCount - 1)];
|
|
|
|
mImpl = NS_REINTERPRET_CAST(Impl*, bytes);
|
|
|
|
if (mImpl) {
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mBits = 0;
|
2005-11-02 19:04:15 +03:00
|
|
|
SetArraySize(aCount);
|
2005-11-02 19:04:22 +03:00
|
|
|
mImpl->mCount = aCount;
|
|
|
|
nsCRT::memset(mImpl->mArray, 0, mImpl->mCount * sizeof(void*));
|
|
|
|
SetArrayOwner(PR_TRUE);
|
2005-11-02 19:04:15 +03:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
1998-06-12 02:48:25 +04:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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) {
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mBits = 0;
|
2005-11-02 19:04:22 +03:00
|
|
|
SetArraySize(otherCount);
|
|
|
|
mImpl->mCount = otherCount;
|
|
|
|
SetArrayOwner(PR_TRUE);
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
else {
|
|
|
|
mImpl = nsnull;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03: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);
|
2005-11-02 19:04:22 +03: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) {
|
2005-11-02 19:04:22 +03: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
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
if (aIndex < 0 || aIndex >= Count()) {
|
1998-04-14 00:24:54 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
return mImpl->mArray[aIndex];
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 nsVoidArray::IndexOf(void* aPossibleElement) const
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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()) {
|
2005-11-02 19:04:22 +03: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;
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mBits = 0;
|
2005-11-02 19:04:22 +03:00
|
|
|
SetArraySize(newCount);
|
|
|
|
mImpl->mCount = oldCount;
|
|
|
|
SetArrayOwner(PR_TRUE);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03: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
|
2005-11-02 19:04:22 +03:00
|
|
|
nsCRT::memmove(mImpl->mArray + aIndex + 1, mImpl->mArray + aIndex,
|
1998-04-14 00:24:54 +04:00
|
|
|
slide * sizeof(void*));
|
|
|
|
}
|
|
|
|
}
|
2005-11-02 19:04:22 +03: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())) {
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 oldCount = Count();
|
1998-07-02 04:28:56 +04:00
|
|
|
PRInt32 requestedCount = aIndex + 1;
|
2005-11-02 19:04:22 +03: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())
|
2005-11-02 19:04:22 +03:00
|
|
|
delete[] NS_REINTERPRET_CAST(char*, mImpl);
|
|
|
|
|
|
|
|
mImpl = newImpl;
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mBits = 0;
|
2005-11-02 19:04:22 +03: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
|
|
|
}
|
2005-11-02 19:04:22 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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)) {
|
2005-11-02 19:04:22 +03:00
|
|
|
nsCRT::memmove(mImpl->mArray + aIndex, mImpl->mArray + aIndex + 1,
|
1998-04-14 00:24:54 +04:00
|
|
|
(oldCount - 1 - aIndex) * sizeof(void*));
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:22 +03:00
|
|
|
mImpl->mCount--;
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::RemoveElement(void* aElement)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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()
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
if (mImpl) {
|
|
|
|
mImpl->mCount = 0;
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Compact()
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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;
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mBits = 0;
|
2005-11-02 19:04:22 +03:00
|
|
|
SetArraySize(count);
|
|
|
|
mImpl->mCount = count;
|
2005-11-02 19:04:23 +03:00
|
|
|
SetArrayOwner(PR_TRUE);
|
2005-11-02 19:04:22 +03: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;
|
|
|
|
|
2005-11-02 19:04:22 +03: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;
|
|
|
|
|
2005-11-02 19:04:22 +03: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()
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
mImpl = NS_REINTERPRET_CAST(Impl*, mAutoBuf);
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mBits = 0;
|
2005-11-02 19:04:22 +03:00
|
|
|
SetArraySize(kAutoBufSize);
|
2005-11-02 19:04:24 +03:00
|
|
|
mImpl->mCount = 0;
|
1999-12-01 03:37:11 +03:00
|
|
|
SetArrayOwner(PR_FALSE);
|
2005-11-02 19:04:24 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
// Copy the pointers
|
|
|
|
nsVoidArray::operator=(other);
|
|
|
|
|
|
|
|
// Now copy the strings
|
|
|
|
for (PRInt32 i = Count() - 1; i >= 0; --i) {
|
|
|
|
nsString* oldString = NS_STATIC_CAST(nsString*, ElementAt(i));
|
|
|
|
mImpl->mArray[i] = new nsString(*oldString);
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
2005-11-02 19:04:22 +03: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);
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
while (0 <= --index) {
|
2005-11-02 19:04:22 +03: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
|
|
|
|
nsStringArray::StringAt(PRInt32 aIndex, nsString& aString) const
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, nsVoidArray::ElementAt(aIndex));
|
1999-01-23 09:58:34 +03:00
|
|
|
if (nsnull != string) {
|
|
|
|
aString = *string;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aString.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString*
|
|
|
|
nsStringArray::StringAt(PRInt32 aIndex) const
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return NS_STATIC_CAST(nsString*, nsVoidArray::ElementAt(aIndex));
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsStringArray::IndexOf(const nsString& aPossibleString) const
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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
|
|
|
|
nsStringArray::IndexOfIgnoreCase(const nsString& aPossibleString) const
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
if (mImpl) {
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end) {
|
|
|
|
nsString* string = NS_STATIC_CAST(nsString*, *ap);
|
|
|
|
if (string->EqualsIgnoreCase(aPossibleString)) {
|
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::InsertStringAt(const nsString& aString, PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
nsString* string = new nsString(aString);
|
|
|
|
if (nsVoidArray::InsertElementAt(string, aIndex)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
delete string;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::ReplaceStringAt(const nsString& aString, PRInt32 aIndex)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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
|
|
|
|
nsStringArray::RemoveString(const nsString& aString)
|
|
|
|
{
|
|
|
|
PRInt32 index = IndexOf(aString);
|
|
|
|
if (-1 < index) {
|
|
|
|
return RemoveStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::RemoveStringIgnoreCase(const nsString& aString)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
while (0 <= --index) {
|
2005-11-02 19:04:22 +03: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;
|
|
|
|
|
2005-11-02 19:04:22 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:22 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
// Copy the pointers
|
|
|
|
nsVoidArray::operator=(other);
|
|
|
|
|
|
|
|
// Now copy the strings
|
|
|
|
for (PRInt32 i = Count() - 1; i >= 0; --i) {
|
|
|
|
nsCString* oldString = NS_STATIC_CAST(nsCString*, ElementAt(i));
|
|
|
|
mImpl->mArray[i] = new nsCString(*oldString);
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03: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);
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
1999-10-30 00:47:17 +04:00
|
|
|
while (0 <= --index) {
|
2005-11-02 19:04:22 +03: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
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return NS_STATIC_CAST(nsCString*, nsVoidArray::ElementAt(aIndex));
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsCStringArray::IndexOf(const nsCString& aPossibleString) const
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03: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)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
1999-10-30 00:47:17 +04:00
|
|
|
while (0 <= --index) {
|
2005-11-02 19:04:22 +03: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;
|
|
|
|
|
2005-11-02 19:04:22 +03: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;
|
|
|
|
|
2005-11-02 19:04:22 +03: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;
|
|
|
|
}
|