2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-03-11 00:09:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsStringEnumerator.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsSupportsPrimitives.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-19 23:31:21 +04:00
|
|
|
#include "nsTArray.h"
|
2003-03-11 00:09:31 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// nsStringEnumerator
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsStringEnumerator final
|
2014-07-09 19:15:21 +04:00
|
|
|
: public nsIStringEnumerator
|
|
|
|
, public nsIUTF8StringEnumerator
|
|
|
|
, public nsISimpleEnumerator
|
2008-02-21 23:39:20 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-07-09 19:15:21 +04:00
|
|
|
nsStringEnumerator(const nsTArray<nsString>* aArray, bool aOwnsArray)
|
|
|
|
: mArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwnsArray(aOwnsArray)
|
|
|
|
, mIsUnicode(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsStringEnumerator(const nsTArray<nsCString>* aArray, bool aOwnsArray)
|
|
|
|
: mCArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwnsArray(aOwnsArray)
|
|
|
|
, mIsUnicode(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsStringEnumerator(const nsTArray<nsString>* aArray, nsISupports* aOwner)
|
|
|
|
: mArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwner(aOwner)
|
|
|
|
, mOwnsArray(false)
|
|
|
|
, mIsUnicode(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsStringEnumerator(const nsTArray<nsCString>* aArray, nsISupports* aOwner)
|
|
|
|
: mCArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwner(aOwner)
|
|
|
|
, mOwnsArray(false)
|
|
|
|
, mIsUnicode(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIUTF8STRINGENUMERATOR
|
|
|
|
|
|
|
|
// have to declare nsIStringEnumerator manually, because of
|
|
|
|
// overlapping method names
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD GetNext(nsAString& aResult) override;
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2008-02-21 23:39:20 +03:00
|
|
|
|
|
|
|
private:
|
2014-07-09 19:15:21 +04:00
|
|
|
~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) {
|
|
|
|
delete const_cast<nsTArray<nsString>*>(mArray);
|
|
|
|
} else {
|
|
|
|
delete const_cast<nsTArray<nsCString>*>(mCArray);
|
|
|
|
}
|
2008-02-21 23:39:20 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const nsTArray<nsString>* mArray;
|
|
|
|
const nsTArray<nsCString>* mCArray;
|
|
|
|
};
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
inline uint32_t Count()
|
|
|
|
{
|
|
|
|
return mIsUnicode ? mArray->Length() : mCArray->Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mIndex;
|
|
|
|
|
|
|
|
// the owner allows us to hold a strong reference to the object
|
|
|
|
// that owns the array. Having a non-null value in mOwner implies
|
|
|
|
// that mOwnsArray is false, because we rely on the real owner
|
|
|
|
// to release the array
|
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
bool mOwnsArray;
|
|
|
|
bool mIsUnicode;
|
2008-02-21 23:39:20 +03:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStringEnumerator,
|
|
|
|
nsIStringEnumerator,
|
|
|
|
nsIUTF8StringEnumerator,
|
|
|
|
nsISimpleEnumerator)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStringEnumerator::HasMore(bool* aResult)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
*aResult = mIndex < Count();
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStringEnumerator::HasMoreElements(bool* aResult)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
return HasMore(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (mIsUnicode) {
|
2016-05-03 23:25:41 +03:00
|
|
|
nsSupportsString* stringImpl = new nsSupportsString();
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!stringImpl) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
stringImpl->SetData(mArray->ElementAt(mIndex++));
|
|
|
|
*aResult = stringImpl;
|
|
|
|
} else {
|
2016-05-03 23:25:41 +03:00
|
|
|
nsSupportsCString* cstringImpl = new nsSupportsCString();
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!cstringImpl) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
|
|
|
|
cstringImpl->SetData(mCArray->ElementAt(mIndex++));
|
|
|
|
*aResult = cstringImpl;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsAString& aResult)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(mIndex >= Count())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsUnicode) {
|
|
|
|
aResult = mArray->ElementAt(mIndex++);
|
|
|
|
} else {
|
|
|
|
CopyUTF8toUTF16(mCArray->ElementAt(mIndex++), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsACString& aResult)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(mIndex >= Count())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsUnicode) {
|
|
|
|
CopyUTF16toUTF8(mArray->ElementAt(mIndex++), aResult);
|
|
|
|
} else {
|
|
|
|
aResult = mCArray->ElementAt(mIndex++);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static inline nsresult
|
2012-07-07 00:14:07 +04:00
|
|
|
StringEnumeratorTail(T** aResult)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!*aResult) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// constructors
|
|
|
|
//
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 23:14:14 +03:00
|
|
|
const nsTArray<nsString>* aArray, nsISupports* aOwner)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, aOwner);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2014-07-09 19:15:21 +04:00
|
|
|
const nsTArray<nsCString>* aArray,
|
|
|
|
nsISupports* aOwner)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, aOwner);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, true);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-22 07:15:34 +03:00
|
|
|
nsTArray<nsCString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, true);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// const ones internally just forward to the non-const equivalents
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 23:14:14 +03:00
|
|
|
const nsTArray<nsString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, false);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-22 07:15:34 +03:00
|
|
|
const nsTArray<nsCString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, false);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|