1999-10-27 16:34:57 +00:00
|
|
|
/*
|
2003-06-02 22:21:58 +00:00
|
|
|
***************************************************************************
|
2004-05-18 22:01:41 +00:00
|
|
|
* Copyright (C) 1999-2004 International Business Machines Corporation *
|
2003-06-02 22:21:58 +00:00
|
|
|
* and others. All rights reserved. *
|
|
|
|
***************************************************************************
|
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
|
|
|
|
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"
|
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"
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-07-22 23:24:55 +00:00
|
|
|
#include "uassert.h"
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
static const int16_t START_STATE = 1; // The state number of the starting state
|
2002-08-08 00:39:13 +00:00
|
|
|
static const int16_t STOP_STATE = 0; // The state-transition value indicating "stop"
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2003-08-22 23:26:53 +00:00
|
|
|
u_init(&status); // Just in case ICU is not yet initialized
|
2002-06-25 17:23:07 +00:00
|
|
|
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 *)
|
|
|
|
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() {
|
2002-06-25 17:23:07 +00:00
|
|
|
delete fText;
|
2002-08-08 00:39:13 +00:00
|
|
|
fText = NULL;
|
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
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
delete fText;
|
|
|
|
fText = NULL;
|
|
|
|
if (that.fText != NULL) {
|
|
|
|
fText = that.fText->clone();
|
|
|
|
}
|
2000-01-08 02:05:05 +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();
|
|
|
|
}
|
|
|
|
fTrace = that.fTrace;
|
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
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
UBool RuleBasedBreakIterator::fTrace = FALSE;
|
|
|
|
void RuleBasedBreakIterator::init() {
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
fText = NULL;
|
|
|
|
fData = NULL;
|
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
|
|
|
fDictionaryCharCount = 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 {
|
2002-09-05 18:51:50 +00:00
|
|
|
UBool r = FALSE;
|
|
|
|
if (that.getDynamicClassID() != getDynamicClassID()) {
|
|
|
|
return r;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2002-09-05 18:51:50 +00:00
|
|
|
const RuleBasedBreakIterator& that2 = (const RuleBasedBreakIterator&) that;
|
|
|
|
if (fText == that2.fText ||
|
|
|
|
(fText != NULL && that2.fText != NULL && *that2.fText == *fText)) {
|
2004-04-16 01:15:04 +00:00
|
|
|
if (that2.fData == fData ||
|
2002-09-05 18:51:50 +00:00
|
|
|
(fData != NULL && that2.fData != NULL && *that2.fData == *fData)) {
|
|
|
|
r = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
return r;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a CharacterIterator over the text being analyzed. This version
|
|
|
|
* of this method returns the actual CharacterIterator we're using internally.
|
|
|
|
* Changing the state of this iterator can have undefined consequences. If
|
|
|
|
* you need to change it, clone it first.
|
|
|
|
* @return An iterator over the text being analyzed.
|
|
|
|
*/
|
|
|
|
const CharacterIterator&
|
|
|
|
RuleBasedBreakIterator::getText() const {
|
|
|
|
RuleBasedBreakIterator* nonConstThis = (RuleBasedBreakIterator*)this;
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
// The iterator is initialized pointing to no text at all, so if this
|
|
|
|
// function is called while we're in that state, we have to fudge an
|
|
|
|
// an iterator to return.
|
2002-08-09 21:39:02 +00:00
|
|
|
if (nonConstThis->fText == NULL) {
|
|
|
|
// TODO: do this in a way that does not do a default conversion!
|
2002-06-25 17:23:07 +00:00
|
|
|
nonConstThis->fText = new StringCharacterIterator("");
|
2002-11-30 04:41:53 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
return *nonConstThis->fText;
|
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) {
|
|
|
|
reset();
|
2002-06-25 17:23:07 +00:00
|
|
|
delete fText;
|
|
|
|
fText = newText;
|
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) {
|
|
|
|
reset();
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText != NULL && fText->getDynamicClassID()
|
2000-01-08 02:05:05 +00:00
|
|
|
== StringCharacterIterator::getStaticClassID()) {
|
2002-06-25 17:23:07 +00:00
|
|
|
((StringCharacterIterator*)fText)->setText(newText);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-06-25 17:23:07 +00:00
|
|
|
delete fText;
|
|
|
|
fText = new StringCharacterIterator(newText);
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
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
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the current iteration position to the beginning of the text.
|
|
|
|
* (i.e., the CharacterIterator's starting offset).
|
|
|
|
* @return The offset of the beginning of the text.
|
|
|
|
*/
|
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;
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText == NULL)
|
2000-01-08 02:05:05 +00:00
|
|
|
return BreakIterator::DONE;
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->first();
|
|
|
|
return fText->getIndex();
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current iteration position to the end of the text.
|
|
|
|
* (i.e., the CharacterIterator's ending offset).
|
|
|
|
* @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
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// I'm not sure why, but t.last() returns the offset of the last character,
|
|
|
|
// rather than the past-the-end offset
|
2002-07-22 22:02:08 +00:00
|
|
|
//
|
|
|
|
// (It's so a loop like for(p=it.last(); p!=DONE; p=it.previous()) ...
|
2002-08-01 21:07:56 +00:00
|
|
|
// will work correctly.)
|
2002-07-22 22:02:08 +00:00
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastStatusIndexValid = FALSE;
|
2002-06-25 17:23:07 +00:00
|
|
|
int32_t pos = fText->endIndex();
|
|
|
|
fText->setIndex(pos);
|
2002-07-22 22:02:08 +00:00
|
|
|
|
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) {
|
|
|
|
result = handleNext();
|
|
|
|
--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) {
|
1999-10-27 16:34:57 +00:00
|
|
|
return handleNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
1999-10-27 16:34:57 +00:00
|
|
|
// if we're already sitting at the beginning of the text, return DONE
|
2002-07-22 22:02:08 +00:00
|
|
|
if (fText == NULL || current() == fText->startIndex()) {
|
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) {
|
|
|
|
return handlePrevious(fData->fReverseTable);
|
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
|
|
|
|
|
|
|
fText->previous32();
|
2003-11-07 22:49:38 +00:00
|
|
|
int32_t lastResult = handlePrevious();
|
2003-11-07 02:02:06 +00:00
|
|
|
int32_t result = lastResult;
|
|
|
|
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 (;;) {
|
2003-11-07 02:02:06 +00:00
|
|
|
result = handleNext();
|
|
|
|
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
|
|
|
|
// handleNext()s to move up to the desired return position, we will have a valid
|
|
|
|
// tag value. But, if handlePrevious() took us to exactly the correct result positon,
|
|
|
|
// we wont have a tag value for that position, which is only set by handleNext().
|
|
|
|
|
|
|
|
// set the current iteration position to be the last break position
|
|
|
|
// before where we started, and then return that value
|
|
|
|
fText->setIndex(lastResult);
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = lastTag; // for use by getRuleStatus()
|
|
|
|
fLastStatusIndexValid = breakTagValid;
|
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) {
|
|
|
|
// if the offset passed in is already past the end of the text,
|
2000-01-08 02:05:05 +00:00
|
|
|
// just return DONE; if it's before the beginning, return the
|
|
|
|
// text's starting offset
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
|
|
|
fLastStatusIndexValid = TRUE;
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText == NULL || offset >= fText->endIndex()) {
|
2002-08-08 00:39:13 +00:00
|
|
|
last();
|
|
|
|
return next();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
else if (offset < fText->startIndex()) {
|
2002-08-08 00:39:13 +00:00
|
|
|
return first();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
|
|
|
// otherwise, set our internal iteration position (temporarily)
|
|
|
|
// 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
|
2004-04-16 01:15:04 +00:00
|
|
|
/// todo synwee
|
2003-11-09 06:52:44 +00:00
|
|
|
fText->setIndex(offset);
|
|
|
|
// move forward one codepoint to prepare for moving back to a
|
|
|
|
// safe point.
|
|
|
|
// this handles offset being between a supplementary character
|
|
|
|
fText->next32();
|
|
|
|
// 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
|
2003-11-07 02:02:06 +00:00
|
|
|
fText->setIndex(offset);
|
2003-11-09 06:52:44 +00:00
|
|
|
fText->previous32();
|
|
|
|
// 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
|
|
|
|
// us up to a known break position before the specified position (if
|
|
|
|
// 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
|
|
|
|
|
|
|
|
fText->setIndex(offset);
|
|
|
|
if (offset == fText->startIndex()) {
|
|
|
|
return handleNext();
|
|
|
|
}
|
|
|
|
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) {
|
2000-01-08 02:05:05 +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
|
2004-03-05 05:04:10 +00:00
|
|
|
|
2000-01-08 02:05:05 +00:00
|
|
|
// text's starting offset
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText == NULL || offset > fText->endIndex()) {
|
2002-08-08 00:39:13 +00:00
|
|
|
// return BreakIterator::DONE;
|
|
|
|
return last();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
else if (offset < fText->startIndex()) {
|
2002-08-08 00:39:13 +00:00
|
|
|
return first();
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
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
|
|
|
/// todo synwee
|
|
|
|
// new rule syntax
|
2003-11-09 06:52:44 +00:00
|
|
|
fText->setIndex(offset);
|
|
|
|
// move backwards one codepoint to prepare for moving forwards to a
|
|
|
|
// safe point.
|
|
|
|
// this handles offset being between a supplementary character
|
2004-05-18 18:23:22 +00:00
|
|
|
// TODO: would it be better to just check for being in the middle of a surrogate pair,
|
|
|
|
// rather than adjusting the position unconditionally?
|
|
|
|
// (Change would interact with safe rules.)
|
2003-11-09 06:52:44 +00:00
|
|
|
fText->previous32();
|
|
|
|
handleNext(fData->fSafeFwdTable);
|
2004-05-18 18:23:22 +00:00
|
|
|
int32_t result = fText->getIndex();
|
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
|
|
|
|
fText->setIndex(offset);
|
|
|
|
fText->next32();
|
|
|
|
// 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
|
|
|
|
fText->setIndex(offset);
|
|
|
|
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
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fText == NULL || offset == fText->startIndex()) {
|
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
|
|
|
}
|
|
|
|
|
2003-11-09 06:52:44 +00:00
|
|
|
if (offset == fText->endIndex()) {
|
|
|
|
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
|
2002-08-08 00:39:13 +00:00
|
|
|
if (offset < fText->startIndex()) {
|
|
|
|
first(); // For side effects on current position, tag values.
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset > fText->endIndex()) {
|
|
|
|
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
|
2002-08-08 00:39:13 +00:00
|
|
|
return following(offset - 1) == offset;
|
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 {
|
2002-06-25 17:23:07 +00:00
|
|
|
return (fText != NULL) ? fText->getIndex() : BreakIterator::DONE;
|
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
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// handleNext()
|
|
|
|
// This method is the actual implementation of the next() method. All iteration
|
|
|
|
// vectors through here. 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
|
2003-10-21 21:47:16 +00:00
|
|
|
// value every time the state machine passes through an accepting state.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------------
|
2003-11-09 06:52:44 +00:00
|
|
|
int32_t RuleBasedBreakIterator::handleNext() {
|
|
|
|
return handleNext(fData->fForwardTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RuleBasedBreakIterator::handleNext(const RBBIStateTable *statetable) {
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fTrace) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("Handle Next pos char state category \n");
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
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;
|
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.
|
2003-10-21 21:47:16 +00:00
|
|
|
if (fText == NULL || fData == NULL || fText->hasNext() == FALSE) {
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
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-10-21 21:47:16 +00:00
|
|
|
int32_t initialPosition = fText->getIndex();
|
|
|
|
int32_t result = initialPosition;
|
2000-01-08 02:05:05 +00:00
|
|
|
int32_t lookaheadResult = 0;
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2002-08-08 00:39:13 +00:00
|
|
|
// Initialize the state machine. Begin in state 1
|
2002-06-27 01:50:22 +00:00
|
|
|
int32_t state = START_STATE;
|
2002-06-25 17:23:07 +00:00
|
|
|
int16_t category;
|
2002-06-27 21:14:47 +00:00
|
|
|
UChar32 c = fText->current32();
|
2002-06-25 17:23:07 +00:00
|
|
|
RBBIStateTableRow *row;
|
|
|
|
int32_t lookaheadStatus = 0;
|
2004-03-05 05:04:10 +00:00
|
|
|
int32_t lookaheadTagIdx = 0;
|
2002-06-27 01:50:22 +00:00
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = 0;
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-08-08 00:39:13 +00:00
|
|
|
row = (RBBIStateTableRow *) // Point to starting row of state table.
|
2003-11-09 06:52:44 +00:00
|
|
|
(statetable->fTableData + (statetable->fRowLen * state));
|
2002-08-08 00:39:13 +00:00
|
|
|
|
|
|
|
// Character Category fetch for starting character.
|
|
|
|
// See comments on character category code within loop, below.
|
2002-06-25 17:23:07 +00:00
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
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
|
|
|
|
for (;;) {
|
2002-07-31 19:05:33 +00:00
|
|
|
if (c == CharacterIterator::DONE && fText->hasNext()==FALSE) {
|
2003-10-21 21:47:16 +00:00
|
|
|
// Reached end of input string.
|
|
|
|
// Note: CharacterIterator::DONE is 0xffff, which is also a legal
|
|
|
|
// character value. Check for DONE first, because it's quicker,
|
|
|
|
// but also need to check fText->hasNext() to be certain.
|
|
|
|
|
|
|
|
if (lookaheadResult > result) {
|
|
|
|
// We ran off the end of the string with a pending look-ahead match.
|
|
|
|
// Treat this as if the look-ahead condition had been met, and return
|
|
|
|
// the match at the / position from the look-ahead rule.
|
2004-03-05 05:04:10 +00:00
|
|
|
result = lookaheadResult;
|
|
|
|
fLastRuleStatusIndex = lookaheadTagIdx;
|
2003-10-21 21:47:16 +00:00
|
|
|
lookaheadStatus = 0;
|
|
|
|
} else if (result == initialPosition) {
|
|
|
|
// Ran off end, no match found.
|
2003-11-11 21:24:09 +00:00
|
|
|
// move forward one
|
|
|
|
fText->setIndex(initialPosition);
|
|
|
|
fText->next32();
|
|
|
|
fText->getIndex();
|
2003-10-21 21:47:16 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// 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,
|
2003-10-21 21:47:16 +00:00
|
|
|
// not the size of the character going in, which is a UChar32.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
|
|
|
|
// Check the dictionary bit in the character's category.
|
2002-08-08 00:39:13 +00:00
|
|
|
// 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.
|
2002-06-25 17:23:07 +00:00
|
|
|
//
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
2002-08-08 00:39:13 +00:00
|
|
|
// And off the dictionary flag bit.
|
2002-06-25 17:23:07 +00:00
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fTrace) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf(" %4d ", fText->getIndex());
|
2002-06-25 17:23:07 +00:00
|
|
|
if (0x20<=c && c<0x7f) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\"%c\" ", c);
|
2002-06-25 17:23:07 +00:00
|
|
|
} else {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%5x ", c);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%3d %3d\n", state, category);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
// look up a state transition in the state table
|
|
|
|
state = row->fNextState[category];
|
|
|
|
row = (RBBIStateTableRow *)
|
2003-11-09 06:52:44 +00:00
|
|
|
(statetable->fTableData + (statetable->fRowLen * state));
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
// Get the next character. Doing it here positions the iterator
|
|
|
|
// to the correct position for recording matches in the code that
|
|
|
|
// follows.
|
|
|
|
c = fText->next32();
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fAccepting == -1) {
|
|
|
|
// Match found, common case, could have lookahead so we move on to check it
|
|
|
|
result = fText->getIndex();
|
|
|
|
/// added
|
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
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fLookAhead != 0) {
|
2004-04-16 01:15:04 +00:00
|
|
|
if (lookaheadStatus != 0
|
|
|
|
&& row->fAccepting == lookaheadStatus) {
|
2003-11-06 19:45:57 +00:00
|
|
|
// Lookahead match is completed. Set the result accordingly, but only
|
|
|
|
// if no other rule has matched further in the mean time.
|
2004-03-05 05:04:10 +00:00
|
|
|
result = lookaheadResult;
|
|
|
|
fLastRuleStatusIndex = lookaheadTagIdx;
|
|
|
|
lookaheadStatus = 0;
|
2003-11-05 23:50:39 +00:00
|
|
|
/// i think we have to back up to read the lookahead character again
|
2003-11-06 19:45:57 +00:00
|
|
|
/// fText->setIndex(lookaheadResult);
|
2003-11-05 23:50:39 +00:00
|
|
|
/// TODO: this is a simple hack since reverse rules only have simple
|
|
|
|
/// lookahead rules that we can definitely break out from.
|
|
|
|
/// we need to make the lookahead rules not chain eventually.
|
2003-11-06 19:45:57 +00:00
|
|
|
/// return result;
|
|
|
|
/// this is going to be the longest match again
|
|
|
|
goto continueOn;
|
2003-11-05 23:50:39 +00:00
|
|
|
}
|
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
int32_t r = fText->getIndex();
|
|
|
|
lookaheadResult = r;
|
|
|
|
lookaheadStatus = row->fLookAhead;
|
2004-03-05 05:04:10 +00:00
|
|
|
lookaheadTagIdx = row->fTagIdx;
|
2003-11-05 23:50:39 +00:00
|
|
|
goto continueOn;
|
|
|
|
}
|
|
|
|
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fAccepting == 0) {
|
|
|
|
// No match, nothing of interest happening, common case.
|
2002-06-25 17:23:07 +00:00
|
|
|
goto continueOn;
|
2000-01-08 02:05:05 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
lookaheadStatus = 0; // clear out any pending look-ahead matches.
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
continueOn:
|
|
|
|
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
|
|
|
}
|
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) {
|
|
|
|
result = fText->setIndex(initialPosition);
|
|
|
|
fText ->next32();
|
|
|
|
result = fText->getIndex();
|
|
|
|
}
|
2000-01-08 02:05:05 +00:00
|
|
|
|
2003-10-21 21:47:16 +00:00
|
|
|
// Leave the iterator at our result position.
|
2002-06-25 17:23:07 +00:00
|
|
|
fText->setIndex(result);
|
|
|
|
if (fTrace) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("result = %d\n\n", result);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-04-16 01:15:04 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// handlePrevious(void) This is the variant used with old style rules
|
|
|
|
// (Overshoot to a safe point, then move forward)
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------
|
2003-11-07 02:02:06 +00:00
|
|
|
int32_t RuleBasedBreakIterator::handlePrevious(void) {
|
|
|
|
if (fText == NULL || fData == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fData->fReverseTable == NULL) {
|
|
|
|
return fText->setToStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t state = START_STATE;
|
|
|
|
int32_t category;
|
|
|
|
int32_t lastCategory = 0;
|
|
|
|
int32_t result = fText->getIndex();
|
|
|
|
int32_t lookaheadStatus = 0;
|
|
|
|
int32_t lookaheadResult = 0;
|
2004-03-05 05:04:10 +00:00
|
|
|
int32_t lookaheadTagIdx = 0;
|
2003-11-07 02:02:06 +00:00
|
|
|
UChar32 c = fText->current32();
|
|
|
|
RBBIStateTableRow *row;
|
|
|
|
|
|
|
|
row = (RBBIStateTableRow *)
|
|
|
|
(this->fData->fReverseTable->fTableData + (state * fData->fReverseTable->fRowLen));
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fTrace) {
|
|
|
|
RBBIDebugPrintf("Handle Prev pos char state category \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop until we reach the beginning of the text or transition to state 0
|
|
|
|
for (;;) {
|
|
|
|
if (c == CharacterIterator::DONE && fText->hasPrevious()==FALSE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the last character's category and look up the current
|
|
|
|
// character's category
|
|
|
|
lastCategory = category;
|
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
|
|
|
|
// Check the dictionary bit in the character's category.
|
|
|
|
// Counter is only used by dictionary based iterators.
|
|
|
|
//
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fTrace) {
|
|
|
|
RBBIDebugPrintf(" %4d ", fText->getIndex());
|
|
|
|
if (0x20<=c && c<0x7f) {
|
|
|
|
RBBIDebugPrintf("\"%c\" ", c);
|
|
|
|
} else {
|
|
|
|
RBBIDebugPrintf("%5x ", c);
|
|
|
|
}
|
|
|
|
RBBIDebugPrintf("%3d %3d\n", state, category);
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up a state transition in the backwards state table
|
|
|
|
state = row->fNextState[category];
|
|
|
|
row = (RBBIStateTableRow *)
|
|
|
|
(this->fData->fReverseTable->fTableData + (state * fData->fReverseTable->fRowLen));
|
|
|
|
|
|
|
|
if (row->fAccepting == 0 && row->fLookAhead == 0) {
|
|
|
|
// No match, nothing of interest happening, common case.
|
|
|
|
goto continueOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row->fAccepting == -1) {
|
|
|
|
// Match found, common case, no lookahead involved.
|
|
|
|
result = fText->getIndex();
|
|
|
|
lookaheadStatus = 0; // clear out any pending look-ahead matches.
|
|
|
|
goto continueOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row->fAccepting == 0 && row->fLookAhead != 0) {
|
|
|
|
// Lookahead match point. Remember it, but only if no other rule
|
|
|
|
// has unconditionally matched to this point.
|
|
|
|
// TODO: handle case where there's a pending match from a different rule
|
|
|
|
// where lookaheadStatus != 0 && lookaheadStatus != row->fLookAhead.
|
|
|
|
int32_t r = fText->getIndex();
|
|
|
|
if (r > result) {
|
|
|
|
lookaheadResult = r;
|
|
|
|
lookaheadStatus = row->fLookAhead;
|
2004-03-05 05:04:10 +00:00
|
|
|
lookaheadTagIdx = row->fTagIdx;
|
2003-11-07 02:02:06 +00:00
|
|
|
}
|
|
|
|
goto continueOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row->fAccepting != 0 && row->fLookAhead != 0) {
|
|
|
|
// Lookahead match is completed. Set the result accordingly, but only
|
|
|
|
// if no other rule has matched further in the mean time.
|
|
|
|
if (lookaheadResult > result) {
|
|
|
|
U_ASSERT(row->fAccepting == lookaheadStatus); // TODO: handle this case
|
|
|
|
// of overlapping lookahead matches.
|
2004-03-05 05:04:10 +00:00
|
|
|
result = lookaheadResult;
|
|
|
|
fLastRuleStatusIndex = lookaheadTagIdx;
|
2003-11-07 02:02:06 +00:00
|
|
|
lookaheadStatus = 0;
|
|
|
|
}
|
|
|
|
goto continueOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
continueOn:
|
|
|
|
if (state == STOP_STATE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then advance one character backwards
|
|
|
|
c = fText->previous32();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: the result postion isn't what is returned to the user by previous(),
|
|
|
|
// but where the implementation of previous() turns around and
|
|
|
|
// starts iterating forward again.
|
|
|
|
if (c == CharacterIterator::DONE && fText->hasPrevious()==FALSE) {
|
|
|
|
result = fText->startIndex();
|
|
|
|
}
|
|
|
|
fText->setIndex(result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// handlePrevious()
|
|
|
|
//
|
|
|
|
// This method backs the iterator back up to a "safe position" in the text.
|
2003-11-05 23:50:39 +00:00
|
|
|
// This is a position that we know, without any context, may be any position
|
|
|
|
// not more than 2 breaks away. Occasionally, the position may be less than
|
|
|
|
// one break away.
|
2002-06-25 17:23:07 +00:00
|
|
|
// The various calling methods then iterate forward from this safe position to
|
2002-08-08 00:39:13 +00:00
|
|
|
// the appropriate position to return.
|
|
|
|
//
|
|
|
|
// 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) {
|
|
|
|
if (fText == NULL || statetable == NULL) {
|
2000-01-08 02:05:05 +00:00
|
|
|
return 0;
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2003-11-05 23:50:39 +00:00
|
|
|
// break tag is no longer valid after icu switched to exact backwards
|
|
|
|
// positioning.
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastStatusIndexValid = FALSE;
|
2003-11-09 06:52:44 +00:00
|
|
|
if (statetable == NULL) {
|
2002-06-25 17:23:07 +00:00
|
|
|
return fText->setToStart();
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2003-11-05 23:50:39 +00:00
|
|
|
int32_t state = START_STATE;
|
2002-06-25 17:23:07 +00:00
|
|
|
int32_t category;
|
2003-11-05 23:50:39 +00:00
|
|
|
int32_t lastCategory = 0;
|
2004-04-16 01:15:04 +00:00
|
|
|
UBool hasPassedStartText = !fText->hasPrevious();
|
2003-11-05 23:50:39 +00:00
|
|
|
UChar32 c = fText->previous32();
|
|
|
|
// previous character
|
2004-04-16 01:15:04 +00:00
|
|
|
int32_t result = fText->getIndex();
|
2003-12-04 02:12:42 +00:00
|
|
|
int32_t lookaheadStatus = 0;
|
|
|
|
int32_t lookaheadResult = 0;
|
2004-03-05 05:04:10 +00:00
|
|
|
int32_t lookaheadTagIdx = 0;
|
2003-12-04 02:12:42 +00:00
|
|
|
UBool lookAheadHardBreak = (statetable->fFlags & RBBI_LOOKAHEAD_HARD_BREAK) != 0;
|
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
RBBIStateTableRow *row;
|
|
|
|
|
|
|
|
row = (RBBIStateTableRow *)
|
2003-11-09 06:52:44 +00:00
|
|
|
(statetable->fTableData + (state * statetable->fRowLen));
|
2002-06-25 17:23:07 +00:00
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2002-06-25 17:23:07 +00:00
|
|
|
if (fTrace) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("Handle Prev pos char state category \n");
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// loop until we reach the beginning of the text or transition to state 0
|
2002-06-25 17:23:07 +00:00
|
|
|
for (;;) {
|
2003-11-05 23:50:39 +00:00
|
|
|
// if (c == CharacterIterator::DONE && fText->hasPrevious()==FALSE) {
|
2004-04-16 01:15:04 +00:00
|
|
|
if (hasPassedStartText) {
|
2003-11-05 23:50:39 +00:00
|
|
|
// if we have already considered the start of the text
|
2003-11-09 06:52:44 +00:00
|
|
|
if (row->fLookAhead != 0 && lookaheadResult == 0) {
|
2003-11-07 22:49:38 +00:00
|
|
|
result = 0;
|
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-10-27 16:34:57 +00:00
|
|
|
|
|
|
|
// save the last character's category and look up the current
|
|
|
|
// character's category
|
|
|
|
lastCategory = category;
|
2002-06-25 17:23:07 +00:00
|
|
|
UTRIE_GET16(&fData->fTrie, c, category);
|
|
|
|
|
|
|
|
// Check the dictionary bit in the character's category.
|
|
|
|
// Counter is only used by dictionary based iterators.
|
|
|
|
//
|
|
|
|
if ((category & 0x4000) != 0) {
|
|
|
|
fDictionaryCharCount++;
|
|
|
|
category &= ~0x4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fTrace) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf(" %4d ", fText->getIndex());
|
2002-06-25 17:23:07 +00:00
|
|
|
if (0x20<=c && c<0x7f) {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("\"%c\" ", c);
|
2002-06-25 17:23:07 +00:00
|
|
|
} else {
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%5x ", c);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-08-01 16:17:41 +00:00
|
|
|
RBBIDebugPrintf("%3d %3d\n", state, category);
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// look up a state transition in the backwards state table
|
|
|
|
state = row->fNextState[category];
|
|
|
|
row = (RBBIStateTableRow *)
|
2003-11-09 06:52:44 +00:00
|
|
|
(statetable->fTableData + (state * statetable->fRowLen));
|
2004-04-16 01:15:04 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fAccepting == -1) {
|
|
|
|
// Match found, common case, could have lookahead so we move on to check it
|
|
|
|
result = fText->getIndex();
|
|
|
|
/// added
|
2004-03-05 05:04:10 +00:00
|
|
|
fLastRuleStatusIndex = row->fTagIdx; // Remember the break status (tag) value.
|
2002-06-25 17:23:07 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
if (row->fLookAhead != 0) {
|
2004-04-16 01:15:04 +00:00
|
|
|
if (lookaheadStatus != 0
|
|
|
|
&& row->fAccepting == lookaheadStatus) {
|
2003-11-06 19:45:57 +00:00
|
|
|
// Lookahead match is completed. Set the result accordingly, but only
|
|
|
|
// if no other rule has matched further in the mean time.
|
2004-03-05 05:04:10 +00:00
|
|
|
result = lookaheadResult;
|
|
|
|
fLastRuleStatusIndex = lookaheadTagIdx;
|
|
|
|
lookaheadStatus = 0;
|
2003-11-05 23:50:39 +00:00
|
|
|
/// i think we have to back up to read the lookahead character again
|
2003-11-06 19:45:57 +00:00
|
|
|
/// fText->setIndex(lookaheadResult);
|
2003-11-05 23:50:39 +00:00
|
|
|
/// TODO: this is a simple hack since reverse rules only have simple
|
|
|
|
/// lookahead rules that we can definitely break out from.
|
|
|
|
/// we need to make the lookahead rules not chain eventually.
|
2003-11-06 19:45:57 +00:00
|
|
|
/// return result;
|
|
|
|
/// this is going to be the longest match again
|
2003-11-07 22:49:38 +00:00
|
|
|
|
|
|
|
/// syn wee todo hard coded for line breaks stuff
|
|
|
|
/// needs to provide a tag in rules to ensure a stop.
|
|
|
|
|
2003-12-04 02:12:42 +00:00
|
|
|
if (lookAheadHardBreak) {
|
2003-11-07 22:49:38 +00:00
|
|
|
fText->setIndex(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
category = lastCategory;
|
|
|
|
fText->setIndex(result);
|
2004-04-16 01:15:04 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
goto continueOn;
|
2003-11-05 23:50:39 +00:00
|
|
|
}
|
|
|
|
|
2004-03-05 05:04:10 +00:00
|
|
|
int32_t r = fText->getIndex();
|
|
|
|
lookaheadResult = r;
|
|
|
|
lookaheadStatus = row->fLookAhead;
|
|
|
|
fLastRuleStatusIndex = row->fTagIdx;
|
2003-11-05 23:50:39 +00:00
|
|
|
goto continueOn;
|
|
|
|
}
|
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
// not lookahead
|
|
|
|
if (row->fAccepting == 0) {
|
|
|
|
// No match, nothing of interest happening, common case.
|
2002-06-25 17:23:07 +00:00
|
|
|
goto continueOn;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
2003-11-06 19:45:57 +00:00
|
|
|
lookaheadStatus = 0; // clear out any pending look-ahead matches.
|
2002-06-25 17:23:07 +00:00
|
|
|
|
|
|
|
continueOn:
|
2004-04-16 01:15:04 +00:00
|
|
|
if (state == STOP_STATE) {
|
2002-06-25 17:23:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
1999-10-27 16:34:57 +00:00
|
|
|
// then advance one character backwards
|
2004-04-16 01:15:04 +00:00
|
|
|
hasPassedStartText = !fText->hasPrevious();
|
2002-06-25 17:23:07 +00:00
|
|
|
c = fText->previous32();
|
1999-10-27 16:34:57 +00:00
|
|
|
}
|
2002-06-27 21:14:47 +00:00
|
|
|
|
|
|
|
// Note: the result postion isn't what is returned to the user by previous(),
|
|
|
|
// but where the implementation of previous() turns around and
|
2002-06-25 17:23:07 +00:00
|
|
|
// starts iterating forward again.
|
2002-06-27 21:14:47 +00:00
|
|
|
fText->setIndex(result);
|
1999-10-27 16:34:57 +00:00
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
// Base-class version of this function is a no-op.
|
|
|
|
// Subclasses may override with their own reset behavior.
|
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.
|
|
|
|
if (fText == NULL || current() == fText->startIndex()) {
|
|
|
|
// 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();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2004-03-05 05:04:10 +00:00
|
|
|
U_ASSERT(fLastStatusIndexValid == TRUE);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// BufferClone TODO: In my (Andy) opinion, this function should be deprecated.
|
|
|
|
// Saving one heap allocation isn't worth the trouble.
|
|
|
|
// Cloning shouldn't be done in tight loops, and
|
|
|
|
// making the clone copy involves other heap operations anyway.
|
|
|
|
// And the application code for correctly dealing with buffer
|
|
|
|
// size problems and the eventual object destruction is ugly.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
2001-02-21 23:40:41 +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
|
|
|
//
|
2002-06-27 21:14:47 +00:00
|
|
|
// If user buffer size is zero this is a preflight operation to
|
2002-06-25 17:23:07 +00:00
|
|
|
// obtain the needed buffer size, allowing for worst case misalignment.
|
|
|
|
//
|
|
|
|
if (bufferSize == 0) {
|
|
|
|
bufferSize = sizeof(RuleBasedBreakIterator) + 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.
|
|
|
|
//
|
|
|
|
char *buf = (char *)stackBuffer;
|
2002-07-23 18:02:13 +00:00
|
|
|
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) {
|
2002-07-23 18:02:13 +00:00
|
|
|
uint32_t offsetUp = (uint32_t)U_ALIGNMENT_OFFSET_UP(buf);
|
2002-06-25 17:23:07 +00:00
|
|
|
s -= offsetUp;
|
|
|
|
buf += offsetUp;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
if (s < sizeof(RuleBasedBreakIterator)) {
|
|
|
|
buf = (char *) new RuleBasedBreakIterator;
|
|
|
|
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
|
|
|
|
|
|
|
//
|
|
|
|
// Clone the object.
|
|
|
|
// TODO: using an overloaded 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...
|
|
|
|
//
|
|
|
|
RuleBasedBreakIterator localIter; // Empty break iterator, source for memcpy
|
|
|
|
RuleBasedBreakIterator *clone = (RuleBasedBreakIterator *)buf;
|
|
|
|
uprv_memcpy(clone, &localIter, sizeof(RuleBasedBreakIterator)); // clone = empty, but initialized, iterator.
|
|
|
|
*clone = *this; // clone = the real one we want.
|
|
|
|
if (status != U_SAFECLONE_ALLOCATED_WARNING) {
|
|
|
|
clone->fBufferClone = TRUE;
|
2001-02-21 23:40:41 +00:00
|
|
|
}
|
2002-06-25 17:23:07 +00:00
|
|
|
|
2002-06-27 21:14:47 +00:00
|
|
|
return clone;
|
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.
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|