ICU-1749 initial port from Java
X-SVN-Rev: 8520
This commit is contained in:
parent
7fb7bd0a21
commit
4212379376
197
icu4c/source/i18n/unicode/usetiter.h
Normal file
197
icu4c/source/i18n/unicode/usetiter.h
Normal file
@ -0,0 +1,197 @@
|
||||
/*
|
||||
**********************************************************************
|
||||
* Copyright (c) 2002, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* $Source: /xsrl/Nsvn/icu/icu/source/i18n/unicode/Attic/usetiter.h,v $
|
||||
* $Date: 2002/04/25 23:36:02 $
|
||||
* $Revision: 1.1 $
|
||||
**********************************************************************
|
||||
*/
|
||||
#ifndef USETITER_H
|
||||
#define USETITER_H
|
||||
|
||||
#include "unicode/unistr.h"
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
class UnicodeSet;
|
||||
class UnicodeString;
|
||||
|
||||
/**
|
||||
* UnicodeSetIterator iterates over the contents of a UnicodeSet. It
|
||||
* iterates over either code points or code point ranges. After all
|
||||
* code points or ranges have been returned, it returns the
|
||||
* multicharacter strings of the UnicodSet, if any.
|
||||
*
|
||||
* <p>To iterate over code points, use a loop like this:
|
||||
* <pre>
|
||||
* UnicodeSetIterator it(set);
|
||||
* while (set.next()) {
|
||||
* if (set.codepoint != UnicodeSetIterator::IS_STRING) {
|
||||
* processCodepoint(set.codepoint);
|
||||
* } else {
|
||||
* processString(set.string);
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>To iterate over code point ranges, use a loop like this:
|
||||
* <pre>
|
||||
* UnicodeSetIterator it(set);
|
||||
* while (set.nextRange()) {
|
||||
* if (set.codepoint != UnicodeSetIterator::IS_STRING) {
|
||||
* processCodepointRange(set.codepoint, set.codepointEnd);
|
||||
* } else {
|
||||
* processString(set.string);
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
* @author M. Davis
|
||||
* @draft
|
||||
*/
|
||||
class U_I18N_API UnicodeSetIterator {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Value of <tt>codepoint</tt> if the iterator points to a string.
|
||||
* If <tt>codepoint == IS_STRING</tt>, then examine
|
||||
* <tt>string</tt> for the current iteration result.
|
||||
*/
|
||||
static enum { IS_STRING = -1 };
|
||||
|
||||
/**
|
||||
* Current code point, or the special value <tt>IS_STRING</tt>, if
|
||||
* the iterator points to a string.
|
||||
*/
|
||||
UChar32 codepoint;
|
||||
|
||||
/**
|
||||
* When iterating over ranges using <tt>nextRange()</tt>,
|
||||
* <tt>codepointEnd</tt> contains the inclusive end of the
|
||||
* iteration range, if <tt>codepoint != IS_STRING</tt>. If
|
||||
* iterating over code points using <tt>next()</tt>, or if
|
||||
* <tt>codepoint == IS_STRING</tt>, then the value of
|
||||
* <tt>codepointEnd</tt> is undefined.
|
||||
*/
|
||||
UChar32 codepointEnd;
|
||||
|
||||
/**
|
||||
* If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
|
||||
* to the current string. If <tt>codepoint != IS_STRING</tt>, the
|
||||
* value of <tt>string</tt> is undefined.
|
||||
*/
|
||||
const UnicodeString* string;
|
||||
|
||||
/**
|
||||
* Create an iterator over the given set. The iterator is valid
|
||||
* only so long as <tt>set</tt> is valid.
|
||||
* @param set set to iterate over
|
||||
*/
|
||||
UnicodeSetIterator(const UnicodeSet& set);
|
||||
|
||||
/**
|
||||
* Create an iterator over nothing. <tt>next()</tt> and
|
||||
* <tt>nextRange()</tt> return false. This is a convenience
|
||||
* constructor allowing the target to be set later.
|
||||
*/
|
||||
UnicodeSetIterator();
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
~UnicodeSetIterator();
|
||||
|
||||
/**
|
||||
* Returns the next element in the set, either a single code point
|
||||
* or a string. If there are no more elements in the set, return
|
||||
* false. If <tt>codepoint == IS_STRING</tt>, the value is a
|
||||
* string in the <tt>string</tt> field. Otherwise the value is a
|
||||
* single code point in the <tt>codepoint</tt> field.
|
||||
*
|
||||
* <p>The order of iteration is all code points in sorted order,
|
||||
* followed by all strings sorted order. <tt>codepointEnd</tt> is
|
||||
* undefined after calling this method. <tt>string</tt> is
|
||||
* undefined unless <tt>codepoint == IS_STRING</tt>. Do not mix
|
||||
* calls to <tt>next()</tt> and <tt>nextRange()</tt> without
|
||||
* calling <tt>reset()</tt> between them. The results of doing so
|
||||
* are undefined.
|
||||
*
|
||||
* @return true if there was another element in the set and this
|
||||
* object contains the element.
|
||||
*/
|
||||
UBool next();
|
||||
|
||||
/**
|
||||
* Returns the next element in the set, either a code point range
|
||||
* or a string. If there are no more elements in the set, return
|
||||
* false. If <tt>codepoint == IS_STRING</tt>, the value is a
|
||||
* string in the <tt>string</tt> field. Otherwise the value is a
|
||||
* range of one or more code points from <tt>codepoint</tt> to
|
||||
* <tt>codepointeEnd</tt> inclusive.
|
||||
*
|
||||
* <p>The order of iteration is all code points ranges in sorted
|
||||
* order, followed by all strings sorted order. Ranges are
|
||||
* disjoint and non-contiguous. <tt>string</tt> is undefined
|
||||
* unless <tt>codepoint == IS_STRING</tt>. Do not mix calls to
|
||||
* <tt>next()</tt> and <tt>nextRange()</tt> without calling
|
||||
* <tt>reset()</tt> between them. The results of doing so are
|
||||
* undefined.
|
||||
*
|
||||
* @return true if there was another element in the set and this
|
||||
* object contains the element.
|
||||
*/
|
||||
UBool nextRange();
|
||||
|
||||
/**
|
||||
* Sets this iterator to visit the elements of the given set and
|
||||
* resets it to the start of that set. The iterator is valid only
|
||||
* so long as <tt>set</tt> is valid.
|
||||
* @param set the set to iterate over.
|
||||
*/
|
||||
void reset(const UnicodeSet& set);
|
||||
|
||||
/**
|
||||
* Resets this iterator to the start of the set.
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* INTERNAL: Causes the interation to only visit part of long ranges
|
||||
* @internal used only for testing
|
||||
*/
|
||||
void setAbbreviated(UBool abbr);
|
||||
|
||||
/**
|
||||
* INTERNAL: Causes the interation to only visit part of long ranges
|
||||
* @internal used only for testing
|
||||
*/
|
||||
UBool getAbbreviated();
|
||||
|
||||
// ======================= PRIVATES ===========================
|
||||
|
||||
private:
|
||||
|
||||
// endElement and nextElements are really UChar32's, but we keep
|
||||
// them as signed int32_t's so we can do comparisons with
|
||||
// endElement set to -1. Leave them as int32_t's.
|
||||
const UnicodeSet* set;
|
||||
int32_t endRange;
|
||||
int32_t range;
|
||||
int32_t endElement;
|
||||
int32_t nextElement;
|
||||
UBool abbreviated;
|
||||
int32_t nextString;
|
||||
int32_t stringCount;
|
||||
|
||||
void loadRange(int32_t range);
|
||||
|
||||
UnicodeSetIterator(const UnicodeSetIterator&); // disallow
|
||||
|
||||
UnicodeSetIterator& operator=(const UnicodeSetIterator&); // disallow
|
||||
};
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
#endif
|
155
icu4c/source/i18n/usetiter.cpp
Normal file
155
icu4c/source/i18n/usetiter.cpp
Normal file
@ -0,0 +1,155 @@
|
||||
/*
|
||||
**********************************************************************
|
||||
* Copyright (c) 2002, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* $Source: /xsrl/Nsvn/icu/icu/source/i18n/Attic/usetiter.cpp,v $
|
||||
* $Date: 2002/04/25 23:35:30 $
|
||||
* $Revision: 1.1 $
|
||||
**********************************************************************
|
||||
*/
|
||||
#include "unicode/usetiter.h"
|
||||
#include "unicode/uniset.h"
|
||||
#include "unicode/unistr.h"
|
||||
#include "uvector.h"
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
/**
|
||||
* Create an iterator
|
||||
* @param set set to iterate over
|
||||
*/
|
||||
UnicodeSetIterator::UnicodeSetIterator(const UnicodeSet& set) :
|
||||
abbreviated(FALSE)
|
||||
{
|
||||
reset(set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an iterator. Convenience for when the contents are to be set later.
|
||||
*/
|
||||
UnicodeSetIterator::UnicodeSetIterator() :
|
||||
abbreviated(FALSE)
|
||||
{
|
||||
this->set = NULL;
|
||||
reset();
|
||||
}
|
||||
|
||||
UnicodeSetIterator::~UnicodeSetIterator() {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++;
|
||||
return TRUE;
|
||||
}
|
||||
if (range < endRange) {
|
||||
loadRange(++range);
|
||||
codepoint = codepointEnd = nextElement++;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (nextString >= stringCount) return FALSE;
|
||||
codepoint = IS_STRING; // signal that value is actually a string
|
||||
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() {
|
||||
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;
|
||||
codepoint = IS_STRING; // signal that value is actually a string
|
||||
string = (const UnicodeString*) set->strings->elementAt(nextString++);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
*@param set the set to iterate over. This allows reuse of the iterator.
|
||||
*/
|
||||
void UnicodeSetIterator::reset(const UnicodeSet& set) {
|
||||
this->set = &set;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Causes the interation to only to part of long ranges
|
||||
* @internal -- used only for testing
|
||||
*/
|
||||
void UnicodeSetIterator::setAbbreviated(UBool abbr) {
|
||||
abbreviated = abbr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Causes the interation to only to part of long ranges
|
||||
* @internal -- used only for testing
|
||||
*/
|
||||
UBool UnicodeSetIterator::getAbbreviated() {
|
||||
return abbreviated;
|
||||
}
|
||||
|
||||
void UnicodeSetIterator::loadRange(int32_t range) {
|
||||
nextElement = set->getRangeStart(range);
|
||||
endElement = set->getRangeEnd(range);
|
||||
if (abbreviated && (endElement > nextElement + 50)) {
|
||||
endElement = nextElement + 50;
|
||||
}
|
||||
}
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
//eof
|
Loading…
Reference in New Issue
Block a user