2002-04-25 23:36:02 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (c) 2002-2006, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2002-04-25 23:36:02 +00:00
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#include "unicode/usetiter.h"
|
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "uvector.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeSetIterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-04-25 23:36:02 +00:00
|
|
|
/**
|
|
|
|
* Create an iterator
|
|
|
|
* @param set set to iterate over
|
|
|
|
*/
|
2002-06-01 06:36:02 +00:00
|
|
|
UnicodeSetIterator::UnicodeSetIterator(const UnicodeSet& uSet) {
|
2006-01-07 01:35:28 +00:00
|
|
|
cpString = NULL;
|
2002-06-01 06:36:02 +00:00
|
|
|
reset(uSet);
|
2002-04-25 23:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an iterator. Convenience for when the contents are to be set later.
|
|
|
|
*/
|
2002-04-30 22:12:01 +00:00
|
|
|
UnicodeSetIterator::UnicodeSetIterator() {
|
2002-04-25 23:36:02 +00:00
|
|
|
this->set = NULL;
|
2006-01-07 01:35:28 +00:00
|
|
|
cpString = NULL;
|
2002-04-25 23:36:02 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeSetIterator::~UnicodeSetIterator() {
|
2006-01-07 01:35:28 +00:00
|
|
|
delete cpString;
|
2002-04-25 23:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next element in the set.
|
|
|
|
* @return true if there was another element in the set.
|
|
|
|
* if so, if codepoint == IS_STRING, the value is a string in the string field
|
|
|
|
* else the value is a single code point in the codepoint field.
|
|
|
|
* <br>You are guaranteed that the codepoints are in sorted order, and the strings are in sorted order,
|
|
|
|
* and that all code points are returned before any strings are returned.
|
|
|
|
* <br>Note also that the codepointEnd is undefined after calling this method.
|
|
|
|
*/
|
|
|
|
UBool UnicodeSetIterator::next() {
|
|
|
|
if (nextElement <= endElement) {
|
|
|
|
codepoint = codepointEnd = nextElement++;
|
2006-01-07 01:35:28 +00:00
|
|
|
string = NULL;
|
2002-04-25 23:36:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (range < endRange) {
|
|
|
|
loadRange(++range);
|
|
|
|
codepoint = codepointEnd = nextElement++;
|
2006-01-07 01:35:28 +00:00
|
|
|
string = NULL;
|
2002-04-25 23:36:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextString >= stringCount) return FALSE;
|
2002-08-22 21:01:43 +00:00
|
|
|
codepoint = (UChar32)IS_STRING; // signal that value is actually a string
|
2002-04-25 23:36:02 +00:00
|
|
|
string = (const UnicodeString*) set->strings->elementAt(nextString++);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if there was another element in the set.
|
|
|
|
* if so, if codepoint == IS_STRING, the value is a string in the string field
|
|
|
|
* else the value is a range of codepoints in the <codepoint, codepointEnd> fields.
|
|
|
|
* <br>Note that the codepoints are in sorted order, and the strings are in sorted order,
|
|
|
|
* and that all code points are returned before any strings are returned.
|
|
|
|
* <br>You are guaranteed that the ranges are in sorted order, and the strings are in sorted order,
|
|
|
|
* and that all ranges are returned before any strings are returned.
|
|
|
|
* <br>You are also guaranteed that ranges are disjoint and non-contiguous.
|
|
|
|
* <br>Note also that the codepointEnd is undefined after calling this method.
|
|
|
|
*/
|
|
|
|
UBool UnicodeSetIterator::nextRange() {
|
2006-01-07 01:35:28 +00:00
|
|
|
string = NULL;
|
2002-04-25 23:36:02 +00:00
|
|
|
if (nextElement <= endElement) {
|
|
|
|
codepointEnd = endElement;
|
|
|
|
codepoint = nextElement;
|
|
|
|
nextElement = endElement+1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (range < endRange) {
|
|
|
|
loadRange(++range);
|
|
|
|
codepointEnd = endElement;
|
|
|
|
codepoint = nextElement;
|
|
|
|
nextElement = endElement+1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextString >= stringCount) return FALSE;
|
2002-08-22 21:01:43 +00:00
|
|
|
codepoint = (UChar32)IS_STRING; // signal that value is actually a string
|
2002-04-25 23:36:02 +00:00
|
|
|
string = (const UnicodeString*) set->strings->elementAt(nextString++);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*@param set the set to iterate over. This allows reuse of the iterator.
|
|
|
|
*/
|
2002-06-01 06:36:02 +00:00
|
|
|
void UnicodeSetIterator::reset(const UnicodeSet& uSet) {
|
|
|
|
this->set = &uSet;
|
2002-04-25 23:36:02 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets to the start, to allow the iteration to start over again.
|
|
|
|
*/
|
|
|
|
void UnicodeSetIterator::reset() {
|
|
|
|
if (set == NULL) {
|
|
|
|
// Set up indices to empty iteration
|
|
|
|
endRange = -1;
|
|
|
|
stringCount = 0;
|
|
|
|
} else {
|
|
|
|
endRange = set->getRangeCount() - 1;
|
|
|
|
stringCount = set->strings->size();
|
|
|
|
}
|
|
|
|
range = 0;
|
|
|
|
endElement = -1;
|
|
|
|
nextElement = 0;
|
|
|
|
if (endRange >= 0) {
|
|
|
|
loadRange(range);
|
|
|
|
}
|
|
|
|
nextString = 0;
|
2006-01-07 01:35:28 +00:00
|
|
|
string = NULL;
|
2002-04-25 23:36:02 +00:00
|
|
|
}
|
|
|
|
|
2002-06-01 06:36:02 +00:00
|
|
|
void UnicodeSetIterator::loadRange(int32_t iRange) {
|
|
|
|
nextElement = set->getRangeStart(iRange);
|
|
|
|
endElement = set->getRangeEnd(iRange);
|
2002-04-25 23:36:02 +00:00
|
|
|
}
|
|
|
|
|
2006-01-07 01:35:28 +00:00
|
|
|
|
2006-01-09 19:20:59 +00:00
|
|
|
const UnicodeString& UnicodeSetIterator::getString() {
|
2006-01-07 01:35:28 +00:00
|
|
|
if (string==NULL && codepoint!=(UChar32)IS_STRING) {
|
|
|
|
if (cpString == NULL) {
|
|
|
|
cpString = new UnicodeString();
|
|
|
|
}
|
|
|
|
if (cpString != NULL) {
|
|
|
|
cpString->setTo((UChar32)codepoint);
|
|
|
|
}
|
|
|
|
string = cpString;
|
|
|
|
}
|
|
|
|
return *string;
|
|
|
|
}
|
|
|
|
|
2002-04-25 23:36:02 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
//eof
|