1999-10-27 16:34:57 +00:00
|
|
|
/*
|
2003-06-02 22:21:58 +00:00
|
|
|
***************************************************************************
|
2016-05-26 22:32:17 +00:00
|
|
|
* Copyright (C) 2016 and later: Unicode, Inc. and others. Corporation
|
2010-05-19 17:29:33 +00:00
|
|
|
* and others. All rights reserved.
|
2003-06-02 22:21:58 +00:00
|
|
|
***************************************************************************
|
1999-10-27 16:34:57 +00:00
|
|
|
*/
|
2003-08-27 01:01:42 +00:00
|
|
|
//
|
|
|
|
// file: rbbi.c Contains the implementation of the rule based break iterator
|
|
|
|
// runtime engine and the API implementation for
|
|
|
|
// class RuleBasedBreakIterator
|
|
|
|
//
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2012-06-28 23:28:00 +00:00
|
|
|
#include "utypeinfo.h" // for 'typeid' to work
|
2010-05-19 17:29:33 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2000-01-10 21:10:21 +00:00
|
|
|
#include "unicode/rbbi.h"
|
|
|
|
#include "unicode/schriter.h"
|
2006-05-08 16:08:53 +00:00
|
|
|
#include "unicode/uchriter.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "unicode/udata.h"
|
2003-08-22 23:26:53 +00:00
|
|
|
#include "unicode/uclean.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
#include "rbbidata.h"
|
|
|
|
#include "rbbirb.h"
|
2001-02-22 01:03:05 +00:00
|
|
|
#include "cmemory.h"
|
2002-08-08 00:39:13 +00:00
|
|
|
#include "cstring.h"
|
2006-09-08 03:35:35 +00:00
|
|
|
#include "umutex.h"
|
2006-03-23 00:54:12 +00:00
|
|
|
#include "ucln_cmn.h"
|
|
|
|
#include "brkeng.h"
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-07-22 23:24:55 +00:00
|
|
|
#include "uassert.h"
|
2006-03-23 00:54:12 +00:00
|
|
|
#include "uvector.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2006-06-30 14:42:58 +00:00
|
|
|
// if U_LOCAL_SERVICE_HOOK is defined, then localsvc.cpp is expected to be included.
|
|
|
|
#if U_LOCAL_SERVICE_HOOK
|
|
|
|
#include "localsvc.h"
|
|
|
|
#endif
|
|
|
|
|
2006-07-14 00:47:15 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
static UBool fTrace = FALSE;
|
|
|
|
#endif
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2008-03-14 06:41:45 +00:00
|
|
|
// The state number of the starting state
|
|
|
|
#define START_STATE 1
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2008-03-14 06:41:45 +00:00
|
|
|
// The state-transition value indicating "stop"
|
|
|
|
#define STOP_STATE 0
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedBreakIterator)
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
//=======================================================================
|
|
|
|
// constructors
|
|
|
|
//=======================================================================
|
|
|
|
|
|
|
|
/**
|
2000-01-08 02:05:05 +00:00
|
|
|
* Constructs a RuleBasedBreakIterator that uses the already-created
|
|
|
|
* tables object that is passed in as a parameter.
|
1999-10-27 16:34:57 +00:00
|
|
|
*/
|
2002-06-25 17:23:07 +00:00
|
|
|
RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status)
|
2000-01-08 02:05:05 +00:00
|
|
|
{
|
2002-06-25 17:23:07 +00:00
|
|
|
init();
|
2003-09-09 23:51:17 +00:00
|
|
|
fData = new RBBIDataWrapper(data, status); // status checked in constructor
|
2002-11-30 04:41:53 +00:00
|
|
|
if (U_FAILURE(status)) {return;}
|
2002-06-29 09:31:05 +00:00
|
|
|
if(fData == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 17:49:01 +00:00
|
|
|
//
|
|
|
|
// Construct from precompiled binary rules (tables). This constructor is public API,
|
|
|
|
// taking the rules as a (const uint8_t *) to match the type produced by getBinaryRules().
|
|
|
|
//
|
|
|
|
RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules,
|
|
|
|
uint32_t ruleLength,
|
|
|
|
UErrorCode &status) {
|
|
|
|
init();
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-05-09 02:56:05 +00:00
|
|
|
if (compiledRules == NULL || ruleLength < sizeof(RBBIDataHeader)) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2011-04-29 17:49:01 +00:00
|
|
|
const RBBIDataHeader *data = (const RBBIDataHeader *)compiledRules;
|
2011-05-09 02:56:05 +00:00
|
|
|
if (data->fLength > ruleLength) {
|
2011-04-29 17:49:01 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fData = new RBBIDataWrapper(data, RBBIDataWrapper::kDontAdopt, status);
|
|
|
|
if (U_FAILURE(status)) {return;}
|
|
|
|
if(fData == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Constructor from a UDataMemory handle to precompiled break rules
|
|
|
|
// stored in an ICU data file.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &status)
|
2000-01-08 02:05:05 +00:00
|
|
|
{
|
2002-06-25 17:23:07 +00:00
|
|
|
init();
|
2003-09-09 23:51:17 +00:00
|
|
|
fData = new RBBIDataWrapper(udm, status); // status checked in constructor
|
2002-11-30 04:41:53 +00:00
|
|
|
if (U_FAILURE(status)) {return;}
|
2002-06-29 09:31:05 +00:00
|
|
|
if(fData == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Constructor from a set of rules supplied as a string.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString &rules,
|
|
|
|
UParseError &parseError,
|
|
|
|
UErrorCode &status)
|
|
|
|
{
|
|
|
|
init();
|
2002-11-30 04:41:53 +00:00
|
|
|
if (U_FAILURE(status)) {return;}
|
2002-06-25 17:23:07 +00:00
|
|
|
RuleBasedBreakIterator *bi = (RuleBasedBreakIterator *)
|
2008-02-17 19:13:10 +00:00
|
|
|
RBBIRuleBuilder::createRuleBasedBreakIterator(rules, &parseError, status);
|
2002-08-08 00:39:13 +00:00
|
|
|
// Note: This is a bit awkward. The RBBI ruleBuilder has a factory method that
|
|
|
|
// creates and returns a complete RBBI. From here, in a constructor, we
|
|
|
|
// can't just return the object created by the builder factory, hence
|
|
|
|
// the assignment of the factory created object to "this".
|
2002-06-25 17:23:07 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
*this = *bi;
|
|
|
|
delete bi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Default Constructor. Create an empty shell that can be set up later.
|
|
|
|
// Used when creating a RuleBasedBreakIterator from a set
|
|
|
|
// of rules.
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
RuleBasedBreakIterator::RuleBasedBreakIterator() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Copy constructor. Will produce a break iterator with the same behavior,
|
|
|
|
// and which iterates over the same text, as the one passed in.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
RuleBasedBreakIterator::RuleBasedBreakIterator(const RuleBasedBreakIterator& other)
|
2002-07-24 19:07:37 +00:00
|
|
|
: BreakIterator(other)
|
2000-01-08 02:05:05 +00:00
|
|
|
{
|
2002-06-25 17:23:07 +00:00
|
|
|
this->init();
|
|
|
|
*this = other;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
/**
|
2000-01-08 02:05:05 +00:00
|
|
|
* Destructor
|
1999-10-27 16:34:57 +00:00
|
|
|
*/
|
2000-01-08 02:05:05 +00:00
|
|
|
RuleBasedBreakIterator::~RuleBasedBreakIterator() {
|
2006-05-08 16:08:53 +00:00
|
|
|
if (fCharIter!=fSCharIter && fCharIter!=fDCharIter) {
|
|
|
|
// fCharIter was adopted from the outside.
|
|
|
|
delete fCharIter;
|
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
fCharIter = NULL;
|
2006-05-08 16:08:53 +00:00
|
|
|
delete fSCharIter;
|
|
|
|
fCharIter = NULL;
|
|
|
|
delete fDCharIter;
|
|
|
|
fDCharIter = NULL;
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_close(fText);
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fData != NULL) {
|
|
|
|
fData->removeReference();
|
2002-08-08 00:39:13 +00:00
|
|
|
fData = NULL;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2006-03-23 00:54:12 +00:00
|
|
|
if (fCachedBreakPositions) {
|
|
|
|
uprv_free(fCachedBreakPositions);
|
|
|
|
fCachedBreakPositions = NULL;
|
|
|
|
}
|
|
|
|
if (fLanguageBreakEngines) {
|
|
|
|
delete fLanguageBreakEngines;
|
|
|
|
fLanguageBreakEngines = NULL;
|
|
|
|
}
|
|
|
|
if (fUnhandledBreakEngine) {
|
|
|
|
delete fUnhandledBreakEngine;
|
|
|
|
fUnhandledBreakEngine = NULL;
|
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-01-08 02:05:05 +00:00
|
|
|
* Assignment operator. Sets this iterator to have the same behavior,
|
|
|
|
* and iterate over the same text, as the one passed in.
|
1999-10-27 16:34:57 +00:00
|
|
|
*/
|
2000-01-08 02:05:05 +00:00
|
|
|
RuleBasedBreakIterator&
|
|
|
|
RuleBasedBreakIterator::operator=(const RuleBasedBreakIterator& that) {
|
2002-06-25 17:23:07 +00:00
|
|
|
if (this == &that) {
|
|
|
|
return *this;
|
|
|
|
}
|
2006-03-23 00:54:12 +00:00
|
|
|
reset(); // Delete break cache information
|
|
|
|
fBreakType = that.fBreakType;
|
|
|
|
if (fLanguageBreakEngines != NULL) {
|
|
|
|
delete fLanguageBreakEngines;
|
|
|
|
fLanguageBreakEngines = NULL; // Just rebuild for now
|
|
|
|
}
|
|
|
|
// TODO: clone fLanguageBreakEngines from "that"
|
2006-04-22 05:29:27 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
fText = utext_clone(fText, that.fText, FALSE, TRUE, &status);
|
2006-05-08 16:08:53 +00:00
|
|
|
|
|
|
|
if (fCharIter!=fSCharIter && fCharIter!=fDCharIter) {
|
|
|
|
delete fCharIter;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2006-05-08 16:08:53 +00:00
|
|
|
fCharIter = NULL;
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2006-05-08 16:08:53 +00:00
|
|
|
if (that.fCharIter != NULL ) {
|
|
|
|
// This is a little bit tricky - it will intially appear that
|
|
|
|
// this->fCharIter is adopted, even if that->fCharIter was
|
|
|
|
// not adopted. That's ok.
|
|
|
|
fCharIter = that.fCharIter->clone();
|
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fData != NULL) {
|
|
|
|
fData->removeReference();
|
|
|
|
fData = NULL;
|
|
|
|
}
|
|
|
|
if (that.fData != NULL) {
|
|
|
|
fData = that.fData->addReference();
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
return *this;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-08-01 21:07:56 +00:00
|
|
|
//
|
2002-06-25 17:23:07 +00:00
|
|
|
// init() Shared initialization routine. Used by all the constructors.
|
2002-08-08 00:39:13 +00:00
|
|
|
// Initializes all fields, leaving the object in a consistent state.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void RuleBasedBreakIterator::init() {
|
2006-04-22 05:29:27 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
fText = utext_openUChars(NULL, NULL, 0, &status);
|
|
|
|
fCharIter = NULL;
|
2006-05-08 16:08:53 +00:00
|
|
|
fSCharIter = NULL;
|
|
|
|
fDCharIter = NULL;
|
2004-03-05 05:04:10 +00:00
|
|
|
fData = NULL;
|
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
|
|
|
fDictionaryCharCount = 0;
|
2009-09-10 23:17:38 +00:00
|
|
|
fBreakType = UBRK_WORD; // Defaulting BreakType to word gives reasonable
|
|
|
|
// dictionary behavior for Break Iterators that are
|
|
|
|
// built from rules. Even better would be the ability to
|
|
|
|
// declare the type in the rules.
|
2006-03-23 00:54:12 +00:00
|
|
|
|
|
|
|
fCachedBreakPositions = NULL;
|
|
|
|
fLanguageBreakEngines = NULL;
|
|
|
|
fUnhandledBreakEngine = NULL;
|
|
|
|
fNumCachedBreakPositions = 0;
|
|
|
|
fPositionInCache = 0;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-01 16:17:41 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
2003-05-16 20:11:01 +00:00
|
|
|
static UBool debugInitDone = FALSE;
|
|
|
|
if (debugInitDone == FALSE) {
|
2002-06-25 17:23:07 +00:00
|
|
|
char *debugEnv = getenv("U_RBBIDEBUG");
|
2002-08-08 00:39:13 +00:00
|
|
|
if (debugEnv && uprv_strstr(debugEnv, "trace")) {
|
2002-06-25 17:23:07 +00:00
|
|
|
fTrace = TRUE;
|
|
|
|
}
|
|
|
|
debugInitDone = TRUE;
|
|
|
|
}
|
2003-05-16 20:11:01 +00:00
|
|
|
#endif
|
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.
|
|
|
|
// Virtual function: does the right thing with subclasses.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2000-01-08 02:05:05 +00:00
|
|
|
BreakIterator*
|
|
|
|
RuleBasedBreakIterator::clone(void) const {
|
|
|
|
return new RuleBasedBreakIterator(*this);
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-01-08 02:05:05 +00:00
|
|
|
* Equality operator. Returns TRUE if both BreakIterators are of the
|
|
|
|
* same class, have the same behavior, and iterate over the same text.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2000-01-08 02:05:05 +00:00
|
|
|
RuleBasedBreakIterator::operator==(const BreakIterator& that) const {
|
2010-05-19 17:29:33 +00:00
|
|
|
if (typeid(*this) != typeid(that)) {
|
2006-04-22 05:29:27 +00:00
|
|
|
return FALSE;
|
2002-09-05 18:51:50 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2002-09-05 18:51:50 +00:00
|
|
|
const RuleBasedBreakIterator& that2 = (const RuleBasedBreakIterator&) that;
|
2006-04-22 05:29:27 +00:00
|
|
|
|
|
|
|
if (!utext_equals(fText, that2.fText)) {
|
|
|
|
// The two break iterators are operating on different text,
|
|
|
|
// or have a different interation position.
|
|
|
|
return FALSE;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: need a check for when in a dictionary region at different offsets.
|
|
|
|
|
|
|
|
if (that2.fData == fData ||
|
|
|
|
(fData != NULL && that2.fData != NULL && *that2.fData == *fData)) {
|
|
|
|
// The two break iterators are using the same rules.
|
|
|
|
return TRUE;
|
2002-09-05 18:51:50 +00:00
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
return FALSE;
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute a hash code for this BreakIterator
|
1999-10-27 16:34:57 +00:00
|
|
|
* @return A hash code
|
|
|
|
*/
|
2000-01-08 02:05:05 +00:00
|
|
|
int32_t
|
|
|
|
RuleBasedBreakIterator::hashCode(void) const {
|
2002-08-09 21:39:02 +00:00
|
|
|
int32_t hash = 0;
|
|
|
|
if (fData != NULL) {
|
|
|
|
hash = fData->hashCode();
|
|
|
|
}
|
|
|
|
return hash;
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
|
|
|
|
void RuleBasedBreakIterator::setText(UText *ut, UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reset();
|
|
|
|
fText = utext_clone(fText, ut, FALSE, TRUE, &status);
|
2006-05-08 16:08:53 +00:00
|
|
|
|
|
|
|
// Set up a dummy CharacterIterator to be returned if anyone
|
|
|
|
// calls getText(). With input from UText, there is no reasonable
|
|
|
|
// way to return a characterIterator over the actual input text.
|
|
|
|
// Return one over an empty string instead - this is the closest
|
|
|
|
// we can come to signaling a failure.
|
|
|
|
// (GetText() is obsolete, this failure is sort of OK)
|
|
|
|
if (fDCharIter == NULL) {
|
2008-01-03 10:07:47 +00:00
|
|
|
static const UChar c = 0;
|
2006-05-08 16:08:53 +00:00
|
|
|
fDCharIter = new UCharCharacterIterator(&c, 0);
|
2008-02-23 19:15:18 +00:00
|
|
|
if (fDCharIter == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2008-01-17 18:49:28 +00:00
|
|
|
}
|
2006-05-08 16:08:53 +00:00
|
|
|
|
|
|
|
if (fCharIter!=fSCharIter && fCharIter!=fDCharIter) {
|
|
|
|
// existing fCharIter was adopted from the outside. Delete it now.
|
|
|
|
delete fCharIter;
|
|
|
|
}
|
|
|
|
fCharIter = fDCharIter;
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
this->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UText *RuleBasedBreakIterator::getUText(UText *fillIn, UErrorCode &status) const {
|
|
|
|
UText *result = utext_clone(fillIn, fText, FALSE, TRUE, &status);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
/**
|
|
|
|
* Returns the description used to create this iterator
|
|
|
|
*/
|
|
|
|
const UnicodeString&
|
|
|
|
RuleBasedBreakIterator::getRules() const {
|
2002-08-09 21:39:02 +00:00
|
|
|
if (fData != NULL) {
|
|
|
|
return fData->getRuleSourceString();
|
|
|
|
} else {
|
|
|
|
static const UnicodeString *s;
|
|
|
|
if (s == NULL) {
|
|
|
|
// TODO: something more elegant here.
|
|
|
|
// perhaps API should return the string by value.
|
|
|
|
// Note: thread unsafe init & leak are semi-ok, better than
|
|
|
|
// what was before. Sould be cleaned up, though.
|
|
|
|
s = new UnicodeString;
|
|
|
|
}
|
|
|
|
return *s;
|
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// BreakIterator overrides
|
|
|
|
//=======================================================================
|
2000-01-08 02:05:05 +00:00
|
|
|
|
|
|
|
/**
|
2006-04-22 05:29:27 +00:00
|
|
|
* Return a CharacterIterator over the text being analyzed.
|
2000-01-08 02:05:05 +00:00
|
|
|
*/
|
2006-04-22 05:29:27 +00:00
|
|
|
CharacterIterator&
|
2000-01-08 02:05:05 +00:00
|
|
|
RuleBasedBreakIterator::getText() const {
|
2006-05-08 16:08:53 +00:00
|
|
|
return *fCharIter;
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the iterator to analyze a new piece of text. This function resets
|
|
|
|
* the current iteration position to the beginning of the text.
|
|
|
|
* @param newText An iterator over the text to analyze.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RuleBasedBreakIterator::adoptText(CharacterIterator* newText) {
|
2006-05-08 16:08:53 +00:00
|
|
|
// If we are holding a CharacterIterator adopted from a
|
|
|
|
// previous call to this function, delete it now.
|
|
|
|
if (fCharIter!=fSCharIter && fCharIter!=fDCharIter) {
|
|
|
|
delete fCharIter;
|
|
|
|
}
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
fCharIter = newText;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-01-08 02:05:05 +00:00
|
|
|
reset();
|
2006-04-22 05:29:27 +00:00
|
|
|
if (newText==NULL || newText->startIndex() != 0) {
|
|
|
|
// startIndex !=0 wants to be an error, but there's no way to report it.
|
|
|
|
// Make the iterator text be an empty string.
|
|
|
|
fText = utext_openUChars(fText, NULL, 0, &status);
|
|
|
|
} else {
|
|
|
|
fText = utext_openCharacterIterator(fText, newText, &status);
|
|
|
|
}
|
2002-08-08 00:39:13 +00:00
|
|
|
this->first();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-06-27 21:14:47 +00:00
|
|
|
* Set the iterator to analyze a new piece of text. This function resets
|
2000-01-08 02:05:05 +00:00
|
|
|
* the current iteration position to the beginning of the text.
|
|
|
|
* @param newText An iterator over the text to analyze.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RuleBasedBreakIterator::setText(const UnicodeString& newText) {
|
2006-04-22 05:29:27 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-01-08 02:05:05 +00:00
|
|
|
reset();
|
2006-04-22 05:29:27 +00:00
|
|
|
fText = utext_openConstUnicodeString(fText, &newText, &status);
|
2006-05-08 16:08:53 +00:00
|
|
|
|
|
|
|
// Set up a character iterator on the string.
|
|
|
|
// Needed in case someone calls getText().
|
|
|
|
// Can not, unfortunately, do this lazily on the (probably never)
|
|
|
|
// call to getText(), because getText is const.
|
|
|
|
if (fSCharIter == NULL) {
|
|
|
|
fSCharIter = new StringCharacterIterator(newText);
|
|
|
|
} else {
|
|
|
|
fSCharIter->setText(newText);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fCharIter!=fSCharIter && fCharIter!=fDCharIter) {
|
|
|
|
// old fCharIter was adopted from the outside. Delete it.
|
|
|
|
delete fCharIter;
|
|
|
|
}
|
|
|
|
fCharIter = fSCharIter;
|
|
|
|
|
2002-08-08 00:39:13 +00:00
|
|
|
this->first();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2001-03-07 22:42:46 +00:00
|
|
|
|
2011-06-09 22:49:40 +00:00
|
|
|
/**
|
|
|
|
* Provide a new UText for the input text. Must reference text with contents identical
|
|
|
|
* to the original.
|
|
|
|
* Intended for use with text data originating in Java (garbage collected) environments
|
|
|
|
* where the data may be moved in memory at arbitrary times.
|
|
|
|
*/
|
|
|
|
RuleBasedBreakIterator &RuleBasedBreakIterator::refreshInputText(UText *input, UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (input == NULL) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
int64_t pos = utext_getNativeIndex(fText);
|
|
|
|
// Shallow read-only clone of the new UText into the existing input UText
|
|
|
|
fText = utext_clone(fText, input, FALSE, TRUE, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
utext_setNativeIndex(fText, pos);
|
|
|
|
if (utext_getNativeIndex(fText) != pos) {
|
|
|
|
// Sanity check. The new input utext is supposed to have the exact same
|
|
|
|
// contents as the old. If we can't set to the same position, it doesn't.
|
|
|
|
// The contents underlying the old utext might be invalid at this point,
|
|
|
|
// so it's not safe to check directly.
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
/**
|
2014-09-08 17:36:46 +00:00
|
|
|
* Sets the current iteration position to the beginning of the text, position zero.
|
|
|
|
* @return The new iterator position, which is zero.
|
1999-10-27 16:34:57 +00:00
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t RuleBasedBreakIterator::first(void) {
|
2000-01-08 02:05:05 +00:00
|
|
|
reset();
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
2006-04-22 05:29:27 +00:00
|
|
|
//if (fText == NULL)
|
|
|
|
// return BreakIterator::DONE;
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, 0);
|
|
|
|
return 0;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current iteration position to the end of the text.
|
|
|
|
* @return The text's past-the-end offset.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t RuleBasedBreakIterator::last(void) {
|
2000-01-08 02:05:05 +00:00
|
|
|
reset();
|
2002-07-22 22:02:08 +00:00
|
|
|
if (fText == NULL) {
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
2000-01-08 02:05:05 +00:00
|
|
|
return BreakIterator::DONE;
|
2002-07-22 22:02:08 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastStatusIndexValid = FALSE;
|
2006-04-22 05:29:27 +00:00
|
|
|
int32_t pos = (int32_t)utext_nativeLength(fText);
|
|
|
|
utext_setNativeIndex(fText, pos);
|
2000-01-08 02:05:05 +00:00
|
|
|
return pos;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances the iterator either forward or backward the specified number of steps.
|
|
|
|
* Negative values move backward, and positive values move forward. This is
|
|
|
|
* equivalent to repeatedly calling next() or previous().
|
|
|
|
* @param n The number of steps to move. The sign indicates the direction
|
|
|
|
* (negative is backwards, and positive is forwards).
|
|
|
|
* @return The character offset of the boundary position n boundaries away from
|
|
|
|
* the current one.
|
|
|
|
*/
|
|
|
|
int32_t RuleBasedBreakIterator::next(int32_t n) {
|
|
|
|
int32_t result = current();
|
|
|
|
while (n > 0) {
|
2006-03-23 00:54:12 +00:00
|
|
|
result = next();
|
1999-10-27 16:34:57 +00:00
|
|
|
--n;
|
|
|
|
}
|
|
|
|
while (n < 0) {
|
|
|
|
result = previous();
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances the iterator to the next boundary position.
|
|
|
|
* @return The position of the first boundary after this one.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t RuleBasedBreakIterator::next(void) {
|
2006-03-23 00:54:12 +00:00
|
|
|
// if we have cached break positions and we're still in the range
|
|
|
|
// covered by them, just move one step forward in the cache
|
|
|
|
if (fCachedBreakPositions != NULL) {
|
|
|
|
if (fPositionInCache < fNumCachedBreakPositions - 1) {
|
|
|
|
++fPositionInCache;
|
2006-04-22 05:29:27 +00:00
|
|
|
int32_t pos = fCachedBreakPositions[fPositionInCache];
|
|
|
|
utext_setNativeIndex(fText, pos);
|
|
|
|
return pos;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t startPos = current();
|
2014-02-10 20:12:03 +00:00
|
|
|
fDictionaryCharCount = 0;
|
2006-03-23 00:54:12 +00:00
|
|
|
int32_t result = handleNext(fData->fForwardTable);
|
|
|
|
if (fDictionaryCharCount > 0) {
|
|
|
|
result = checkDictionary(startPos, result, FALSE);
|
|
|
|
}
|
|
|
|
return result;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances the iterator backwards, to the last boundary preceding this one.
|
|
|
|
* @return The position of the last boundary position preceding this one.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t RuleBasedBreakIterator::previous(void) {
|
2006-03-23 00:54:12 +00:00
|
|
|
int32_t result;
|
|
|
|
int32_t startPos;
|
|
|
|
|
|
|
|
// 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 (fCachedBreakPositions != NULL) {
|
|
|
|
if (fPositionInCache > 0) {
|
|
|
|
--fPositionInCache;
|
2007-04-17 23:01:42 +00:00
|
|
|
// If we're at the beginning of the cache, need to reevaluate the
|
|
|
|
// rule status
|
|
|
|
if (fPositionInCache <= 0) {
|
|
|
|
fLastStatusIndexValid = FALSE;
|
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
int32_t pos = fCachedBreakPositions[fPositionInCache];
|
|
|
|
utext_setNativeIndex(fText, pos);
|
|
|
|
return pos;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// if we're already sitting at the beginning of the text, return DONE
|
2006-04-22 05:29:27 +00:00
|
|
|
if (fText == NULL || (startPos = current()) == 0) {
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
2000-01-08 02:05:05 +00:00
|
|
|
return BreakIterator::DONE;
|
2002-07-22 22:02:08 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2003-11-09 06:52:44 +00:00
|
|
|
if (fData->fSafeRevTable != NULL || fData->fSafeFwdTable != NULL) {
|
2006-03-23 00:54:12 +00:00
|
|
|
result = handlePrevious(fData->fReverseTable);
|
|
|
|
if (fDictionaryCharCount > 0) {
|
|
|
|
result = checkDictionary(result, startPos, TRUE);
|
|
|
|
}
|
|
|
|
return result;
|
2003-11-07 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
2003-11-07 02:02:06 +00:00
|
|
|
// old rule syntax
|
|
|
|
// set things up. handlePrevious() will back us up to some valid
|
|
|
|
// break position before the current position (we back our internal
|
|
|
|
// iterator up one step to prevent handlePrevious() from returning
|
|
|
|
// the current position), but not necessarily the last one before
|
|
|
|
// where we started
|
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
int32_t start = current();
|
2003-11-07 02:02:06 +00:00
|
|
|
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_PREVIOUS32(fText);
|
2005-09-29 04:20:36 +00:00
|
|
|
int32_t lastResult = handlePrevious(fData->fReverseTable);
|
2006-01-21 07:00:54 +00:00
|
|
|
if (lastResult == UBRK_DONE) {
|
2006-04-22 05:29:27 +00:00
|
|
|
lastResult = 0;
|
|
|
|
utext_setNativeIndex(fText, 0);
|
2005-09-29 04:20:36 +00:00
|
|
|
}
|
2006-03-23 00:54:12 +00:00
|
|
|
result = lastResult;
|
2003-11-07 02:02:06 +00:00
|
|
|
int32_t lastTag = 0;
|
|
|
|
UBool breakTagValid = FALSE;
|
|
|
|
|
|
|
|
// iterate forward from the known break position until we pass our
|
|
|
|
// starting point. The last break position before the starting
|
|
|
|
// point is our return value
|
|
|
|
|
2002-07-22 22:02:08 +00:00
|
|
|
for (;;) {
|
2006-03-23 00:54:12 +00:00
|
|
|
result = next();
|
2003-11-07 02:02:06 +00:00
|
|
|
if (result == BreakIterator::DONE || result >= start) {
|
|
|
|
break;
|
2002-07-22 22:02:08 +00:00
|
|
|
}
|
2003-11-07 02:02:06 +00:00
|
|
|
lastResult = result;
|
2004-03-05 05:04:10 +00:00
|
|
|
lastTag = fLastRuleStatusIndex;
|
2003-11-07 02:02:06 +00:00
|
|
|
breakTagValid = TRUE;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2003-11-07 02:02:06 +00:00
|
|
|
|
|
|
|
// fLastBreakTag wants to have the value for section of text preceding
|
|
|
|
// the result position that we are to return (in lastResult.) If
|
|
|
|
// the backwards rules overshot and the above loop had to do two or more
|
2006-03-23 00:54:12 +00:00
|
|
|
// next()s to move up to the desired return position, we will have a valid
|
2014-02-10 20:12:03 +00:00
|
|
|
// tag value. But, if handlePrevious() took us to exactly the correct result position,
|
2003-11-07 02:02:06 +00:00
|
|
|
// we wont have a tag value for that position, which is only set by handleNext().
|
|
|
|
|
2014-02-10 20:12:03 +00:00
|
|
|
// Set the current iteration position to be the last break position
|
|
|
|
// before where we started, and then return that value.
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, lastResult);
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = lastTag; // for use by getRuleStatus()
|
|
|
|
fLastStatusIndexValid = breakTagValid;
|
2006-03-23 00:54:12 +00:00
|
|
|
|
|
|
|
// No need to check the dictionary; it will have been handled by
|
|
|
|
// next()
|
|
|
|
|
2003-11-07 02:02:06 +00:00
|
|
|
return lastResult;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the iterator to refer to the first boundary position following
|
|
|
|
* the specified position.
|
|
|
|
* @offset The position from which to begin searching for a break position.
|
|
|
|
* @return The position of the first break after the current position.
|
|
|
|
*/
|
|
|
|
int32_t RuleBasedBreakIterator::following(int32_t offset) {
|
2014-05-17 00:44:39 +00:00
|
|
|
// 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
|
|
|
|
if (fText == NULL || offset >= utext_nativeLength(fText)) {
|
|
|
|
last();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
else if (offset < 0) {
|
|
|
|
return first();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move requested offset to a code point start. It might be on a trail surrogate,
|
|
|
|
// or on a trail byte if the input is UTF-8.
|
|
|
|
utext_setNativeIndex(fText, offset);
|
2016-01-22 18:47:55 +00:00
|
|
|
offset = (int32_t)utext_getNativeIndex(fText);
|
2014-05-17 00:44:39 +00:00
|
|
|
|
2006-03-23 00:54:12 +00:00
|
|
|
// if we have cached break positions and offset is in the range
|
|
|
|
// covered by them, use them
|
|
|
|
// TODO: could use binary search
|
|
|
|
// TODO: what if offset is outside range, but break is not?
|
|
|
|
if (fCachedBreakPositions != NULL) {
|
|
|
|
if (offset >= fCachedBreakPositions[0]
|
|
|
|
&& offset < fCachedBreakPositions[fNumCachedBreakPositions - 1]) {
|
|
|
|
fPositionInCache = 0;
|
|
|
|
// We are guaranteed not to leave the array due to range test above
|
2006-04-22 05:29:27 +00:00
|
|
|
while (offset >= fCachedBreakPositions[fPositionInCache]) {
|
2006-03-23 00:54:12 +00:00
|
|
|
++fPositionInCache;
|
2006-04-22 05:29:27 +00:00
|
|
|
}
|
|
|
|
int32_t pos = fCachedBreakPositions[fPositionInCache];
|
|
|
|
utext_setNativeIndex(fText, pos);
|
|
|
|
return pos;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:44:39 +00:00
|
|
|
// Set our internal iteration position (temporarily)
|
1999-10-27 16:34:57 +00:00
|
|
|
// to the position passed in. If this is the _beginning_ position,
|
|
|
|
// then we can just use next() to get our return value
|
2004-04-16 01:15:04 +00:00
|
|
|
|
2003-11-07 02:02:06 +00:00
|
|
|
int32_t result = 0;
|
|
|
|
|
|
|
|
if (fData->fSafeRevTable != NULL) {
|
|
|
|
// new rule syntax
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, offset);
|
2003-11-09 06:52:44 +00:00
|
|
|
// move forward one codepoint to prepare for moving back to a
|
|
|
|
// safe point.
|
|
|
|
// this handles offset being between a supplementary character
|
2014-05-17 00:44:39 +00:00
|
|
|
// TODO: is this still needed, with move to code point boundary handled above?
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_NEXT32(fText);
|
2003-11-09 06:52:44 +00:00
|
|
|
// handlePrevious will move most of the time to < 1 boundary away
|
|
|
|
handlePrevious(fData->fSafeRevTable);
|
|
|
|
int32_t result = next();
|
|
|
|
while (result <= offset) {
|
|
|
|
result = next();
|
|
|
|
}
|
|
|
|
return result;
|
2003-11-07 02:02:06 +00:00
|
|
|
}
|
2003-11-09 06:52:44 +00:00
|
|
|
if (fData->fSafeFwdTable != NULL) {
|
|
|
|
// backup plan if forward safe table is not available
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, offset);
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_PREVIOUS32(fText);
|
2003-11-09 06:52:44 +00:00
|
|
|
// handle next will give result >= offset
|
|
|
|
handleNext(fData->fSafeFwdTable);
|
2004-04-16 01:15:04 +00:00
|
|
|
// previous will give result 0 or 1 boundary away from offset,
|
2003-11-09 06:52:44 +00:00
|
|
|
// most of the time
|
2004-04-16 01:15:04 +00:00
|
|
|
// we have to
|
2003-11-09 06:52:44 +00:00
|
|
|
int32_t oldresult = previous();
|
|
|
|
while (oldresult > offset) {
|
|
|
|
int32_t result = previous();
|
|
|
|
if (result <= offset) {
|
|
|
|
return oldresult;
|
|
|
|
}
|
|
|
|
oldresult = result;
|
2003-11-07 02:02:06 +00:00
|
|
|
}
|
2003-11-09 06:52:44 +00:00
|
|
|
int32_t result = next();
|
|
|
|
if (result <= offset) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return result;
|
2003-11-07 02:02:06 +00:00
|
|
|
}
|
2003-11-09 06:52:44 +00:00
|
|
|
// otherwise, we have to sync up first. Use handlePrevious() to back
|
2006-04-22 05:29:27 +00:00
|
|
|
// up to a known break position before the specified position (if
|
2003-11-09 06:52:44 +00:00
|
|
|
// we can determine that the specified position is a break position,
|
|
|
|
// we don't back up at all). This may or may not be the last break
|
|
|
|
// position at or before our starting position. Advance forward
|
|
|
|
// from here until we've passed the starting position. The position
|
|
|
|
// we stop on will be the first break position after the specified one.
|
|
|
|
// old rule syntax
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, offset);
|
2006-06-22 00:53:43 +00:00
|
|
|
if (offset==0 ||
|
2010-07-12 18:03:29 +00:00
|
|
|
(offset==1 && utext_getNativeIndex(fText)==0)) {
|
2006-03-23 00:54:12 +00:00
|
|
|
return next();
|
2003-11-09 06:52:44 +00:00
|
|
|
}
|
|
|
|
result = previous();
|
2003-11-07 02:02:06 +00:00
|
|
|
|
2002-07-22 22:02:08 +00:00
|
|
|
while (result != BreakIterator::DONE && result <= offset) {
|
|
|
|
result = next();
|
|
|
|
}
|
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the iterator to refer to the last boundary position before the
|
|
|
|
* specified position.
|
|
|
|
* @offset The position to begin searching for a break from.
|
|
|
|
* @return The position of the last boundary before the starting position.
|
|
|
|
*/
|
|
|
|
int32_t RuleBasedBreakIterator::preceding(int32_t offset) {
|
2014-05-17 00:44:39 +00:00
|
|
|
// 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
|
|
|
|
if (fText == NULL || offset > utext_nativeLength(fText)) {
|
|
|
|
return last();
|
|
|
|
}
|
|
|
|
else if (offset < 0) {
|
|
|
|
return first();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move requested offset to a code point start. It might be on a trail surrogate,
|
|
|
|
// or on a trail byte if the input is UTF-8.
|
|
|
|
utext_setNativeIndex(fText, offset);
|
2016-01-22 18:47:55 +00:00
|
|
|
offset = (int32_t)utext_getNativeIndex(fText);
|
2014-05-17 00:44:39 +00:00
|
|
|
|
2006-03-23 00:54:12 +00:00
|
|
|
// if we have cached break positions and offset is in the range
|
|
|
|
// covered by them, use them
|
|
|
|
if (fCachedBreakPositions != NULL) {
|
|
|
|
// TODO: binary search?
|
|
|
|
// TODO: What if offset is outside range, but break is not?
|
|
|
|
if (offset > fCachedBreakPositions[0]
|
|
|
|
&& offset <= fCachedBreakPositions[fNumCachedBreakPositions - 1]) {
|
|
|
|
fPositionInCache = 0;
|
|
|
|
while (fPositionInCache < fNumCachedBreakPositions
|
|
|
|
&& offset > fCachedBreakPositions[fPositionInCache])
|
|
|
|
++fPositionInCache;
|
|
|
|
--fPositionInCache;
|
2007-04-17 23:01:42 +00:00
|
|
|
// If we're at the beginning of the cache, need to reevaluate the
|
|
|
|
// rule status
|
|
|
|
if (fPositionInCache <= 0) {
|
|
|
|
fLastStatusIndexValid = FALSE;
|
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, fCachedBreakPositions[fPositionInCache]);
|
2006-03-23 00:54:12 +00:00
|
|
|
return fCachedBreakPositions[fPositionInCache];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// if we start by updating the current iteration position to the
|
|
|
|
// position specified by the caller, we can just use previous()
|
|
|
|
// to carry out this operation
|
2003-11-05 23:50:39 +00:00
|
|
|
|
2003-11-09 06:52:44 +00:00
|
|
|
if (fData->fSafeFwdTable != NULL) {
|
2003-11-07 02:02:06 +00:00
|
|
|
// new rule syntax
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, offset);
|
2006-05-23 22:50:54 +00:00
|
|
|
int32_t newOffset = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2005-07-08 01:57:58 +00:00
|
|
|
if (newOffset != offset) {
|
|
|
|
// Will come here if specified offset was not a code point boundary AND
|
|
|
|
// the underlying implmentation is using UText, which snaps any non-code-point-boundary
|
|
|
|
// indices to the containing code point.
|
|
|
|
// For breakitereator::preceding only, these non-code-point indices need to be moved
|
|
|
|
// up to refer to the following codepoint.
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_NEXT32(fText);
|
2006-05-23 22:50:54 +00:00
|
|
|
offset = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2005-07-08 01:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: (synwee) would it be better to just check for being in the middle of a surrogate pair,
|
2004-05-18 18:23:22 +00:00
|
|
|
// rather than adjusting the position unconditionally?
|
|
|
|
// (Change would interact with safe rules.)
|
2005-07-08 01:57:58 +00:00
|
|
|
// TODO: change RBBI behavior for off-boundary indices to match that of UText?
|
|
|
|
// affects only preceding(), seems cleaner, but is slightly different.
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_PREVIOUS32(fText);
|
2003-11-09 06:52:44 +00:00
|
|
|
handleNext(fData->fSafeFwdTable);
|
2006-05-23 22:50:54 +00:00
|
|
|
int32_t result = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2003-11-09 06:52:44 +00:00
|
|
|
while (result >= offset) {
|
2003-11-07 02:02:06 +00:00
|
|
|
result = previous();
|
|
|
|
}
|
2003-11-09 06:52:44 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (fData->fSafeRevTable != NULL) {
|
|
|
|
// backup plan if forward safe table is not available
|
2006-07-14 06:09:40 +00:00
|
|
|
// TODO: check whether this path can be discarded
|
|
|
|
// It's probably OK to say that rules must supply both safe tables
|
|
|
|
// if they use safe tables at all. We have certainly never described
|
|
|
|
// to anyone how to work with just one safe table.
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, offset);
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_NEXT32(fText);
|
2006-07-14 06:09:40 +00:00
|
|
|
|
2003-11-09 06:52:44 +00:00
|
|
|
// handle previous will give result <= offset
|
|
|
|
handlePrevious(fData->fSafeRevTable);
|
|
|
|
|
2004-04-16 01:15:04 +00:00
|
|
|
// next will give result 0 or 1 boundary away from offset,
|
2003-11-09 06:52:44 +00:00
|
|
|
// most of the time
|
2004-04-16 01:15:04 +00:00
|
|
|
// we have to
|
2003-11-09 06:52:44 +00:00
|
|
|
int32_t oldresult = next();
|
|
|
|
while (oldresult < offset) {
|
|
|
|
int32_t result = next();
|
|
|
|
if (result >= offset) {
|
|
|
|
return oldresult;
|
|
|
|
}
|
|
|
|
oldresult = result;
|
|
|
|
}
|
|
|
|
int32_t result = previous();
|
|
|
|
if (result >= offset) {
|
|
|
|
return previous();
|
|
|
|
}
|
2003-11-07 02:02:06 +00:00
|
|
|
return result;
|
2003-11-05 23:50:39 +00:00
|
|
|
}
|
|
|
|
|
2003-11-07 02:02:06 +00:00
|
|
|
// old rule syntax
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, offset);
|
2003-11-07 02:02:06 +00:00
|
|
|
return previous();
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specfied position is a boundary position. As a side
|
|
|
|
* effect, leaves the iterator pointing to the first boundary position at
|
|
|
|
* or after "offset".
|
|
|
|
* @param offset the offset to check.
|
|
|
|
* @return True if "offset" is a boundary position.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool RuleBasedBreakIterator::isBoundary(int32_t offset) {
|
2000-01-08 02:05:05 +00:00
|
|
|
// the beginning index of the iterator is always a boundary position by definition
|
2006-04-22 05:29:27 +00:00
|
|
|
if (offset == 0) {
|
2002-08-08 00:39:13 +00:00
|
|
|
first(); // For side effects on current position, tag values.
|
1999-10-27 16:34:57 +00:00
|
|
|
return TRUE;
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
if (offset == (int32_t)utext_nativeLength(fText)) {
|
2003-11-09 06:52:44 +00:00
|
|
|
last(); // For side effects on current position, tag values.
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
// out-of-range indexes are never boundary positions
|
2006-04-22 05:29:27 +00:00
|
|
|
if (offset < 0) {
|
2002-08-08 00:39:13 +00:00
|
|
|
first(); // For side effects on current position, tag values.
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-04-22 05:29:27 +00:00
|
|
|
if (offset > utext_nativeLength(fText)) {
|
2002-08-08 00:39:13 +00:00
|
|
|
last(); // For side effects on current position, tag values.
|
2000-01-08 02:05:05 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// otherwise, we can use following() on the position before the specified
|
2002-08-08 00:39:13 +00:00
|
|
|
// one and return true if the position we get back is the one the user
|
1999-10-27 16:34:57 +00:00
|
|
|
// specified
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_previous32From(fText, offset);
|
2006-05-23 22:50:54 +00:00
|
|
|
int32_t backOne = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2005-09-27 00:03:32 +00:00
|
|
|
UBool result = following(backOne) == offset;
|
|
|
|
return result;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current iteration position.
|
|
|
|
* @return The current iteration position.
|
|
|
|
*/
|
2000-01-08 02:05:05 +00:00
|
|
|
int32_t RuleBasedBreakIterator::current(void) const {
|
2006-05-23 22:50:54 +00:00
|
|
|
int32_t pos = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2006-04-22 05:29:27 +00:00
|
|
|
return pos;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
//=======================================================================
|
2002-06-27 21:14:47 +00:00
|
|
|
// implementation
|
1999-10-27 16:34:57 +00:00
|
|
|
//=======================================================================
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2005-09-29 04:20:36 +00:00
|
|
|
//
|
|
|
|
// RBBIRunMode - the state machine runs an extra iteration at the beginning and end
|
|
|
|
// of user text. A variable with this enum type keeps track of where we
|
|
|
|
// are. The state machine only fetches user input while in the RUN mode.
|
|
|
|
//
|
|
|
|
enum RBBIRunMode {
|
|
|
|
RBBI_START, // state machine processing is before first char of input
|
|
|
|
RBBI_RUN, // state machine processing is in the user text
|
|
|
|
RBBI_END // state machine processing is after end of user text.
|
|
|
|
};
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2016-02-26 21:58:26 +00:00
|
|
|
// Map from look-ahead break states (corresponds to rules) to boundary positions.
|
|
|
|
// Allows multiple lookahead break rules to be in flight at the same time.
|
|
|
|
//
|
|
|
|
// This is a temporary approach for ICU 57. A better fix is to make the look-ahead numbers
|
|
|
|
// in the state table be sequential, then we can just index an array. And the
|
|
|
|
// table could also tell us in advance how big that array needs to be.
|
|
|
|
//
|
|
|
|
// Before ICU 57 there was just a single simple variable for a look-ahead match that
|
|
|
|
// was in progress. Two rules at once did not work.
|
|
|
|
|
|
|
|
static const int32_t kMaxLookaheads = 8;
|
|
|
|
struct LookAheadResults {
|
|
|
|
int32_t fUsedSlotLimit;
|
|
|
|
int32_t fPositions[8];
|
|
|
|
int16_t fKeys[8];
|
|
|
|
|
|
|
|
LookAheadResults() : fUsedSlotLimit(0), fPositions(), fKeys() {};
|
|
|
|
|
|
|
|
int32_t getPosition(int16_t key) {
|
|
|
|
for (int32_t i=0; i<fUsedSlotLimit; ++i) {
|
|
|
|
if (fKeys[i] == key) {
|
|
|
|
return fPositions[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPosition(int16_t key, int32_t position) {
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<fUsedSlotLimit; ++i) {
|
|
|
|
if (fKeys[i] == key) {
|
|
|
|
fPositions[i] = position;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i >= kMaxLookaheads) {
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
i = kMaxLookaheads - 1;
|
|
|
|
}
|
|
|
|
fKeys[i] = key;
|
|
|
|
fPositions[i] = position;
|
|
|
|
U_ASSERT(fUsedSlotLimit == i);
|
|
|
|
fUsedSlotLimit = i + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-09-29 04:20:36 +00:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// handleNext(stateTable)
|
|
|
|
// This method is the actual implementation of the rbbi next() method.
|
|
|
|
// This method initializes the state machine to state 1
|
|
|
|
// and advances through the text character by character until we reach the end
|
|
|
|
// of the text or the state machine transitions to state 0. We update our return
|
|
|
|
// value every time the state machine passes through an accepting state.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------------
|
2003-11-09 06:52:44 +00:00
|
|
|
int32_t RuleBasedBreakIterator::handleNext(const RBBIStateTable *statetable) {
|
2005-09-27 00:03:32 +00:00
|
|
|
int32_t state;
|
2012-02-06 19:57:08 +00:00
|
|
|
uint16_t category = 0;
|
2005-09-29 04:20:36 +00:00
|
|
|
RBBIRunMode mode;
|
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
RBBIStateTableRow *row;
|
2005-09-29 04:20:36 +00:00
|
|
|
UChar32 c;
|
2016-02-26 21:58:26 +00:00
|
|
|
LookAheadResults lookAheadMatches;
|
|
|
|
int32_t result = 0;
|
|
|
|
int32_t initialPosition = 0;
|
|
|
|
const char *tableData = statetable->fTableData;
|
|
|
|
uint32_t tableRowLen = statetable->fRowLen;
|
2005-09-27 00:03:32 +00:00
|
|
|
|
2006-07-14 00:47:15 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
if (fTrace) {
|
|
|
|
RBBIDebugPuts("Handle Next pos char state category");
|
|
|
|
}
|
|
|
|
#endif
|
2002-07-22 22:02:08 +00:00
|
|
|
|
|
|
|
// No matter what, handleNext alway correctly sets the break tag value.
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastStatusIndexValid = TRUE;
|
2005-09-27 00:03:32 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
2002-07-22 22:02:08 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// if we're already at the end of the text, return DONE.
|
2006-07-14 06:09:40 +00:00
|
|
|
initialPosition = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
|
|
|
result = initialPosition;
|
2006-08-01 00:06:20 +00:00
|
|
|
c = UTEXT_NEXT32(fText);
|
2006-04-22 05:29:27 +00:00
|
|
|
if (fData == NULL || c==U_SENTINEL) {
|
2000-01-08 02:05:05 +00:00
|
|
|
return BreakIterator::DONE;
|
2002-07-22 22:02:08 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// Set the initial state for the state machine
|
|
|
|
state = START_STATE;
|
|
|
|
row = (RBBIStateTableRow *)
|
2006-08-01 00:06:20 +00:00
|
|
|
//(statetable->fTableData + (statetable->fRowLen * state));
|
|
|
|
(tableData + tableRowLen * state);
|
|
|
|
|
2006-07-14 06:09:40 +00:00
|
|
|
|
2005-09-29 04:20:36 +00:00
|
|
|
mode = RBBI_RUN;
|
|
|
|
if (statetable->fFlags & RBBI_BOF_REQUIRED) {
|
|
|
|
category = 2;
|
|
|
|
mode = RBBI_START;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2002-08-08 00:39:13 +00:00
|
|
|
// loop until we reach the end of the text or transition to state 0
|
2005-09-27 00:03:32 +00:00
|
|
|
//
|
2002-08-08 00:39:13 +00:00
|
|
|
for (;;) {
|
2006-07-14 06:09:40 +00:00
|
|
|
if (c == U_SENTINEL) {
|
2003-10-21 21:47:16 +00:00
|
|
|
// Reached end of input string.
|
2005-09-29 04:20:36 +00:00
|
|
|
if (mode == RBBI_END) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// We have already run the loop one last time with the
|
|
|
|
// character set to the psueudo {eof} value. Now it is time
|
|
|
|
// to unconditionally bail out.
|
|
|
|
break;
|
2003-10-21 21:47:16 +00:00
|
|
|
}
|
2005-09-27 00:03:32 +00:00
|
|
|
// Run the loop one last time with the fake end-of-input character category.
|
2005-09-29 04:20:36 +00:00
|
|
|
mode = RBBI_END;
|
2005-09-27 00:03:32 +00:00
|
|
|
category = 1;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2005-09-27 00:03:32 +00:00
|
|
|
// Get the char category. An incoming category of 1 or 2 means that
|
|
|
|
// we are preset for doing the beginning or end of input, and
|
|
|
|
// that we shouldn't get a category from an actual text input character.
|
|
|
|
//
|
2005-09-29 04:20:36 +00:00
|
|
|
if (mode == RBBI_RUN) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// look up the current character's character category, which tells us
|
|
|
|
// which column in the state table to look at.
|
|
|
|
// Note: the 16 in UTRIE_GET16 refers to the size of the data being returned,
|
|
|
|
// not the size of the character going in, which is a UChar32.
|
|
|
|
//
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
|
|
|
|
// Check the dictionary bit in the character's category.
|
|
|
|
// Counter is only used by dictionary based iterators (subclasses).
|
|
|
|
// Chars that need to be handled by a dictionary have a flag bit set
|
|
|
|
// in their category values.
|
|
|
|
//
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
// And off the dictionary flag bit.
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2011-11-14 19:32:51 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
2004-09-15 17:11:47 +00:00
|
|
|
if (fTrace) {
|
2009-09-10 23:17:38 +00:00
|
|
|
RBBIDebugPrintf(" %4ld ", utext_getNativeIndex(fText));
|
2004-09-15 17:11:47 +00:00
|
|
|
if (0x20<=c && c<0x7f) {
|
|
|
|
RBBIDebugPrintf("\"%c\" ", c);
|
|
|
|
} else {
|
|
|
|
RBBIDebugPrintf("%5x ", c);
|
|
|
|
}
|
|
|
|
RBBIDebugPrintf("%3d %3d\n", state, category);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2004-09-15 17:11:47 +00:00
|
|
|
#endif
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// State Transition - move machine to its next state
|
|
|
|
//
|
2011-11-14 19:32:51 +00:00
|
|
|
|
|
|
|
// Note: fNextState is defined as uint16_t[2], but we are casting
|
|
|
|
// a generated RBBI table to RBBIStateTableRow and some tables
|
|
|
|
// actually have more than 2 categories.
|
|
|
|
U_ASSERT(category<fData->fHeader->fCatCount);
|
|
|
|
state = row->fNextState[category]; /*Not accessing beyond memory*/
|
2002-06-25 17:23:07 +00:00
|
|
|
row = (RBBIStateTableRow *)
|
2006-08-01 00:06:20 +00:00
|
|
|
// (statetable->fTableData + (statetable->fRowLen * state));
|
|
|
|
(tableData + tableRowLen * state);
|
2002-06-27 21:14:47 +00:00
|
|
|
|
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fAccepting == -1) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// Match found, common case.
|
2006-07-14 06:09:40 +00:00
|
|
|
if (mode != RBBI_START) {
|
|
|
|
result = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
|
|
|
}
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = row->fTagIdx; // Remember the break status (tag) values.
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2016-02-26 21:58:26 +00:00
|
|
|
int16_t completedRule = row->fAccepting;
|
|
|
|
if (completedRule > 0) {
|
|
|
|
// Lookahead match is completed.
|
|
|
|
int32_t lookaheadResult = lookAheadMatches.getPosition(completedRule);
|
|
|
|
if (lookaheadResult >= 0) {
|
|
|
|
fLastRuleStatusIndex = row->fTagIdx;
|
|
|
|
UTEXT_SETNATIVEINDEX(fText, lookaheadResult);
|
|
|
|
return lookaheadResult;
|
2003-11-05 23:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-26 21:58:26 +00:00
|
|
|
int16_t rule = row->fLookAhead;
|
|
|
|
if (rule != 0) {
|
|
|
|
// At the position of a '/' in a look-ahead match. Record it.
|
|
|
|
int32_t pos = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
|
|
|
lookAheadMatches.setPosition(rule, pos);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
if (state == STOP_STATE) {
|
2003-10-21 21:47:16 +00:00
|
|
|
// This is the normal exit from the lookup state machine.
|
|
|
|
// We have advanced through the string until it is certain that no
|
|
|
|
// longer match is possible, no matter what characters follow.
|
2002-06-25 17:23:07 +00:00
|
|
|
break;
|
2004-04-16 01:15:04 +00:00
|
|
|
}
|
2006-07-14 06:09:40 +00:00
|
|
|
|
|
|
|
// Advance to the next character.
|
|
|
|
// If this is a beginning-of-input loop iteration, don't advance
|
|
|
|
// the input position. The next iteration will be processing the
|
|
|
|
// first real input character.
|
|
|
|
if (mode == RBBI_RUN) {
|
|
|
|
c = UTEXT_NEXT32(fText);
|
|
|
|
} else {
|
|
|
|
if (mode == RBBI_START) {
|
|
|
|
mode = RBBI_RUN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2003-10-21 21:47:16 +00:00
|
|
|
// The state machine is done. Check whether it found a match...
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2003-10-21 21:47:16 +00:00
|
|
|
// If the iterator failed to advance in the match engine, force it ahead by one.
|
|
|
|
// (This really indicates a defect in the break rules. They should always match
|
|
|
|
// at least one character.)
|
|
|
|
if (result == initialPosition) {
|
2007-05-02 22:49:20 +00:00
|
|
|
UTEXT_SETNATIVEINDEX(fText, initialPosition);
|
2006-07-14 06:09:40 +00:00
|
|
|
UTEXT_NEXT32(fText);
|
2006-05-23 22:50:54 +00:00
|
|
|
result = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2003-10-21 21:47:16 +00:00
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2003-10-21 21:47:16 +00:00
|
|
|
// Leave the iterator at our result position.
|
2007-05-02 22:49:20 +00:00
|
|
|
UTEXT_SETNATIVEINDEX(fText, result);
|
2004-12-22 00:23:15 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
if (fTrace) {
|
|
|
|
RBBIDebugPrintf("result = %d\n\n", result);
|
|
|
|
}
|
|
|
|
#endif
|
1999-10-27 16:34:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-04-16 01:15:04 +00:00
|
|
|
|
2003-11-07 02:02:06 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// handlePrevious()
|
|
|
|
//
|
2005-03-23 02:13:53 +00:00
|
|
|
// Iterate backwards, according to the logic of the reverse rules.
|
|
|
|
// This version handles the exact style backwards rules.
|
2002-08-08 00:39:13 +00:00
|
|
|
//
|
|
|
|
// The logic of this function is very similar to handleNext(), above.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------------
|
2003-11-09 06:52:44 +00:00
|
|
|
int32_t RuleBasedBreakIterator::handlePrevious(const RBBIStateTable *statetable) {
|
2005-09-27 00:03:32 +00:00
|
|
|
int32_t state;
|
2012-02-06 19:57:08 +00:00
|
|
|
uint16_t category = 0;
|
2005-09-29 04:20:36 +00:00
|
|
|
RBBIRunMode mode;
|
2005-09-27 00:03:32 +00:00
|
|
|
RBBIStateTableRow *row;
|
2005-09-29 04:20:36 +00:00
|
|
|
UChar32 c;
|
2016-02-26 21:58:26 +00:00
|
|
|
LookAheadResults lookAheadMatches;
|
2005-09-27 00:03:32 +00:00
|
|
|
int32_t result = 0;
|
|
|
|
int32_t initialPosition = 0;
|
|
|
|
|
2006-07-14 00:47:15 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
if (fTrace) {
|
|
|
|
RBBIDebugPuts("Handle Previous pos char state category");
|
|
|
|
}
|
|
|
|
#endif
|
2005-09-27 00:03:32 +00:00
|
|
|
|
|
|
|
// handlePrevious() never gets the rule status.
|
|
|
|
// Flag the status as invalid; if the user ever asks for status, we will need
|
|
|
|
// to back up, then re-find the break position using handleNext(), which does
|
|
|
|
// get the status value.
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastStatusIndexValid = FALSE;
|
2005-09-27 00:03:32 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
|
|
|
|
// if we're already at the start of the text, return DONE.
|
2006-05-23 22:50:54 +00:00
|
|
|
if (fText == NULL || fData == NULL || UTEXT_GETNATIVEINDEX(fText)==0) {
|
2005-09-27 00:03:32 +00:00
|
|
|
return BreakIterator::DONE;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// Set up the starting char.
|
2006-05-23 22:50:54 +00:00
|
|
|
initialPosition = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2005-09-27 00:03:32 +00:00
|
|
|
result = initialPosition;
|
2006-07-19 17:19:02 +00:00
|
|
|
c = UTEXT_PREVIOUS32(fText);
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// Set the initial state for the state machine
|
|
|
|
state = START_STATE;
|
2002-06-25 17:23:07 +00:00
|
|
|
row = (RBBIStateTableRow *)
|
2005-09-27 00:03:32 +00:00
|
|
|
(statetable->fTableData + (statetable->fRowLen * state));
|
2005-09-29 04:20:36 +00:00
|
|
|
category = 3;
|
|
|
|
mode = RBBI_RUN;
|
|
|
|
if (statetable->fFlags & RBBI_BOF_REQUIRED) {
|
|
|
|
category = 2;
|
|
|
|
mode = RBBI_START;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// loop until we reach the start of the text or transition to state 0
|
|
|
|
//
|
2002-06-25 17:23:07 +00:00
|
|
|
for (;;) {
|
2006-04-22 05:29:27 +00:00
|
|
|
if (c == U_SENTINEL) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// Reached end of input string.
|
2010-07-12 18:03:29 +00:00
|
|
|
if (mode == RBBI_END) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// We have already run the loop one last time with the
|
|
|
|
// character set to the psueudo {eof} value. Now it is time
|
|
|
|
// to unconditionally bail out.
|
2016-02-26 21:58:26 +00:00
|
|
|
if (result == initialPosition) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// Ran off start, no match found.
|
|
|
|
// move one index one (towards the start, since we are doing a previous())
|
2007-05-02 22:49:20 +00:00
|
|
|
UTEXT_SETNATIVEINDEX(fText, initialPosition);
|
2011-07-06 20:05:38 +00:00
|
|
|
(void)UTEXT_PREVIOUS32(fText); // TODO: shouldn't be necessary. We're already at beginning. Check.
|
2005-03-28 05:21:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-09-27 00:03:32 +00:00
|
|
|
// Run the loop one last time with the fake end-of-input character category.
|
2005-09-29 04:20:36 +00:00
|
|
|
mode = RBBI_END;
|
2005-03-23 02:13:53 +00:00
|
|
|
category = 1;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
2005-09-27 00:03:32 +00:00
|
|
|
// Get the char category. An incoming category of 1 or 2 means that
|
|
|
|
// we are preset for doing the beginning or end of input, and
|
|
|
|
// that we shouldn't get a category from an actual text input character.
|
|
|
|
//
|
2005-09-29 04:20:36 +00:00
|
|
|
if (mode == RBBI_RUN) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// look up the current character's character category, which tells us
|
|
|
|
// which column in the state table to look at.
|
|
|
|
// Note: the 16 in UTRIE_GET16 refers to the size of the data being returned,
|
|
|
|
// not the size of the character going in, which is a UChar32.
|
|
|
|
//
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
|
|
|
|
// Check the dictionary bit in the character's category.
|
|
|
|
// Counter is only used by dictionary based iterators (subclasses).
|
|
|
|
// Chars that need to be handled by a dictionary have a flag bit set
|
|
|
|
// in their category values.
|
|
|
|
//
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
// And off the dictionary flag bit.
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
2004-09-15 17:11:47 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
if (fTrace) {
|
2006-04-22 05:29:27 +00:00
|
|
|
RBBIDebugPrintf(" %4d ", (int32_t)utext_getNativeIndex(fText));
|
2004-09-15 17:11:47 +00:00
|
|
|
if (0x20<=c && c<0x7f) {
|
|
|
|
RBBIDebugPrintf("\"%c\" ", c);
|
|
|
|
} else {
|
|
|
|
RBBIDebugPrintf("%5x ", c);
|
|
|
|
}
|
|
|
|
RBBIDebugPrintf("%3d %3d\n", state, category);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2004-09-15 17:11:47 +00:00
|
|
|
#endif
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// State Transition - move machine to its next state
|
|
|
|
//
|
2011-11-14 19:32:51 +00:00
|
|
|
|
|
|
|
// Note: fNextState is defined as uint16_t[2], but we are casting
|
|
|
|
// a generated RBBI table to RBBIStateTableRow and some tables
|
|
|
|
// actually have more than 2 categories.
|
|
|
|
U_ASSERT(category<fData->fHeader->fCatCount);
|
|
|
|
state = row->fNextState[category]; /*Not accessing beyond memory*/
|
2002-06-25 17:23:07 +00:00
|
|
|
row = (RBBIStateTableRow *)
|
2005-09-27 00:03:32 +00:00
|
|
|
(statetable->fTableData + (statetable->fRowLen * state));
|
2004-04-16 01:15:04 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fAccepting == -1) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// Match found, common case.
|
2006-05-23 22:50:54 +00:00
|
|
|
result = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2016-02-26 21:58:26 +00:00
|
|
|
int16_t completedRule = row->fAccepting;
|
|
|
|
if (completedRule > 0) {
|
|
|
|
// Lookahead match is completed.
|
|
|
|
int32_t lookaheadResult = lookAheadMatches.getPosition(completedRule);
|
|
|
|
if (lookaheadResult >= 0) {
|
|
|
|
UTEXT_SETNATIVEINDEX(fText, lookaheadResult);
|
|
|
|
return lookaheadResult;
|
2003-11-05 23:50:39 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2016-02-26 21:58:26 +00:00
|
|
|
int16_t rule = row->fLookAhead;
|
|
|
|
if (rule != 0) {
|
|
|
|
// At the position of a '/' in a look-ahead match. Record it.
|
|
|
|
int32_t pos = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
|
|
|
lookAheadMatches.setPosition(rule, pos);
|
2005-03-23 02:13:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2004-04-16 01:15:04 +00:00
|
|
|
if (state == STOP_STATE) {
|
2005-09-27 00:03:32 +00:00
|
|
|
// This is the normal exit from the lookup state machine.
|
|
|
|
// We have advanced through the string until it is certain that no
|
|
|
|
// longer match is possible, no matter what characters follow.
|
2002-06-25 17:23:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// Move (backwards) to the next character to process.
|
|
|
|
// If this is a beginning-of-input loop iteration, don't advance
|
|
|
|
// the input position. The next iteration will be processing the
|
|
|
|
// first real input character.
|
2005-09-29 04:20:36 +00:00
|
|
|
if (mode == RBBI_RUN) {
|
2006-07-19 17:19:02 +00:00
|
|
|
c = UTEXT_PREVIOUS32(fText);
|
2005-09-29 04:20:36 +00:00
|
|
|
} else {
|
|
|
|
if (mode == RBBI_START) {
|
|
|
|
mode = RBBI_RUN;
|
|
|
|
}
|
2005-03-23 02:13:53 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// The state machine is done. Check whether it found a match...
|
2005-03-23 02:13:53 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// If the iterator failed to advance in the match engine, force it ahead by one.
|
|
|
|
// (This really indicates a defect in the break rules. They should always match
|
|
|
|
// at least one character.)
|
|
|
|
if (result == initialPosition) {
|
2007-05-02 22:49:20 +00:00
|
|
|
UTEXT_SETNATIVEINDEX(fText, initialPosition);
|
2006-07-19 17:19:02 +00:00
|
|
|
UTEXT_PREVIOUS32(fText);
|
2006-05-23 22:50:54 +00:00
|
|
|
result = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2005-09-27 00:03:32 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2005-09-27 00:03:32 +00:00
|
|
|
// Leave the iterator at our result position.
|
2007-05-02 22:49:20 +00:00
|
|
|
UTEXT_SETNATIVEINDEX(fText, result);
|
2005-09-27 00:03:32 +00:00
|
|
|
#ifdef RBBI_DEBUG
|
|
|
|
if (fTrace) {
|
|
|
|
RBBIDebugPrintf("result = %d\n\n", result);
|
|
|
|
}
|
|
|
|
#endif
|
2002-06-25 17:23:07 +00:00
|
|
|
return result;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
void
|
|
|
|
RuleBasedBreakIterator::reset()
|
|
|
|
{
|
2006-03-23 00:54:12 +00:00
|
|
|
if (fCachedBreakPositions) {
|
|
|
|
uprv_free(fCachedBreakPositions);
|
|
|
|
}
|
|
|
|
fCachedBreakPositions = NULL;
|
|
|
|
fNumCachedBreakPositions = 0;
|
|
|
|
fDictionaryCharCount = 0;
|
|
|
|
fPositionInCache = 0;
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2001-02-21 23:40:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
2002-08-08 00:39:13 +00:00
|
|
|
// getRuleStatus() Return the break rule tag associated with the current
|
|
|
|
// iterator position. If the iterator arrived at its current
|
|
|
|
// position by iterating forwards, the value will have been
|
|
|
|
// cached by the handleNext() function.
|
|
|
|
//
|
|
|
|
// If no cached status value is available, the status is
|
|
|
|
// found by doing a previous() followed by a next(), which
|
|
|
|
// leaves the iterator where it started, and computes the
|
|
|
|
// status while doing the next().
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
2004-03-05 05:04:10 +00:00
|
|
|
void RuleBasedBreakIterator::makeRuleStatusValid() {
|
|
|
|
if (fLastStatusIndexValid == FALSE) {
|
2002-08-08 00:39:13 +00:00
|
|
|
// No cached status is available.
|
2006-04-22 05:29:27 +00:00
|
|
|
if (fText == NULL || current() == 0) {
|
2002-08-08 00:39:13 +00:00
|
|
|
// At start of text, or there is no text. Status is always zero.
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
2002-07-22 22:02:08 +00:00
|
|
|
} else {
|
2002-08-08 00:39:13 +00:00
|
|
|
// Not at start of text. Find status the tedious way.
|
2002-07-22 22:02:08 +00:00
|
|
|
int32_t pa = current();
|
2004-03-05 05:04:10 +00:00
|
|
|
previous();
|
2006-03-23 00:54:12 +00:00
|
|
|
if (fNumCachedBreakPositions > 0) {
|
|
|
|
reset(); // Blow off the dictionary cache
|
|
|
|
}
|
2004-03-05 05:04:10 +00:00
|
|
|
int32_t pb = next();
|
2004-01-13 19:54:06 +00:00
|
|
|
if (pa != pb) {
|
|
|
|
// note: the if (pa != pb) test is here only to eliminate warnings for
|
|
|
|
// unused local variables on gcc. Logically, it isn't needed.
|
|
|
|
U_ASSERT(pa == pb);
|
|
|
|
}
|
2002-07-22 22:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
U_ASSERT(fLastRuleStatusIndex >= 0 && fLastRuleStatusIndex < fData->fStatusMaxIdx);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
int32_t RuleBasedBreakIterator::getRuleStatus() const {
|
|
|
|
RuleBasedBreakIterator *nonConstThis = (RuleBasedBreakIterator *)this;
|
|
|
|
nonConstThis->makeRuleStatusValid();
|
|
|
|
|
|
|
|
// fLastRuleStatusIndex indexes to the start of the appropriate status record
|
|
|
|
// (the number of status values.)
|
|
|
|
// This function returns the last (largest) of the array of status values.
|
|
|
|
int32_t idx = fLastRuleStatusIndex + fData->fRuleStatusTable[fLastRuleStatusIndex];
|
|
|
|
int32_t tagVal = fData->fRuleStatusTable[idx];
|
|
|
|
|
|
|
|
return tagVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int32_t RuleBasedBreakIterator::getRuleStatusVec(
|
2004-04-16 01:15:04 +00:00
|
|
|
int32_t *fillInVec, int32_t capacity, UErrorCode &status)
|
2004-03-05 05:04:10 +00:00
|
|
|
{
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedBreakIterator *nonConstThis = (RuleBasedBreakIterator *)this;
|
|
|
|
nonConstThis->makeRuleStatusValid();
|
|
|
|
int32_t numVals = fData->fRuleStatusTable[fLastRuleStatusIndex];
|
|
|
|
int32_t numValsToCopy = numVals;
|
|
|
|
if (numVals > capacity) {
|
2004-04-16 01:15:04 +00:00
|
|
|
status = U_BUFFER_OVERFLOW_ERROR;
|
2004-03-05 05:04:10 +00:00
|
|
|
numValsToCopy = capacity;
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i=0; i<numValsToCopy; i++) {
|
|
|
|
fillInVec[i] = fData->fRuleStatusTable[fLastRuleStatusIndex + i + 1];
|
|
|
|
}
|
|
|
|
return numVals;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
2002-08-08 00:39:13 +00:00
|
|
|
// getBinaryRules Access to the compiled form of the rules,
|
2002-06-25 17:23:07 +00:00
|
|
|
// for use by build system tools that save the data
|
|
|
|
// for standard iterator types.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
2002-06-27 01:50:22 +00:00
|
|
|
const uint8_t *RuleBasedBreakIterator::getBinaryRules(uint32_t &length) {
|
2002-06-25 17:23:07 +00:00
|
|
|
const uint8_t *retPtr = NULL;
|
2002-06-27 01:50:22 +00:00
|
|
|
length = 0;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
if (fData != NULL) {
|
|
|
|
retPtr = (const uint8_t *)fData->fHeader;
|
2002-08-08 00:39:13 +00:00
|
|
|
length = fData->fHeader->fLength;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
return retPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-17 19:48:50 +00:00
|
|
|
BreakIterator * RuleBasedBreakIterator::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 (bufferSize == 0) {
|
2013-09-17 19:48:50 +00:00
|
|
|
bufferSize = 1; // preflighting for deprecated functionality
|
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
|
|
|
|
2013-09-17 19:48:50 +00:00
|
|
|
BreakIterator *clonedBI = clone();
|
|
|
|
if (clonedBI == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else {
|
|
|
|
status = U_SAFECLONE_ALLOCATED_WARNING;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2013-09-17 19:48:50 +00:00
|
|
|
return (RuleBasedBreakIterator *)clonedBI;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// isDictionaryChar Return true if the category lookup for this char
|
|
|
|
// indicates that it is in the set of dictionary lookup
|
|
|
|
// chars.
|
|
|
|
//
|
|
|
|
// This function is intended for use by dictionary based
|
|
|
|
// break iterators.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
2006-05-26 00:57:09 +00:00
|
|
|
/*UBool RuleBasedBreakIterator::isDictionaryChar(UChar32 c) {
|
2002-08-09 21:39:02 +00:00
|
|
|
if (fData == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
uint16_t category;
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
return (category & 0x4000) != 0;
|
2006-05-26 00:57:09 +00:00
|
|
|
}*/
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
|
2006-03-23 00:54:12 +00:00
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// checkDictionary This function handles all processing of characters in
|
|
|
|
// the "dictionary" set. It will determine the appropriate
|
|
|
|
// course of action, and possibly set up a cache in the
|
|
|
|
// process.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
int32_t RuleBasedBreakIterator::checkDictionary(int32_t startPos,
|
|
|
|
int32_t endPos,
|
|
|
|
UBool reverse) {
|
|
|
|
// Reset the old break cache first.
|
|
|
|
reset();
|
|
|
|
|
2012-08-16 23:01:49 +00:00
|
|
|
// note: code segment below assumes that dictionary chars are in the
|
|
|
|
// startPos-endPos range
|
|
|
|
// value returned should be next character in sequence
|
|
|
|
if ((endPos - startPos) <= 1) {
|
2006-03-23 00:54:12 +00:00
|
|
|
return (reverse ? startPos : endPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting from the starting point, scan towards the proposed result,
|
|
|
|
// looking for the first dictionary character (which may be the one
|
|
|
|
// we're on, if we're starting in the middle of a range).
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, reverse ? endPos : startPos);
|
2006-03-23 00:54:12 +00:00
|
|
|
if (reverse) {
|
2006-07-19 17:19:02 +00:00
|
|
|
UTEXT_PREVIOUS32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t rangeStart = startPos;
|
|
|
|
int32_t rangeEnd = endPos;
|
|
|
|
|
|
|
|
uint16_t category;
|
|
|
|
int32_t current;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UStack breaks(status);
|
|
|
|
int32_t foundBreakCount = 0;
|
2006-04-22 05:29:27 +00:00
|
|
|
UChar32 c = utext_current32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
|
|
|
|
// Is the character we're starting on a dictionary character? If so, we
|
|
|
|
// need to back up to include the entire run; otherwise the results of
|
|
|
|
// the break algorithm will differ depending on where we start. Since
|
|
|
|
// the result is cached and there is typically a non-dictionary break
|
|
|
|
// within a small number of words, there should be little performance impact.
|
|
|
|
if (category & 0x4000) {
|
|
|
|
if (reverse) {
|
|
|
|
do {
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_next32(fText); // TODO: recast to work directly with postincrement.
|
|
|
|
c = utext_current32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
2006-04-22 05:29:27 +00:00
|
|
|
} while (c != U_SENTINEL && (category & 0x4000));
|
2006-03-23 00:54:12 +00:00
|
|
|
// Back up to the last dictionary character
|
2006-05-23 22:50:54 +00:00
|
|
|
rangeEnd = (int32_t)UTEXT_GETNATIVEINDEX(fText);
|
2006-04-22 05:29:27 +00:00
|
|
|
if (c == U_SENTINEL) {
|
|
|
|
// c = fText->last32();
|
|
|
|
// TODO: why was this if needed?
|
2006-07-19 17:19:02 +00:00
|
|
|
c = UTEXT_PREVIOUS32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-07-19 17:19:02 +00:00
|
|
|
c = UTEXT_PREVIOUS32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
do {
|
2006-07-19 17:19:02 +00:00
|
|
|
c = UTEXT_PREVIOUS32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
}
|
2006-04-22 05:29:27 +00:00
|
|
|
while (c != U_SENTINEL && (category & 0x4000));
|
2006-03-23 00:54:12 +00:00
|
|
|
// Back up to the last dictionary character
|
2006-04-22 05:29:27 +00:00
|
|
|
if (c == U_SENTINEL) {
|
|
|
|
// c = fText->first32();
|
|
|
|
c = utext_current32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_next32(fText);
|
|
|
|
c = utext_current32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
2006-05-23 22:50:54 +00:00
|
|
|
rangeStart = (int32_t)UTEXT_GETNATIVEINDEX(fText);;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through the text, looking for ranges of dictionary characters.
|
|
|
|
// For each span, find the appropriate break engine, and ask it to find
|
|
|
|
// any breaks within the span.
|
2007-04-17 23:01:42 +00:00
|
|
|
// Note: we always do this in the forward direction, so that the break
|
|
|
|
// cache is built in the right order.
|
|
|
|
if (reverse) {
|
|
|
|
utext_setNativeIndex(fText, rangeStart);
|
2007-05-02 22:32:12 +00:00
|
|
|
c = utext_current32(fText);
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
2007-04-17 23:01:42 +00:00
|
|
|
}
|
2006-03-23 00:54:12 +00:00
|
|
|
while(U_SUCCESS(status)) {
|
2007-04-17 23:01:42 +00:00
|
|
|
while((current = (int32_t)UTEXT_GETNATIVEINDEX(fText)) < rangeEnd && (category & 0x4000) == 0) {
|
|
|
|
utext_next32(fText); // TODO: tweak for post-increment operation
|
|
|
|
c = utext_current32(fText);
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
2007-04-17 23:01:42 +00:00
|
|
|
if (current >= rangeEnd) {
|
|
|
|
break;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We now have a dictionary character. Get the appropriate language object
|
|
|
|
// to deal with it.
|
|
|
|
const LanguageBreakEngine *lbe = getLanguageBreakEngine(c);
|
|
|
|
|
|
|
|
// Ask the language object if there are any breaks. It will leave the text
|
|
|
|
// pointer on the other side of its range, ready to search for the next one.
|
|
|
|
if (lbe != NULL) {
|
2007-04-17 23:01:42 +00:00
|
|
|
foundBreakCount += lbe->findBreaks(fText, rangeStart, rangeEnd, FALSE, fBreakType, breaks);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reload the loop variables for the next go-round
|
2006-04-22 05:29:27 +00:00
|
|
|
c = utext_current32(fText);
|
2006-03-23 00:54:12 +00:00
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found breaks, build a new break cache. The first and last entries must
|
|
|
|
// be the original starting and ending position.
|
|
|
|
if (foundBreakCount > 0) {
|
2014-02-10 20:12:03 +00:00
|
|
|
U_ASSERT(foundBreakCount == breaks.size());
|
2006-03-23 00:54:12 +00:00
|
|
|
int32_t totalBreaks = foundBreakCount;
|
|
|
|
if (startPos < breaks.elementAti(0)) {
|
|
|
|
totalBreaks += 1;
|
|
|
|
}
|
|
|
|
if (endPos > breaks.peeki()) {
|
|
|
|
totalBreaks += 1;
|
|
|
|
}
|
|
|
|
fCachedBreakPositions = (int32_t *)uprv_malloc(totalBreaks * sizeof(int32_t));
|
|
|
|
if (fCachedBreakPositions != NULL) {
|
|
|
|
int32_t out = 0;
|
|
|
|
fNumCachedBreakPositions = totalBreaks;
|
|
|
|
if (startPos < breaks.elementAti(0)) {
|
|
|
|
fCachedBreakPositions[out++] = startPos;
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < foundBreakCount; ++i) {
|
|
|
|
fCachedBreakPositions[out++] = breaks.elementAti(i);
|
|
|
|
}
|
|
|
|
if (endPos > fCachedBreakPositions[out-1]) {
|
|
|
|
fCachedBreakPositions[out] = endPos;
|
|
|
|
}
|
|
|
|
// If there are breaks, then by definition, we are replacing the original
|
|
|
|
// proposed break by one of the breaks we found. Use following() and
|
|
|
|
// preceding() to do the work. They should never recurse in this case.
|
|
|
|
if (reverse) {
|
2012-08-16 23:01:49 +00:00
|
|
|
return preceding(endPos);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return following(startPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the allocation failed, just fall through to the "no breaks found" case.
|
|
|
|
}
|
|
|
|
|
2007-04-17 23:01:42 +00:00
|
|
|
// If we get here, there were no language-based breaks. Set the text pointer
|
|
|
|
// to the original proposed break.
|
2006-04-22 05:29:27 +00:00
|
|
|
utext_setNativeIndex(fText, reverse ? startPos : endPos);
|
2006-03-23 00:54:12 +00:00
|
|
|
return (reverse ? startPos : endPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
|
2011-07-06 04:03:35 +00:00
|
|
|
static icu::UStack *gLanguageBreakFactories = NULL;
|
2013-09-10 00:34:12 +00:00
|
|
|
static icu::UInitOnce gLanguageBreakFactoriesInitOnce = U_INITONCE_INITIALIZER;
|
2006-09-03 17:08:23 +00:00
|
|
|
|
2006-03-23 00:54:12 +00:00
|
|
|
/**
|
|
|
|
* Release all static memory held by breakiterator.
|
|
|
|
*/
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool U_CALLCONV breakiterator_cleanup_dict(void) {
|
|
|
|
if (gLanguageBreakFactories) {
|
|
|
|
delete gLanguageBreakFactories;
|
|
|
|
gLanguageBreakFactories = NULL;
|
|
|
|
}
|
2013-06-01 03:37:16 +00:00
|
|
|
gLanguageBreakFactoriesInitOnce.reset();
|
2006-03-23 00:54:12 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void U_CALLCONV _deleteFactory(void *obj) {
|
2011-07-06 04:03:35 +00:00
|
|
|
delete (icu::LanguageBreakFactory *) obj;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2013-06-01 03:37:16 +00:00
|
|
|
static void U_CALLCONV initLanguageFactories() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
U_ASSERT(gLanguageBreakFactories == NULL);
|
|
|
|
gLanguageBreakFactories = new UStack(_deleteFactory, NULL, status);
|
|
|
|
if (gLanguageBreakFactories != NULL && U_SUCCESS(status)) {
|
|
|
|
ICULanguageBreakFactory *builtIn = new ICULanguageBreakFactory(status);
|
|
|
|
gLanguageBreakFactories->push(builtIn, status);
|
2006-03-23 00:54:12 +00:00
|
|
|
#ifdef U_LOCAL_SERVICE_HOOK
|
2013-06-01 03:37:16 +00:00
|
|
|
LanguageBreakFactory *extra = (LanguageBreakFactory *)uprv_svc_hook("languageBreakFactory", &status);
|
|
|
|
if (extra != NULL) {
|
|
|
|
gLanguageBreakFactories->push(extra, status);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
2013-06-01 03:37:16 +00:00
|
|
|
#endif
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
2013-06-01 03:37:16 +00:00
|
|
|
ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR_DICT, breakiterator_cleanup_dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const LanguageBreakEngine*
|
|
|
|
getLanguageBreakEngineFromFactory(UChar32 c, int32_t breakType)
|
|
|
|
{
|
|
|
|
umtx_initOnce(gLanguageBreakFactoriesInitOnce, &initLanguageFactories);
|
2006-03-23 00:54:12 +00:00
|
|
|
if (gLanguageBreakFactories == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t i = gLanguageBreakFactories->size();
|
|
|
|
const LanguageBreakEngine *lbe = NULL;
|
|
|
|
while (--i >= 0) {
|
|
|
|
LanguageBreakFactory *factory = (LanguageBreakFactory *)(gLanguageBreakFactories->elementAt(i));
|
|
|
|
lbe = factory->getEngineFor(c, breakType);
|
|
|
|
if (lbe != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lbe;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// getLanguageBreakEngine Find an appropriate LanguageBreakEngine for the
|
2012-08-16 23:01:49 +00:00
|
|
|
// the character c.
|
2006-03-23 00:54:12 +00:00
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
const LanguageBreakEngine *
|
|
|
|
RuleBasedBreakIterator::getLanguageBreakEngine(UChar32 c) {
|
|
|
|
const LanguageBreakEngine *lbe = NULL;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
if (fLanguageBreakEngines == NULL) {
|
|
|
|
fLanguageBreakEngines = new UStack(status);
|
2008-01-14 18:52:26 +00:00
|
|
|
if (fLanguageBreakEngines == NULL || U_FAILURE(status)) {
|
2006-03-23 00:54:12 +00:00
|
|
|
delete fLanguageBreakEngines;
|
|
|
|
fLanguageBreakEngines = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t i = fLanguageBreakEngines->size();
|
|
|
|
while (--i >= 0) {
|
|
|
|
lbe = (const LanguageBreakEngine *)(fLanguageBreakEngines->elementAt(i));
|
|
|
|
if (lbe->handles(c, fBreakType)) {
|
|
|
|
return lbe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No existing dictionary took the character. See if a factory wants to
|
|
|
|
// give us a new LanguageBreakEngine for this character.
|
|
|
|
lbe = getLanguageBreakEngineFromFactory(c, fBreakType);
|
|
|
|
|
|
|
|
// If we got one, use it and push it on our stack.
|
|
|
|
if (lbe != NULL) {
|
|
|
|
fLanguageBreakEngines->push((void *)lbe, status);
|
|
|
|
// Even if we can't remember it, we can keep looking it up, so
|
|
|
|
// return it even if the push fails.
|
|
|
|
return lbe;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No engine is forthcoming for this character. Add it to the
|
|
|
|
// reject set. Create the reject break engine if needed.
|
|
|
|
if (fUnhandledBreakEngine == NULL) {
|
|
|
|
fUnhandledBreakEngine = new UnhandledEngine(status);
|
|
|
|
if (U_SUCCESS(status) && fUnhandledBreakEngine == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
// Put it last so that scripts for which we have an engine get tried
|
|
|
|
// first.
|
|
|
|
fLanguageBreakEngines->insertElementAt(fUnhandledBreakEngine, 0, status);
|
|
|
|
// If we can't insert it, or creation failed, get rid of it
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete fUnhandledBreakEngine;
|
|
|
|
fUnhandledBreakEngine = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the reject engine about the character; at its discretion, it may
|
|
|
|
// add more than just the one character.
|
|
|
|
fUnhandledBreakEngine->handleCharacter(c, fBreakType);
|
|
|
|
|
|
|
|
return fUnhandledBreakEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-23 05:51:28 +00:00
|
|
|
|
2006-05-26 00:57:09 +00:00
|
|
|
/*int32_t RuleBasedBreakIterator::getBreakType() const {
|
2006-03-23 00:54:12 +00:00
|
|
|
return fBreakType;
|
2006-05-26 00:57:09 +00:00
|
|
|
}*/
|
2006-03-23 00:54:12 +00:00
|
|
|
|
|
|
|
void RuleBasedBreakIterator::setBreakType(int32_t type) {
|
|
|
|
fBreakType = type;
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
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 */
|