ICU-1852 cleaning up samples, search sample is old

X-SVN-Rev: 8466
This commit is contained in:
Steven R. Loomis 2002-04-15 21:49:42 +00:00
parent aafccbcc5d
commit c00ca482eb
2 changed files with 0 additions and 668 deletions

View File

@ -1,281 +0,0 @@
/*
**********************************************************************
* Copyright (C) 1999-2000 IBM and others. All rights reserved.
**********************************************************************
* Date Name Description
* 03/22/2000 helena Creation.
**********************************************************************
*/
#include "unicode/brkiter.h"
#include "unicode/schriter.h"
#include "srchiter.h"
int32_t const SearchIterator::DONE = -1;
int32_t const SearchIterator::BEFORE = -2;
SearchIterator::SearchIterator(void) :
index(0),
length(0),
target(0),
backward(FALSE), /* going forward */
breaker(NULL),
overlap(TRUE)
{
UErrorCode status = U_ZERO_ERROR;
this->breaker = BreakIterator::createCharacterInstance(Locale::getDefault(), status);
if (U_FAILURE(status)) return;
}
SearchIterator::SearchIterator(CharacterIterator* target,
BreakIterator* breaker) :
index(0),
length(0),
target(0),
backward(FALSE), /* going forward */
breaker(NULL),
overlap(TRUE)
{
this->target = target;
this->breaker = breaker;
this->breaker->adoptText(this->target);
index = this->target->startIndex();
length = 0;
}
SearchIterator::SearchIterator(const SearchIterator& other) :
length(other.length),
target(0),
backward(other.backward), /* going forward */
breaker(NULL),
overlap(other.overlap)
{
index = other.target->startIndex();
this->target = other.target->clone();
this->breaker = ((BreakIterator&)other.breaker).clone();
this->breaker->adoptText(this->target);
}
SearchIterator::~SearchIterator()
{
// deletion of breaker will delete target
if (breaker != NULL) {
delete breaker;
breaker = 0;
}
}
UBool SearchIterator::operator == (const SearchIterator& that) const
{
if (this == &that) return TRUE;
if (*that.breaker != *breaker) return FALSE;
else if (*that.target != *target) return FALSE;
else if (that.backward != backward) return FALSE;
else if (that.index != index) return FALSE;
else if (that.length != length) return FALSE;
else if (that.overlap != overlap) return FALSE;
else return TRUE;
}
int32_t SearchIterator::first(void)
{
setIndex(SearchIterator::BEFORE);
return next();
}
int32_t SearchIterator::following(int32_t pos)
{
setIndex(pos);
return next();
}
int32_t SearchIterator::last(void)
{
setIndex(SearchIterator::DONE);
return previous();
}
int32_t SearchIterator::preceding(int32_t pos)
{
setIndex(pos);
return previous();
}
int32_t SearchIterator::next(void)
{
if (index == SearchIterator::BEFORE){
// Starting at the beginning of the text
index = target->startIndex();
} else if (index == SearchIterator::DONE) {
return SearchIterator::DONE;
} else if (length > 0) {
// Finding the next match after a previous one
index += overlap ? 1 : length;
}
index -= 1;
backward = FALSE;
do {
UErrorCode status = U_ZERO_ERROR;
length = 0;
index = handleNext(index + 1, status);
if (U_FAILURE(status))
{
return SearchIterator::DONE;
}
} while (index != SearchIterator::DONE && !isBreakUnit(index, index+length));
return index;
}
int32_t SearchIterator::previous(void)
{
if (index == SearchIterator::DONE) {
index = target->endIndex();
} else if (index == SearchIterator::BEFORE) {
return SearchIterator::DONE;
} else if (length > 0) {
// Finding the previous match before a following one
index = overlap ? index + length - 1 : index;
}
index += 1;
backward = TRUE;
do {
UErrorCode status = U_ZERO_ERROR;
length = 0;
index = handlePrev(index - 1, status);
if (U_FAILURE(status))
{
return SearchIterator::DONE;
}
} while (index != SearchIterator::DONE && !isBreakUnit(index, index+length));
if (index == SearchIterator::DONE) {
index = SearchIterator::BEFORE;
}
return getIndex();
}
int32_t SearchIterator::getIndex() const
{
return index == SearchIterator::BEFORE ? SearchIterator::DONE : index;
}
void SearchIterator::setOverlapping(UBool allowOverlap)
{
overlap = allowOverlap;
}
UBool SearchIterator::isOverlapping(void) const
{
return overlap;
}
int32_t SearchIterator::getMatchLength(void) const
{
return length;
}
void SearchIterator::reset(void)
{
length = 0;
if (backward == FALSE) {
index = 0;
target->setToStart();
breaker->first();
} else {
index = SearchIterator::DONE;
target->setToEnd();
breaker->last();
}
overlap = TRUE;
}
void SearchIterator::setBreakIterator(const BreakIterator* iterator)
{
CharacterIterator *buffer = target->clone();
delete breaker;
breaker = iterator->clone();
breaker->adoptText(buffer);
}
const BreakIterator& SearchIterator::getBreakIterator(void) const
{
return *breaker;
}
void SearchIterator::setTarget(const UnicodeString& newText)
{
if (target != NULL && target->getDynamicClassID()
== StringCharacterIterator::getStaticClassID()) {
((StringCharacterIterator*)target)->setText(newText);
}
else {
delete target;
target = new StringCharacterIterator(newText);
target->first();
breaker->adoptText(target);
}
}
void SearchIterator::adoptTarget(CharacterIterator* iterator) {
target = iterator;
breaker->adoptText(target);
setIndex(SearchIterator::BEFORE);
}
const CharacterIterator& SearchIterator::getTarget(void) const
{
SearchIterator* nonConstThis = (SearchIterator*)this;
// The iterator is initialized pointing to no text at all, so if this
// function is called while we're in that state, we have to fudge an
// an iterator to return.
if (nonConstThis->target == NULL)
nonConstThis->target = new StringCharacterIterator("");
return *nonConstThis->target;
}
void SearchIterator::getMatchedText(UnicodeString& result)
{
result.remove();
if (length > 0) {
int i = 0;
for (UChar c = target->setIndex(index); i < length; c = target->next(), i++)
{
result += c;
}
}
}
void SearchIterator::setMatchLength(int32_t length)
{
this->length = length;
}
void SearchIterator::setIndex(int32_t pos) {
index = pos;
length = 0;
}
UBool SearchIterator::isBreakUnit(int32_t start,
int32_t end)
{
if (breaker == NULL) {
return TRUE;
}
UBool startBound = breaker->isBoundary(start);
UBool endBound = (end == target->endIndex()) || breaker->isBoundary(end);
return startBound && endBound;
}

View File

@ -1,387 +0,0 @@
/*
**********************************************************************
* Copyright (C) 1999-2000 IBM and others. All rights reserved.
**********************************************************************
* Date Name Description
* 03/22/2000 helena Creation.
**********************************************************************
*/
#ifndef SRCHITER_H
#define SRCHITER_H
#include "unicode/utypes.h"
#include "unicode/unistr.h"
#include "unicode/chariter.h"
#include "unicode/brkiter.h"
/**
* <code>SearchIterator</code> is an abstract base class that provides methods
* to search for a pattern within a text string. Instances of
* <code>SearchIterator</code> maintain a current position and scan over
* the target text, returning the indices the pattern is matched
* and the length of each match.
* <p>
* <code>SearchIterator</code> is an abstract base class that defines a
* protocol for text searching. Subclasses provide concrete implementations of
* various search algorithms. For example, {@link StringSearch}
* implements language-sensitive pattern matching based on the comparison rules
* defined in a {@link RuleBasedCollator} object.
* <p>
* Internally, <code>SearchIterator</code> scans text using a
* {@link CharacterIterator}, and is thus able to scan text held
* by any object implementing that protocol. A <code>StringCharacterIterator</code>
* is used to scan <code>String</code> objects passed to <code>setText</code>.
* <p>
* <code>SearchIterator</code> provides an API that is similar to that of
* other text iteration classes such as <code>BreakIterator</code>. Using this
* class, it is easy to scan through text looking for all occurances of a
* given pattern. The following example uses a <code>StringSearch</code> object to
* find all instances of "fox" in the target string. Any other subclass of
* <code>SearchIterator</code> can be used in an identical manner.
* <pre><code>
* UnicodeString target("The quick brown fox jumped over the lazy fox");
* UnicodeString pattern("fox");
*
* SearchIterator *iter = new StringSearch(pattern, target);
*
* for (int pos = iter->first(); pos != SearchIterator::DONE; pos = iter->next()) {
* printf("Found match at %d pos, length is %d\n", pos, iter.getMatchLength());
* }
* </code></pre>
*
* @see StringSearch
*/
class SearchIterator {
public:
/**
* DONE is returned by previous() and next() after all valid
* matches have been returned, and by first() and last() if
* there are no matches at all.
*/
static const int32_t DONE;
//=======================================================================
// boilerplate
//=======================================================================
/**
* Destructor
*/
virtual ~SearchIterator();
/** copy constructor */
SearchIterator(const SearchIterator& other);
/**
* Equality operator. Returns TRUE if both BreakIterators are of the
* same class, have the same behavior, and iterate over the same text.
*/
virtual UBool operator==(const SearchIterator& that) const;
/**
* Not-equal operator. If operator== returns TRUE, this returns FALSE,
* and vice versa.
*/
UBool operator!=(const SearchIterator& that) const;
/**
* Returns a newly-constructed RuleBasedBreakIterator with the same
* behavior, and iterating over the same text, as this one.
*/
virtual SearchIterator* clone(void) const = 0;
/**
* Return a polymorphic class ID for this object. Different subclasses
* will return distinct unequal values.
* @stable
*/
virtual UClassID getDynamicClassID(void) const = 0;
/**
* Return the first index at which the target text matches the search
* pattern. The iterator is adjusted so that its current index
* (as returned by {@link #getIndex}) is the match posisition if one was found
* and <code>DONE</code> if one was not.
*
* @return The character index of the first match, or <code>DONE</code> if there
* are no matches.
*/
int32_t first(void);
/**
* Return the first index greater than <tt>pos</tt> at which the target
* text matches the search pattern. The iterator is adjusted so that its current index
* (as returned by {@link #getIndex}) is the match posisition if one was found
* and <code>DONE</code> if one was not.
*
* @return The character index of the first match following <code>pos</code>,
* or <tt>DONE</tt> if there are no matches.
*/
int32_t following(int32_t pos);
/**
* Return the last index in the target text at which it matches
* the search pattern and adjusts the iteration to point to that position.
*
* @return The index of the first match, or <tt>DONE</tt> if there
* are no matches.
*/
int32_t last(void);
/**
* Return the first index less than <code>pos</code> at which the target
* text matches the search pattern. The iterator is adjusted so that its current index
* (as returned by {@link #getIndex}) is the match posisition if one was found
* and <tt>DONE</tt> if one was not.
*
* @return The character index of the first match preceding <code>pos</code>,
* or <code>DONE</code> if there are no matches.
*/
int32_t preceding(int32_t pos);
/**
* Return the index of the next point at which the text matches the
* search pattern, starting from the current position
* <p>
* @return The index of the next match after the current position,
* or <code>DONE</code> if there are no more matches.
*
* @see #first
*/
int32_t next(void);
/**
* Return the index of the previous point at which the text matches
* the search pattern, starting at the current position
*
* @return The index of the previous match before the current position,
* or <code>DONE</code> if there are no more matches.
*/
int32_t previous(void);
/**
* Return the current index in the text being searched.
* If the iteration has gone past the end of the text
* (or past the beginning for a backwards search),
* {@link #DONE} is returned.
*/
int32_t getIndex(void) const;
/**
* Determines whether overlapping matches are returned. If this
* property is <code>true</code>, matches that begin within the
* boundry of the previous match are considered valid and will
* be returned. For example, when searching for "abab" in the
* target text "ababab", both offsets 0 and 2 will be returned
* as valid matches if this property is <code>true</code>.
* <p>
* The default setting of this property is <tt>true</tt>
*/
void setOverlapping(UBool allowOverlap);
/**
* Determines whether overlapping matches are returned.
*
* @see #setOverlapping
*/
UBool isOverlapping(void) const;
/**
* Returns the length of text in the target which matches the search
* pattern. This call returns a valid result only after a successful
* call to {@link #first}, {@link #next}, {@link #previous}, or {@link #last}.
* Just after construction, or after a searching method returns
* <tt>DONE</tt>, this method will return 0.
*
* @return The length of the match in the target text, or 0 if there
* is no match currently.
*/
int32_t getMatchLength(void) const;
/**
* Set the BreakIterator that will be used to restrict the points
* at which matches are detected.
*
* @param breaker A {@link java.text.BreakIterator BreakIterator}
* that will be used to restrict the points
* at which matches are detected. If a match is found, but the match's start
* or end index is not a boundary as determined by
* the <tt>BreakIterator</tt>, the match will be rejected and
* another will be searched for.
*
* If this parameter is <tt>null</tt>, no break
* detection is attempted.
*
* @see #getBreakIterator
*/
/* HSYS : Check, aliasing or owning */
void setBreakIterator(const BreakIterator* iterator);
/**
* Returns the BreakIterator that is used to restrict the points
* at which matches are detected. This will be the same object
* that was passed to the constructor or to <code>setBreakIterator</code>.
* Note that <tt>null</tt> is a legal value; it means that break
* detection should not be attempted.
*
* @see #setBreakIterator
*/
const BreakIterator& getBreakIterator(void) const;
/**
* Set the target text which should be searched and resets the
* iterator's position to point before the start of the target text.
* This method is useful if you want to re-use an iterator to
* search for the same pattern within a different body of text.
*
* @see #getTarget
*/
virtual void setTarget(const UnicodeString& newText);
/**
* Set the target text which should be searched and resets the
* iterator's position to point before the start of the target text.
* This method is useful if you want to re-use an iterator to
* search for the same pattern within a different body of text.
*
* @see #getTarget
*/
virtual void adoptTarget(CharacterIterator* iterator);
/**
* Return the target text which is being searched
*
* @see #setTarget
*/
const CharacterIterator& getTarget(void) const;
/** Reset the iteration.
*/
virtual void reset(void);
/**
* Returns the text that was matched by the most recent call to
* {@link #first}, {@link #next}, {@link #previous}, or {@link #last}.
* If the iterator is not pointing at a valid match (e.g. just after
* construction or after <tt>DONE</tt> has been returned, returns
* an empty string.
*/
void getMatchedText(UnicodeString& result);
//-------------------------------------------------------------------
// Protected interface for subclasses
//-------------------------------------------------------------------
protected:
SearchIterator();
/**
* Constructor for use by subclasses
* <p>
* @param target The target text to be searched. This is for internal
* use by this class. Subclasses need to maintain their
* own reference to or iterator over the target text
* for use by their {@link #handleNext handleNext} and
* {@link #handlePrev handlePrev} methods. The target will
* be adopted and owned by the SearchIterator object.
*
* @param breaker A {@link BreakIterator} that is used to restrict the points
* at which matches are detected. If <tt>handleNext</tt> or
* <tt>handlePrev</tt> finds a match, but the match's start
* or end index is not a boundary as determined by
* the <tt>BreakIterator</tt>, the match is rejected and
* <tt>handleNext</tt> or <tt>handlePrev</tt> is called again.
* If this parameter is <tt>null</tt>, no break
* detection is attempted.
*
*/
SearchIterator(CharacterIterator* target,
BreakIterator* breaker);
/**
* Abstract method which subclasses override to provide the mechanism
* for finding the next match in the target text. This allows different
* subclasses to provide different search algorithms.
* <p>
* If a match is found, the implementation should return the index at
* which the match starts and should call {@link #setMatchLength setMatchLength}
* with the number of characters in the target
* text that make up the match. If no match is found, the method
* should return DONE and should not call <tt>setMatchLength</tt>.
* <p>
* @param startAt The index in the target text at which the search
* should start.
*
* @see #setMatchLength
*/
virtual int32_t handleNext(int32_t startAt, UErrorCode& status) = 0;
/**
* Abstract method which subclasses override to provide the mechanism
* for finding the previous match in the target text. This allows different
* subclasses to provide different search algorithms.
* <p>
* If a match is found, the implementation should return the index at
* which the match starts and should call {@link #setMatchLength setMatchLength}
* with the number of characters in the target
* text that make up the match. If no match is found, the method
* should return DONE and should not call <tt>setMatchLength</tt>.
* <p>
* @param startAt The index in the target text at which the search
* should start.
*
* @see #setMatchLength
*/
virtual int32_t handlePrev(int32_t startAt, UErrorCode& status) = 0;
/**
* Sets the length of the currently matched string in the target text.
* Subclasses' <code>handleNext</code> and <code>handlePrev</code>
* methods should call this when they find a match in the target text.
*/
void setMatchLength(int32_t length);
//-------------------------------------------------------------------
// Privates
//
private:
/**
* Class ID
*/
static char fgClassID;
private:
/**
* Private value indicating that the iterator is pointing
* before the beginning of the target text.
*/
static const int32_t BEFORE;
/**
* Internal method used by preceding and following. Sets the index
* to point to the given position, and clears any state that's
* affected.
*/
void setIndex(int32_t pos);
/**
* Determine whether the target text bounded by <code>start</code> and
* <code>end</code> is one or more whole units of text as determined by
* the current <code>BreakIterator</code>.
*/
UBool isBreakUnit(int32_t start, int32_t end);
//-------------------------------------------------------------------------
// Private data...
//-------------------------------------------------------------------------
int32_t index; // Current position in the target text
int32_t length; // Length of matched text, or 0
UBool overlap; // Return overlapping matches?
CharacterIterator* target; // Target text to be searched
BreakIterator* breaker; // Break iterator to constrain matches
UBool backward;
};
inline UBool SearchIterator::operator!=(const SearchIterator& that) const
{
return !operator==(that);
}
#endif