2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsStringEnumerator.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsSupportsPrimitives.h"
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-19 12:31:21 -07:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// nsStringEnumerator
|
|
|
|
//
|
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsStringEnumerator MOZ_FINAL : public nsIStringEnumerator,
|
|
|
|
public nsIUTF8StringEnumerator,
|
|
|
|
public nsISimpleEnumerator
|
2008-02-21 12:39:20 -08:00
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStringEnumerator(const nsTArray<nsString>* aArray, bool aOwnsArray) :
|
|
|
|
mArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(true)
|
2008-02-21 12:39:20 -08:00
|
|
|
{}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStringEnumerator(const nsTArray<nsCString>* aArray, bool aOwnsArray) :
|
|
|
|
mCArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(false)
|
2008-02-21 12:39:20 -08:00
|
|
|
{}
|
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
nsStringEnumerator(const nsTArray<nsString>* aArray, nsISupports* aOwner) :
|
2011-09-28 23:19:26 -07:00
|
|
|
mArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(false), mIsUnicode(true)
|
2008-02-21 12:39:20 -08:00
|
|
|
{}
|
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
nsStringEnumerator(const nsTArray<nsCString>* aArray, nsISupports* aOwner) :
|
2011-09-28 23:19:26 -07:00
|
|
|
mCArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(false), mIsUnicode(false)
|
2008-02-21 12:39:20 -08:00
|
|
|
{}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIUTF8STRINGENUMERATOR
|
|
|
|
|
|
|
|
// have to declare nsIStringEnumerator manually, because of
|
|
|
|
// overlapping method names
|
|
|
|
NS_IMETHOD GetNext(nsAString& aResult);
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsStringEnumerator() {
|
|
|
|
if (mOwnsArray) {
|
|
|
|
// const-casting is safe here, because the NS_New*
|
|
|
|
// constructors make sure mOwnsArray is consistent with
|
|
|
|
// the constness of the objects
|
|
|
|
if (mIsUnicode)
|
2009-01-18 12:14:14 -08:00
|
|
|
delete const_cast<nsTArray<nsString>*>(mArray);
|
2008-02-21 12:39:20 -08:00
|
|
|
else
|
2009-01-21 20:15:34 -08:00
|
|
|
delete const_cast<nsTArray<nsCString>*>(mCArray);
|
2008-02-21 12:39:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
2009-01-18 12:14:14 -08:00
|
|
|
const nsTArray<nsString>* mArray;
|
2009-01-21 20:15:34 -08:00
|
|
|
const nsTArray<nsCString>* mCArray;
|
2008-02-21 12:39:20 -08:00
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
inline uint32_t Count() {
|
2009-01-21 20:15:34 -08:00
|
|
|
return mIsUnicode ? mArray->Length() : mCArray->Length();
|
2008-02-21 12:39:20 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mIndex;
|
2008-02-21 12:39:20 -08:00
|
|
|
|
|
|
|
// the owner allows us to hold a strong reference to the object
|
|
|
|
// that owns the array. Having a non-null value in mOwner implies
|
2011-09-28 23:19:26 -07:00
|
|
|
// that mOwnsArray is false, because we rely on the real owner
|
2008-02-21 12:39:20 -08:00
|
|
|
// to release the array
|
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mOwnsArray;
|
|
|
|
bool mIsUnicode;
|
2008-02-21 12:39:20 -08:00
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsStringEnumerator,
|
|
|
|
nsIStringEnumerator,
|
|
|
|
nsIUTF8StringEnumerator,
|
|
|
|
nsISimpleEnumerator)
|
2008-02-21 12:39:20 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStringEnumerator::HasMore(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aResult = mIndex < Count();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStringEnumerator::HasMoreElements(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return HasMore(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
if (mIsUnicode) {
|
|
|
|
nsSupportsStringImpl* stringImpl = new nsSupportsStringImpl();
|
|
|
|
if (!stringImpl) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
stringImpl->SetData(mArray->ElementAt(mIndex++));
|
2007-03-22 10:30:00 -07:00
|
|
|
*aResult = stringImpl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsSupportsCStringImpl* cstringImpl = new nsSupportsCStringImpl();
|
|
|
|
if (!cstringImpl) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
cstringImpl->SetData(mCArray->ElementAt(mIndex++));
|
2007-03-22 10:30:00 -07:00
|
|
|
*aResult = cstringImpl;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsAString& aResult)
|
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(mIndex >= Count()))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mIsUnicode)
|
2009-01-18 12:14:14 -08:00
|
|
|
aResult = mArray->ElementAt(mIndex++);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2009-01-21 20:15:34 -08:00
|
|
|
CopyUTF8toUTF16(mCArray->ElementAt(mIndex++), aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsACString& aResult)
|
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(mIndex >= Count()))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mIsUnicode)
|
2009-01-18 12:14:14 -08:00
|
|
|
CopyUTF16toUTF8(mArray->ElementAt(mIndex++), aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2009-01-21 20:15:34 -08:00
|
|
|
aResult = mCArray->ElementAt(mIndex++);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static inline nsresult
|
2012-07-06 13:14:07 -07:00
|
|
|
StringEnumeratorTail(T** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// constructors
|
|
|
|
//
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 12:14:14 -08:00
|
|
|
const nsTArray<nsString>* aArray, nsISupports* aOwner)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, aOwner);
|
|
|
|
return StringEnumeratorTail(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-21 20:15:34 -08:00
|
|
|
const nsTArray<nsCString>* aArray, nsISupports* aOwner)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, aOwner);
|
|
|
|
return StringEnumeratorTail(aResult);
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray<nsString>* aArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
*aResult = new nsStringEnumerator(aArray, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
return StringEnumeratorTail(aResult);
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString>* aArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
*aResult = new nsStringEnumerator(aArray, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
return StringEnumeratorTail(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// const ones internally just forward to the non-const equivalents
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 12:14:14 -08:00
|
|
|
const nsTArray<nsString>* aArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
*aResult = new nsStringEnumerator(aArray, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return StringEnumeratorTail(aResult);
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-21 20:15:34 -08:00
|
|
|
const nsTArray<nsCString>* aArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
*aResult = new nsStringEnumerator(aArray, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return StringEnumeratorTail(aResult);
|
|
|
|
}
|
|
|
|
|