2000-01-08 02:05:05 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2003-08-27 01:01:42 +00:00
|
|
|
* Copyright (C) 1999-2003 IBM Corp. All rights reserved.
|
2000-01-08 02:05:05 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 12/1/99 rgillam Complete port from Java.
|
2000-01-14 00:13:59 +00:00
|
|
|
* 01/13/2000 helena Added UErrorCode to ctors.
|
2000-01-08 02:05:05 +00:00
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
2000-01-10 21:21:52 +00:00
|
|
|
#include "unicode/dbbi.h"
|
2002-07-25 14:49:10 +00:00
|
|
|
#include "unicode/schriter.h"
|
2000-01-08 02:05:05 +00:00
|
|
|
#include "dbbi_tbl.h"
|
|
|
|
#include "uvector.h"
|
2001-02-22 01:03:05 +00:00
|
|
|
#include "cmemory.h"
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DictionaryBasedBreakIterator)
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
2000-01-08 02:05:05 +00:00
|
|
|
// constructors
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
DictionaryBasedBreakIterator::DictionaryBasedBreakIterator() :
|
|
|
|
RuleBasedBreakIterator() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
DictionaryBasedBreakIterator::DictionaryBasedBreakIterator(UDataMemory* rbbiData,
|
2001-10-09 22:57:29 +00:00
|
|
|
const char* dictionaryFilename,
|
2000-01-14 00:13:59 +00:00
|
|
|
UErrorCode& status)
|
2002-06-25 17:23:07 +00:00
|
|
|
: RuleBasedBreakIterator(rbbiData, status)
|
2000-01-08 02:05:05 +00:00
|
|
|
{
|
2002-06-25 17:23:07 +00:00
|
|
|
init();
|
2002-08-01 21:31:06 +00:00
|
|
|
if (U_FAILURE(status)) {return;};
|
2002-06-25 17:23:07 +00:00
|
|
|
fTables = new DictionaryBasedBreakIteratorTables(dictionaryFilename, status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if(fTables == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-01-14 00:13:59 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-06-25 17:23:07 +00:00
|
|
|
fTables->removeReference();
|
|
|
|
fTables = NULL;
|
2000-01-14 00:13:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
DictionaryBasedBreakIterator::DictionaryBasedBreakIterator(const DictionaryBasedBreakIterator &other) :
|
|
|
|
RuleBasedBreakIterator(other)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
if (other.fTables != NULL) {
|
|
|
|
fTables = other.fTables;
|
|
|
|
fTables->addReference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// Destructor
|
|
|
|
//
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2000-01-08 02:05:05 +00:00
|
|
|
DictionaryBasedBreakIterator::~DictionaryBasedBreakIterator()
|
|
|
|
{
|
2002-03-29 01:56:09 +00:00
|
|
|
uprv_free(cachedBreakPositions);
|
2002-06-25 17:23:07 +00:00
|
|
|
cachedBreakPositions = NULL;
|
|
|
|
if (fTables != NULL) {fTables->removeReference();};
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// Assignment operator. Sets this iterator to have the same behavior,
|
|
|
|
// and iterate over the same text, as the one passed in.
|
|
|
|
//
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2000-01-08 02:05:05 +00:00
|
|
|
DictionaryBasedBreakIterator&
|
|
|
|
DictionaryBasedBreakIterator::operator=(const DictionaryBasedBreakIterator& that) {
|
2002-06-25 17:23:07 +00:00
|
|
|
if (this == &that) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
reset(); // clears out cached break positions.
|
2000-01-08 02:05:05 +00:00
|
|
|
RuleBasedBreakIterator::operator=(that);
|
2002-06-25 17:23:07 +00:00
|
|
|
if (this->fTables != that.fTables) {
|
|
|
|
if (this->fTables != NULL) {this->fTables->removeReference();};
|
|
|
|
this->fTables = that.fTables;
|
|
|
|
if (this->fTables != NULL) {this->fTables->addReference();};
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// Clone() Returns a newly-constructed RuleBasedBreakIterator with the same
|
|
|
|
// behavior, and iterating over the same text, as this one.
|
|
|
|
//
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2000-01-08 02:05:05 +00:00
|
|
|
BreakIterator*
|
|
|
|
DictionaryBasedBreakIterator::clone() const {
|
|
|
|
return new DictionaryBasedBreakIterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// BreakIterator overrides
|
|
|
|
//=======================================================================
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances the iterator one step backwards.
|
|
|
|
* @return The position of the last boundary position before the
|
|
|
|
* current iteration position
|
|
|
|
*/
|
|
|
|
int32_t
|
|
|
|
DictionaryBasedBreakIterator::previous()
|
|
|
|
{
|
|
|
|
// if we have cached break positions and we're still in the range
|
|
|
|
// covered by them, just move one step backward in the cache
|
|
|
|
if (cachedBreakPositions != NULL && positionInCache > 0) {
|
|
|
|
--positionInCache;
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(cachedBreakPositions[positionInCache]);
|
2000-01-08 02:05:05 +00:00
|
|
|
return cachedBreakPositions[positionInCache];
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, dump the cache and use the inherited previous() method to move
|
|
|
|
// backward. This may fill up the cache with new break positions, in which
|
|
|
|
// case we have to mark our position in the cache
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
int32_t result = RuleBasedBreakIterator::previous();
|
|
|
|
if (cachedBreakPositions != NULL) {
|
|
|
|
positionInCache = numCachedBreakPositions - 2;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current iteration position to the last boundary position
|
|
|
|
* before the specified position.
|
|
|
|
* @param offset The position to begin searching from
|
|
|
|
* @return The position of the last boundary before "offset"
|
|
|
|
*/
|
|
|
|
int32_t
|
|
|
|
DictionaryBasedBreakIterator::preceding(int32_t offset)
|
|
|
|
{
|
|
|
|
// if the offset passed in is already past the end of the text,
|
|
|
|
// just return DONE; if it's before the beginning, return the
|
|
|
|
// text's starting offset
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText == NULL || offset > fText->endIndex()) {
|
2000-01-08 02:05:05 +00:00
|
|
|
return BreakIterator::DONE;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
else if (offset < fText->startIndex()) {
|
|
|
|
return fText->startIndex();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we have no cached break positions, or "offset" is outside the
|
|
|
|
// range covered by the cache, we can just call the inherited routine
|
|
|
|
// (which will eventually call other routines in this class that may
|
|
|
|
// refresh the cache)
|
|
|
|
if (cachedBreakPositions == NULL || offset <= cachedBreakPositions[0] ||
|
|
|
|
offset > cachedBreakPositions[numCachedBreakPositions - 1]) {
|
|
|
|
reset();
|
|
|
|
return RuleBasedBreakIterator::preceding(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// on the other hand, if "offset" is within the range covered by the cache,
|
|
|
|
// then all we have to do is search the cache for the last break position
|
|
|
|
// before "offset"
|
|
|
|
else {
|
|
|
|
positionInCache = 0;
|
|
|
|
while (positionInCache < numCachedBreakPositions
|
|
|
|
&& offset > cachedBreakPositions[positionInCache])
|
|
|
|
++positionInCache;
|
|
|
|
--positionInCache;
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(cachedBreakPositions[positionInCache]);
|
|
|
|
return fText->getIndex();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current iteration position to the first boundary position after
|
|
|
|
* the specified position.
|
|
|
|
* @param offset The position to begin searching forward from
|
|
|
|
* @return The position of the first boundary after "offset"
|
|
|
|
*/
|
|
|
|
int32_t
|
|
|
|
DictionaryBasedBreakIterator::following(int32_t offset)
|
|
|
|
{
|
|
|
|
// if the offset passed in is already past the end of the text,
|
|
|
|
// just return DONE; if it's before the beginning, return the
|
|
|
|
// text's starting offset
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText == NULL || offset > fText->endIndex()) {
|
2000-01-08 02:05:05 +00:00
|
|
|
return BreakIterator::DONE;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
else if (offset < fText->startIndex()) {
|
|
|
|
return fText->startIndex();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we have no cached break positions, or if "offset" is outside the
|
|
|
|
// range covered by the cache, then dump the cache and call our
|
|
|
|
// inherited following() method. This will call other methods in this
|
|
|
|
// class that may refresh the cache.
|
|
|
|
if (cachedBreakPositions == NULL || offset < cachedBreakPositions[0] ||
|
|
|
|
offset >= cachedBreakPositions[numCachedBreakPositions - 1]) {
|
|
|
|
reset();
|
|
|
|
return RuleBasedBreakIterator::following(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// on the other hand, if "offset" is within the range covered by the
|
|
|
|
// cache, then just search the cache for the first break position
|
|
|
|
// after "offset"
|
|
|
|
else {
|
|
|
|
positionInCache = 0;
|
|
|
|
while (positionInCache < numCachedBreakPositions
|
|
|
|
&& offset >= cachedBreakPositions[positionInCache])
|
|
|
|
++positionInCache;
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(cachedBreakPositions[positionInCache]);
|
|
|
|
return fText->getIndex();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the implementation function for next().
|
|
|
|
*/
|
|
|
|
int32_t
|
|
|
|
DictionaryBasedBreakIterator::handleNext()
|
|
|
|
{
|
2001-08-23 01:06:08 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-01-08 02:05:05 +00:00
|
|
|
// if there are no cached break positions, or if we've just moved
|
|
|
|
// off the end of the range covered by the cache, we have to dump
|
|
|
|
// and possibly regenerate the cache
|
|
|
|
if (cachedBreakPositions == NULL || positionInCache == numCachedBreakPositions - 1) {
|
|
|
|
|
|
|
|
// start by using the inherited handleNext() to find a tentative return
|
|
|
|
// value. dictionaryCharCount tells us how many dictionary characters
|
|
|
|
// we passed over on our way to the tentative return value
|
2002-06-25 17:23:07 +00:00
|
|
|
int32_t startPos = fText->getIndex();
|
|
|
|
fDictionaryCharCount = 0;
|
2000-01-08 02:05:05 +00:00
|
|
|
int32_t result = RuleBasedBreakIterator::handleNext();
|
|
|
|
|
|
|
|
// if we passed over more than one dictionary character, then we use
|
|
|
|
// divideUpDictionaryRange() to regenerate the cached break positions
|
|
|
|
// for the new range
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fDictionaryCharCount > 1 && result - startPos > 1) {
|
2001-08-23 01:06:08 +00:00
|
|
|
divideUpDictionaryRange(startPos, result, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return -9999; // SHOULD NEVER GET HERE!
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, the value we got back from the inherited fuction
|
|
|
|
// is our return value, and we can dump the cache
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the cache of break positions has been regenerated (or existed all
|
|
|
|
// along), then just advance to the next break position in the cache
|
|
|
|
// and return it
|
|
|
|
if (cachedBreakPositions != NULL) {
|
|
|
|
++positionInCache;
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(cachedBreakPositions[positionInCache]);
|
2000-01-08 02:05:05 +00:00
|
|
|
return cachedBreakPositions[positionInCache];
|
|
|
|
}
|
|
|
|
return -9999; // SHOULD NEVER GET HERE!
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DictionaryBasedBreakIterator::reset()
|
|
|
|
{
|
2002-03-29 01:56:09 +00:00
|
|
|
uprv_free(cachedBreakPositions);
|
2000-01-08 02:05:05 +00:00
|
|
|
cachedBreakPositions = NULL;
|
|
|
|
numCachedBreakPositions = 0;
|
2002-06-25 17:23:07 +00:00
|
|
|
fDictionaryCharCount = 0;
|
2000-01-08 02:05:05 +00:00
|
|
|
positionInCache = 0;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:40:41 +00:00
|
|
|
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// init() Common initialization routine, for use by constructors, etc.
|
|
|
|
//
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
void DictionaryBasedBreakIterator::init() {
|
|
|
|
cachedBreakPositions = NULL;
|
|
|
|
fTables = NULL;
|
|
|
|
numCachedBreakPositions = 0;
|
|
|
|
fDictionaryCharCount = 0;
|
|
|
|
positionInCache = 0;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:40:41 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// BufferClone
|
|
|
|
//
|
2003-08-27 01:01:42 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2001-02-21 23:40:41 +00:00
|
|
|
BreakIterator * DictionaryBasedBreakIterator::createBufferClone(void *stackBuffer,
|
2002-06-25 17:23:07 +00:00
|
|
|
int32_t &bufferSize,
|
2001-02-21 23:40:41 +00:00
|
|
|
UErrorCode &status)
|
|
|
|
{
|
|
|
|
if (U_FAILURE(status)){
|
2002-06-25 17:23:07 +00:00
|
|
|
return NULL;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2001-09-26 21:09:18 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// If user buffer size is zero this is a preflight operation to
|
|
|
|
// obtain the needed buffer size, allowing for worst case misalignment.
|
|
|
|
//
|
|
|
|
if (bufferSize == 0) {
|
|
|
|
bufferSize = sizeof(DictionaryBasedBreakIterator) + U_ALIGNMENT_OFFSET_UP(0);
|
|
|
|
return NULL;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2001-09-26 21:09:18 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// Check the alignment and size of the user supplied buffer.
|
|
|
|
// Allocate heap memory if the user supplied memory is insufficient.
|
|
|
|
//
|
2002-07-24 19:07:37 +00:00
|
|
|
char *buf = (char *)stackBuffer;
|
|
|
|
uint32_t s = bufferSize;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
if (stackBuffer == NULL) {
|
|
|
|
s = 0; // Ignore size, force allocation if user didn't give us a buffer.
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
if (U_ALIGNMENT_OFFSET(stackBuffer) != 0) {
|
|
|
|
int32_t offsetUp = (int32_t)U_ALIGNMENT_OFFSET_UP(buf);
|
|
|
|
s -= offsetUp;
|
|
|
|
buf += offsetUp;
|
2001-02-22 01:03:05 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
if (s < sizeof(DictionaryBasedBreakIterator)) {
|
|
|
|
buf = (char *) new DictionaryBasedBreakIterator();
|
|
|
|
if (buf == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
status = U_SAFECLONE_ALLOCATED_WARNING;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
// Initialize the clone object.
|
|
|
|
// TODO: using an overloaded C++ "operator new" to directly initialize the
|
|
|
|
// copy in the user's buffer would be better, but it doesn't seem
|
|
|
|
// to get along with namespaces. Investigate why.
|
|
|
|
//
|
|
|
|
// The memcpy is only safe with an empty (default constructed)
|
|
|
|
// break iterator. Use on others can screw up reference counts
|
|
|
|
// to data. memcpy-ing objects is not really a good idea...
|
|
|
|
//
|
|
|
|
DictionaryBasedBreakIterator localIter; // Empty break iterator, source for memcpy
|
|
|
|
DictionaryBasedBreakIterator *clone = (DictionaryBasedBreakIterator *)buf;
|
|
|
|
uprv_memcpy(clone, &localIter, sizeof(DictionaryBasedBreakIterator)); // clone = empty, but initialized, iterator.
|
|
|
|
*clone = *this; // clone = the real one we want.
|
|
|
|
if (status != U_SAFECLONE_ALLOCATED_WARNING) {
|
|
|
|
clone->fBufferClone = TRUE;
|
|
|
|
}
|
|
|
|
return clone;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
/**
|
|
|
|
* This is the function that actually implements the dictionary-based
|
|
|
|
* algorithm. Given the endpoints of a range of text, it uses the
|
|
|
|
* dictionary to determine the positions of any boundaries in this
|
|
|
|
* range. It stores all the boundary positions it discovers in
|
|
|
|
* cachedBreakPositions so that we only have to do this work once
|
|
|
|
* for each time we enter the range.
|
|
|
|
*/
|
|
|
|
void
|
2001-08-23 01:06:08 +00:00
|
|
|
DictionaryBasedBreakIterator::divideUpDictionaryRange(int32_t startPos, int32_t endPos, UErrorCode &status)
|
2000-01-08 02:05:05 +00:00
|
|
|
{
|
|
|
|
// the range we're dividing may begin or end with non-dictionary characters
|
|
|
|
// (i.e., for line breaking, we may have leading or trailing punctuation
|
|
|
|
// that needs to be kept with the word). Seek from the beginning of the
|
|
|
|
// range to the first dictionary character
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(startPos);
|
|
|
|
UChar c = fText->current();
|
|
|
|
while (isDictionaryChar(c) == FALSE) {
|
|
|
|
c = fText->next();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
// initialize. We maintain two stacks: currentBreakPositions contains
|
|
|
|
// the list of break positions that will be returned if we successfully
|
|
|
|
// finish traversing the whole range now. possibleBreakPositions lists
|
|
|
|
// all other possible word ends we've passed along the way. (Whenever
|
|
|
|
// we reach an error [a sequence of characters that can't begin any word
|
|
|
|
// in the dictionary], we back up, possibly delete some breaks from
|
|
|
|
// currentBreakPositions, move a break from possibleBreakPositions
|
|
|
|
// to currentBreakPositions, and start over from there. This process
|
|
|
|
// continues in this way until we either successfully make it all the way
|
|
|
|
// across the range, or exhaust all of our combinations of break
|
|
|
|
// positions.) wrongBreakPositions is used to keep track of paths we've
|
|
|
|
// tried on previous iterations. As the iterator backs up further and
|
|
|
|
// further, this saves us from having to follow each possible path
|
|
|
|
// through the text all the way to the error (hopefully avoiding many
|
|
|
|
// future recursive calls as well).
|
2001-08-23 01:06:08 +00:00
|
|
|
UStack currentBreakPositions(status);
|
|
|
|
UStack possibleBreakPositions(status);
|
|
|
|
UVector wrongBreakPositions(status);
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
// the dictionary is implemented as a trie, which is treated as a state
|
|
|
|
// machine. -1 represents the end of a legal word. Every word in the
|
|
|
|
// dictionary is represented by a path from the root node to -1. A path
|
|
|
|
// that ends in state 0 is an illegal combination of characters.
|
|
|
|
int16_t state = 0;
|
|
|
|
|
|
|
|
// these two variables are used for error handling. We keep track of the
|
|
|
|
// farthest we've gotten through the range being divided, and the combination
|
|
|
|
// of breaks that got us that far. If we use up all possible break
|
|
|
|
// combinations, the text contains an error or a word that's not in the
|
|
|
|
// dictionary. In this case, we "bless" the break positions that got us the
|
|
|
|
// farthest as real break positions, and then start over from scratch with
|
|
|
|
// the character where the error occurred.
|
2002-06-25 17:23:07 +00:00
|
|
|
int32_t farthestEndPoint = fText->getIndex();
|
2001-08-23 01:06:08 +00:00
|
|
|
UStack bestBreakPositions(status);
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool bestBreakPositionsInitialized = FALSE;
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
// initialize (we always exit the loop with a break statement)
|
2002-06-25 17:23:07 +00:00
|
|
|
c = fText->current();
|
2000-08-11 02:08:59 +00:00
|
|
|
for (;;) {
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
// if we can transition to state "-1" from our current state, we're
|
|
|
|
// on the last character of a legal word. Push that position onto
|
|
|
|
// the possible-break-positions stack
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fTables->fDictionary->at(state, (int32_t)0) == -1) {
|
|
|
|
possibleBreakPositions.push(fText->getIndex(), status);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// look up the new state to transition to in the dictionary
|
2002-06-25 17:23:07 +00:00
|
|
|
state = fTables->fDictionary->at(state, c);
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
// if the character we're sitting on causes us to transition to
|
|
|
|
// the "end of word" state, then it was a non-dictionary character
|
|
|
|
// and we've successfully traversed the whole range. Drop out
|
|
|
|
// of the loop.
|
|
|
|
if (state == -1) {
|
2002-06-25 17:23:07 +00:00
|
|
|
currentBreakPositions.push(fText->getIndex(), status);
|
2000-01-08 02:05:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the character we're sitting on causes us to transition to
|
|
|
|
// the error state, or if we've gone off the end of the range
|
|
|
|
// without transitioning to the "end of word" state, we've hit
|
|
|
|
// an error...
|
2002-06-25 17:23:07 +00:00
|
|
|
else if (state == 0 || fText->getIndex() >= endPos) {
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
// if this is the farthest we've gotten, take note of it in
|
|
|
|
// case there's an error in the text
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText->getIndex() > farthestEndPoint) {
|
|
|
|
farthestEndPoint = fText->getIndex();
|
2000-01-08 02:05:05 +00:00
|
|
|
bestBreakPositions.removeAllElements();
|
|
|
|
bestBreakPositionsInitialized = TRUE;
|
|
|
|
for (int32_t i = 0; i < currentBreakPositions.size(); i++) {
|
2002-03-29 01:56:09 +00:00
|
|
|
bestBreakPositions.push(currentBreakPositions.elementAti(i), status);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrongBreakPositions is a list of all break positions we've tried starting
|
|
|
|
// that didn't allow us to traverse all the way through the text. Every time
|
|
|
|
// we pop a break position off of currentBreakPositions, we put it into
|
|
|
|
// wrongBreakPositions to avoid trying it again later. If we make it to this
|
|
|
|
// spot, we're either going to back up to a break in possibleBreakPositions
|
|
|
|
// and try starting over from there, or we've exhausted all possible break
|
|
|
|
// positions and are going to do the fallback procedure. This loop prevents
|
|
|
|
// us from messing with anything in possibleBreakPositions that didn't work as
|
|
|
|
// a starting point the last time we tried it (this is to prevent a bunch of
|
|
|
|
// repetitive checks from slowing down some extreme cases)
|
|
|
|
while (!possibleBreakPositions.isEmpty() && wrongBreakPositions.contains(
|
2002-03-29 01:56:09 +00:00
|
|
|
possibleBreakPositions.peeki())) {
|
|
|
|
possibleBreakPositions.popi();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we've used up all possible break-position combinations, there's
|
|
|
|
// an error or an unknown word in the text. In this case, we start
|
|
|
|
// over, treating the farthest character we've reached as the beginning
|
|
|
|
// of the range, and "blessing" the break positions that got us that
|
|
|
|
// far as real break positions
|
|
|
|
if (possibleBreakPositions.isEmpty()) {
|
|
|
|
if (bestBreakPositionsInitialized) {
|
|
|
|
currentBreakPositions.removeAllElements();
|
|
|
|
for (int32_t i = 0; i < bestBreakPositions.size(); i++) {
|
2002-03-29 01:56:09 +00:00
|
|
|
currentBreakPositions.push(bestBreakPositions.elementAti(i), status);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
bestBreakPositions.removeAllElements();
|
|
|
|
if (farthestEndPoint < endPos) {
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(farthestEndPoint + 1);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((currentBreakPositions.isEmpty()
|
2002-06-25 17:23:07 +00:00
|
|
|
|| currentBreakPositions.peeki() != fText->getIndex())
|
|
|
|
&& fText->getIndex() != startPos) {
|
|
|
|
currentBreakPositions.push(fText->getIndex(), status);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->next();
|
|
|
|
currentBreakPositions.push(fText->getIndex(), status);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we still have more break positions we can try, then promote the
|
|
|
|
// last break in possibleBreakPositions into currentBreakPositions,
|
|
|
|
// and get rid of all entries in currentBreakPositions that come after
|
|
|
|
// it. Then back up to that position and start over from there (i.e.,
|
|
|
|
// treat that position as the beginning of a new word)
|
|
|
|
else {
|
2002-03-29 01:56:09 +00:00
|
|
|
int32_t temp = possibleBreakPositions.popi();
|
|
|
|
int32_t temp2 = 0;
|
2000-01-08 02:05:05 +00:00
|
|
|
while (!currentBreakPositions.isEmpty() && temp <
|
2002-03-29 01:56:09 +00:00
|
|
|
currentBreakPositions.peeki()) {
|
|
|
|
temp2 = currentBreakPositions.popi();
|
2001-08-23 01:06:08 +00:00
|
|
|
wrongBreakPositions.addElement(temp2, status);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-03-29 01:56:09 +00:00
|
|
|
currentBreakPositions.push(temp, status);
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(currentBreakPositions.peeki());
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-sync "c" for the next go-round, and drop out of the loop if
|
|
|
|
// we've made it off the end of the range
|
2002-06-25 17:23:07 +00:00
|
|
|
c = fText->current();
|
|
|
|
if (fText->getIndex() >= endPos) {
|
2000-01-08 02:05:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't hit any exceptional conditions on this last iteration,
|
|
|
|
// just advance to the next character and loop
|
|
|
|
else {
|
2002-06-25 17:23:07 +00:00
|
|
|
c = fText->next();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump the last break position in the list, and replace it with the actual
|
|
|
|
// end of the range (which may be the same character, or may be further on
|
|
|
|
// because the range actually ended with non-dictionary characters we want to
|
|
|
|
// keep with the word)
|
|
|
|
if (!currentBreakPositions.isEmpty()) {
|
2002-03-29 01:56:09 +00:00
|
|
|
currentBreakPositions.popi();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-03-29 01:56:09 +00:00
|
|
|
currentBreakPositions.push(endPos, status);
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
// create a regular array to hold the break positions and copy
|
|
|
|
// the break positions from the stack to the array (in addition,
|
|
|
|
// our starting position goes into this array as a break position).
|
|
|
|
// This array becomes the cache of break positions used by next()
|
|
|
|
// and previous(), so this is where we actually refresh the cache.
|
2001-03-14 01:38:29 +00:00
|
|
|
if (cachedBreakPositions != NULL) {
|
2002-03-29 01:56:09 +00:00
|
|
|
uprv_free(cachedBreakPositions);
|
2001-03-14 01:38:29 +00:00
|
|
|
}
|
2002-03-29 01:56:09 +00:00
|
|
|
cachedBreakPositions = (int32_t *)uprv_malloc((currentBreakPositions.size() + 1) * sizeof(int32_t));
|
2002-07-02 15:10:30 +00:00
|
|
|
/* Test for NULL */
|
2002-07-03 11:59:21 +00:00
|
|
|
if(cachedBreakPositions == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
numCachedBreakPositions = currentBreakPositions.size() + 1;
|
|
|
|
cachedBreakPositions[0] = startPos;
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < currentBreakPositions.size(); i++) {
|
2002-03-29 01:56:09 +00:00
|
|
|
cachedBreakPositions[i + 1] = currentBreakPositions.elementAti(i);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
positionInCache = 0;
|
|
|
|
}
|
2000-03-29 21:41:11 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
|
2001-10-09 21:58:57 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
/* eof */
|