2001-08-25 02:03:53 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2011-03-08 03:04:44 +00:00
|
|
|
* Copyright (C) 2001-2011 IBM and others. All rights reserved.
|
2001-08-25 02:03:53 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 07/02/2001 synwee Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2008-06-20 22:53:25 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
|
2002-09-20 01:54:48 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
#include "unicode/usearch.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/uchar.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "normalizer2impl.h"
|
2001-08-25 02:03:53 +00:00
|
|
|
#include "ucol_imp.h"
|
|
|
|
#include "usrchimp.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cmemory.h"
|
2004-08-26 14:15:30 +00:00
|
|
|
#include "ucln_in.h"
|
2008-05-23 04:22:28 +00:00
|
|
|
#include "uassert.h"
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "ustr_imp.h"
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2008-06-06 21:51:25 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// don't use Boyer-Moore
|
2010-02-27 01:01:45 +00:00
|
|
|
// (and if we decide to turn this on again there are several new TODOs that will need to be addressed)
|
2008-05-23 04:22:28 +00:00
|
|
|
#define BOYER_MOORE 0
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2005-05-03 23:17:03 +00:00
|
|
|
#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// internal definition ---------------------------------------------------
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
#define LAST_BYTE_MASK_ 0xFF
|
|
|
|
#define SECOND_LAST_BYTE_SHIFT_ 8
|
|
|
|
#define SUPPLEMENTARY_MIN_VALUE_ 0x10000
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2008-10-22 19:50:07 +00:00
|
|
|
static const uint16_t *fcdTrieIndex = NULL;
|
|
|
|
static UChar32 fcdHighStart = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
// internal methods -------------------------------------------------
|
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
/**
|
|
|
|
* Fast collation element iterator setOffset.
|
|
|
|
* This function does not check for bounds.
|
|
|
|
* @param coleiter collation element iterator
|
|
|
|
* @param offset to set
|
|
|
|
*/
|
2009-04-22 23:10:07 +00:00
|
|
|
static
|
2002-08-26 16:52:05 +00:00
|
|
|
inline void setColEIterOffset(UCollationElements *elems,
|
|
|
|
int32_t offset)
|
|
|
|
{
|
2003-08-31 20:53:46 +00:00
|
|
|
collIterate *ci = &(elems->iteratordata_);
|
|
|
|
ci->pos = ci->string + offset;
|
2007-12-15 00:30:35 +00:00
|
|
|
ci->CEpos = ci->toReturn = ci->extendCEs ? ci->extendCEs : ci->CEs;
|
2003-08-31 20:53:46 +00:00
|
|
|
if (ci->flags & UCOL_ITER_INNORMBUF) {
|
|
|
|
ci->flags = ci->origFlags;
|
|
|
|
}
|
|
|
|
ci->fcdPosition = NULL;
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2011-03-22 03:50:26 +00:00
|
|
|
ci->offsetReturn = NULL;
|
2008-05-23 04:22:28 +00:00
|
|
|
ci->offsetStore = ci->offsetBuffer;
|
2011-03-22 03:50:26 +00:00
|
|
|
ci->offsetRepeatCount = ci->offsetRepeatValue = 0;
|
2002-08-26 16:52:05 +00:00
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
|
|
|
* Getting the mask for collation strength
|
|
|
|
* @param strength collation strength
|
|
|
|
* @return collation element mask
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline uint32_t getMask(UCollationStrength strength)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2009-04-22 23:10:07 +00:00
|
|
|
switch (strength)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
case UCOL_PRIMARY:
|
|
|
|
return UCOL_PRIMARYORDERMASK;
|
|
|
|
case UCOL_SECONDARY:
|
|
|
|
return UCOL_SECONDARYORDERMASK | UCOL_PRIMARYORDERMASK;
|
|
|
|
default:
|
2009-04-22 23:10:07 +00:00
|
|
|
return UCOL_TERTIARYORDERMASK | UCOL_SECONDARYORDERMASK |
|
2001-08-25 02:03:53 +00:00
|
|
|
UCOL_PRIMARYORDERMASK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is to squeeze the 21bit ces into a 256 table
|
|
|
|
* @param ce collation element
|
|
|
|
* @return collapsed version of the collation element
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int hash(uint32_t ce)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
// the old value UCOL_PRIMARYORDER(ce) % MAX_TABLE_SIZE_ does not work
|
|
|
|
// well with the new collation where most of the latin 1 characters
|
|
|
|
// are of the value xx000xxx. their hashes will most of the time be 0
|
|
|
|
// to be discussed on the hash algo.
|
2001-09-07 21:56:18 +00:00
|
|
|
return UCOL_PRIMARYORDER(ce) % MAX_TABLE_SIZE_;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 23:07:29 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool U_CALLCONV
|
|
|
|
usearch_cleanup(void) {
|
2008-10-22 19:50:07 +00:00
|
|
|
fcdTrieIndex = NULL;
|
2004-09-12 23:07:29 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
2001-11-09 17:39:42 +00:00
|
|
|
* Initializing the fcd tables.
|
|
|
|
* Internal method, status assumed to be a success.
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2001-11-09 17:39:42 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
2001-08-25 02:03:53 +00:00
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline void initializeFCD(UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2008-10-22 19:50:07 +00:00
|
|
|
if (fcdTrieIndex == NULL) {
|
|
|
|
fcdTrieIndex = unorm_getFCDTrieIndex(fcdHighStart, status);
|
2004-09-12 23:07:29 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_USEARCH, usearch_cleanup);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the fcd value for a character at the argument index.
|
|
|
|
* This method takes into accounts of the supplementary characters.
|
|
|
|
* @param str UTF16 string where character for fcd retrieval resides
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param offset position of the character whose fcd is to be retrieved, to be
|
|
|
|
* overwritten with the next character position, taking
|
2001-08-25 02:03:53 +00:00
|
|
|
* surrogate characters into consideration.
|
|
|
|
* @param strlength length of the argument string
|
|
|
|
* @return fcd value
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
uint16_t getFCD(const UChar *str, int32_t *offset,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t strlength)
|
|
|
|
{
|
2008-10-22 19:50:07 +00:00
|
|
|
const UChar *temp = str + *offset;
|
|
|
|
uint16_t result = unorm_nextFCD16(fcdTrieIndex, fcdHighStart, temp, str + strlength);
|
|
|
|
*offset = (int32_t)(temp - str);
|
2001-08-25 02:03:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Getting the modified collation elements taking into account the collation
|
2001-08-25 02:03:53 +00:00
|
|
|
* attributes
|
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param sourcece
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return the modified collation element
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2004-04-01 22:04:52 +00:00
|
|
|
inline int32_t getCE(const UStringSearch *strsrch, uint32_t sourcece)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
// note for tertiary we can't use the collator->tertiaryMask, that
|
|
|
|
// is a preprocessed mask that takes into account case options. since
|
|
|
|
// we are only concerned with exact matches, we don't need that.
|
|
|
|
sourcece &= strsrch->ceMask;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
if (strsrch->toShift) {
|
|
|
|
// alternate handling here, since only the 16 most significant digits
|
|
|
|
// is only used, we can safely do a compare without masking
|
|
|
|
// if the ce is a variable, we mask and get only the primary values
|
|
|
|
// no shifting to quartenary is required since all primary values
|
|
|
|
// less than variabletop will need to be masked off anyway.
|
|
|
|
if (strsrch->variableTop > sourcece) {
|
2009-02-03 19:50:39 +00:00
|
|
|
if (strsrch->strength >= UCOL_QUATERNARY) {
|
2001-08-25 02:03:53 +00:00
|
|
|
sourcece &= UCOL_PRIMARYORDERMASK;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
else {
|
2001-08-25 02:03:53 +00:00
|
|
|
sourcece = UCOL_IGNORABLE;
|
|
|
|
}
|
|
|
|
}
|
2009-02-03 19:50:39 +00:00
|
|
|
} else if (strsrch->strength >= UCOL_QUATERNARY && sourcece == UCOL_IGNORABLE) {
|
|
|
|
sourcece = 0xFFFF;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sourcece;
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
/**
|
2001-11-09 17:39:42 +00:00
|
|
|
* Allocate a memory and returns NULL if it failed.
|
|
|
|
* Internal method, status assumed to be a success.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param size to allocate
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2001-11-09 17:39:42 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return newly allocated array, NULL otherwise
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline void * allocateMemory(uint32_t size, UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
uint32_t *result = (uint32_t *)uprv_malloc(size);
|
|
|
|
if (result == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a uint32_t value to a destination array.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Creates a new array if we run out of space. The caller will have to
|
2001-08-25 02:03:53 +00:00
|
|
|
* manually deallocate the newly allocated array.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method. destination not to be NULL and has at least
|
2001-11-09 17:39:42 +00:00
|
|
|
* size destinationlength.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param destination target array
|
|
|
|
* @param offset destination offset to add value
|
|
|
|
* @param destinationlength target array size, return value for the new size
|
|
|
|
* @param value to be added
|
|
|
|
* @param increments incremental size expected
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2001-11-09 17:39:42 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return new destination array, destination if there was no new allocation
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int32_t * addTouint32_tArray(int32_t *destination,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t *destinationlength,
|
2004-04-01 22:04:52 +00:00
|
|
|
uint32_t value,
|
2009-04-22 23:10:07 +00:00
|
|
|
uint32_t increments,
|
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
uint32_t newlength = *destinationlength;
|
|
|
|
if (offset + 1 == newlength) {
|
|
|
|
newlength += increments;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *temp = (int32_t *)allocateMemory(
|
|
|
|
sizeof(int32_t) * newlength, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-04-01 22:04:52 +00:00
|
|
|
uprv_memcpy(temp, destination, sizeof(int32_t) * offset);
|
2001-08-25 02:03:53 +00:00
|
|
|
*destinationlength = newlength;
|
|
|
|
destination = temp;
|
|
|
|
}
|
|
|
|
destination[offset] = value;
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
/**
|
|
|
|
* Adds a uint64_t value to a destination array.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Creates a new array if we run out of space. The caller will have to
|
2008-05-23 04:22:28 +00:00
|
|
|
* manually deallocate the newly allocated array.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method. destination not to be NULL and has at least
|
2008-05-23 04:22:28 +00:00
|
|
|
* size destinationlength.
|
|
|
|
* @param destination target array
|
|
|
|
* @param offset destination offset to add value
|
|
|
|
* @param destinationlength target array size, return value for the new size
|
|
|
|
* @param value to be added
|
|
|
|
* @param increments incremental size expected
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2008-05-23 04:22:28 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
|
|
|
* @return new destination array, destination if there was no new allocation
|
|
|
|
*/
|
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int64_t * addTouint64_tArray(int64_t *destination,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t *destinationlength,
|
2008-05-23 04:22:28 +00:00
|
|
|
uint64_t value,
|
2009-04-22 23:10:07 +00:00
|
|
|
uint32_t increments,
|
|
|
|
UErrorCode *status)
|
2008-05-23 04:22:28 +00:00
|
|
|
{
|
|
|
|
uint32_t newlength = *destinationlength;
|
|
|
|
if (offset + 1 == newlength) {
|
|
|
|
newlength += increments;
|
|
|
|
int64_t *temp = (int64_t *)allocateMemory(
|
|
|
|
sizeof(int64_t) * newlength, status);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uprv_memcpy(temp, destination, sizeof(int64_t) * offset);
|
|
|
|
*destinationlength = newlength;
|
|
|
|
destination = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
destination[offset] = value;
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
|
|
|
* Initializing the ce table for a pattern.
|
|
|
|
* Stores non-ignorable collation keys.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Table size will be estimated by the size of the pattern text. Table
|
|
|
|
* expansion will be perform as we go along. Adding 1 to ensure that the table
|
2001-08-25 02:03:53 +00:00
|
|
|
* size definitely increases.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be a success.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2001-11-09 17:39:42 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
2009-04-22 23:10:07 +00:00
|
|
|
* @return total number of expansions
|
2001-08-25 02:03:53 +00:00
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline uint16_t initializePatternCETable(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
UPattern *pattern = &(strsrch->pattern);
|
|
|
|
uint32_t cetablesize = INITIAL_ARRAY_SIZE_;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *cetable = pattern->CEBuffer;
|
2001-11-09 17:39:42 +00:00
|
|
|
uint32_t patternlength = pattern->textLength;
|
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
if (coleiter == NULL) {
|
2009-04-22 23:10:07 +00:00
|
|
|
coleiter = ucol_openElements(strsrch->collator, pattern->text,
|
2001-11-09 17:39:42 +00:00
|
|
|
patternlength, status);
|
2009-04-22 23:10:07 +00:00
|
|
|
// status will be checked in ucol_next(..) later and if it is an
|
|
|
|
// error UCOL_NULLORDER the result of ucol_next(..) and 0 will be
|
2001-11-09 17:39:42 +00:00
|
|
|
// returned.
|
|
|
|
strsrch->utilIter = coleiter;
|
|
|
|
}
|
|
|
|
else {
|
2002-07-16 01:46:42 +00:00
|
|
|
uprv_init_collIterate(strsrch->collator, pattern->text,
|
|
|
|
pattern->textLength,
|
2010-01-06 23:50:03 +00:00
|
|
|
&coleiter->iteratordata_,
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return 0;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
if (pattern->CE != cetable && pattern->CE) {
|
|
|
|
uprv_free(pattern->CE);
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
uint16_t offset = 0;
|
|
|
|
uint16_t result = 0;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce;
|
2001-11-09 17:39:42 +00:00
|
|
|
|
|
|
|
while ((ce = ucol_next(coleiter, status)) != UCOL_NULLORDER &&
|
|
|
|
U_SUCCESS(*status)) {
|
|
|
|
uint32_t newce = getCE(strsrch, ce);
|
|
|
|
if (newce) {
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t *temp = addTouint32_tArray(cetable, offset, &cetablesize,
|
2001-11-09 17:39:42 +00:00
|
|
|
newce,
|
2009-04-22 23:10:07 +00:00
|
|
|
patternlength - ucol_getOffset(coleiter) + 1,
|
2001-11-09 17:39:42 +00:00
|
|
|
status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
offset ++;
|
|
|
|
if (cetable != temp && cetable != pattern->CEBuffer) {
|
|
|
|
uprv_free(cetable);
|
|
|
|
}
|
|
|
|
cetable = temp;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2002-08-21 22:16:34 +00:00
|
|
|
result += (uint16_t)(ucol_getMaxExpansion(coleiter, ce) - 1);
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
cetable[offset] = 0;
|
|
|
|
pattern->CE = cetable;
|
|
|
|
pattern->CELength = offset;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
return result;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
/**
|
|
|
|
* Initializing the pce table for a pattern.
|
|
|
|
* Stores non-ignorable collation keys.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Table size will be estimated by the size of the pattern text. Table
|
|
|
|
* expansion will be perform as we go along. Adding 1 to ensure that the table
|
2008-05-23 04:22:28 +00:00
|
|
|
* size definitely increases.
|
|
|
|
* Internal method, status assumed to be a success.
|
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2008-05-23 04:22:28 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
2009-04-22 23:10:07 +00:00
|
|
|
* @return total number of expansions
|
2008-05-23 04:22:28 +00:00
|
|
|
*/
|
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline uint16_t initializePatternPCETable(UStringSearch *strsrch,
|
2008-05-23 04:22:28 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
UPattern *pattern = &(strsrch->pattern);
|
|
|
|
uint32_t pcetablesize = INITIAL_ARRAY_SIZE_;
|
|
|
|
int64_t *pcetable = pattern->PCEBuffer;
|
|
|
|
uint32_t patternlength = pattern->textLength;
|
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (coleiter == NULL) {
|
2009-04-22 23:10:07 +00:00
|
|
|
coleiter = ucol_openElements(strsrch->collator, pattern->text,
|
2008-05-23 04:22:28 +00:00
|
|
|
patternlength, status);
|
2009-04-22 23:10:07 +00:00
|
|
|
// status will be checked in ucol_next(..) later and if it is an
|
|
|
|
// error UCOL_NULLORDER the result of ucol_next(..) and 0 will be
|
2008-05-23 04:22:28 +00:00
|
|
|
// returned.
|
|
|
|
strsrch->utilIter = coleiter;
|
|
|
|
} else {
|
|
|
|
uprv_init_collIterate(strsrch->collator, pattern->text,
|
|
|
|
pattern->textLength,
|
2010-01-06 23:50:03 +00:00
|
|
|
&coleiter->iteratordata_,
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return 0;
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (pattern->PCE != pcetable && pattern->PCE != NULL) {
|
|
|
|
uprv_free(pattern->PCE);
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
uint16_t offset = 0;
|
|
|
|
uint16_t result = 0;
|
|
|
|
int64_t pce;
|
|
|
|
|
|
|
|
uprv_init_pce(coleiter);
|
|
|
|
|
|
|
|
// ** Should processed CEs be signed or unsigned?
|
2009-04-22 23:10:07 +00:00
|
|
|
// ** (the rest of the code in this file seems to play fast-and-loose with
|
2008-05-23 04:22:28 +00:00
|
|
|
// ** whether a CE is signed or unsigned. For example, look at routine above this one.)
|
|
|
|
while ((pce = ucol_nextProcessed(coleiter, NULL, NULL, status)) != UCOL_PROCESSED_NULLORDER &&
|
|
|
|
U_SUCCESS(*status)) {
|
2009-04-22 23:10:07 +00:00
|
|
|
int64_t *temp = addTouint64_tArray(pcetable, offset, &pcetablesize,
|
2008-05-23 04:22:28 +00:00
|
|
|
pce,
|
2009-04-22 23:10:07 +00:00
|
|
|
patternlength - ucol_getOffset(coleiter) + 1,
|
2008-05-23 04:22:28 +00:00
|
|
|
status);
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += 1;
|
|
|
|
|
|
|
|
if (pcetable != temp && pcetable != pattern->PCEBuffer) {
|
|
|
|
uprv_free(pcetable);
|
|
|
|
}
|
|
|
|
|
|
|
|
pcetable = temp;
|
|
|
|
//result += (uint16_t)(ucol_getMaxExpansion(coleiter, ce) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pcetable[offset] = 0;
|
|
|
|
pattern->PCE = pcetable;
|
|
|
|
pattern->PCELength = offset;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
2001-11-09 17:39:42 +00:00
|
|
|
* Initializes the pattern struct.
|
|
|
|
* Internal method, status assumed to be success.
|
|
|
|
* @param strsrch UStringSearch data storage
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param status output error if any, caller to check status before calling
|
2001-11-09 17:39:42 +00:00
|
|
|
* method, status assumed to be success when passed in.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return expansionsize the total expansion size of the pattern
|
2009-04-22 23:10:07 +00:00
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int16_t initializePattern(UStringSearch *strsrch, UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
UPattern *pattern = &(strsrch->pattern);
|
|
|
|
const UChar *patterntext = pattern->text;
|
|
|
|
int32_t length = pattern->textLength;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t index = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-10-18 22:29:39 +00:00
|
|
|
// Since the strength is primary, accents are ignored in the pattern.
|
|
|
|
if (strsrch->strength == UCOL_PRIMARY) {
|
2011-03-22 03:50:26 +00:00
|
|
|
pattern->hasPrefixAccents = 0;
|
|
|
|
pattern->hasSuffixAccents = 0;
|
2007-10-18 22:29:39 +00:00
|
|
|
} else {
|
2011-03-22 03:50:26 +00:00
|
|
|
pattern->hasPrefixAccents = getFCD(patterntext, &index, length) >>
|
|
|
|
SECOND_LAST_BYTE_SHIFT_;
|
|
|
|
index = length;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(patterntext, 0, index);
|
2011-03-22 03:50:26 +00:00
|
|
|
pattern->hasSuffixAccents = getFCD(patterntext, &index, length) &
|
|
|
|
LAST_BYTE_MASK_;
|
2007-10-18 22:29:39 +00:00
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
// ** HACK **
|
|
|
|
if (strsrch->pattern.PCE != NULL) {
|
|
|
|
if (strsrch->pattern.PCE != strsrch->pattern.PCEBuffer) {
|
|
|
|
uprv_free(strsrch->pattern.PCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
strsrch->pattern.PCE = NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
// since intializePattern is an internal method status is a success.
|
2009-04-22 23:10:07 +00:00
|
|
|
return initializePatternCETable(strsrch, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializing shift tables, with the default values.
|
|
|
|
* If a corresponding default value is 0, the shift table is not set.
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param shift table for forwards shift
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param backshift table for backwards shift
|
|
|
|
* @param cetable table containing pattern ce
|
|
|
|
* @param cesize size of the pattern ces
|
|
|
|
* @param expansionsize total size of the expansions
|
|
|
|
* @param defaultforward the default forward value
|
|
|
|
* @param defaultbackward the default backward value
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline void setShiftTable(int16_t shift[], int16_t backshift[],
|
|
|
|
int32_t *cetable, int32_t cesize,
|
2001-09-07 21:56:18 +00:00
|
|
|
int16_t expansionsize,
|
|
|
|
int16_t defaultforward,
|
|
|
|
int16_t defaultbackward)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2009-04-22 23:10:07 +00:00
|
|
|
// estimate the value to shift. to do that we estimate the smallest
|
2001-08-25 02:03:53 +00:00
|
|
|
// number of characters to give the relevant ces, ie approximately
|
2009-04-22 23:10:07 +00:00
|
|
|
// the number of ces minus their expansion, since expansions can come
|
2001-08-25 02:03:53 +00:00
|
|
|
// from a character.
|
|
|
|
int32_t count;
|
|
|
|
for (count = 0; count < MAX_TABLE_SIZE_; count ++) {
|
|
|
|
shift[count] = defaultforward;
|
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
cesize --; // down to the last index
|
|
|
|
for (count = 0; count < cesize; count ++) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// number of ces from right of array to the count
|
|
|
|
int temp = defaultforward - count - 1;
|
|
|
|
shift[hash(cetable[count])] = temp > 1 ? temp : 1;
|
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
shift[hash(cetable[cesize])] = 1;
|
2001-08-25 02:03:53 +00:00
|
|
|
// for ignorables we just shift by one. see test examples.
|
|
|
|
shift[hash(0)] = 1;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
for (count = 0; count < MAX_TABLE_SIZE_; count ++) {
|
|
|
|
backshift[count] = defaultbackward;
|
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
for (count = cesize; count > 0; count --) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// the original value count does not seem to work
|
2009-04-22 23:10:07 +00:00
|
|
|
backshift[hash(cetable[count])] = count > expansionsize ?
|
2002-08-21 22:16:34 +00:00
|
|
|
(int16_t)(count - expansionsize) : 1;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
backshift[hash(cetable[0])] = 1;
|
|
|
|
backshift[hash(0)] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Building of the pattern collation element list and the boyer moore strsrch
|
|
|
|
* table.
|
|
|
|
* The canonical match will only be performed after the default match fails.
|
|
|
|
* For both cases we need to remember the size of the composed and decomposed
|
|
|
|
* versions of the string. Since the Boyer-Moore shift calculations shifts by
|
2009-04-22 23:10:07 +00:00
|
|
|
* a number of characters in the text and tries to match the pattern from that
|
|
|
|
* offset, the shift value can not be too large in case we miss some
|
|
|
|
* characters. To choose a right shift size, we estimate the NFC form of the
|
|
|
|
* and use its size as a shift guide. The NFC form should be the small
|
2001-08-25 02:03:53 +00:00
|
|
|
* possible representation of the pattern. Anyways, we'll err on the smaller
|
|
|
|
* shift size. Hence the calculation for minlength.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Canonical match will be performed slightly differently. We'll split the
|
|
|
|
* pattern into 3 parts, the prefix accents (PA), the middle string bounded by
|
|
|
|
* the first and last base character (MS), the ending accents (EA). Matches
|
2001-08-25 02:03:53 +00:00
|
|
|
* will be done on MS first, and only when we match MS then some processing
|
|
|
|
* will be required for the prefix and end accents in order to determine if
|
2009-04-22 23:10:07 +00:00
|
|
|
* they match PA and EA. Hence the default shift values
|
|
|
|
* for the canonical match will take the size of either end's accent into
|
2001-08-25 02:03:53 +00:00
|
|
|
* consideration. Forwards search will take the end accents into consideration
|
|
|
|
* for the default shift values and the backwards search will take the prefix
|
|
|
|
* accents into consideration.
|
|
|
|
* If pattern has no non-ignorable ce, we return a illegal argument error.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be success.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch UStringSearch data storage
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status for output errors if it occurs, status is assumed to be a
|
|
|
|
* success when it is passed in.
|
2009-04-22 23:10:07 +00:00
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline void initialize(UStringSearch *strsrch, UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2009-04-22 23:10:07 +00:00
|
|
|
int16_t expandlength = initializePattern(strsrch, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (U_SUCCESS(*status) && strsrch->pattern.CELength > 0) {
|
2001-09-07 21:56:18 +00:00
|
|
|
UPattern *pattern = &strsrch->pattern;
|
|
|
|
int32_t cesize = pattern->CELength;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
int16_t minlength = cesize > expandlength
|
2003-08-31 20:53:46 +00:00
|
|
|
? (int16_t)cesize - expandlength : 1;
|
2001-08-25 02:03:53 +00:00
|
|
|
pattern->defaultShiftSize = minlength;
|
|
|
|
setShiftTable(pattern->shift, pattern->backShift, pattern->CE,
|
2001-11-09 17:39:42 +00:00
|
|
|
cesize, expandlength, minlength, minlength);
|
|
|
|
return;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
strsrch->pattern.defaultShiftSize = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2007-10-18 22:29:39 +00:00
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Check to make sure that the match length is at the end of the character by
|
2007-10-18 22:29:39 +00:00
|
|
|
* using the breakiterator.
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param strsrch string search data
|
2007-10-18 22:29:39 +00:00
|
|
|
* @param start target text start offset
|
|
|
|
* @param end target text end offset
|
|
|
|
*/
|
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
void checkBreakBoundary(const UStringSearch *strsrch, int32_t * /*start*/,
|
2007-10-18 22:29:39 +00:00
|
|
|
int32_t *end)
|
|
|
|
{
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2007-12-05 21:13:44 +00:00
|
|
|
UBreakIterator *breakiterator = strsrch->search->internalBreakIter;
|
2007-10-18 22:29:39 +00:00
|
|
|
if (breakiterator) {
|
2011-03-22 03:50:26 +00:00
|
|
|
int32_t matchend = *end;
|
|
|
|
//int32_t matchstart = *start;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2011-03-22 03:50:26 +00:00
|
|
|
if (!ubrk_isBoundary(breakiterator, matchend)) {
|
|
|
|
*end = ubrk_following(breakiterator, matchend);
|
2007-12-05 05:37:54 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2011-03-22 03:50:26 +00:00
|
|
|
/* Check the start of the matched text to make sure it doesn't have any accents
|
|
|
|
* before it. This code may not be necessary and so it is commented out */
|
|
|
|
/*if (!ubrk_isBoundary(breakiterator, matchstart) && !ubrk_isBoundary(breakiterator, matchstart-1)) {
|
|
|
|
*start = ubrk_preceding(breakiterator, matchstart);
|
|
|
|
}*/
|
2007-10-18 22:29:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Determine whether the target text in UStringSearch bounded by the offset
|
|
|
|
* start and end is one or more whole units of text as
|
2001-08-25 02:03:53 +00:00
|
|
|
* determined by the breakiterator in UStringSearch.
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param strsrch string search data
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param start target text start offset
|
|
|
|
* @param end target text end offset
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool isBreakUnit(const UStringSearch *strsrch, int32_t start,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t end)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2001-08-25 02:03:53 +00:00
|
|
|
UBreakIterator *breakiterator = strsrch->search->breakIter;
|
2007-10-18 22:29:39 +00:00
|
|
|
//TODO: Add here.
|
2007-10-25 22:02:54 +00:00
|
|
|
if (breakiterator) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t startindex = ubrk_first(breakiterator);
|
|
|
|
int32_t endindex = ubrk_last(breakiterator);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// out-of-range indexes are never boundary positions
|
|
|
|
if (start < startindex || start > endindex ||
|
|
|
|
end < startindex || end > endindex) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
// otherwise, we can use following() on the position before the
|
|
|
|
// specified one and return true of the position we get back is the
|
2001-08-25 02:03:53 +00:00
|
|
|
// one the user specified
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool result = (start == startindex ||
|
|
|
|
ubrk_following(breakiterator, start - 1) == start) &&
|
|
|
|
(end == endindex ||
|
2001-08-25 02:03:53 +00:00
|
|
|
ubrk_following(breakiterator, end - 1) == end);
|
2002-03-19 21:50:15 +00:00
|
|
|
if (result) {
|
|
|
|
// iterates the individual ces
|
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
2009-04-22 23:10:07 +00:00
|
|
|
const UChar *text = strsrch->search->text +
|
2002-03-19 21:50:15 +00:00
|
|
|
start;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ucol_setText(coleiter, text, end - start, &status);
|
|
|
|
for (int32_t count = 0; count < strsrch->pattern.CELength;
|
|
|
|
count ++) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_next(coleiter, &status));
|
2002-07-24 19:57:46 +00:00
|
|
|
if (ce == UCOL_IGNORABLE) {
|
|
|
|
count --;
|
|
|
|
continue;
|
|
|
|
}
|
2002-03-19 21:50:15 +00:00
|
|
|
if (U_FAILURE(status) || ce != strsrch->pattern.CE[count]) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t nextce = ucol_next(coleiter, &status);
|
2002-07-24 19:57:46 +00:00
|
|
|
while (ucol_getOffset(coleiter) == (end - start)
|
|
|
|
&& getCE(strsrch, nextce) == UCOL_IGNORABLE) {
|
|
|
|
nextce = ucol_next(coleiter, &status);
|
|
|
|
}
|
|
|
|
if (ucol_getOffset(coleiter) == (end - start)
|
|
|
|
&& nextce != UCOL_NULLORDER) {
|
2002-03-19 21:50:15 +00:00
|
|
|
// extra collation elements at the end of the match
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Getting the next base character offset if current offset is an accent,
|
|
|
|
* or the current offset if the current character contains a base character.
|
2001-08-25 02:03:53 +00:00
|
|
|
* accents the following base character will be returned
|
|
|
|
* @param text string
|
|
|
|
* @param textoffset current offset
|
|
|
|
* @param textlength length of text string
|
|
|
|
* @return the next base character or the current offset
|
|
|
|
* if the current character is contains a base character.
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int32_t getNextBaseOffset(const UChar *text,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength)
|
|
|
|
{
|
2001-09-07 21:56:18 +00:00
|
|
|
if (textoffset < textlength) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = textoffset;
|
2001-09-07 21:56:18 +00:00
|
|
|
if (getFCD(text, &temp, textlength) >> SECOND_LAST_BYTE_SHIFT_) {
|
2009-04-22 23:10:07 +00:00
|
|
|
while (temp < textlength) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = temp;
|
2009-04-22 23:10:07 +00:00
|
|
|
if ((getFCD(text, &temp, textlength) >>
|
2001-09-07 21:56:18 +00:00
|
|
|
SECOND_LAST_BYTE_SHIFT_) == 0) {
|
|
|
|
return result;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
return textlength;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return textoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the next base character offset depending on the string search pattern
|
|
|
|
* data
|
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset current offset, one offset away from the last character
|
|
|
|
* to search for.
|
|
|
|
* @return start index of the next base character or the current offset
|
|
|
|
* if the current character is contains a base character.
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int32_t getNextUStringSearchBaseOffset(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2003-08-31 20:53:46 +00:00
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2009-04-22 23:10:07 +00:00
|
|
|
if (strsrch->pattern.hasSuffixAccents &&
|
2002-08-26 16:52:05 +00:00
|
|
|
textoffset < textlength) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = textoffset;
|
2001-09-07 21:56:18 +00:00
|
|
|
const UChar *text = strsrch->search->text;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(text, 0, temp);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (getFCD(text, &temp, textlength) & LAST_BYTE_MASK_) {
|
|
|
|
return getNextBaseOffset(text, textoffset, textlength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return textoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shifting the collation element iterator position forward to prepare for
|
|
|
|
* a following match. If the last character is a unsafe character, we'll only
|
|
|
|
* shift by 1 to capture contractions, normalization etc.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be success.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param text strsrch string search data
|
|
|
|
* @param textoffset start text position to do search
|
|
|
|
* @param ce the text ce which failed the match.
|
|
|
|
* @param patternceindex index of the ce within the pattern ce buffer which
|
|
|
|
* failed the match
|
|
|
|
* @return final offset
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
inline int32_t shiftForward(UStringSearch *strsrch,
|
|
|
|
int32_t textoffset,
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce,
|
2002-08-26 16:52:05 +00:00
|
|
|
int32_t patternceindex)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2003-08-31 20:53:46 +00:00
|
|
|
UPattern *pattern = &(strsrch->pattern);
|
2002-12-10 06:02:50 +00:00
|
|
|
if (ce != UCOL_NULLORDER) {
|
|
|
|
int32_t shift = pattern->shift[hash(ce)];
|
2009-04-22 23:10:07 +00:00
|
|
|
// this is to adjust for characters in the middle of the
|
2002-12-10 06:02:50 +00:00
|
|
|
// substring for matching that failed.
|
|
|
|
int32_t adjust = pattern->CELength - patternceindex;
|
|
|
|
if (adjust > 1 && shift >= adjust) {
|
|
|
|
shift -= adjust - 1;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2002-12-10 06:02:50 +00:00
|
|
|
textoffset += shift;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-12-10 06:02:50 +00:00
|
|
|
textoffset += pattern->defaultShiftSize;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
textoffset = getNextUStringSearchBaseOffset(strsrch, textoffset);
|
|
|
|
// check for unsafe characters
|
2009-04-22 23:10:07 +00:00
|
|
|
// * if it is the start or middle of a contraction: to be done after
|
2001-11-09 17:39:42 +00:00
|
|
|
// a initial match is found
|
|
|
|
// * thai or lao base consonant character: similar to contraction
|
|
|
|
// * high surrogate character: similar to contraction
|
|
|
|
// * next character is a accent: shift to the next base character
|
2001-08-25 02:03:53 +00:00
|
|
|
return textoffset;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
#endif // #if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* sets match not found
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline void setMatchNotFound(UStringSearch *strsrch)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
// this method resets the match result regardless of the error status.
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->matchedIndex = USEARCH_DONE;
|
|
|
|
strsrch->search->matchedLength = 0;
|
|
|
|
if (strsrch->search->isForwardSearching) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, strsrch->search->textLength);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, 0);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
|
|
|
* Gets the offset to the next safe point in text.
|
|
|
|
* ie. not the middle of a contraction, swappable characters or supplementary
|
|
|
|
* characters.
|
|
|
|
* @param collator collation sata
|
|
|
|
* @param text string to work with
|
|
|
|
* @param textoffset offset in string
|
|
|
|
* @param textlength length of text string
|
|
|
|
* @return offset to the next safe character
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int32_t getNextSafeOffset(const UCollator *collator,
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *text,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength)
|
|
|
|
{
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = textoffset; // first contraction character
|
2001-08-25 02:03:53 +00:00
|
|
|
while (result != textlength && ucol_unsafeCP(text[result], collator)) {
|
|
|
|
result ++;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
return result;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
/**
|
2001-08-25 02:03:53 +00:00
|
|
|
* This checks for accents in the potential match started with a .
|
|
|
|
* composite character.
|
2009-04-22 23:10:07 +00:00
|
|
|
* This is really painful... we have to check that composite character do not
|
|
|
|
* have any extra accents. We have to normalize the potential match and find
|
2001-08-25 02:03:53 +00:00
|
|
|
* the immediate decomposed character before the match.
|
2009-04-22 23:10:07 +00:00
|
|
|
* The first composite character would have been taken care of by the fcd
|
2001-08-25 02:03:53 +00:00
|
|
|
* checks in checkForwardExactMatch.
|
2009-04-22 23:10:07 +00:00
|
|
|
* This is the slow path after the fcd of the first character and
|
|
|
|
* the last character has been checked by checkForwardExactMatch and we
|
2001-08-25 02:03:53 +00:00
|
|
|
* determine that the potential match has extra non-ignorable preceding
|
|
|
|
* ces.
|
2009-04-22 23:10:07 +00:00
|
|
|
* E.g. looking for \u0301 acute in \u01FA A ring above and acute,
|
2001-09-07 21:56:18 +00:00
|
|
|
* checkExtraMatchAccent should fail since there is a middle ring in \u01FA
|
|
|
|
* Note here that accents checking are slow and cautioned in the API docs.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be a success, caller should check status
|
|
|
|
* before calling this method
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param start index of the potential unfriendly composite character
|
|
|
|
* @param end index of the potential unfriendly composite character
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return TRUE if there is non-ignorable accents before at the beginning
|
|
|
|
* of the match, FALSE otherwise.
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
|
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
UBool checkExtraMatchAccents(const UStringSearch *strsrch, int32_t start,
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t end,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
UBool result = FALSE;
|
|
|
|
if (strsrch->pattern.hasPrefixAccents) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t length = end - start;
|
|
|
|
int32_t offset = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *text = strsrch->search->text + start;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_FWD_1(text, offset, length);
|
2001-08-25 02:03:53 +00:00
|
|
|
// we are only concerned with the first composite character
|
|
|
|
if (unorm_quickCheck(text, offset, UNORM_NFD, status) == UNORM_NO) {
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t safeoffset = getNextSafeOffset(strsrch->collator,
|
2001-11-09 17:39:42 +00:00
|
|
|
text, 0, length);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (safeoffset != length) {
|
|
|
|
safeoffset ++;
|
|
|
|
}
|
|
|
|
UChar *norm = NULL;
|
|
|
|
UChar buffer[INITIAL_ARRAY_SIZE_];
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t size = unorm_normalize(text, safeoffset, UNORM_NFD, 0,
|
|
|
|
buffer, INITIAL_ARRAY_SIZE_,
|
|
|
|
status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (size >= INITIAL_ARRAY_SIZE_) {
|
|
|
|
norm = (UChar *)allocateMemory((size + 1) * sizeof(UChar),
|
|
|
|
status);
|
2009-04-22 23:10:07 +00:00
|
|
|
// if allocation failed, status will be set to
|
2001-11-09 17:39:42 +00:00
|
|
|
// U_MEMORY_ALLOCATION_ERROR and unorm_normalize internally
|
|
|
|
// checks for it.
|
2009-04-22 23:10:07 +00:00
|
|
|
size = unorm_normalize(text, safeoffset, UNORM_NFD, 0, norm,
|
2001-08-25 02:03:53 +00:00
|
|
|
size, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status) && norm != NULL) {
|
|
|
|
uprv_free(norm);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
norm = buffer;
|
|
|
|
}
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
|
|
|
ucol_setText(coleiter, norm, size, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
uint32_t firstce = strsrch->pattern.CE[0];
|
|
|
|
UBool ignorable = TRUE;
|
|
|
|
uint32_t ce = UCOL_IGNORABLE;
|
2007-12-05 05:37:54 +00:00
|
|
|
while (U_SUCCESS(*status) && ce != firstce && ce != (uint32_t)UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
offset = ucol_getOffset(coleiter);
|
|
|
|
if (ce != firstce && ce != UCOL_IGNORABLE) {
|
|
|
|
ignorable = FALSE;
|
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
ce = ucol_next(coleiter, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
UChar32 codepoint;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_PREV(norm, 0, offset, codepoint);
|
2001-08-25 02:03:53 +00:00
|
|
|
result = !ignorable && (u_getCombiningClass(codepoint) != 0);
|
|
|
|
|
|
|
|
if (norm != buffer) {
|
|
|
|
uprv_free(norm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Used by exact matches, checks if there are accents before the match.
|
2001-08-25 02:03:53 +00:00
|
|
|
* This is really painful... we have to check that composite characters at
|
2009-04-22 23:10:07 +00:00
|
|
|
* the start of the matches have to not have any extra accents.
|
|
|
|
* We check the FCD of the character first, if it starts with an accent and
|
2001-09-07 21:56:18 +00:00
|
|
|
* the first pattern ce does not match the first ce of the character, we bail.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Otherwise we try normalizing the first composite
|
|
|
|
* character and find the immediate decomposed character before the match to
|
2001-08-25 02:03:53 +00:00
|
|
|
* see if it is an non-ignorable accent.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Now normalizing the first composite character is enough because we ensure
|
|
|
|
* that when the match is passed in here with extra beginning ces, the
|
2001-08-25 02:03:53 +00:00
|
|
|
* first or last ce that match has to occur within the first character.
|
2009-04-22 23:10:07 +00:00
|
|
|
* E.g. looking for \u0301 acute in \u01FA A ring above and acute,
|
2001-09-07 21:56:18 +00:00
|
|
|
* checkExtraMatchAccent should fail since there is a middle ring in \u01FA
|
|
|
|
* Note here that accents checking are slow and cautioned in the API docs.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param start offset
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param end offset
|
2009-04-22 23:10:07 +00:00
|
|
|
* @return TRUE if there are accents on either side of the match,
|
2001-08-25 02:03:53 +00:00
|
|
|
* FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
UBool hasAccentsBeforeMatch(const UStringSearch *strsrch, int32_t start,
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t end)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
if (strsrch->pattern.hasPrefixAccents) {
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
// we have been iterating forwards previously
|
|
|
|
uint32_t ignorable = TRUE;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t firstce = strsrch->pattern.CE[0];
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
setColEIterOffset(coleiter, start);
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_next(coleiter, &status));
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2001-11-09 17:39:42 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
while (ce != firstce) {
|
|
|
|
if (ce != UCOL_IGNORABLE) {
|
|
|
|
ignorable = FALSE;
|
|
|
|
}
|
|
|
|
ce = getCE(strsrch, ucol_next(coleiter, &status));
|
2008-05-23 04:22:28 +00:00
|
|
|
if (U_FAILURE(status) || ce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2003-08-31 20:53:46 +00:00
|
|
|
if (!ignorable && inNormBuf(coleiter)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// within normalization buffer, discontiguous handled here
|
2003-08-31 20:53:46 +00:00
|
|
|
return TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
// within text
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = start;
|
2003-08-31 20:53:46 +00:00
|
|
|
// original code
|
2009-04-22 23:10:07 +00:00
|
|
|
// accent = (getFCD(strsrch->search->text, &temp,
|
|
|
|
// strsrch->search->textLength)
|
|
|
|
// >> SECOND_LAST_BYTE_SHIFT_);
|
2003-08-31 20:53:46 +00:00
|
|
|
// however this code does not work well with VC7 .net in release mode.
|
2009-04-22 23:10:07 +00:00
|
|
|
// maybe the inlines for getFCD combined with shifting has bugs in
|
2003-08-31 20:53:46 +00:00
|
|
|
// VC7. anyways this is a work around.
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool accent = getFCD(strsrch->search->text, &temp,
|
2002-08-21 22:16:34 +00:00
|
|
|
strsrch->search->textLength) > 0xFF;
|
2001-08-25 02:03:53 +00:00
|
|
|
if (!accent) {
|
2003-08-31 20:53:46 +00:00
|
|
|
return checkExtraMatchAccents(strsrch, start, end, &status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2003-08-31 20:53:46 +00:00
|
|
|
if (!ignorable) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (start > 0) {
|
|
|
|
temp = start;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(strsrch->search->text, 0, temp);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (getFCD(strsrch->search->text, &temp,
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->textLength) & LAST_BYTE_MASK_) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, start);
|
2001-08-25 02:03:53 +00:00
|
|
|
ce = ucol_previous(coleiter, &status);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (U_FAILURE(status) ||
|
2001-08-25 02:03:53 +00:00
|
|
|
(ce != UCOL_NULLORDER && ce != UCOL_IGNORABLE)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by exact matches, checks if there are accents bounding the match.
|
|
|
|
* Note this is the initial boundary check. If the potential match
|
|
|
|
* starts or ends with composite characters, the accents in those
|
|
|
|
* characters will be determined later.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Not doing backwards iteration here, since discontiguos contraction for
|
2001-08-25 02:03:53 +00:00
|
|
|
* backwards collation element iterator, use up too many characters.
|
2009-04-22 23:10:07 +00:00
|
|
|
* E.g. looking for \u030A ring in \u01FA A ring above and acute,
|
2001-09-07 21:56:18 +00:00
|
|
|
* should fail since there is a acute at the end of \u01FA
|
|
|
|
* Note here that accents checking are slow and cautioned in the API docs.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param start offset of match
|
|
|
|
* @param end end offset of the match
|
2009-04-22 23:10:07 +00:00
|
|
|
* @return TRUE if there are accents on either side of the match,
|
2001-08-25 02:03:53 +00:00
|
|
|
* FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool hasAccentsAfterMatch(const UStringSearch *strsrch, int32_t start,
|
|
|
|
int32_t end)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
if (strsrch->pattern.hasSuffixAccents) {
|
|
|
|
const UChar *text = strsrch->search->text;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = end;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(text, 0, temp);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (getFCD(text, &temp, textlength) & LAST_BYTE_MASK_) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t firstce = strsrch->pattern.CE[0];
|
2001-08-25 02:03:53 +00:00
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2011-03-22 03:50:26 +00:00
|
|
|
int32_t ce;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, start);
|
2008-05-23 04:22:28 +00:00
|
|
|
while ((ce = getCE(strsrch, ucol_next(coleiter, &status))) != firstce) {
|
|
|
|
if (U_FAILURE(status) || ce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t count = 1;
|
|
|
|
while (count < strsrch->pattern.CELength) {
|
2009-04-22 23:10:07 +00:00
|
|
|
if (getCE(strsrch, ucol_next(coleiter, &status))
|
2002-07-12 20:33:01 +00:00
|
|
|
== UCOL_IGNORABLE) {
|
|
|
|
// Thai can give an ignorable here.
|
|
|
|
count --;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
count ++;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2011-03-22 03:50:26 +00:00
|
|
|
ce = ucol_next(coleiter, &status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (ce != UCOL_NULLORDER && ce != UCOL_IGNORABLE) {
|
2011-03-22 03:50:26 +00:00
|
|
|
ce = getCE(strsrch, ce);
|
2007-10-23 19:13:33 +00:00
|
|
|
}
|
|
|
|
if (ce != UCOL_NULLORDER && ce != UCOL_IGNORABLE) {
|
2001-09-07 21:56:18 +00:00
|
|
|
if (ucol_getOffset(coleiter) <= end) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (getFCD(text, &end, textlength) >> SECOND_LAST_BYTE_SHIFT_) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
#endif // #if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the offset runs out of the text string
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param offset
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param textlength of the text string
|
|
|
|
* @return TRUE if offset is out of bounds, FALSE otherwise
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
inline UBool isOutOfBounds(int32_t textlength, int32_t offset)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
return offset < 0 || offset > textlength;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for identical match
|
|
|
|
* @param strsrch string search data
|
|
|
|
* @param start offset of possible match
|
|
|
|
* @param end offset of possible match
|
|
|
|
* @return TRUE if identical match is found
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UBool checkIdentical(const UStringSearch *strsrch, int32_t start,
|
|
|
|
int32_t end)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
if (strsrch->strength != UCOL_IDENTICAL) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
// Note: We could use Normalizer::compare() or similar, but for short strings
|
|
|
|
// which may not be in FCD it might be faster to just NFD them.
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UnicodeString t2, p2;
|
|
|
|
strsrch->nfd->normalize(
|
|
|
|
UnicodeString(FALSE, strsrch->search->text + start, end - start), t2, status);
|
|
|
|
strsrch->nfd->normalize(
|
|
|
|
UnicodeString(FALSE, strsrch->pattern.text, strsrch->pattern.textLength), p2, status);
|
2005-05-03 23:17:03 +00:00
|
|
|
// return FALSE if NFD failed
|
2010-01-06 23:50:03 +00:00
|
|
|
return U_SUCCESS(status) && t2 == p2;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-10-07 23:37:42 +00:00
|
|
|
#if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if the match is repeated
|
|
|
|
* @param strsrch string search data
|
|
|
|
* @param start new match start index
|
|
|
|
* @param end new match end index
|
|
|
|
* @return TRUE if the the match is repeated, FALSE otherwise
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2001-08-25 02:03:53 +00:00
|
|
|
inline UBool checkRepeatedMatch(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
|
|
|
int32_t end)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t lastmatchindex = strsrch->search->matchedIndex;
|
2001-08-25 02:03:53 +00:00
|
|
|
UBool result;
|
|
|
|
if (lastmatchindex == USEARCH_DONE) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (strsrch->search->isForwardSearching) {
|
|
|
|
result = start <= lastmatchindex;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = start >= lastmatchindex;
|
|
|
|
}
|
2003-07-03 21:48:39 +00:00
|
|
|
if (!result && !strsrch->search->isOverlap) {
|
2001-08-25 02:03:53 +00:00
|
|
|
if (strsrch->search->isForwardSearching) {
|
|
|
|
result = start < lastmatchindex + strsrch->search->matchedLength;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = end > lastmatchindex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the collation element iterator's current offset.
|
|
|
|
* @param coleiter collation element iterator
|
|
|
|
* @param forwards flag TRUE if we are moving in th forwards direction
|
2009-04-22 23:10:07 +00:00
|
|
|
* @return current offset
|
2001-08-25 02:03:53 +00:00
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
inline int32_t getColElemIterOffset(const UCollationElements *coleiter,
|
2001-08-25 02:03:53 +00:00
|
|
|
UBool forwards)
|
|
|
|
{
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
// intricacies of the the backwards collation element iterator
|
2008-05-23 04:22:28 +00:00
|
|
|
if (FALSE && !forwards && inNormBuf(coleiter) && !isFCDPointerNull(coleiter)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
result ++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks match for contraction.
|
2001-09-07 21:56:18 +00:00
|
|
|
* If the match ends with a partial contraction we fail.
|
|
|
|
* If the match starts too far off (because of backwards iteration) we try to
|
2002-03-19 21:50:15 +00:00
|
|
|
* chip off the extra characters depending on whether a breakiterator has
|
|
|
|
* been used.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, error assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param start offset of potential match, to be modified if necessary
|
|
|
|
* @param end offset of potential match, to be modified if necessary
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-09-07 21:56:18 +00:00
|
|
|
* @return TRUE if match passes the contraction test, FALSE otherwise
|
2001-08-25 02:03:53 +00:00
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
|
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool checkNextExactContractionMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *start,
|
|
|
|
int32_t *end, UErrorCode *status)
|
2001-09-07 21:56:18 +00:00
|
|
|
{
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2008-05-23 04:22:28 +00:00
|
|
|
int32_t temp = *start;
|
2001-09-07 21:56:18 +00:00
|
|
|
const UCollator *collator = strsrch->collator;
|
|
|
|
const UChar *text = strsrch->search->text;
|
2009-04-22 23:10:07 +00:00
|
|
|
// This part checks if either ends of the match contains potential
|
2001-09-07 21:56:18 +00:00
|
|
|
// contraction. If so we'll have to iterate through them
|
2003-08-31 20:53:46 +00:00
|
|
|
// The start contraction needs to be checked since ucol_previous dumps
|
|
|
|
// all characters till the first safe character into the buffer.
|
2009-04-22 23:10:07 +00:00
|
|
|
// *start + 1 is used to test for the unsafe characters instead of *start
|
|
|
|
// because ucol_prev takes all unsafe characters till the first safe
|
|
|
|
// character ie *start. so by testing *start + 1, we can estimate if
|
|
|
|
// excess prefix characters has been included in the potential search
|
2003-08-31 20:53:46 +00:00
|
|
|
// results.
|
2009-04-22 23:10:07 +00:00
|
|
|
if ((*end < textlength && ucol_unsafeCP(text[*end], collator)) ||
|
|
|
|
(*start + 1 < textlength
|
2002-08-12 21:47:57 +00:00
|
|
|
&& ucol_unsafeCP(text[*start + 1], collator))) {
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t expansion = getExpansionPrefix(coleiter);
|
|
|
|
UBool expandflag = expansion > 0;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, *start);
|
|
|
|
while (expansion > 0) {
|
2001-09-07 21:56:18 +00:00
|
|
|
// getting rid of the redundant ce, caused by setOffset.
|
2009-04-22 23:10:07 +00:00
|
|
|
// since backward contraction/expansion may have extra ces if we
|
|
|
|
// are in the normalization buffer, hasAccentsBeforeMatch would
|
2001-09-07 21:56:18 +00:00
|
|
|
// have taken care of it.
|
|
|
|
// E.g. the character \u01FA will have an expansion of 3, but if
|
|
|
|
// we are only looking for acute and ring \u030A and \u0301, we'll
|
|
|
|
// have to skip the first ce in the expansion buffer.
|
2001-08-25 02:03:53 +00:00
|
|
|
ucol_next(coleiter, status);
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (ucol_getOffset(coleiter) != temp) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*start = temp;
|
2001-08-25 02:03:53 +00:00
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
|
|
|
expansion --;
|
|
|
|
}
|
|
|
|
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
|
|
|
int32_t count = 0;
|
|
|
|
while (count < patterncelength) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_next(coleiter, status));
|
2001-08-25 02:03:53 +00:00
|
|
|
if (ce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (expandflag && count == 0 && ucol_getOffset(coleiter) != temp) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*start = temp;
|
|
|
|
temp = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
if (U_FAILURE(*status) || ce != patternce[count]) {
|
2001-09-07 21:56:18 +00:00
|
|
|
(*end) ++;
|
2009-04-22 23:10:07 +00:00
|
|
|
*end = getNextUStringSearchBaseOffset(strsrch, *end);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
count ++;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
/**
|
|
|
|
* Checks and sets the match information if found.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks
|
2001-09-07 21:56:18 +00:00
|
|
|
* <ul>
|
|
|
|
* <li> the potential match does not repeat the previous match
|
|
|
|
* <li> boundaries are correct
|
|
|
|
* <li> exact matches has no extra accents
|
2002-03-19 21:50:15 +00:00
|
|
|
* <li> identical matchesb
|
2001-09-07 21:56:18 +00:00
|
|
|
* <li> potential match does not end in the middle of a contraction
|
|
|
|
* <\ul>
|
|
|
|
* Otherwise the offset will be shifted to the next character.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset offset in the collation element text. the returned value
|
2009-04-22 23:10:07 +00:00
|
|
|
* will be the truncated end offset of the match or the new start
|
2001-09-07 21:56:18 +00:00
|
|
|
* search offset.
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-09-07 21:56:18 +00:00
|
|
|
* @return TRUE if the match is valid, FALSE otherwise
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UBool checkNextExactMatch(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t *textoffset, UErrorCode *status)
|
2001-09-07 21:56:18 +00:00
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t start = getColElemIterOffset(coleiter, FALSE);
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
if (!checkNextExactContractionMatch(strsrch, &start, textoffset, status)) {
|
|
|
|
return FALSE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
// this totally matches, however we need to check if it is repeating
|
|
|
|
if (!isBreakUnit(strsrch, start, *textoffset) ||
|
2009-04-22 23:10:07 +00:00
|
|
|
checkRepeatedMatch(strsrch, start, *textoffset) ||
|
|
|
|
hasAccentsBeforeMatch(strsrch, start, *textoffset) ||
|
2001-11-09 17:39:42 +00:00
|
|
|
!checkIdentical(strsrch, start, *textoffset) ||
|
|
|
|
hasAccentsAfterMatch(strsrch, start, *textoffset)) {
|
2003-08-31 20:53:46 +00:00
|
|
|
|
|
|
|
(*textoffset) ++;
|
2009-04-22 23:10:07 +00:00
|
|
|
*textoffset = getNextUStringSearchBaseOffset(strsrch, *textoffset);
|
2003-08-31 20:53:46 +00:00
|
|
|
return FALSE;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2007-10-18 22:29:39 +00:00
|
|
|
|
|
|
|
//Add breakiterator boundary check for primary strength search.
|
2007-10-25 22:02:54 +00:00
|
|
|
if (!strsrch->search->breakIter && strsrch->strength == UCOL_PRIMARY) {
|
2011-03-22 03:50:26 +00:00
|
|
|
checkBreakBoundary(strsrch, &start, textoffset);
|
2007-10-18 22:29:39 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
// totally match, we will get rid of the ending ignorables.
|
|
|
|
strsrch->search->matchedIndex = start;
|
|
|
|
strsrch->search->matchedLength = *textoffset - start;
|
2003-08-31 20:53:46 +00:00
|
|
|
return TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Getting the previous base character offset, or the current offset if the
|
2001-08-25 02:03:53 +00:00
|
|
|
* current character is a base character
|
|
|
|
* @param text string
|
|
|
|
* @param textoffset one offset after the current character
|
2009-04-22 23:10:07 +00:00
|
|
|
* @return the offset of the next character after the base character or the first
|
2001-08-25 02:03:53 +00:00
|
|
|
* composed character with accents
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int32_t getPreviousBaseOffset(const UChar *text,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-08-31 02:05:14 +00:00
|
|
|
if (textoffset > 0) {
|
2006-12-19 06:06:30 +00:00
|
|
|
for (;;) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = textoffset;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(text, 0, textoffset);
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = textoffset;
|
2001-08-31 02:05:14 +00:00
|
|
|
uint16_t fcd = getFCD(text, &temp, result);
|
|
|
|
if ((fcd >> SECOND_LAST_BYTE_SHIFT_) == 0) {
|
|
|
|
if (fcd & LAST_BYTE_MASK_) {
|
|
|
|
return textoffset;
|
|
|
|
}
|
|
|
|
return result;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (textoffset == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
return textoffset;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getting the indexes of the accents that are not blocked in the argument
|
|
|
|
* accent array
|
|
|
|
* @param accents array of accents in nfd terminated by a 0.
|
|
|
|
* @param accentsindex array of indexes of the accents that are not blocked
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
inline int getUnblockedAccentIndex(UChar *accents, int32_t *accentsindex)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t index = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t length = u_strlen(accents);
|
|
|
|
UChar32 codepoint = 0;
|
|
|
|
int cclass = 0;
|
|
|
|
int result = 0;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp;
|
2001-08-25 02:03:53 +00:00
|
|
|
while (index < length) {
|
|
|
|
temp = index;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_NEXT(accents, index, length, codepoint);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (u_getCombiningClass(codepoint) != cclass) {
|
|
|
|
cclass = u_getCombiningClass(codepoint);
|
|
|
|
accentsindex[result] = temp;
|
|
|
|
result ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accentsindex[result] = length;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends 3 UChar arrays to a destination array.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Creates a new array if we run out of space. The caller will have to
|
2001-08-25 02:03:53 +00:00
|
|
|
* manually deallocate the newly allocated array.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method. destination not to be NULL and has at least
|
2001-11-09 17:39:42 +00:00
|
|
|
* size destinationlength.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param destination target array
|
|
|
|
* @param destinationlength target array size, returning the appended length
|
|
|
|
* @param source1 null-terminated first array
|
|
|
|
* @param source2 second array
|
|
|
|
* @param source2length length of seond array
|
|
|
|
* @param source3 null-terminated third array
|
|
|
|
* @param status error status if any
|
|
|
|
* @return new destination array, destination if there was no new allocation
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UChar * addToUCharArray( UChar *destination,
|
|
|
|
int32_t *destinationlength,
|
|
|
|
const UChar *source1,
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *source2,
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t source2length,
|
|
|
|
const UChar *source3,
|
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
int32_t source1length = source1 ? u_strlen(source1) : 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t source3length = source3 ? u_strlen(source3) : 0;
|
|
|
|
if (*destinationlength < source1length + source2length + source3length +
|
|
|
|
1)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
destination = (UChar *)allocateMemory(
|
|
|
|
(source1length + source2length + source3length + 1) * sizeof(UChar),
|
|
|
|
status);
|
2009-04-22 23:10:07 +00:00
|
|
|
// if error allocating memory, status will be
|
2001-11-09 17:39:42 +00:00
|
|
|
// U_MEMORY_ALLOCATION_ERROR
|
|
|
|
if (U_FAILURE(*status)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
*destinationlength = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (source1length != 0) {
|
|
|
|
uprv_memcpy(destination, source1, sizeof(UChar) * source1length);
|
|
|
|
}
|
|
|
|
if (source2length != 0) {
|
2009-04-22 23:10:07 +00:00
|
|
|
uprv_memcpy(destination + source1length, source2,
|
2001-08-25 02:03:53 +00:00
|
|
|
sizeof(UChar) * source2length);
|
|
|
|
}
|
|
|
|
if (source3length != 0) {
|
2009-04-22 23:10:07 +00:00
|
|
|
uprv_memcpy(destination + source1length + source2length, source3,
|
2001-08-25 02:03:53 +00:00
|
|
|
sizeof(UChar) * source3length);
|
|
|
|
}
|
|
|
|
*destinationlength = source1length + source2length + source3length;
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Running through a collation element iterator to see if the contents matches
|
|
|
|
* pattern in string search data
|
|
|
|
* @param strsrch string search data
|
|
|
|
* @param coleiter collation element iterator
|
2002-05-21 00:53:52 +00:00
|
|
|
* @return TRUE if a match if found, FALSE otherwise
|
2001-08-25 02:03:53 +00:00
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UBool checkCollationMatch(const UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UCollationElements *coleiter)
|
|
|
|
{
|
|
|
|
int patternceindex = strsrch->pattern.CELength;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
while (patternceindex > 0) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_next(coleiter, &status));
|
2001-08-25 02:03:53 +00:00
|
|
|
if (ce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status) || ce != *patternce) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
patternce ++;
|
|
|
|
patternceindex --;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-09-07 21:56:18 +00:00
|
|
|
* Rearranges the front accents to try matching.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Prefix accents in the text will be grouped according to their combining
|
|
|
|
* class and the groups will be mixed and matched to try find the perfect
|
2001-09-07 21:56:18 +00:00
|
|
|
* match with the pattern.
|
|
|
|
* So for instance looking for "\u0301" in "\u030A\u0301\u0325"
|
|
|
|
* step 1: split "\u030A\u0301" into 6 other type of potential accent substrings
|
2009-04-22 23:10:07 +00:00
|
|
|
* "\u030A", "\u0301", "\u0325", "\u030A\u0301", "\u030A\u0325",
|
2001-09-07 21:56:18 +00:00
|
|
|
* "\u0301\u0325".
|
2001-11-09 17:39:42 +00:00
|
|
|
* step 2: check if any of the generated substrings matches the pattern.
|
|
|
|
* Internal method, status is assumed to be success, caller has to check status
|
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search match
|
|
|
|
* @param start first offset of the accents to start searching
|
|
|
|
* @param end start of the last accent set
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return USEARCH_DONE if a match is not found, otherwise return the starting
|
|
|
|
* offset of the match. Note this start includes all preceding accents.
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t doNextCanonicalPrefixMatch(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t end,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
const UChar *text = strsrch->search->text;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t tempstart = start;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
if ((getFCD(text, &tempstart, textlength) & LAST_BYTE_MASK_) == 0) {
|
|
|
|
// die... failed at a base character
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset = getNextBaseOffset(text, tempstart, textlength);
|
2001-08-25 02:03:53 +00:00
|
|
|
start = getPreviousBaseOffset(text, tempstart);
|
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
UChar accents[INITIAL_ARRAY_SIZE_];
|
|
|
|
// normalizing the offensive string
|
2009-04-22 23:10:07 +00:00
|
|
|
unorm_normalize(text + start, offset - start, UNORM_NFD, 0, accents,
|
|
|
|
INITIAL_ARRAY_SIZE_, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
int32_t accentsindex[INITIAL_ARRAY_SIZE_];
|
|
|
|
int32_t accentsize = getUnblockedAccentIndex(accents,
|
2001-09-07 21:56:18 +00:00
|
|
|
accentsindex);
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t count = (2 << (accentsize - 1)) - 1;
|
2001-11-09 17:39:42 +00:00
|
|
|
UChar buffer[INITIAL_ARRAY_SIZE_];
|
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
|
|
|
while (U_SUCCESS(*status) && count > 0) {
|
|
|
|
UChar *rearrange = strsrch->canonicalPrefixAccents;
|
|
|
|
// copy the base characters
|
|
|
|
for (int k = 0; k < accentsindex[0]; k ++) {
|
|
|
|
*rearrange ++ = accents[k];
|
|
|
|
}
|
|
|
|
// forming all possible canonical rearrangement by dropping
|
|
|
|
// sets of accents
|
|
|
|
for (int i = 0; i <= accentsize - 1; i ++) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t mask = 1 << (accentsize - i - 1);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (count & mask) {
|
|
|
|
for (int j = accentsindex[i]; j < accentsindex[i + 1]; j ++) {
|
|
|
|
*rearrange ++ = accents[j];
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
|
|
|
*rearrange = 0;
|
|
|
|
int32_t matchsize = INITIAL_ARRAY_SIZE_;
|
|
|
|
UChar *match = addToUCharArray(buffer, &matchsize,
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->canonicalPrefixAccents,
|
|
|
|
strsrch->search->text + offset,
|
|
|
|
end - offset,
|
|
|
|
strsrch->canonicalSuffixAccents,
|
|
|
|
status);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure, ucol_setText does nothing.
|
|
|
|
// run the collator iterator through this match
|
|
|
|
ucol_setText(coleiter, match, matchsize, status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
if (checkCollationMatch(strsrch, coleiter)) {
|
|
|
|
if (match != buffer) {
|
|
|
|
uprv_free(match);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
return start;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
count --;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the offset to the safe point in text before textoffset.
|
|
|
|
* ie. not the middle of a contraction, swappable characters or supplementary
|
|
|
|
* characters.
|
|
|
|
* @param collator collation sata
|
|
|
|
* @param text string to work with
|
|
|
|
* @param textoffset offset in string
|
|
|
|
* @param textlength length of text string
|
|
|
|
* @return offset to the previous safe character
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline uint32_t getPreviousSafeOffset(const UCollator *collator,
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *text,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = textoffset; // first contraction character
|
2001-08-25 02:03:53 +00:00
|
|
|
while (result != 0 && ucol_unsafeCP(text[result - 1], collator)) {
|
|
|
|
result --;
|
|
|
|
}
|
|
|
|
if (result != 0) {
|
|
|
|
// the first contraction character is consider unsafe here
|
|
|
|
result --;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
return result;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleaning up after we passed the safe zone
|
|
|
|
* @param strsrch string search data
|
|
|
|
* @param safetext safe text array
|
|
|
|
* @param safebuffer safe text buffer
|
|
|
|
* @param coleiter collation element iterator for safe text
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2001-08-25 02:03:53 +00:00
|
|
|
inline void cleanUpSafeText(const UStringSearch *strsrch, UChar *safetext,
|
2001-09-11 21:40:05 +00:00
|
|
|
UChar *safebuffer)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2009-04-22 23:10:07 +00:00
|
|
|
if (safetext != safebuffer && safetext != strsrch->canonicalSuffixAccents)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
uprv_free(safetext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the rearranged end accents and tries matching. If match failed at
|
|
|
|
* a seperate preceding set of accents (seperated from the rearranged on by
|
2009-04-22 23:10:07 +00:00
|
|
|
* at least a base character) then we rearrange the preceding accents and
|
2001-08-25 02:03:53 +00:00
|
|
|
* tries matching again.
|
2009-04-22 23:10:07 +00:00
|
|
|
* We allow skipping of the ends of the accent set if the ces do not match.
|
2001-08-25 02:03:53 +00:00
|
|
|
* However if the failure is found before the accent set, it fails.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset of the start of the rearranged accent
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return USEARCH_DONE if a match is not found, otherwise return the starting
|
|
|
|
* offset of the match. Note this start includes all preceding accents.
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t doNextCanonicalSuffixMatch(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
const UChar *text = strsrch->search->text;
|
|
|
|
const UCollator *collator = strsrch->collator;
|
|
|
|
int32_t safelength = 0;
|
|
|
|
UChar *safetext;
|
|
|
|
int32_t safetextlength;
|
|
|
|
UChar safebuffer[INITIAL_ARRAY_SIZE_];
|
2001-09-07 21:56:18 +00:00
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t safeoffset = textoffset;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
if (textoffset != 0 && ucol_unsafeCP(strsrch->canonicalSuffixAccents[0],
|
2001-08-25 02:03:53 +00:00
|
|
|
collator)) {
|
|
|
|
safeoffset = getPreviousSafeOffset(collator, text, textoffset);
|
|
|
|
safelength = textoffset - safeoffset;
|
|
|
|
safetextlength = INITIAL_ARRAY_SIZE_;
|
2009-04-22 23:10:07 +00:00
|
|
|
safetext = addToUCharArray(safebuffer, &safetextlength, NULL,
|
|
|
|
text + safeoffset, safelength,
|
|
|
|
strsrch->canonicalSuffixAccents,
|
2001-08-25 02:03:53 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
safetextlength = u_strlen(strsrch->canonicalSuffixAccents);
|
|
|
|
safetext = strsrch->canonicalSuffixAccents;
|
|
|
|
}
|
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure, ucol_setText does nothing
|
2001-09-07 21:56:18 +00:00
|
|
|
ucol_setText(coleiter, safetext, safetextlength, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked in loop below
|
|
|
|
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *ce = strsrch->pattern.CE;
|
|
|
|
int32_t celength = strsrch->pattern.CELength;
|
2001-08-25 02:03:53 +00:00
|
|
|
int ceindex = celength - 1;
|
|
|
|
UBool isSafe = TRUE; // indication flag for position in safe zone
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
while (ceindex >= 0) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t textce = ucol_previous(coleiter, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
if (isSafe) {
|
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (textce == UCOL_NULLORDER) {
|
|
|
|
// check if we have passed the safe buffer
|
|
|
|
if (coleiter == strsrch->textIter) {
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
safetext = safebuffer;
|
|
|
|
coleiter = strsrch->textIter;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, safeoffset);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked at the start of the loop
|
2001-08-25 02:03:53 +00:00
|
|
|
isSafe = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
textce = getCE(strsrch, textce);
|
|
|
|
if (textce != UCOL_IGNORABLE && textce != ce[ceindex]) {
|
|
|
|
// do the beginning stuff
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t failedoffset = getColElemIterOffset(coleiter, FALSE);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (isSafe && failedoffset >= safelength) {
|
|
|
|
// alas... no hope. failed at rearranged accent set
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (isSafe) {
|
|
|
|
failedoffset += safeoffset;
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// try rearranging the front accents
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t result = doNextCanonicalPrefixMatch(strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
failedoffset, textoffset, status);
|
|
|
|
if (result != USEARCH_DONE) {
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure, ucol_setOffset does nothing
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, result);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (textce == ce[ceindex]) {
|
|
|
|
ceindex --;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set offset here
|
|
|
|
if (isSafe) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = getColElemIterOffset(coleiter, FALSE);
|
2001-08-25 02:03:53 +00:00
|
|
|
// sets the text iterator here with the correct expansion and offset
|
|
|
|
int32_t leftoverces = getExpansionPrefix(coleiter);
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (result >= safelength) {
|
2001-08-25 02:03:53 +00:00
|
|
|
result = textoffset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result += safeoffset;
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, result);
|
2009-04-22 23:10:07 +00:00
|
|
|
strsrch->textIter->iteratordata_.toReturn =
|
2001-08-25 02:03:53 +00:00
|
|
|
setExpansionPrefix(strsrch->textIter, leftoverces);
|
|
|
|
return result;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
return ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trying out the substring and sees if it can be a canonical match.
|
|
|
|
* This will try normalizing the end accents and arranging them into canonical
|
|
|
|
* equivalents and check their corresponding ces with the pattern ce.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Suffix accents in the text will be grouped according to their combining
|
|
|
|
* class and the groups will be mixed and matched to try find the perfect
|
2001-09-07 21:56:18 +00:00
|
|
|
* match with the pattern.
|
|
|
|
* So for instance looking for "\u0301" in "\u030A\u0301\u0325"
|
|
|
|
* step 1: split "\u030A\u0301" into 6 other type of potential accent substrings
|
2009-04-22 23:10:07 +00:00
|
|
|
* "\u030A", "\u0301", "\u0325", "\u030A\u0301", "\u030A\u0325",
|
2001-09-07 21:56:18 +00:00
|
|
|
* "\u0301\u0325".
|
2001-11-09 17:39:42 +00:00
|
|
|
* step 2: check if any of the generated substrings matches the pattern.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param textoffset end offset in the collation element text that ends with
|
2001-08-25 02:03:53 +00:00
|
|
|
* the accents to be rearranged
|
|
|
|
* @param status error status if any
|
|
|
|
* @return TRUE if the match is valid, FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool doNextCanonicalMatch(UStringSearch *strsrch,
|
|
|
|
int32_t textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
const UChar *text = strsrch->search->text;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = textoffset;
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(text, 0, temp);
|
2001-08-25 02:03:53 +00:00
|
|
|
if ((getFCD(text, &temp, textoffset) & LAST_BYTE_MASK_) == 0) {
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset = getColElemIterOffset(coleiter, FALSE);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (strsrch->pattern.hasPrefixAccents) {
|
2009-04-22 23:10:07 +00:00
|
|
|
offset = doNextCanonicalPrefixMatch(strsrch, offset, textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status) && offset != USEARCH_DONE) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, offset);
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strsrch->pattern.hasSuffixAccents) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar accents[INITIAL_ARRAY_SIZE_];
|
|
|
|
// offset to the last base character in substring to search
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t baseoffset = getPreviousBaseOffset(text, textoffset);
|
2001-08-25 02:03:53 +00:00
|
|
|
// normalizing the offensive string
|
2009-04-22 23:10:07 +00:00
|
|
|
unorm_normalize(text + baseoffset, textoffset - baseoffset, UNORM_NFD,
|
|
|
|
0, accents, INITIAL_ARRAY_SIZE_, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked in loop below
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t accentsindex[INITIAL_ARRAY_SIZE_];
|
|
|
|
int32_t size = getUnblockedAccentIndex(accents, accentsindex);
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2003-07-03 19:17:35 +00:00
|
|
|
// 2 power n - 1 plus the full set of accents
|
|
|
|
int32_t count = (2 << (size - 1)) - 1;
|
2001-08-25 02:03:53 +00:00
|
|
|
while (U_SUCCESS(*status) && count > 0) {
|
|
|
|
UChar *rearrange = strsrch->canonicalSuffixAccents;
|
|
|
|
// copy the base characters
|
|
|
|
for (int k = 0; k < accentsindex[0]; k ++) {
|
|
|
|
*rearrange ++ = accents[k];
|
|
|
|
}
|
|
|
|
// forming all possible canonical rearrangement by dropping
|
|
|
|
// sets of accents
|
|
|
|
for (int i = 0; i <= size - 1; i ++) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t mask = 1 << (size - i - 1);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (count & mask) {
|
|
|
|
for (int j = accentsindex[i]; j < accentsindex[i + 1]; j ++) {
|
|
|
|
*rearrange ++ = accents[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*rearrange = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t offset = doNextCanonicalSuffixMatch(strsrch, baseoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
status);
|
|
|
|
if (offset != USEARCH_DONE) {
|
|
|
|
return TRUE; // match found
|
|
|
|
}
|
|
|
|
count --;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Gets the previous base character offset depending on the string search
|
2001-08-25 02:03:53 +00:00
|
|
|
* pattern data
|
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset current offset, current character
|
|
|
|
* @return the offset of the next character after this base character or itself
|
|
|
|
* if it is a composed character with accents
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline int32_t getPreviousUStringSearchBaseOffset(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-07 21:56:18 +00:00
|
|
|
if (strsrch->pattern.hasPrefixAccents && textoffset > 0) {
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *text = strsrch->search->text;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset = textoffset;
|
2009-04-22 23:10:07 +00:00
|
|
|
if (getFCD(text, &offset, strsrch->search->textLength) >>
|
2001-08-25 02:03:53 +00:00
|
|
|
SECOND_LAST_BYTE_SHIFT_) {
|
|
|
|
return getPreviousBaseOffset(text, textoffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return textoffset;
|
|
|
|
}
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks match for contraction.
|
2001-09-07 21:56:18 +00:00
|
|
|
* If the match ends with a partial contraction we fail.
|
|
|
|
* If the match starts too far off (because of backwards iteration) we try to
|
|
|
|
* chip off the extra characters
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param start offset of potential match, to be modified if necessary
|
|
|
|
* @param end offset of potential match, to be modified if necessary
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-09-07 21:56:18 +00:00
|
|
|
* @return TRUE if match passes the contraction test, FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool checkNextCanonicalContractionMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *start,
|
|
|
|
int32_t *end,
|
|
|
|
UErrorCode *status)
|
2001-09-07 21:56:18 +00:00
|
|
|
{
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = *start;
|
2001-09-07 21:56:18 +00:00
|
|
|
const UCollator *collator = strsrch->collator;
|
|
|
|
const UChar *text = strsrch->search->text;
|
2009-04-22 23:10:07 +00:00
|
|
|
// This part checks if either ends of the match contains potential
|
2001-09-07 21:56:18 +00:00
|
|
|
// contraction. If so we'll have to iterate through them
|
2009-04-22 23:10:07 +00:00
|
|
|
if ((*end < textlength && ucol_unsafeCP(text[*end], collator)) ||
|
|
|
|
(*start + 1 < textlength
|
2002-12-06 17:55:33 +00:00
|
|
|
&& ucol_unsafeCP(text[*start + 1], collator))) {
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t expansion = getExpansionPrefix(coleiter);
|
|
|
|
UBool expandflag = expansion > 0;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, *start);
|
|
|
|
while (expansion > 0) {
|
2001-09-07 21:56:18 +00:00
|
|
|
// getting rid of the redundant ce, caused by setOffset.
|
2009-04-22 23:10:07 +00:00
|
|
|
// since backward contraction/expansion may have extra ces if we
|
|
|
|
// are in the normalization buffer, hasAccentsBeforeMatch would
|
2001-09-07 21:56:18 +00:00
|
|
|
// have taken care of it.
|
|
|
|
// E.g. the character \u01FA will have an expansion of 3, but if
|
|
|
|
// we are only looking for acute and ring \u030A and \u0301, we'll
|
|
|
|
// have to skip the first ce in the expansion buffer.
|
|
|
|
ucol_next(coleiter, status);
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (ucol_getOffset(coleiter) != temp) {
|
|
|
|
*start = temp;
|
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
|
|
|
expansion --;
|
|
|
|
}
|
|
|
|
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
|
|
|
int32_t count = 0;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
|
|
|
while (count < patterncelength) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_next(coleiter, status));
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below, note that if status is a failure
|
|
|
|
// ucol_next returns UCOL_NULLORDER
|
2001-09-07 21:56:18 +00:00
|
|
|
if (ce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (expandflag && count == 0 && ucol_getOffset(coleiter) != temp) {
|
|
|
|
*start = temp;
|
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 && ce != patternce[0]) {
|
2009-04-22 23:10:07 +00:00
|
|
|
// accents may have extra starting ces, this occurs when a
|
2001-09-07 21:56:18 +00:00
|
|
|
// pure accent pattern is matched without rearrangement
|
|
|
|
// text \u0325\u0300 and looking for \u0300
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t expected = patternce[0];
|
2001-09-07 21:56:18 +00:00
|
|
|
if (getFCD(text, start, textlength) & LAST_BYTE_MASK_) {
|
|
|
|
ce = getCE(strsrch, ucol_next(coleiter, status));
|
2009-04-22 23:10:07 +00:00
|
|
|
while (U_SUCCESS(*status) && ce != expected &&
|
2001-11-09 17:39:42 +00:00
|
|
|
ce != UCOL_NULLORDER &&
|
2001-09-07 21:56:18 +00:00
|
|
|
ucol_getOffset(coleiter) <= *end) {
|
|
|
|
ce = getCE(strsrch, ucol_next(coleiter, status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(*status) || ce != patternce[count]) {
|
|
|
|
(*end) ++;
|
2009-04-22 23:10:07 +00:00
|
|
|
*end = getNextUStringSearchBaseOffset(strsrch, *end);
|
2001-09-07 21:56:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
count ++;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
|
|
|
* Checks and sets the match information if found.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks
|
2001-08-25 02:03:53 +00:00
|
|
|
* <ul>
|
|
|
|
* <li> the potential match does not repeat the previous match
|
|
|
|
* <li> boundaries are correct
|
|
|
|
* <li> potential match does not end in the middle of a contraction
|
|
|
|
* <li> identical matches
|
|
|
|
* <\ul>
|
|
|
|
* Otherwise the offset will be shifted to the next character.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check the
|
2001-11-09 17:39:42 +00:00
|
|
|
* status before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset offset in the collation element text. the returned value
|
2009-04-22 23:10:07 +00:00
|
|
|
* will be the truncated end offset of the match or the new start
|
2001-08-25 02:03:53 +00:00
|
|
|
* search offset.
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return TRUE if the match is valid, FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UBool checkNextCanonicalMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *textoffset,
|
2001-09-07 21:56:18 +00:00
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
// to ensure that the start and ends are not composite characters
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2001-09-07 21:56:18 +00:00
|
|
|
// if we have a canonical accent match
|
2009-04-22 23:10:07 +00:00
|
|
|
if ((strsrch->pattern.hasSuffixAccents &&
|
|
|
|
strsrch->canonicalSuffixAccents[0]) ||
|
|
|
|
(strsrch->pattern.hasPrefixAccents &&
|
2001-09-07 21:56:18 +00:00
|
|
|
strsrch->canonicalPrefixAccents[0])) {
|
|
|
|
strsrch->search->matchedIndex = getPreviousUStringSearchBaseOffset(
|
|
|
|
strsrch,
|
|
|
|
ucol_getOffset(coleiter));
|
2009-04-22 23:10:07 +00:00
|
|
|
strsrch->search->matchedLength = *textoffset -
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->matchedIndex;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start = getColElemIterOffset(coleiter, FALSE);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (!checkNextCanonicalContractionMatch(strsrch, &start, textoffset,
|
2001-11-09 17:39:42 +00:00
|
|
|
status) || U_FAILURE(*status)) {
|
2001-09-07 21:56:18 +00:00
|
|
|
return FALSE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
start = getPreviousUStringSearchBaseOffset(strsrch, start);
|
|
|
|
// this totally matches, however we need to check if it is repeating
|
2009-04-22 23:10:07 +00:00
|
|
|
if (checkRepeatedMatch(strsrch, start, *textoffset) ||
|
|
|
|
!isBreakUnit(strsrch, start, *textoffset) ||
|
2001-09-07 21:56:18 +00:00
|
|
|
!checkIdentical(strsrch, start, *textoffset)) {
|
|
|
|
(*textoffset) ++;
|
2009-04-22 23:10:07 +00:00
|
|
|
*textoffset = getNextBaseOffset(strsrch->search->text, *textoffset,
|
2001-09-07 21:56:18 +00:00
|
|
|
strsrch->search->textLength);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
strsrch->search->matchedIndex = start;
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->matchedLength = *textoffset - start;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shifting the collation element iterator position forward to prepare for
|
|
|
|
* a preceding match. If the first character is a unsafe character, we'll only
|
|
|
|
* shift by 1 to capture contractions, normalization etc.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param text strsrch string search data
|
|
|
|
* @param textoffset start text position to do search
|
|
|
|
* @param ce the text ce which failed the match.
|
|
|
|
* @param patternceindex index of the ce within the pattern ce buffer which
|
|
|
|
* failed the match
|
|
|
|
* @return final offset
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2002-03-12 01:32:42 +00:00
|
|
|
inline int32_t reverseShift(UStringSearch *strsrch,
|
|
|
|
int32_t textoffset,
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce,
|
2002-08-26 16:52:05 +00:00
|
|
|
int32_t patternceindex)
|
2009-04-22 23:10:07 +00:00
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (strsrch->search->isOverlap) {
|
|
|
|
if (textoffset != strsrch->search->textLength) {
|
|
|
|
textoffset --;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2001-11-09 17:39:42 +00:00
|
|
|
textoffset -= strsrch->pattern.defaultShiftSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ce != UCOL_NULLORDER) {
|
|
|
|
int32_t shift = strsrch->pattern.backShift[hash(ce)];
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
// this is to adjust for characters in the middle of the substring
|
2001-11-09 17:39:42 +00:00
|
|
|
// for matching that failed.
|
|
|
|
int32_t adjust = patternceindex;
|
|
|
|
if (adjust > 1 && shift > adjust) {
|
|
|
|
shift -= adjust - 1;
|
2001-09-07 21:56:18 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
textoffset -= shift;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textoffset -= strsrch->pattern.defaultShiftSize;
|
2001-09-07 21:56:18 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
textoffset = getPreviousUStringSearchBaseOffset(strsrch, textoffset);
|
2001-08-25 02:03:53 +00:00
|
|
|
return textoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks match for contraction.
|
2001-09-07 21:56:18 +00:00
|
|
|
* If the match starts with a partial contraction we fail.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param start offset of potential match, to be modified if necessary
|
|
|
|
* @param end offset of potential match, to be modified if necessary
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-09-07 21:56:18 +00:00
|
|
|
* @return TRUE if match passes the contraction test, FALSE otherwise
|
2001-08-25 02:03:53 +00:00
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool checkPreviousExactContractionMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *start,
|
|
|
|
int32_t *end, UErrorCode *status)
|
2001-09-07 21:56:18 +00:00
|
|
|
{
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2002-12-03 23:59:06 +00:00
|
|
|
int32_t temp = *end;
|
2001-09-07 21:56:18 +00:00
|
|
|
const UCollator *collator = strsrch->collator;
|
|
|
|
const UChar *text = strsrch->search->text;
|
2009-04-22 23:10:07 +00:00
|
|
|
// This part checks if either if the start of the match contains potential
|
2001-09-07 21:56:18 +00:00
|
|
|
// contraction. If so we'll have to iterate through them
|
2009-04-22 23:10:07 +00:00
|
|
|
// Since we used ucol_next while previously looking for the potential
|
2003-08-31 20:53:46 +00:00
|
|
|
// match, this guarantees that our end will not be a partial contraction,
|
|
|
|
// or a partial supplementary character.
|
2002-12-03 23:59:06 +00:00
|
|
|
if (*start < textlength && ucol_unsafeCP(text[*start], collator)) {
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t expansion = getExpansionSuffix(coleiter);
|
|
|
|
UBool expandflag = expansion > 0;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, *end);
|
2001-11-09 17:39:42 +00:00
|
|
|
while (U_SUCCESS(*status) && expansion > 0) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// getting rid of the redundant ce
|
|
|
|
// since forward contraction/expansion may have extra ces
|
|
|
|
// if we are in the normalization buffer, hasAccentsBeforeMatch
|
|
|
|
// would have taken care of it.
|
2001-09-07 21:56:18 +00:00
|
|
|
// E.g. the character \u01FA will have an expansion of 3, but if
|
2009-04-22 23:10:07 +00:00
|
|
|
// we are only looking for A ring A\u030A, we'll have to skip the
|
2001-09-07 21:56:18 +00:00
|
|
|
// last ce in the expansion buffer
|
2001-08-25 02:03:53 +00:00
|
|
|
ucol_previous(coleiter, status);
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (ucol_getOffset(coleiter) != temp) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*end = temp;
|
2001-08-25 02:03:53 +00:00
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
|
|
|
expansion --;
|
|
|
|
}
|
|
|
|
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t count = patterncelength;
|
2001-08-25 02:03:53 +00:00
|
|
|
while (count > 0) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_previous(coleiter, status));
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below, note that if status is a failure
|
|
|
|
// ucol_previous returns UCOL_NULLORDER
|
2001-08-25 02:03:53 +00:00
|
|
|
if (ce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
if (expandflag && count == 0 &&
|
2001-08-25 02:03:53 +00:00
|
|
|
getColElemIterOffset(coleiter, FALSE) != temp) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*end = temp;
|
2001-08-25 02:03:53 +00:00
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(*status) || ce != patternce[count - 1]) {
|
2001-09-07 21:56:18 +00:00
|
|
|
(*start) --;
|
|
|
|
*start = getPreviousBaseOffset(text, *start);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
count --;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
/**
|
|
|
|
* Checks and sets the match information if found.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks
|
2001-09-07 21:56:18 +00:00
|
|
|
* <ul>
|
|
|
|
* <li> the current match does not repeat the last match
|
|
|
|
* <li> boundaries are correct
|
|
|
|
* <li> exact matches has no extra accents
|
|
|
|
* <li> identical matches
|
|
|
|
* <\ul>
|
|
|
|
* Otherwise the offset will be shifted to the preceding character.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method.
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param collator
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param coleiter collation element iterator
|
|
|
|
* @param text string
|
|
|
|
* @param textoffset offset in the collation element text. the returned value
|
2009-04-22 23:10:07 +00:00
|
|
|
* will be the truncated start offset of the match or the new start
|
2001-09-07 21:56:18 +00:00
|
|
|
* search offset.
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-09-07 21:56:18 +00:00
|
|
|
* @return TRUE if the match is valid, FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UBool checkPreviousExactMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *textoffset,
|
2001-09-07 21:56:18 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
// to ensure that the start and ends are not composite characters
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t end = ucol_getOffset(strsrch->textIter);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (!checkPreviousExactContractionMatch(strsrch, textoffset, &end, status)
|
|
|
|
|| U_FAILURE(*status)) {
|
2001-09-07 21:56:18 +00:00
|
|
|
return FALSE;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
// this totally matches, however we need to check if it is repeating
|
|
|
|
// the old match
|
2009-04-22 23:10:07 +00:00
|
|
|
if (checkRepeatedMatch(strsrch, *textoffset, end) ||
|
2001-11-09 17:39:42 +00:00
|
|
|
!isBreakUnit(strsrch, *textoffset, end) ||
|
|
|
|
hasAccentsBeforeMatch(strsrch, *textoffset, end) ||
|
2009-04-22 23:10:07 +00:00
|
|
|
!checkIdentical(strsrch, *textoffset, end) ||
|
2001-11-09 17:39:42 +00:00
|
|
|
hasAccentsAfterMatch(strsrch, *textoffset, end)) {
|
|
|
|
(*textoffset) --;
|
2009-04-22 23:10:07 +00:00
|
|
|
*textoffset = getPreviousBaseOffset(strsrch->search->text,
|
2001-11-09 17:39:42 +00:00
|
|
|
*textoffset);
|
|
|
|
return FALSE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-10-18 22:29:39 +00:00
|
|
|
//Add breakiterator boundary check for primary strength search.
|
2007-10-25 22:02:54 +00:00
|
|
|
if (!strsrch->search->breakIter && strsrch->strength == UCOL_PRIMARY) {
|
2011-03-22 03:50:26 +00:00
|
|
|
checkBreakBoundary(strsrch, textoffset, &end);
|
2007-10-18 22:29:39 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
strsrch->search->matchedIndex = *textoffset;
|
|
|
|
strsrch->search->matchedLength = end - *textoffset;
|
|
|
|
return TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-09-07 21:56:18 +00:00
|
|
|
* Rearranges the end accents to try matching.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Suffix accents in the text will be grouped according to their combining
|
|
|
|
* class and the groups will be mixed and matched to try find the perfect
|
2001-09-07 21:56:18 +00:00
|
|
|
* match with the pattern.
|
|
|
|
* So for instance looking for "\u0301" in "\u030A\u0301\u0325"
|
|
|
|
* step 1: split "\u030A\u0301" into 6 other type of potential accent substrings
|
2009-04-22 23:10:07 +00:00
|
|
|
* "\u030A", "\u0301", "\u0325", "\u030A\u0301", "\u030A\u0325",
|
2001-09-07 21:56:18 +00:00
|
|
|
* "\u0301\u0325".
|
2001-11-09 17:39:42 +00:00
|
|
|
* step 2: check if any of the generated substrings matches the pattern.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, user has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search match
|
|
|
|
* @param start offset of the first base character
|
|
|
|
* @param end start of the last accent set
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status only error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return USEARCH_DONE if a match is not found, otherwise return the ending
|
|
|
|
* offset of the match. Note this start includes all following accents.
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t doPreviousCanonicalSuffixMatch(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t end,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
const UChar *text = strsrch->search->text;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t tempend = end;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(text, 0, tempend);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (!(getFCD(text, &tempend, strsrch->search->textLength) &
|
2001-09-07 21:56:18 +00:00
|
|
|
LAST_BYTE_MASK_)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// die... failed at a base character
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
end = getNextBaseOffset(text, end, strsrch->search->textLength);
|
|
|
|
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
UChar accents[INITIAL_ARRAY_SIZE_];
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset = getPreviousBaseOffset(text, end);
|
2001-08-25 02:03:53 +00:00
|
|
|
// normalizing the offensive string
|
2009-04-22 23:10:07 +00:00
|
|
|
unorm_normalize(text + offset, end - offset, UNORM_NFD, 0, accents,
|
|
|
|
INITIAL_ARRAY_SIZE_, status);
|
|
|
|
|
|
|
|
int32_t accentsindex[INITIAL_ARRAY_SIZE_];
|
|
|
|
int32_t accentsize = getUnblockedAccentIndex(accents,
|
2001-08-25 02:03:53 +00:00
|
|
|
accentsindex);
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t count = (2 << (accentsize - 1)) - 1;
|
2001-09-11 21:40:05 +00:00
|
|
|
UChar buffer[INITIAL_ARRAY_SIZE_];
|
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
2001-08-25 02:03:53 +00:00
|
|
|
while (U_SUCCESS(*status) && count > 0) {
|
|
|
|
UChar *rearrange = strsrch->canonicalSuffixAccents;
|
|
|
|
// copy the base characters
|
|
|
|
for (int k = 0; k < accentsindex[0]; k ++) {
|
|
|
|
*rearrange ++ = accents[k];
|
|
|
|
}
|
|
|
|
// forming all possible canonical rearrangement by dropping
|
|
|
|
// sets of accents
|
|
|
|
for (int i = 0; i <= accentsize - 1; i ++) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t mask = 1 << (accentsize - i - 1);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (count & mask) {
|
|
|
|
for (int j = accentsindex[i]; j < accentsindex[i + 1]; j ++) {
|
|
|
|
*rearrange ++ = accents[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*rearrange = 0;
|
|
|
|
int32_t matchsize = INITIAL_ARRAY_SIZE_;
|
|
|
|
UChar *match = addToUCharArray(buffer, &matchsize,
|
|
|
|
strsrch->canonicalPrefixAccents,
|
|
|
|
strsrch->search->text + start,
|
|
|
|
offset - start,
|
|
|
|
strsrch->canonicalSuffixAccents,
|
|
|
|
status);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// run the collator iterator through this match
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure ucol_setText does nothing
|
2001-08-25 02:03:53 +00:00
|
|
|
ucol_setText(coleiter, match, matchsize, status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
if (checkCollationMatch(strsrch, coleiter)) {
|
|
|
|
if (match != buffer) {
|
|
|
|
uprv_free(match);
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count --;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the rearranged start accents and tries matching. If match failed at
|
|
|
|
* a seperate following set of accents (seperated from the rearranged on by
|
2009-04-22 23:10:07 +00:00
|
|
|
* at least a base character) then we rearrange the preceding accents and
|
2001-08-25 02:03:53 +00:00
|
|
|
* tries matching again.
|
2009-04-22 23:10:07 +00:00
|
|
|
* We allow skipping of the ends of the accent set if the ces do not match.
|
2001-08-25 02:03:53 +00:00
|
|
|
* However if the failure is found before the accent set, it fails.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
2001-11-09 17:39:42 +00:00
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset of the ends of the rearranged accent
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return USEARCH_DONE if a match is not found, otherwise return the ending
|
|
|
|
* offset of the match. Note this start includes all following accents.
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t doPreviousCanonicalPrefixMatch(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-09-07 21:56:18 +00:00
|
|
|
const UChar *text = strsrch->search->text;
|
|
|
|
const UCollator *collator = strsrch->collator;
|
|
|
|
int32_t safelength = 0;
|
|
|
|
UChar *safetext;
|
|
|
|
int32_t safetextlength;
|
|
|
|
UChar safebuffer[INITIAL_ARRAY_SIZE_];
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t safeoffset = textoffset;
|
2001-09-07 21:56:18 +00:00
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
if (textoffset &&
|
2001-09-07 21:56:18 +00:00
|
|
|
ucol_unsafeCP(strsrch->canonicalPrefixAccents[
|
2001-08-25 02:03:53 +00:00
|
|
|
u_strlen(strsrch->canonicalPrefixAccents) - 1
|
|
|
|
], collator)) {
|
2009-04-22 23:10:07 +00:00
|
|
|
safeoffset = getNextSafeOffset(collator, text, textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->textLength);
|
|
|
|
safelength = safeoffset - textoffset;
|
|
|
|
safetextlength = INITIAL_ARRAY_SIZE_;
|
2009-04-22 23:10:07 +00:00
|
|
|
safetext = addToUCharArray(safebuffer, &safetextlength,
|
|
|
|
strsrch->canonicalPrefixAccents,
|
|
|
|
text + textoffset, safelength,
|
2001-08-25 02:03:53 +00:00
|
|
|
NULL, status);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
safetextlength = u_strlen(strsrch->canonicalPrefixAccents);
|
|
|
|
safetext = strsrch->canonicalPrefixAccents;
|
|
|
|
}
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
UCollationElements *coleiter = strsrch->utilIter;
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure, ucol_setText does nothing
|
2001-09-07 21:56:18 +00:00
|
|
|
ucol_setText(coleiter, safetext, safetextlength, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked in loop below
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *ce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t celength = strsrch->pattern.CELength;
|
|
|
|
int ceindex = 0;
|
|
|
|
UBool isSafe = TRUE; // safe zone indication flag for position
|
|
|
|
int32_t prefixlength = u_strlen(strsrch->canonicalPrefixAccents);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
while (ceindex < celength) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t textce = ucol_next(coleiter, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
if (isSafe) {
|
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
if (textce == UCOL_NULLORDER) {
|
|
|
|
// check if we have passed the safe buffer
|
|
|
|
if (coleiter == strsrch->textIter) {
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
safetext = safebuffer;
|
|
|
|
coleiter = strsrch->textIter;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, safeoffset);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked at the start of the loop
|
2001-08-25 02:03:53 +00:00
|
|
|
isSafe = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
textce = getCE(strsrch, textce);
|
|
|
|
if (textce != UCOL_IGNORABLE && textce != ce[ceindex]) {
|
|
|
|
// do the beginning stuff
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t failedoffset = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (isSafe && failedoffset <= prefixlength) {
|
|
|
|
// alas... no hope. failed at rearranged accent set
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (isSafe) {
|
|
|
|
failedoffset = safeoffset - failedoffset;
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// try rearranging the end accents
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t result = doPreviousCanonicalSuffixMatch(strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
textoffset, failedoffset, status);
|
|
|
|
if (result != USEARCH_DONE) {
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure, ucol_setOffset does nothing
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, result);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (textce == ce[ceindex]) {
|
|
|
|
ceindex ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set offset here
|
|
|
|
if (isSafe) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
// sets the text iterator here with the correct expansion and offset
|
|
|
|
int32_t leftoverces = getExpansionSuffix(coleiter);
|
2001-09-11 21:40:05 +00:00
|
|
|
cleanUpSafeText(strsrch, safetext, safebuffer);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (result <= prefixlength) {
|
2001-08-25 02:03:53 +00:00
|
|
|
result = textoffset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = textoffset + (safeoffset - result);
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, result);
|
2001-08-25 02:03:53 +00:00
|
|
|
setExpansionSuffix(strsrch->textIter, leftoverces);
|
|
|
|
return result;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
return ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trying out the substring and sees if it can be a canonical match.
|
2009-04-22 23:10:07 +00:00
|
|
|
* This will try normalizing the starting accents and arranging them into
|
2001-08-25 02:03:53 +00:00
|
|
|
* canonical equivalents and check their corresponding ces with the pattern ce.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Prefix accents in the text will be grouped according to their combining
|
|
|
|
* class and the groups will be mixed and matched to try find the perfect
|
2001-09-07 21:56:18 +00:00
|
|
|
* match with the pattern.
|
|
|
|
* So for instance looking for "\u0301" in "\u030A\u0301\u0325"
|
|
|
|
* step 1: split "\u030A\u0301" into 6 other type of potential accent substrings
|
2009-04-22 23:10:07 +00:00
|
|
|
* "\u030A", "\u0301", "\u0325", "\u030A\u0301", "\u030A\u0325",
|
2001-09-07 21:56:18 +00:00
|
|
|
* "\u0301\u0325".
|
2001-11-09 17:39:42 +00:00
|
|
|
* step 2: check if any of the generated substrings matches the pattern.
|
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
2009-04-22 23:10:07 +00:00
|
|
|
* @param textoffset start offset in the collation element text that starts
|
2001-08-25 02:03:53 +00:00
|
|
|
* with the accents to be rearranged
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status output error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return TRUE if the match is valid, FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool doPreviousCanonicalMatch(UStringSearch *strsrch,
|
|
|
|
int32_t textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
const UChar *text = strsrch->search->text;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = textoffset;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength = strsrch->search->textLength;
|
|
|
|
if ((getFCD(text, &temp, textlength) >> SECOND_LAST_BYTE_SHIFT_) == 0) {
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (strsrch->pattern.hasSuffixAccents) {
|
2009-04-22 23:10:07 +00:00
|
|
|
offset = doPreviousCanonicalSuffixMatch(strsrch, textoffset,
|
2001-08-25 02:03:53 +00:00
|
|
|
offset, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status) && offset != USEARCH_DONE) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, offset);
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strsrch->pattern.hasPrefixAccents) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar accents[INITIAL_ARRAY_SIZE_];
|
|
|
|
// offset to the last base character in substring to search
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t baseoffset = getNextBaseOffset(text, textoffset, textlength);
|
2001-08-25 02:03:53 +00:00
|
|
|
// normalizing the offensive string
|
2009-04-22 23:10:07 +00:00
|
|
|
unorm_normalize(text + textoffset, baseoffset - textoffset, UNORM_NFD,
|
|
|
|
0, accents, INITIAL_ARRAY_SIZE_, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked in loop
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t accentsindex[INITIAL_ARRAY_SIZE_];
|
|
|
|
int32_t size = getUnblockedAccentIndex(accents, accentsindex);
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2003-07-03 19:17:35 +00:00
|
|
|
// 2 power n - 1 plus the full set of accents
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t count = (2 << (size - 1)) - 1;
|
2001-08-25 02:03:53 +00:00
|
|
|
while (U_SUCCESS(*status) && count > 0) {
|
|
|
|
UChar *rearrange = strsrch->canonicalPrefixAccents;
|
|
|
|
// copy the base characters
|
|
|
|
for (int k = 0; k < accentsindex[0]; k ++) {
|
|
|
|
*rearrange ++ = accents[k];
|
|
|
|
}
|
|
|
|
// forming all possible canonical rearrangement by dropping
|
|
|
|
// sets of accents
|
|
|
|
for (int i = 0; i <= size - 1; i ++) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t mask = 1 << (size - i - 1);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (count & mask) {
|
|
|
|
for (int j = accentsindex[i]; j < accentsindex[i + 1]; j ++) {
|
|
|
|
*rearrange ++ = accents[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*rearrange = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t offset = doPreviousCanonicalPrefixMatch(strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
baseoffset, status);
|
|
|
|
if (offset != USEARCH_DONE) {
|
|
|
|
return TRUE; // match found
|
|
|
|
}
|
|
|
|
count --;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks match for contraction.
|
2001-09-07 21:56:18 +00:00
|
|
|
* If the match starts with a partial contraction we fail.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method.
|
2001-09-07 21:56:18 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param start offset of potential match, to be modified if necessary
|
|
|
|
* @param end offset of potential match, to be modified if necessary
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status only error status if any
|
2001-09-07 21:56:18 +00:00
|
|
|
* @return TRUE if match passes the contraction test, FALSE otherwise
|
|
|
|
*/
|
2001-10-20 01:09:31 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool checkPreviousCanonicalContractionMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *start,
|
|
|
|
int32_t *end, UErrorCode *status)
|
2001-09-07 21:56:18 +00:00
|
|
|
{
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t temp = *end;
|
2001-09-07 21:56:18 +00:00
|
|
|
const UCollator *collator = strsrch->collator;
|
|
|
|
const UChar *text = strsrch->search->text;
|
2009-04-22 23:10:07 +00:00
|
|
|
// This part checks if either if the start of the match contains potential
|
2001-09-07 21:56:18 +00:00
|
|
|
// contraction. If so we'll have to iterate through them
|
2009-04-22 23:10:07 +00:00
|
|
|
// Since we used ucol_next while previously looking for the potential
|
2003-08-31 20:53:46 +00:00
|
|
|
// match, this guarantees that our end will not be a partial contraction,
|
|
|
|
// or a partial supplementary character.
|
2002-12-06 17:55:33 +00:00
|
|
|
if (*start < textlength && ucol_unsafeCP(text[*start], collator)) {
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t expansion = getExpansionSuffix(coleiter);
|
|
|
|
UBool expandflag = expansion > 0;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, *end);
|
|
|
|
while (expansion > 0) {
|
2001-09-07 21:56:18 +00:00
|
|
|
// getting rid of the redundant ce
|
|
|
|
// since forward contraction/expansion may have extra ces
|
|
|
|
// if we are in the normalization buffer, hasAccentsBeforeMatch
|
|
|
|
// would have taken care of it.
|
|
|
|
// E.g. the character \u01FA will have an expansion of 3, but if
|
2009-04-22 23:10:07 +00:00
|
|
|
// we are only looking for A ring A\u030A, we'll have to skip the
|
2001-09-07 21:56:18 +00:00
|
|
|
// last ce in the expansion buffer
|
|
|
|
ucol_previous(coleiter, status);
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (ucol_getOffset(coleiter) != temp) {
|
|
|
|
*end = temp;
|
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
|
|
|
expansion --;
|
|
|
|
}
|
|
|
|
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
|
|
|
int32_t count = patterncelength;
|
|
|
|
while (count > 0) {
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t ce = getCE(strsrch, ucol_previous(coleiter, status));
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below, note that if status is a failure
|
|
|
|
// ucol_previous returns UCOL_NULLORDER
|
2001-09-07 21:56:18 +00:00
|
|
|
if (ce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
if (expandflag && count == 0 &&
|
2001-09-07 21:56:18 +00:00
|
|
|
getColElemIterOffset(coleiter, FALSE) != temp) {
|
|
|
|
*end = temp;
|
|
|
|
temp = ucol_getOffset(coleiter);
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
if (count == patterncelength &&
|
2001-09-07 21:56:18 +00:00
|
|
|
ce != patternce[patterncelength - 1]) {
|
2009-04-22 23:10:07 +00:00
|
|
|
// accents may have extra starting ces, this occurs when a
|
2001-09-07 21:56:18 +00:00
|
|
|
// pure accent pattern is matched without rearrangement
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t expected = patternce[patterncelength - 1];
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(text, 0, *end);
|
2001-09-07 21:56:18 +00:00
|
|
|
if (getFCD(text, end, textlength) & LAST_BYTE_MASK_) {
|
|
|
|
ce = getCE(strsrch, ucol_previous(coleiter, status));
|
2009-04-22 23:10:07 +00:00
|
|
|
while (U_SUCCESS(*status) && ce != expected &&
|
2001-11-09 17:39:42 +00:00
|
|
|
ce != UCOL_NULLORDER &&
|
2001-09-07 21:56:18 +00:00
|
|
|
ucol_getOffset(coleiter) <= *start) {
|
|
|
|
ce = getCE(strsrch, ucol_previous(coleiter, status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(*status) || ce != patternce[count - 1]) {
|
|
|
|
(*start) --;
|
|
|
|
*start = getPreviousBaseOffset(text, *start);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
count --;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
|
|
|
* Checks and sets the match information if found.
|
2009-04-22 23:10:07 +00:00
|
|
|
* Checks
|
2001-08-25 02:03:53 +00:00
|
|
|
* <ul>
|
|
|
|
* <li> the potential match does not repeat the previous match
|
|
|
|
* <li> boundaries are correct
|
|
|
|
* <li> potential match does not end in the middle of a contraction
|
|
|
|
* <li> identical matches
|
|
|
|
* <\ul>
|
|
|
|
* Otherwise the offset will be shifted to the next character.
|
2001-11-09 17:39:42 +00:00
|
|
|
* Internal method, status assumed to be success, caller has to check status
|
|
|
|
* before calling this method.
|
2001-08-25 02:03:53 +00:00
|
|
|
* @param strsrch string search data
|
|
|
|
* @param textoffset offset in the collation element text. the returned value
|
2009-04-22 23:10:07 +00:00
|
|
|
* will be the truncated start offset of the match or the new start
|
2001-08-25 02:03:53 +00:00
|
|
|
* search offset.
|
2001-11-09 17:39:42 +00:00
|
|
|
* @param status only error status if any
|
2001-08-25 02:03:53 +00:00
|
|
|
* @return TRUE if the match is valid, FALSE otherwise
|
|
|
|
*/
|
2001-10-22 05:30:22 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
inline UBool checkPreviousCanonicalMatch(UStringSearch *strsrch,
|
|
|
|
int32_t *textoffset,
|
2001-09-07 21:56:18 +00:00
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
// to ensure that the start and ends are not composite characters
|
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2001-09-07 21:56:18 +00:00
|
|
|
// if we have a canonical accent match
|
2009-04-22 23:10:07 +00:00
|
|
|
if ((strsrch->pattern.hasSuffixAccents &&
|
|
|
|
strsrch->canonicalSuffixAccents[0]) ||
|
|
|
|
(strsrch->pattern.hasPrefixAccents &&
|
2001-09-07 21:56:18 +00:00
|
|
|
strsrch->canonicalPrefixAccents[0])) {
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->matchedIndex = *textoffset;
|
2009-04-22 23:10:07 +00:00
|
|
|
strsrch->search->matchedLength =
|
|
|
|
getNextUStringSearchBaseOffset(strsrch,
|
2001-09-07 21:56:18 +00:00
|
|
|
getColElemIterOffset(coleiter, FALSE))
|
|
|
|
- *textoffset;
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t end = ucol_getOffset(coleiter);
|
2001-09-07 21:56:18 +00:00
|
|
|
if (!checkPreviousCanonicalContractionMatch(strsrch, textoffset, &end,
|
2009-04-22 23:10:07 +00:00
|
|
|
status) ||
|
2001-11-09 17:39:42 +00:00
|
|
|
U_FAILURE(*status)) {
|
2001-09-07 21:56:18 +00:00
|
|
|
return FALSE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end = getNextUStringSearchBaseOffset(strsrch, end);
|
|
|
|
// this totally matches, however we need to check if it is repeating
|
2009-04-22 23:10:07 +00:00
|
|
|
if (checkRepeatedMatch(strsrch, *textoffset, end) ||
|
|
|
|
!isBreakUnit(strsrch, *textoffset, end) ||
|
2001-09-07 21:56:18 +00:00
|
|
|
!checkIdentical(strsrch, *textoffset, end)) {
|
|
|
|
(*textoffset) --;
|
2009-04-22 23:10:07 +00:00
|
|
|
*textoffset = getPreviousBaseOffset(strsrch->search->text,
|
2001-09-07 21:56:18 +00:00
|
|
|
*textoffset);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->matchedIndex = *textoffset;
|
|
|
|
strsrch->search->matchedLength = end - *textoffset;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
#endif // #if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
// constructors and destructor -------------------------------------------
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern,
|
|
|
|
int32_t patternlength,
|
|
|
|
const UChar *text,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength,
|
|
|
|
const char *locale,
|
|
|
|
UBreakIterator *breakiter,
|
2009-04-22 23:10:07 +00:00
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-11-09 22:42:20 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-09-20 01:54:48 +00:00
|
|
|
#if UCONFIG_NO_BREAK_ITERATION
|
|
|
|
if (breakiter != NULL) {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2001-09-20 14:00:02 +00:00
|
|
|
if (locale) {
|
|
|
|
// ucol_open internally checks for status
|
|
|
|
UCollator *collator = ucol_open(locale, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// pattern, text checks are done in usearch_openFromCollator
|
2009-04-22 23:10:07 +00:00
|
|
|
UStringSearch *result = usearch_openFromCollator(pattern,
|
|
|
|
patternlength, text, textlength,
|
2001-09-11 21:40:05 +00:00
|
|
|
collator, breakiter, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-09-20 14:00:02 +00:00
|
|
|
if (result == NULL || U_FAILURE(*status)) {
|
|
|
|
if (collator) {
|
2001-09-11 21:40:05 +00:00
|
|
|
ucol_close(collator);
|
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result->ownCollator = TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
return result;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2001-08-25 02:03:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UStringSearch * U_EXPORT2 usearch_openFromCollator(
|
2009-04-22 23:10:07 +00:00
|
|
|
const UChar *pattern,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patternlength,
|
2009-04-22 23:10:07 +00:00
|
|
|
const UChar *text,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength,
|
|
|
|
const UCollator *collator,
|
|
|
|
UBreakIterator *breakiter,
|
2009-04-22 23:10:07 +00:00
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-11-09 22:42:20 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-09-20 01:54:48 +00:00
|
|
|
#if UCONFIG_NO_BREAK_ITERATION
|
|
|
|
if (breakiter != NULL) {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2001-09-20 14:00:02 +00:00
|
|
|
if (pattern == NULL || text == NULL || collator == NULL) {
|
2001-08-25 02:03:53 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2006-03-30 09:26:24 +00:00
|
|
|
return NULL;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2004-02-16 19:08:50 +00:00
|
|
|
// string search does not really work when numeric collation is turned on
|
|
|
|
if(ucol_getAttribute(collator, UCOL_NUMERIC_COLLATION, status) == UCOL_ON) {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
2006-03-30 09:26:24 +00:00
|
|
|
return NULL;
|
2004-02-16 19:08:50 +00:00
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
if (U_SUCCESS(*status)) {
|
2001-09-20 14:00:02 +00:00
|
|
|
initializeFCD(status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
UStringSearch *result;
|
2001-09-07 21:56:18 +00:00
|
|
|
if (textlength == -1) {
|
|
|
|
textlength = u_strlen(text);
|
|
|
|
}
|
|
|
|
if (patternlength == -1) {
|
|
|
|
patternlength = u_strlen(pattern);
|
|
|
|
}
|
2001-09-20 14:00:02 +00:00
|
|
|
if (textlength <= 0 || patternlength <= 0) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
result = (UStringSearch *)uprv_malloc(sizeof(UStringSearch));
|
|
|
|
if (result == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->collator = collator;
|
|
|
|
result->strength = ucol_getStrength(collator);
|
|
|
|
result->ceMask = getMask(result->strength);
|
2009-04-22 23:10:07 +00:00
|
|
|
result->toShift =
|
|
|
|
ucol_getAttribute(collator, UCOL_ALTERNATE_HANDLING, status) ==
|
2001-08-25 02:03:53 +00:00
|
|
|
UCOL_SHIFTED;
|
|
|
|
result->variableTop = ucol_getVariableTop(collator, status);
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
result->nfd = Normalizer2Factory::getNFDInstance(*status);
|
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
uprv_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
result->search = (USearch *)uprv_malloc(sizeof(USearch));
|
2001-11-09 17:39:42 +00:00
|
|
|
if (result->search == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
uprv_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
result->search->text = text;
|
2001-09-07 21:56:18 +00:00
|
|
|
result->search->textLength = textlength;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
result->pattern.text = pattern;
|
|
|
|
result->pattern.textLength = patternlength;
|
|
|
|
result->pattern.CE = NULL;
|
2008-05-23 04:22:28 +00:00
|
|
|
result->pattern.PCE = NULL;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-10-25 22:02:54 +00:00
|
|
|
result->search->breakIter = breakiter;
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2009-04-22 23:10:07 +00:00
|
|
|
result->search->internalBreakIter = ubrk_open(UBRK_CHARACTER, ucol_getLocaleByType(result->collator, ULOC_VALID_LOCALE, status), text, textlength, status);
|
2001-09-20 14:00:02 +00:00
|
|
|
if (breakiter) {
|
2011-03-22 03:50:26 +00:00
|
|
|
ubrk_setText(breakiter, text, textlength, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
result->ownCollator = FALSE;
|
2001-08-25 02:03:53 +00:00
|
|
|
result->search->matchedLength = 0;
|
|
|
|
result->search->matchedIndex = USEARCH_DONE;
|
2008-02-10 20:17:14 +00:00
|
|
|
result->utilIter = NULL;
|
2009-04-22 23:10:07 +00:00
|
|
|
result->textIter = ucol_openElements(collator, text,
|
2001-09-07 21:56:18 +00:00
|
|
|
textlength, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
usearch_close(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
result->search->isOverlap = FALSE;
|
|
|
|
result->search->isCanonicalMatch = FALSE;
|
2010-01-15 00:55:58 +00:00
|
|
|
result->search->elementComparisonType = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
result->search->isForwardSearching = TRUE;
|
|
|
|
result->search->reset = TRUE;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
initialize(result, status);
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
usearch_close(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 usearch_close(UStringSearch *strsrch)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
|
|
|
if (strsrch->pattern.CE != strsrch->pattern.CEBuffer &&
|
|
|
|
strsrch->pattern.CE) {
|
2001-08-25 02:03:53 +00:00
|
|
|
uprv_free(strsrch->pattern.CE);
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
if (strsrch->pattern.PCE != NULL &&
|
|
|
|
strsrch->pattern.PCE != strsrch->pattern.PCEBuffer) {
|
|
|
|
uprv_free(strsrch->pattern.PCE);
|
|
|
|
}
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
ucol_closeElements(strsrch->textIter);
|
|
|
|
ucol_closeElements(strsrch->utilIter);
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch->ownCollator && strsrch->collator) {
|
2001-08-25 02:03:53 +00:00
|
|
|
ucol_close((UCollator *)strsrch->collator);
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2008-06-11 21:52:29 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2007-12-05 23:57:11 +00:00
|
|
|
if (strsrch->search->internalBreakIter) {
|
2011-03-22 03:50:26 +00:00
|
|
|
ubrk_close(strsrch->search->internalBreakIter);
|
2007-12-05 21:13:44 +00:00
|
|
|
}
|
2008-06-11 21:52:29 +00:00
|
|
|
#endif
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
uprv_free(strsrch->search);
|
|
|
|
uprv_free(strsrch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set and get methods --------------------------------------------------
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI void U_EXPORT2 usearch_setOffset(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t position,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status) && strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
if (isOutOfBounds(strsrch->search->textLength, position)) {
|
|
|
|
*status = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, position);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
strsrch->search->matchedIndex = USEARCH_DONE;
|
|
|
|
strsrch->search->matchedLength = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
strsrch->search->reset = FALSE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_getOffset(const UStringSearch *strsrch)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t result = ucol_getOffset(strsrch->textIter);
|
2001-08-25 02:03:53 +00:00
|
|
|
if (isOutOfBounds(strsrch->search->textLength, result)) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 usearch_setAttribute(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
USearchAttribute attribute,
|
|
|
|
USearchAttributeValue value,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status) && strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
switch (attribute)
|
|
|
|
{
|
|
|
|
case USEARCH_OVERLAP :
|
|
|
|
strsrch->search->isOverlap = (value == USEARCH_ON ? TRUE : FALSE);
|
|
|
|
break;
|
|
|
|
case USEARCH_CANONICAL_MATCH :
|
2009-04-22 23:10:07 +00:00
|
|
|
strsrch->search->isCanonicalMatch = (value == USEARCH_ON ? TRUE :
|
2001-08-25 02:03:53 +00:00
|
|
|
FALSE);
|
|
|
|
break;
|
2010-01-15 00:55:58 +00:00
|
|
|
case USEARCH_ELEMENT_COMPARISON :
|
|
|
|
if (value == USEARCH_PATTERN_BASE_WEIGHT_IS_WILDCARD || value == USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD) {
|
|
|
|
strsrch->search->elementComparisonType = (int16_t)value;
|
|
|
|
} else {
|
|
|
|
strsrch->search->elementComparisonType = 0;
|
|
|
|
}
|
|
|
|
break;
|
2001-08-25 02:03:53 +00:00
|
|
|
case USEARCH_ATTRIBUTE_COUNT :
|
|
|
|
default:
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value == USEARCH_ATTRIBUTE_VALUE_COUNT) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
U_CAPI USearchAttributeValue U_EXPORT2 usearch_getAttribute(
|
|
|
|
const UStringSearch *strsrch,
|
|
|
|
USearchAttribute attribute)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
switch (attribute) {
|
|
|
|
case USEARCH_OVERLAP :
|
2009-04-22 23:10:07 +00:00
|
|
|
return (strsrch->search->isOverlap == TRUE ? USEARCH_ON :
|
2001-08-25 02:03:53 +00:00
|
|
|
USEARCH_OFF);
|
|
|
|
case USEARCH_CANONICAL_MATCH :
|
2009-04-22 23:10:07 +00:00
|
|
|
return (strsrch->search->isCanonicalMatch == TRUE ? USEARCH_ON :
|
2001-08-25 02:03:53 +00:00
|
|
|
USEARCH_OFF);
|
2010-01-15 00:55:58 +00:00
|
|
|
case USEARCH_ELEMENT_COMPARISON :
|
|
|
|
{
|
|
|
|
int16_t value = strsrch->search->elementComparisonType;
|
|
|
|
if (value == USEARCH_PATTERN_BASE_WEIGHT_IS_WILDCARD || value == USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD) {
|
|
|
|
return (USearchAttributeValue)value;
|
|
|
|
} else {
|
|
|
|
return USEARCH_STANDARD_ELEMENT_COMPARISON;
|
|
|
|
}
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
case USEARCH_ATTRIBUTE_COUNT :
|
|
|
|
return USEARCH_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return USEARCH_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_getMatchedStart(
|
2001-08-25 02:03:53 +00:00
|
|
|
const UStringSearch *strsrch)
|
|
|
|
{
|
|
|
|
if (strsrch == NULL) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
return strsrch->search->matchedIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_getMatchedText(const UStringSearch *strsrch,
|
|
|
|
UChar *result,
|
|
|
|
int32_t resultCapacity,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2001-10-05 22:54:00 +00:00
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
if (strsrch == NULL || resultCapacity < 0 || (resultCapacity > 0 &&
|
2001-11-09 17:39:42 +00:00
|
|
|
result == NULL)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
2001-10-05 22:54:00 +00:00
|
|
|
int32_t copylength = strsrch->search->matchedLength;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t copyindex = strsrch->search->matchedIndex;
|
2001-10-05 22:54:00 +00:00
|
|
|
if (copyindex == USEARCH_DONE) {
|
|
|
|
u_terminateUChars(result, resultCapacity, 0, status);
|
|
|
|
return USEARCH_DONE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2001-10-05 22:54:00 +00:00
|
|
|
if (resultCapacity < copylength) {
|
|
|
|
copylength = resultCapacity;
|
|
|
|
}
|
|
|
|
if (copylength > 0) {
|
2009-04-22 23:10:07 +00:00
|
|
|
uprv_memcpy(result, strsrch->search->text + copyindex,
|
2001-10-05 22:54:00 +00:00
|
|
|
copylength * sizeof(UChar));
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
return u_terminateUChars(result, resultCapacity,
|
2001-11-09 17:39:42 +00:00
|
|
|
strsrch->search->matchedLength, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_getMatchedLength(
|
|
|
|
const UStringSearch *strsrch)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return strsrch->search->matchedLength;
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI void U_EXPORT2 usearch_setBreakIterator(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UBreakIterator *breakiter,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 22:42:20 +00:00
|
|
|
if (U_SUCCESS(*status) && strsrch) {
|
2011-03-22 03:50:26 +00:00
|
|
|
strsrch->search->breakIter = breakiter;
|
2001-09-20 14:00:02 +00:00
|
|
|
if (breakiter) {
|
2009-04-22 23:10:07 +00:00
|
|
|
ubrk_setText(breakiter, strsrch->search->text,
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->textLength, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-20 01:54:48 +00:00
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI const UBreakIterator* U_EXPORT2
|
2001-11-21 01:08:55 +00:00
|
|
|
usearch_getBreakIterator(const UStringSearch *strsrch)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return strsrch->search->breakIter;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 usearch_setText( UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *text,
|
|
|
|
int32_t textlength,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status)) {
|
2009-04-22 23:10:07 +00:00
|
|
|
if (strsrch == NULL || text == NULL || textlength < -1 ||
|
2001-11-09 17:39:42 +00:00
|
|
|
textlength == 0) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
else {
|
|
|
|
if (textlength == -1) {
|
|
|
|
textlength = u_strlen(text);
|
|
|
|
}
|
|
|
|
strsrch->search->text = text;
|
|
|
|
strsrch->search->textLength = textlength;
|
|
|
|
ucol_setText(strsrch->textIter, text, textlength, status);
|
|
|
|
strsrch->search->matchedIndex = USEARCH_DONE;
|
|
|
|
strsrch->search->matchedLength = 0;
|
|
|
|
strsrch->search->reset = TRUE;
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2003-08-31 20:53:46 +00:00
|
|
|
if (strsrch->search->breakIter != NULL) {
|
2009-04-22 23:10:07 +00:00
|
|
|
ubrk_setText(strsrch->search->breakIter, text,
|
2003-08-31 20:53:46 +00:00
|
|
|
textlength, status);
|
|
|
|
}
|
2007-12-05 21:13:44 +00:00
|
|
|
ubrk_setText(strsrch->search->internalBreakIter, text, textlength, status);
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI const UChar * U_EXPORT2 usearch_getText(const UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t *length)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
*length = strsrch->search->textLength;
|
|
|
|
return strsrch->search->text;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI void U_EXPORT2 usearch_setCollator( UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
const UCollator *collator,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
if (collator == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2001-11-09 17:39:42 +00:00
|
|
|
if (strsrch) {
|
2002-02-13 01:01:33 +00:00
|
|
|
if (strsrch->ownCollator && (strsrch->collator != collator)) {
|
2001-11-09 17:39:42 +00:00
|
|
|
ucol_close((UCollator *)strsrch->collator);
|
2002-02-13 01:01:33 +00:00
|
|
|
strsrch->ownCollator = FALSE;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
|
|
|
strsrch->collator = collator;
|
|
|
|
strsrch->strength = ucol_getStrength(collator);
|
|
|
|
strsrch->ceMask = getMask(strsrch->strength);
|
2007-10-25 22:02:54 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
2011-03-22 03:50:26 +00:00
|
|
|
ubrk_close(strsrch->search->internalBreakIter);
|
|
|
|
strsrch->search->internalBreakIter = ubrk_open(UBRK_CHARACTER, ucol_getLocaleByType(collator, ULOC_VALID_LOCALE, status),
|
|
|
|
strsrch->search->text, strsrch->search->textLength, status);
|
2007-10-25 22:02:54 +00:00
|
|
|
#endif
|
2001-11-09 17:39:42 +00:00
|
|
|
// if status is a failure, ucol_getAttribute returns UCOL_DEFAULT
|
2009-04-22 23:10:07 +00:00
|
|
|
strsrch->toShift =
|
|
|
|
ucol_getAttribute(collator, UCOL_ALTERNATE_HANDLING, status) ==
|
2001-11-09 17:39:42 +00:00
|
|
|
UCOL_SHIFTED;
|
|
|
|
// if status is a failure, ucol_getVariableTop returns 0
|
|
|
|
strsrch->variableTop = ucol_getVariableTop(collator, status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
initialize(strsrch, status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
2008-06-09 21:18:46 +00:00
|
|
|
/* free offset buffer to avoid memory leak before initializing. */
|
2009-03-27 00:37:55 +00:00
|
|
|
ucol_freeOffsetBuffer(&(strsrch->textIter->iteratordata_));
|
2009-04-22 23:10:07 +00:00
|
|
|
uprv_init_collIterate(collator, strsrch->search->text,
|
|
|
|
strsrch->search->textLength,
|
2010-01-06 23:50:03 +00:00
|
|
|
&(strsrch->textIter->iteratordata_),
|
|
|
|
status);
|
2003-08-31 20:53:46 +00:00
|
|
|
strsrch->utilIter->iteratordata_.coll = collator;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
// **** are these calls needed?
|
|
|
|
// **** we call uprv_init_pce in initializePatternPCETable
|
|
|
|
// **** and the CEBuffer constructor...
|
|
|
|
#if 0
|
|
|
|
uprv_init_pce(strsrch->textIter);
|
|
|
|
uprv_init_pce(strsrch->utilIter);
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UCollator * U_EXPORT2 usearch_getCollator(const UStringSearch *strsrch)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
return (UCollator *)strsrch->collator;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI void U_EXPORT2 usearch_setPattern( UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternlength,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(*status)) {
|
2001-11-09 17:39:42 +00:00
|
|
|
if (strsrch == NULL || pattern == NULL) {
|
2001-09-07 21:56:18 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-11-09 17:39:42 +00:00
|
|
|
else {
|
|
|
|
if (patternlength == -1) {
|
|
|
|
patternlength = u_strlen(pattern);
|
|
|
|
}
|
|
|
|
if (patternlength == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strsrch->pattern.text = pattern;
|
|
|
|
strsrch->pattern.textLength = patternlength;
|
|
|
|
initialize(strsrch, status);
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI const UChar* U_EXPORT2
|
|
|
|
usearch_getPattern(const UStringSearch *strsrch,
|
2001-11-21 01:08:55 +00:00
|
|
|
int32_t *length)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2001-08-25 02:03:53 +00:00
|
|
|
*length = strsrch->pattern.textLength;
|
|
|
|
return strsrch->pattern.text;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// miscellanous methods --------------------------------------------------
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_first(UStringSearch *strsrch,
|
|
|
|
UErrorCode *status)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch && U_SUCCESS(*status)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->isForwardSearching = TRUE;
|
|
|
|
usearch_setOffset(strsrch, 0, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
return usearch_next(strsrch, status);
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_following(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t position,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch && U_SUCCESS(*status)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->isForwardSearching = TRUE;
|
2001-11-09 17:39:42 +00:00
|
|
|
// position checked in usearch_setOffset
|
2001-08-25 02:03:53 +00:00
|
|
|
usearch_setOffset(strsrch, position, status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
2009-04-22 23:10:07 +00:00
|
|
|
return usearch_next(strsrch, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_last(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch && U_SUCCESS(*status)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->isForwardSearching = FALSE;
|
|
|
|
usearch_setOffset(strsrch, strsrch->search->textLength, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
return usearch_previous(strsrch, status);
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_preceding(UStringSearch *strsrch,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t position,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch && U_SUCCESS(*status)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->isForwardSearching = FALSE;
|
2001-11-09 17:39:42 +00:00
|
|
|
// position checked in usearch_setOffset
|
2001-08-25 02:03:53 +00:00
|
|
|
usearch_setOffset(strsrch, position, status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
2009-04-22 23:10:07 +00:00
|
|
|
return usearch_previous(strsrch, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
/**
|
2009-04-22 23:10:07 +00:00
|
|
|
* If a direction switch is required, we'll count the number of ces till the
|
|
|
|
* beginning of the collation element iterator and iterate forwards that
|
|
|
|
* number of times. This is so that we get to the correct point within the
|
2001-08-25 02:03:53 +00:00
|
|
|
* string to continue the search in. Imagine when we are in the middle of the
|
|
|
|
* normalization buffer when the change in direction is request. arrrgghh....
|
|
|
|
* After searching the offset within the collation element iterator will be
|
|
|
|
* shifted to the start of the match. If a match is not found, the offset would
|
2009-04-22 23:10:07 +00:00
|
|
|
* have been set to the end of the text string in the collation element
|
2001-08-25 02:03:53 +00:00
|
|
|
* iterator.
|
|
|
|
* Okay, here's my take on normalization buffer. The only time when there can
|
|
|
|
* be 2 matches within the same normalization is when the pattern is consists
|
|
|
|
* of all accents. But since the offset returned is from the text string, we
|
2009-04-22 23:10:07 +00:00
|
|
|
* should not confuse the caller by returning the second match within the
|
2001-08-25 02:03:53 +00:00
|
|
|
* same normalization buffer. If we do, the 2 results will have the same match
|
|
|
|
* offsets, and that'll be confusing. I'll return the next match that doesn't
|
2009-04-22 23:10:07 +00:00
|
|
|
* fall within the same normalization buffer. Note this does not affect the
|
2001-08-25 02:03:53 +00:00
|
|
|
* results of matches spanning the text and the normalization buffer.
|
|
|
|
* The position to start searching is taken from the collation element
|
|
|
|
* iterator. Callers of this API would have to set the offset in the collation
|
|
|
|
* element iterator before using this method.
|
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_next(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
2009-04-22 23:10:07 +00:00
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (U_SUCCESS(*status) && strsrch) {
|
2003-07-03 21:48:39 +00:00
|
|
|
// note offset is either equivalent to the start of the previous match
|
|
|
|
// or is set by the user
|
|
|
|
int32_t offset = usearch_getOffset(strsrch);
|
|
|
|
USearch *search = strsrch->search;
|
|
|
|
search->reset = FALSE;
|
|
|
|
int32_t textlength = search->textLength;
|
|
|
|
if (search->isForwardSearching) {
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2003-07-03 21:48:39 +00:00
|
|
|
if (offset == textlength
|
2009-04-22 23:10:07 +00:00
|
|
|
|| (!search->isOverlap &&
|
2003-07-03 21:48:39 +00:00
|
|
|
(offset + strsrch->pattern.defaultShiftSize > textlength ||
|
2009-04-22 23:10:07 +00:00
|
|
|
(search->matchedIndex != USEARCH_DONE &&
|
2003-07-03 21:48:39 +00:00
|
|
|
offset + search->matchedLength >= textlength)))) {
|
|
|
|
// not enough characters to match
|
|
|
|
setMatchNotFound(strsrch);
|
2009-04-22 23:10:07 +00:00
|
|
|
return USEARCH_DONE;
|
2003-07-03 21:48:39 +00:00
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
|
|
|
if (offset == textlength ||
|
|
|
|
(! search->isOverlap &&
|
|
|
|
(search->matchedIndex != USEARCH_DONE &&
|
|
|
|
offset + search->matchedLength > textlength))) {
|
|
|
|
// not enough characters to match
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-04-22 23:10:07 +00:00
|
|
|
// switching direction.
|
|
|
|
// if matchedIndex == USEARCH_DONE, it means that either a
|
2001-08-25 02:03:53 +00:00
|
|
|
// setOffset has been called or that previous ran off the text
|
2009-04-22 23:10:07 +00:00
|
|
|
// string. the iterator would have been set to offset 0 if a
|
2001-08-25 02:03:53 +00:00
|
|
|
// match is not found.
|
2001-09-07 21:56:18 +00:00
|
|
|
search->isForwardSearching = TRUE;
|
2003-07-03 23:19:42 +00:00
|
|
|
if (search->matchedIndex != USEARCH_DONE) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// there's no need to set the collation element iterator
|
|
|
|
// the next call to next will set the offset.
|
2003-07-03 23:19:42 +00:00
|
|
|
return search->matchedIndex;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
if (strsrch->pattern.CELength == 0) {
|
2003-07-03 21:48:39 +00:00
|
|
|
if (search->matchedIndex == USEARCH_DONE) {
|
2001-09-07 21:56:18 +00:00
|
|
|
search->matchedIndex = offset;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
else { // moves by codepoints
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_FWD_1(search->text, search->matchedIndex, textlength);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
search->matchedLength = 0;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, search->matchedIndex);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below
|
2001-09-07 21:56:18 +00:00
|
|
|
if (search->matchedIndex == textlength) {
|
|
|
|
search->matchedIndex = USEARCH_DONE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
else {
|
2003-08-31 20:53:46 +00:00
|
|
|
if (search->matchedLength > 0) {
|
|
|
|
// if matchlength is 0 we are at the start of the iteration
|
|
|
|
if (search->isOverlap) {
|
|
|
|
ucol_setOffset(strsrch->textIter, offset + 1, status);
|
|
|
|
}
|
|
|
|
else {
|
2009-04-22 23:10:07 +00:00
|
|
|
ucol_setOffset(strsrch->textIter,
|
2003-08-31 20:53:46 +00:00
|
|
|
offset + search->matchedLength, status);
|
|
|
|
}
|
|
|
|
}
|
2003-07-03 21:48:39 +00:00
|
|
|
else {
|
|
|
|
// for boundary check purposes. this will ensure that the
|
|
|
|
// next match will not preceed the current offset
|
|
|
|
// note search->matchedIndex will always be set to something
|
|
|
|
// in the code
|
|
|
|
search->matchedIndex = offset - 1;
|
|
|
|
}
|
2003-08-31 20:53:46 +00:00
|
|
|
|
|
|
|
if (search->isCanonicalMatch) {
|
|
|
|
// can't use exact here since extra accents are allowed.
|
|
|
|
usearch_handleNextCanonical(strsrch, status);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
usearch_handleNextExact(strsrch, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2003-07-03 19:17:35 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if !BOYER_MOORE
|
|
|
|
if (search->matchedIndex == USEARCH_DONE) {
|
|
|
|
ucol_setOffset(strsrch->textIter, search->textLength, status);
|
|
|
|
} else {
|
|
|
|
ucol_setOffset(strsrch->textIter, search->matchedIndex, status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
return search->matchedIndex;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 usearch_previous(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-09-20 14:00:02 +00:00
|
|
|
if (U_SUCCESS(*status) && strsrch) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset;
|
2001-09-07 21:56:18 +00:00
|
|
|
USearch *search = strsrch->search;
|
|
|
|
if (search->reset) {
|
|
|
|
offset = search->textLength;
|
|
|
|
search->isForwardSearching = FALSE;
|
|
|
|
search->reset = FALSE;
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, offset);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
offset = usearch_getOffset(strsrch);
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t matchedindex = search->matchedIndex;
|
2001-09-07 21:56:18 +00:00
|
|
|
if (search->isForwardSearching == TRUE) {
|
2009-04-22 23:10:07 +00:00
|
|
|
// switching direction.
|
|
|
|
// if matchedIndex == USEARCH_DONE, it means that either a
|
2001-08-25 02:03:53 +00:00
|
|
|
// setOffset has been called or that next ran off the text
|
2009-04-22 23:10:07 +00:00
|
|
|
// string. the iterator would have been set to offset textLength if
|
2001-08-25 02:03:53 +00:00
|
|
|
// a match is not found.
|
2001-09-07 21:56:18 +00:00
|
|
|
search->isForwardSearching = FALSE;
|
|
|
|
if (matchedindex != USEARCH_DONE) {
|
|
|
|
return matchedindex;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2001-09-20 14:00:02 +00:00
|
|
|
if (offset == 0 || matchedindex == 0 ||
|
2009-04-22 23:10:07 +00:00
|
|
|
(!search->isOverlap &&
|
2001-09-20 14:00:02 +00:00
|
|
|
(offset < strsrch->pattern.defaultShiftSize ||
|
2009-04-22 23:10:07 +00:00
|
|
|
(matchedindex != USEARCH_DONE &&
|
2001-09-20 14:00:02 +00:00
|
|
|
matchedindex < strsrch->pattern.defaultShiftSize)))) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// not enough characters to match
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2009-04-22 23:10:07 +00:00
|
|
|
return USEARCH_DONE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
|
|
|
// Could check pattern length, but the
|
|
|
|
// linear search will do the right thing
|
|
|
|
if (offset == 0 || matchedindex == 0) {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
if (strsrch->pattern.CELength == 0) {
|
2009-04-22 23:10:07 +00:00
|
|
|
search->matchedIndex =
|
2001-09-07 21:56:18 +00:00
|
|
|
(matchedindex == USEARCH_DONE ? offset : matchedindex);
|
|
|
|
if (search->matchedIndex == 0) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
else { // move by codepoints
|
2011-07-27 05:53:56 +00:00
|
|
|
U16_BACK_1(search->text, 0, search->matchedIndex);
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(strsrch->textIter, search->matchedIndex);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below
|
2001-09-07 21:56:18 +00:00
|
|
|
search->matchedLength = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (strsrch->search->isCanonicalMatch) {
|
|
|
|
// can't use exact here since extra accents are allowed.
|
|
|
|
usearch_handlePreviousCanonical(strsrch, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
usearch_handlePreviousExact(strsrch, status);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked below
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-09-07 21:56:18 +00:00
|
|
|
return search->matchedIndex;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return USEARCH_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
U_CAPI void U_EXPORT2 usearch_reset(UStringSearch *strsrch)
|
|
|
|
{
|
2009-04-22 23:10:07 +00:00
|
|
|
/*
|
|
|
|
reset is setting the attributes that are already in
|
2002-02-13 01:01:33 +00:00
|
|
|
string search, hence all attributes in the collator should
|
|
|
|
be retrieved without any problems
|
|
|
|
*/
|
2001-09-20 14:00:02 +00:00
|
|
|
if (strsrch) {
|
2002-02-13 01:56:45 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UBool sameCollAttribute = TRUE;
|
|
|
|
uint32_t ceMask;
|
|
|
|
UBool shift;
|
|
|
|
uint32_t varTop;
|
|
|
|
|
2008-10-07 23:37:42 +00:00
|
|
|
// **** hack to deal w/ how processed CEs encode quaternary ****
|
|
|
|
UCollationStrength newStrength = ucol_getStrength(strsrch->collator);
|
|
|
|
if ((strsrch->strength < UCOL_QUATERNARY && newStrength >= UCOL_QUATERNARY) ||
|
|
|
|
(strsrch->strength >= UCOL_QUATERNARY && newStrength < UCOL_QUATERNARY)) {
|
|
|
|
sameCollAttribute = FALSE;
|
|
|
|
}
|
|
|
|
|
2002-02-13 01:56:45 +00:00
|
|
|
strsrch->strength = ucol_getStrength(strsrch->collator);
|
|
|
|
ceMask = getMask(strsrch->strength);
|
|
|
|
if (strsrch->ceMask != ceMask) {
|
|
|
|
strsrch->ceMask = ceMask;
|
|
|
|
sameCollAttribute = FALSE;
|
|
|
|
}
|
2008-10-07 23:37:42 +00:00
|
|
|
|
2002-02-13 01:01:33 +00:00
|
|
|
// if status is a failure, ucol_getAttribute returns UCOL_DEFAULT
|
2009-04-22 23:10:07 +00:00
|
|
|
shift = ucol_getAttribute(strsrch->collator, UCOL_ALTERNATE_HANDLING,
|
2002-02-13 01:56:45 +00:00
|
|
|
&status) == UCOL_SHIFTED;
|
|
|
|
if (strsrch->toShift != shift) {
|
|
|
|
strsrch->toShift = shift;
|
|
|
|
sameCollAttribute = FALSE;
|
|
|
|
}
|
|
|
|
|
2002-02-13 01:01:33 +00:00
|
|
|
// if status is a failure, ucol_getVariableTop returns 0
|
2002-02-13 01:56:45 +00:00
|
|
|
varTop = ucol_getVariableTop(strsrch->collator, &status);
|
|
|
|
if (strsrch->variableTop != varTop) {
|
|
|
|
strsrch->variableTop = varTop;
|
|
|
|
sameCollAttribute = FALSE;
|
|
|
|
}
|
|
|
|
if (!sameCollAttribute) {
|
|
|
|
initialize(strsrch, &status);
|
|
|
|
}
|
2008-06-09 21:18:46 +00:00
|
|
|
/* free offset buffer to avoid memory leak before initializing. */
|
2009-03-27 00:37:55 +00:00
|
|
|
ucol_freeOffsetBuffer(&(strsrch->textIter->iteratordata_));
|
2009-04-22 23:10:07 +00:00
|
|
|
uprv_init_collIterate(strsrch->collator, strsrch->search->text,
|
|
|
|
strsrch->search->textLength,
|
2010-01-06 23:50:03 +00:00
|
|
|
&(strsrch->textIter->iteratordata_),
|
|
|
|
&status);
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->matchedLength = 0;
|
|
|
|
strsrch->search->matchedIndex = USEARCH_DONE;
|
|
|
|
strsrch->search->isOverlap = FALSE;
|
|
|
|
strsrch->search->isCanonicalMatch = FALSE;
|
2010-01-15 00:55:58 +00:00
|
|
|
strsrch->search->elementComparisonType = 0;
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->search->isForwardSearching = TRUE;
|
|
|
|
strsrch->search->reset = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
//
|
|
|
|
// CEI Collation Element + source text index.
|
|
|
|
// These structs are kept in the circular buffer.
|
|
|
|
//
|
|
|
|
struct CEI {
|
|
|
|
int64_t ce;
|
|
|
|
int32_t lowIndex;
|
|
|
|
int32_t highIndex;
|
|
|
|
};
|
|
|
|
|
2008-06-06 21:51:25 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
//
|
|
|
|
// CEBuffer A circular buffer of CEs from the text being searched.
|
|
|
|
//
|
2011-07-09 14:57:34 +00:00
|
|
|
#define DEFAULT_CEBUFFER_SIZE 96
|
|
|
|
#define CEBUFFER_EXTRA 32
|
|
|
|
// Some typical max values to make buffer size more reasonable for asymmetric search.
|
|
|
|
// #8694 is for a better long-term solution to allocation of this buffer.
|
|
|
|
#define MAX_TARGET_IGNORABLES_PER_PAT_JAMO_L 8
|
|
|
|
#define MAX_TARGET_IGNORABLES_PER_PAT_OTHER 3
|
|
|
|
#define MIGHT_BE_JAMO_L(c) ((c >= 0x1100 && c <= 0x115E) || (c >= 0x3131 && c <= 0x314E) || (c >= 0x3165 && c <= 0x3186))
|
2008-05-23 04:22:28 +00:00
|
|
|
struct CEBuffer {
|
|
|
|
CEI defBuf[DEFAULT_CEBUFFER_SIZE];
|
|
|
|
CEI *buf;
|
|
|
|
int32_t bufSize;
|
|
|
|
int32_t firstIx;
|
|
|
|
int32_t limitIx;
|
|
|
|
UCollationElements *ceIter;
|
|
|
|
UStringSearch *strSearch;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CEBuffer(UStringSearch *ss, UErrorCode *status);
|
|
|
|
~CEBuffer();
|
|
|
|
const CEI *get(int32_t index);
|
|
|
|
const CEI *getPrevious(int32_t index);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CEBuffer::CEBuffer(UStringSearch *ss, UErrorCode *status) {
|
|
|
|
buf = defBuf;
|
|
|
|
strSearch = ss;
|
2011-07-09 14:57:34 +00:00
|
|
|
bufSize = ss->pattern.PCELength + CEBUFFER_EXTRA;
|
|
|
|
if (ss->search->elementComparisonType != 0) {
|
|
|
|
const UChar * patText = ss->pattern.text;
|
|
|
|
if (patText) {
|
|
|
|
const UChar * patTextLimit = patText + ss->pattern.textLength;
|
|
|
|
while ( patText < patTextLimit ) {
|
|
|
|
UChar c = *patText++;
|
|
|
|
if (MIGHT_BE_JAMO_L(c)) {
|
|
|
|
bufSize += MAX_TARGET_IGNORABLES_PER_PAT_JAMO_L;
|
|
|
|
} else {
|
|
|
|
// No check for surrogates, we might allocate slightly more buffer than necessary.
|
|
|
|
bufSize += MAX_TARGET_IGNORABLES_PER_PAT_OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
ceIter = ss->textIter;
|
|
|
|
firstIx = 0;
|
|
|
|
limitIx = 0;
|
|
|
|
|
|
|
|
uprv_init_pce(ceIter);
|
|
|
|
|
|
|
|
if (bufSize>DEFAULT_CEBUFFER_SIZE) {
|
|
|
|
buf = (CEI *)uprv_malloc(bufSize * sizeof(CEI));
|
|
|
|
if (buf == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add a reset or init function so that allocated
|
|
|
|
// buffers can be retained & reused.
|
|
|
|
|
|
|
|
CEBuffer::~CEBuffer() {
|
|
|
|
if (buf != defBuf) {
|
|
|
|
uprv_free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the CE with the specified index.
|
|
|
|
// Index must be in the range
|
|
|
|
// n-history_size < index < n+1
|
|
|
|
// where n is the largest index to have been fetched by some previous call to this function.
|
|
|
|
// The CE value will be UCOL__PROCESSED_NULLORDER at end of input.
|
|
|
|
//
|
|
|
|
const CEI *CEBuffer::get(int32_t index) {
|
|
|
|
int i = index % bufSize;
|
|
|
|
|
|
|
|
if (index>=firstIx && index<limitIx) {
|
|
|
|
// The request was for an entry already in our buffer.
|
|
|
|
// Just return it.
|
|
|
|
return &buf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Caller is requesting a new, never accessed before, CE.
|
|
|
|
// Verify that it is the next one in sequence, which is all
|
|
|
|
// that is allowed.
|
|
|
|
if (index != limitIx) {
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manage the circular CE buffer indexing
|
|
|
|
limitIx++;
|
|
|
|
|
|
|
|
if (limitIx - firstIx >= bufSize) {
|
|
|
|
// The buffer is full, knock out the lowest-indexed entry.
|
|
|
|
firstIx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
buf[i].ce = ucol_nextProcessed(ceIter, &buf[i].lowIndex, &buf[i].highIndex, &status);
|
|
|
|
|
|
|
|
return &buf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the CE with the specified index.
|
|
|
|
// Index must be in the range
|
|
|
|
// n-history_size < index < n+1
|
|
|
|
// where n is the largest index to have been fetched by some previous call to this function.
|
|
|
|
// The CE value will be UCOL__PROCESSED_NULLORDER at end of input.
|
|
|
|
//
|
|
|
|
const CEI *CEBuffer::getPrevious(int32_t index) {
|
|
|
|
int i = index % bufSize;
|
|
|
|
|
|
|
|
if (index>=firstIx && index<limitIx) {
|
|
|
|
// The request was for an entry already in our buffer.
|
|
|
|
// Just return it.
|
|
|
|
return &buf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Caller is requesting a new, never accessed before, CE.
|
|
|
|
// Verify that it is the next one in sequence, which is all
|
|
|
|
// that is allowed.
|
|
|
|
if (index != limitIx) {
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manage the circular CE buffer indexing
|
|
|
|
limitIx++;
|
|
|
|
|
|
|
|
if (limitIx - firstIx >= bufSize) {
|
|
|
|
// The buffer is full, knock out the lowest-indexed entry.
|
|
|
|
firstIx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
buf[i].ce = ucol_previousProcessed(ceIter, &buf[i].lowIndex, &buf[i].highIndex, &status);
|
|
|
|
|
|
|
|
return &buf[i];
|
|
|
|
}
|
|
|
|
|
2008-06-06 21:51:25 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// #define USEARCH_DEBUG
|
|
|
|
|
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the next break boundary after startIndex. If the UStringSearch object
|
|
|
|
* has an external break iterator, use that. Otherwise use the internal character
|
|
|
|
* break iterator.
|
|
|
|
*/
|
|
|
|
static int32_t nextBoundaryAfter(UStringSearch *strsrch, int32_t startIndex) {
|
|
|
|
#if 0
|
|
|
|
const UChar *text = strsrch->search->text;
|
|
|
|
int32_t textLen = strsrch->search->textLength;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
U_ASSERT(startIndex>=0);
|
|
|
|
U_ASSERT(startIndex<=textLen);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (startIndex >= textLen) {
|
|
|
|
return startIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
int32_t i = startIndex;
|
|
|
|
U16_NEXT(text, i, textLen, c);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// If we are on a control character, stop without looking for combining marks.
|
|
|
|
// Control characters do not combine.
|
|
|
|
int32_t gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
|
|
|
|
if (gcProperty==U_GCB_CONTROL || gcProperty==U_GCB_LF || gcProperty==U_GCB_CR) {
|
|
|
|
return i;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// The initial character was not a control, and can thus accept trailing
|
|
|
|
// combining characters. Advance over however many of them there are.
|
|
|
|
int32_t indexOfLastCharChecked;
|
|
|
|
for (;;) {
|
|
|
|
indexOfLastCharChecked = i;
|
|
|
|
if (i>=textLen) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
U16_NEXT(text, i, textLen, c);
|
|
|
|
gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
|
|
|
|
if (gcProperty != U_GCB_EXTEND && gcProperty != U_GCB_SPACING_MARK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indexOfLastCharChecked;
|
|
|
|
#elif !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
UBreakIterator *breakiterator = strsrch->search->breakIter;
|
|
|
|
|
|
|
|
if (breakiterator == NULL) {
|
|
|
|
breakiterator = strsrch->search->internalBreakIter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (breakiterator != NULL) {
|
2011-03-22 03:50:26 +00:00
|
|
|
return ubrk_following(breakiterator, startIndex);
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return startIndex;
|
|
|
|
#else
|
|
|
|
// **** or should we use the original code? ****
|
|
|
|
return startIndex;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns TRUE if index is on a break boundary. If the UStringSearch
|
|
|
|
* has an external break iterator, test using that, otherwise test
|
|
|
|
* using the internal character break iterator.
|
|
|
|
*/
|
|
|
|
static UBool isBreakBoundary(UStringSearch *strsrch, int32_t index) {
|
|
|
|
#if 0
|
|
|
|
const UChar *text = strsrch->search->text;
|
|
|
|
int32_t textLen = strsrch->search->textLength;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
U_ASSERT(index>=0);
|
|
|
|
U_ASSERT(index<=textLen);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (index>=textLen || index<=0) {
|
2011-04-22 05:37:46 +00:00
|
|
|
return TRUE;
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// If the character at the current index is not a GRAPHEME_EXTEND
|
|
|
|
// then we can not be within a combining sequence.
|
|
|
|
UChar32 c;
|
|
|
|
U16_GET(text, 0, index, textLen, c);
|
|
|
|
int32_t gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
|
|
|
|
if (gcProperty != U_GCB_EXTEND && gcProperty != U_GCB_SPACING_MARK) {
|
2011-04-22 05:37:46 +00:00
|
|
|
return TRUE;
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// We are at a combining mark. If the preceding character is anything
|
|
|
|
// except a CONTROL, CR or LF, we are in a combining sequence.
|
2009-04-22 23:10:07 +00:00
|
|
|
U16_PREV(text, 0, index, c);
|
2008-05-23 04:22:28 +00:00
|
|
|
gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool combining = !(gcProperty==U_GCB_CONTROL || gcProperty==U_GCB_LF || gcProperty==U_GCB_CR);
|
2011-04-22 05:37:46 +00:00
|
|
|
return !combining;
|
2008-05-23 04:22:28 +00:00
|
|
|
#elif !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
UBreakIterator *breakiterator = strsrch->search->breakIter;
|
|
|
|
|
|
|
|
if (breakiterator == NULL) {
|
|
|
|
breakiterator = strsrch->search->internalBreakIter;
|
|
|
|
}
|
|
|
|
|
2011-04-22 05:37:46 +00:00
|
|
|
return (breakiterator != NULL && ubrk_isBoundary(breakiterator, index));
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
|
|
|
// **** or use the original code? ****
|
2011-04-22 05:37:46 +00:00
|
|
|
return TRUE;
|
2008-05-23 04:22:28 +00:00
|
|
|
#endif
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
static UBool onBreakBoundaries(const UStringSearch *strsrch, int32_t start, int32_t end)
|
|
|
|
{
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
UBreakIterator *breakiterator = strsrch->search->breakIter;
|
|
|
|
|
|
|
|
if (breakiterator != NULL) {
|
|
|
|
int32_t startindex = ubrk_first(breakiterator);
|
|
|
|
int32_t endindex = ubrk_last(breakiterator);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// out-of-range indexes are never boundary positions
|
|
|
|
if (start < startindex || start > endindex ||
|
|
|
|
end < startindex || end > endindex) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
return ubrk_isBoundary(breakiterator, start) &&
|
2008-05-23 04:22:28 +00:00
|
|
|
ubrk_isBoundary(breakiterator, end);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-27 01:01:45 +00:00
|
|
|
typedef enum {
|
|
|
|
U_CE_MATCH = -1,
|
|
|
|
U_CE_NO_MATCH = 0,
|
|
|
|
U_CE_SKIP_TARG,
|
|
|
|
U_CE_SKIP_PATN
|
|
|
|
} UCompareCEsResult;
|
|
|
|
#define U_CE_LEVEL2_BASE 0x00000005
|
|
|
|
#define U_CE_LEVEL3_BASE 0x00050000
|
|
|
|
|
|
|
|
static UCompareCEsResult compareCE64s(int64_t targCE, int64_t patCE, int16_t compareType) {
|
|
|
|
if (targCE == patCE) {
|
|
|
|
return U_CE_MATCH;
|
|
|
|
}
|
|
|
|
if (compareType == 0) {
|
|
|
|
return U_CE_NO_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t targCEshifted = targCE >> 32;
|
|
|
|
int64_t patCEshifted = patCE >> 32;
|
|
|
|
int64_t mask;
|
|
|
|
|
|
|
|
mask = 0xFFFF0000;
|
2010-02-27 07:43:41 +00:00
|
|
|
int32_t targLev1 = (int32_t)(targCEshifted & mask);
|
|
|
|
int32_t patLev1 = (int32_t)(patCEshifted & mask);
|
2010-02-27 01:01:45 +00:00
|
|
|
if ( targLev1 != patLev1 ) {
|
|
|
|
if ( targLev1 == 0 ) {
|
|
|
|
return U_CE_SKIP_TARG;
|
|
|
|
}
|
|
|
|
if ( patLev1 == 0 && compareType == USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD ) {
|
|
|
|
return U_CE_SKIP_PATN;
|
|
|
|
}
|
|
|
|
return U_CE_NO_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 0x0000FFFF;
|
2010-02-27 07:43:41 +00:00
|
|
|
int32_t targLev2 = (int32_t)(targCEshifted & mask);
|
|
|
|
int32_t patLev2 = (int32_t)(patCEshifted & mask);
|
2010-02-27 01:01:45 +00:00
|
|
|
if ( targLev2 != patLev2 ) {
|
|
|
|
if ( targLev2 == 0 ) {
|
|
|
|
return U_CE_SKIP_TARG;
|
|
|
|
}
|
|
|
|
if ( patLev2 == 0 && compareType == USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD ) {
|
|
|
|
return U_CE_SKIP_PATN;
|
|
|
|
}
|
|
|
|
return (patLev2 == U_CE_LEVEL2_BASE || (compareType == USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD && targLev2 == U_CE_LEVEL2_BASE) )?
|
|
|
|
U_CE_MATCH: U_CE_NO_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 0xFFFF0000;
|
2010-02-27 07:43:41 +00:00
|
|
|
int32_t targLev3 = (int32_t)(targCE & mask);
|
|
|
|
int32_t patLev3 = (int32_t)(patCE & mask);
|
2010-02-27 01:01:45 +00:00
|
|
|
if ( targLev3 != patLev3 ) {
|
|
|
|
return (patLev3 == U_CE_LEVEL3_BASE || (compareType == USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD && targLev3 == U_CE_LEVEL3_BASE) )?
|
|
|
|
U_CE_MATCH: U_CE_NO_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return U_CE_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BOYER_MOORE
|
|
|
|
// TODO: #if BOYER_MOORE, need 32-bit version of compareCE64s
|
|
|
|
#endif
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
U_CAPI UBool U_EXPORT2 usearch_search(UStringSearch *strsrch,
|
|
|
|
int32_t startIdx,
|
|
|
|
int32_t *matchStart,
|
|
|
|
int32_t *matchLimit,
|
2009-04-22 23:10:07 +00:00
|
|
|
UErrorCode *status)
|
2008-05-23 04:22:28 +00:00
|
|
|
{
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: reject search patterns beginning with a combining char.
|
|
|
|
|
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
if (getenv("USEARCH_DEBUG") != NULL) {
|
|
|
|
printf("Pattern CEs\n");
|
|
|
|
for (int ii=0; ii<strsrch->pattern.CELength; ii++) {
|
|
|
|
printf(" %8x", strsrch->pattern.CE[ii]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#endif
|
|
|
|
// Input parameter sanity check.
|
|
|
|
// TODO: should input indicies clip to the text length
|
|
|
|
// in the same way that UText does.
|
2009-04-22 23:10:07 +00:00
|
|
|
if(strsrch->pattern.CELength == 0 ||
|
2008-05-23 04:22:28 +00:00
|
|
|
startIdx < 0 ||
|
|
|
|
startIdx > strsrch->search->textLength ||
|
|
|
|
strsrch->pattern.CE == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strsrch->pattern.PCE == NULL) {
|
|
|
|
initializePatternPCETable(strsrch, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
ucol_setOffset(strsrch->textIter, startIdx, status);
|
|
|
|
CEBuffer ceb(strsrch, status);
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
int32_t targetIx = 0;
|
2010-02-27 07:43:41 +00:00
|
|
|
const CEI *targetCEI = NULL;
|
2008-05-23 04:22:28 +00:00
|
|
|
int32_t patIx;
|
|
|
|
UBool found;
|
|
|
|
|
|
|
|
int32_t mStart = -1;
|
|
|
|
int32_t mLimit = -1;
|
|
|
|
int32_t minLimit;
|
|
|
|
int32_t maxLimit;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// Outer loop moves over match starting positions in the
|
|
|
|
// target CE space.
|
2010-02-27 01:01:45 +00:00
|
|
|
// Here we see the target as a sequence of collation elements, resulting from the following:
|
|
|
|
// 1. Target characters were decomposed, and (if appropriate) other compressions and expansions are applied
|
|
|
|
// (for example, digraphs such as IJ may be broken into two characters).
|
|
|
|
// 2. An int64_t CE weight is determined for each resulting unit (high 16 bits are primary strength, next
|
|
|
|
// 16 bits are secondary, next 16 (the high 16 bits of the low 32-bit half) are tertiary. Any of these
|
|
|
|
// fields that are for strengths below that of the collator are set to 0. If this makes the int64_t
|
|
|
|
// CE weight 0 (as for a combining diacritic with secondary weight when the collator strentgh is primary),
|
|
|
|
// then the CE is deleted, so the following code sees only CEs that are relevant.
|
|
|
|
// For each CE, the lowIndex and highIndex correspond to where this CE begins and ends in the original text.
|
|
|
|
// If lowIndex==highIndex, either the CE resulted from an expansion/decomposition of one of the original text
|
|
|
|
// characters, or the CE marks the limit of the target text (in which case the CE weight is UCOL_PROCESSED_NULLORDER).
|
|
|
|
//
|
2008-05-23 04:22:28 +00:00
|
|
|
for(targetIx=0; ; targetIx++)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
// Inner loop checks for a match beginning at each
|
|
|
|
// position from the outer loop.
|
2010-02-27 01:01:45 +00:00
|
|
|
int32_t targetIxOffset = 0;
|
|
|
|
int64_t patCE = 0;
|
2011-07-09 14:57:34 +00:00
|
|
|
// For targetIx > 0, this ceb.get gets a CE that is as far back in the ring buffer
|
|
|
|
// (compared to the last CE fetched for the previous targetIx value) as we need to go
|
|
|
|
// for this targetIx value, so if it is non-NULL then other ceb.get calls should be OK.
|
|
|
|
const CEI *firstCEI = ceb.get(targetIx);
|
|
|
|
if (firstCEI == NULL) {
|
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-22 00:24:48 +00:00
|
|
|
for (patIx=0; patIx<strsrch->pattern.PCELength; patIx++) {
|
2010-02-27 01:01:45 +00:00
|
|
|
patCE = strsrch->pattern.PCE[patIx];
|
|
|
|
targetCEI = ceb.get(targetIx+patIx+targetIxOffset);
|
2008-05-23 04:22:28 +00:00
|
|
|
// Compare CE from target string with CE from the pattern.
|
2010-02-27 01:01:45 +00:00
|
|
|
// Note that the target CE will be UCOL_PROCESSED_NULLORDER if we reach the end of input,
|
2008-05-23 04:22:28 +00:00
|
|
|
// which will fail the compare, below.
|
2010-02-27 01:01:45 +00:00
|
|
|
UCompareCEsResult ceMatch = compareCE64s(targetCEI->ce, patCE, strsrch->search->elementComparisonType);
|
|
|
|
if ( ceMatch == U_CE_NO_MATCH ) {
|
2008-05-23 04:22:28 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
2010-02-27 01:01:45 +00:00
|
|
|
} else if ( ceMatch > U_CE_NO_MATCH ) {
|
|
|
|
if ( ceMatch == U_CE_SKIP_TARG ) {
|
|
|
|
// redo with same patCE, next targCE
|
|
|
|
patIx--;
|
|
|
|
targetIxOffset++;
|
|
|
|
} else { // ceMatch == U_CE_SKIP_PATN
|
|
|
|
// redo with same targCE, next patCE
|
|
|
|
targetIxOffset--;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-27 01:01:45 +00:00
|
|
|
targetIxOffset += strsrch->pattern.PCELength; // this is now the offset in target CE space to end of the match so far
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2010-02-27 07:43:41 +00:00
|
|
|
if (!found && ((targetCEI == NULL) || (targetCEI->ce != UCOL_PROCESSED_NULLORDER))) {
|
2008-05-23 04:22:28 +00:00
|
|
|
// No match at this targetIx. Try again at the next.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
// No match at all, we have run off the end of the target text.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We have found a match in CE space.
|
|
|
|
// Now determine the bounds in string index space.
|
|
|
|
// There still is a chance of match failure if the CE range not correspond to
|
|
|
|
// an acceptable character range.
|
|
|
|
//
|
2010-02-27 01:01:45 +00:00
|
|
|
const CEI *lastCEI = ceb.get(targetIx + targetIxOffset - 1);
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
mStart = firstCEI->lowIndex;
|
|
|
|
minLimit = lastCEI->lowIndex;
|
|
|
|
|
|
|
|
// Look at the CE following the match. If it is UCOL_NULLORDER the match
|
|
|
|
// extended to the end of input, and the match is good.
|
|
|
|
|
|
|
|
// Look at the high and low indices of the CE following the match. If
|
|
|
|
// they are the same it means one of two things:
|
|
|
|
// 1. The match extended to the last CE from the target text, which is OK, or
|
|
|
|
// 2. The last CE that was part of the match is in an expansion that extends
|
|
|
|
// to the first CE after the match. In this case, we reject the match.
|
2011-04-22 05:37:46 +00:00
|
|
|
const CEI *nextCEI = 0;
|
2010-02-27 01:01:45 +00:00
|
|
|
if (strsrch->search->elementComparisonType == 0) {
|
2011-04-22 05:37:46 +00:00
|
|
|
nextCEI = ceb.get(targetIx + targetIxOffset);
|
2010-02-27 01:01:45 +00:00
|
|
|
maxLimit = nextCEI->lowIndex;
|
|
|
|
if (nextCEI->lowIndex == nextCEI->highIndex && nextCEI->ce != UCOL_PROCESSED_NULLORDER) {
|
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ( ; ; ++targetIxOffset ) {
|
|
|
|
nextCEI = ceb.get(targetIx + targetIxOffset);
|
|
|
|
maxLimit = nextCEI->lowIndex;
|
2011-03-22 03:50:26 +00:00
|
|
|
// If we are at the end of the target too, match succeeds
|
2010-02-27 01:01:45 +00:00
|
|
|
if ( nextCEI->ce == UCOL_PROCESSED_NULLORDER ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// As long as the next CE has primary weight of 0,
|
|
|
|
// it is part of the last target element matched by the pattern;
|
|
|
|
// make sure it can be part of a match with the last patCE
|
|
|
|
if ( (((nextCEI->ce) >> 32) & 0xFFFF0000UL) == 0 ) {
|
2011-03-22 03:50:26 +00:00
|
|
|
UCompareCEsResult ceMatch = compareCE64s(nextCEI->ce, patCE, strsrch->search->elementComparisonType);
|
|
|
|
if ( ceMatch == U_CE_NO_MATCH || ceMatch == U_CE_SKIP_PATN ) {
|
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-27 01:01:45 +00:00
|
|
|
// If lowIndex == highIndex, this target CE is part of an expansion of the last matched
|
|
|
|
// target element, but it has non-zero primary weight => match fails
|
|
|
|
} else if ( nextCEI->lowIndex == nextCEI->highIndex ) {
|
2011-03-22 03:50:26 +00:00
|
|
|
found = false;
|
|
|
|
break;
|
2010-02-27 01:01:45 +00:00
|
|
|
// Else the target CE is not part of an expansion of the last matched element, match succeeds
|
|
|
|
} else {
|
2011-03-22 03:50:26 +00:00
|
|
|
break;
|
2010-02-27 01:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
// Check for the start of the match being within a combining sequence.
|
|
|
|
// This can happen if the pattern itself begins with a combining char, and
|
|
|
|
// the match found combining marks in the target text that were attached
|
|
|
|
// to something else.
|
|
|
|
// This type of match should be rejected for not completely consuming a
|
|
|
|
// combining sequence.
|
2011-04-22 05:37:46 +00:00
|
|
|
if (!isBreakBoundary(strsrch, mStart)) {
|
2008-05-23 04:22:28 +00:00
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the start of the match being within an Collation Element Expansion,
|
|
|
|
// meaning that the first char of the match is only partially matched.
|
2009-04-22 23:10:07 +00:00
|
|
|
// With exapnsions, the first CE will report the index of the source
|
2008-05-23 04:22:28 +00:00
|
|
|
// character, and all subsequent (expansions) CEs will report the source index of the
|
2009-04-22 23:10:07 +00:00
|
|
|
// _following_ character.
|
2008-05-23 04:22:28 +00:00
|
|
|
int32_t secondIx = firstCEI->highIndex;
|
|
|
|
if (mStart == secondIx) {
|
|
|
|
found = FALSE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// Advance the match end position to the first acceptable match boundary.
|
|
|
|
// This advances the index over any combining charcters.
|
|
|
|
mLimit = maxLimit;
|
|
|
|
if (minLimit < maxLimit) {
|
2011-04-22 05:37:46 +00:00
|
|
|
// When the last CE's low index is same with its high index, the CE is likely
|
|
|
|
// a part of expansion. In this case, the index is located just after the
|
|
|
|
// character corresponding to the CEs compared above. If the index is right
|
|
|
|
// at the break boundary, move the position to the next boundary will result
|
|
|
|
// incorrect match length when there are ignorable characters exist between
|
|
|
|
// the position and the next character produces CE(s). See ticket#8482.
|
|
|
|
if (minLimit == lastCEI->highIndex && isBreakBoundary(strsrch, minLimit)) {
|
|
|
|
mLimit = minLimit;
|
|
|
|
} else {
|
|
|
|
int32_t nba = nextBoundaryAfter(strsrch, minLimit);
|
|
|
|
if (nba >= lastCEI->highIndex) {
|
|
|
|
mLimit = nba;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
if (getenv("USEARCH_DEBUG") != NULL) {
|
|
|
|
printf("minLimit, maxLimit, mLimit = %d, %d, %d\n", minLimit, maxLimit, mLimit);
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// If advancing to the end of a combining sequence in character indexing space
|
2009-04-22 23:10:07 +00:00
|
|
|
// advanced us beyond the end of the match in CE space, reject this match.
|
2008-05-23 04:22:28 +00:00
|
|
|
if (mLimit > maxLimit) {
|
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-22 05:37:46 +00:00
|
|
|
if (!isBreakBoundary(strsrch, mLimit)) {
|
2008-05-23 04:22:28 +00:00
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
2009-01-22 00:24:48 +00:00
|
|
|
if (! checkIdentical(strsrch, mStart, mLimit)) {
|
2008-10-07 23:37:42 +00:00
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
if (getenv("USEARCH_DEBUG") != NULL) {
|
|
|
|
printf("Target CEs [%d .. %d]\n", ceb.firstIx, ceb.limitIx);
|
|
|
|
int32_t lastToPrint = ceb.limitIx+2;
|
|
|
|
for (int ii=ceb.firstIx; ii<lastToPrint; ii++) {
|
|
|
|
printf("%8x@%d ", ceb.get(ii)->ce, ceb.get(ii)->srcIndex);
|
|
|
|
}
|
|
|
|
printf("\n%s\n", found? "match found" : "no match");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// All Done. Store back the match bounds to the caller.
|
|
|
|
//
|
|
|
|
if (found==FALSE) {
|
|
|
|
mLimit = -1;
|
|
|
|
mStart = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchStart != NULL) {
|
|
|
|
*matchStart= mStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchLimit != NULL) {
|
|
|
|
*matchLimit = mLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2 usearch_searchBackwards(UStringSearch *strsrch,
|
|
|
|
int32_t startIdx,
|
|
|
|
int32_t *matchStart,
|
|
|
|
int32_t *matchLimit,
|
2009-04-22 23:10:07 +00:00
|
|
|
UErrorCode *status)
|
2008-05-23 04:22:28 +00:00
|
|
|
{
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: reject search patterns beginning with a combining char.
|
|
|
|
|
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
if (getenv("USEARCH_DEBUG") != NULL) {
|
|
|
|
printf("Pattern CEs\n");
|
|
|
|
for (int ii=0; ii<strsrch->pattern.CELength; ii++) {
|
|
|
|
printf(" %8x", strsrch->pattern.CE[ii]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#endif
|
|
|
|
// Input parameter sanity check.
|
|
|
|
// TODO: should input indicies clip to the text length
|
|
|
|
// in the same way that UText does.
|
2009-04-22 23:10:07 +00:00
|
|
|
if(strsrch->pattern.CELength == 0 ||
|
2008-05-23 04:22:28 +00:00
|
|
|
startIdx < 0 ||
|
|
|
|
startIdx > strsrch->search->textLength ||
|
|
|
|
strsrch->pattern.CE == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strsrch->pattern.PCE == NULL) {
|
|
|
|
initializePatternPCETable(strsrch, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
CEBuffer ceb(strsrch, status);
|
2009-04-22 23:10:07 +00:00
|
|
|
int32_t targetIx = 0;
|
2008-05-23 04:22:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre-load the buffer with the CE's for the grapheme
|
|
|
|
* after our starting position so that we're sure that
|
|
|
|
* we can look at the CE following the match when we
|
|
|
|
* check the match boundaries.
|
|
|
|
*
|
|
|
|
* This will also pre-fetch the first CE that we'll
|
|
|
|
* consider for the match.
|
|
|
|
*/
|
|
|
|
if (startIdx < strsrch->search->textLength) {
|
|
|
|
UBreakIterator *bi = strsrch->search->internalBreakIter;
|
|
|
|
int32_t next = ubrk_following(bi, startIdx);
|
|
|
|
|
|
|
|
ucol_setOffset(strsrch->textIter, next, status);
|
|
|
|
|
|
|
|
for (targetIx = 0; ; targetIx += 1) {
|
|
|
|
if (ceb.getPrevious(targetIx)->lowIndex < startIdx) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ucol_setOffset(strsrch->textIter, startIdx, status);
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2010-02-27 07:43:41 +00:00
|
|
|
const CEI *targetCEI = NULL;
|
2008-05-23 04:22:28 +00:00
|
|
|
int32_t patIx;
|
|
|
|
UBool found;
|
|
|
|
|
|
|
|
int32_t limitIx = targetIx;
|
|
|
|
int32_t mStart = -1;
|
|
|
|
int32_t mLimit = -1;
|
|
|
|
int32_t minLimit;
|
|
|
|
int32_t maxLimit;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
// Outer loop moves over match starting positions in the
|
|
|
|
// target CE space.
|
2010-02-27 01:01:45 +00:00
|
|
|
// Here, targetIx values increase toward the beginning of the base text (i.e. we get the text CEs in reverse order).
|
|
|
|
// But patIx is 0 at the beginning of the pattern and increases toward the end.
|
|
|
|
// So this loop performs a comparison starting with the end of pattern, and prcessd toward the beginning of the pattern
|
|
|
|
// and the beginning of the base text.
|
2008-05-23 04:22:28 +00:00
|
|
|
for(targetIx = limitIx; ; targetIx += 1)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
2011-07-09 14:57:34 +00:00
|
|
|
// For targetIx > limitIx, this ceb.getPrevious gets a CE that is as far back in the ring buffer
|
|
|
|
// (compared to the last CE fetched for the previous targetIx value) as we need to go
|
|
|
|
// for this targetIx value, so if it is non-NULL then other ceb.getPrevious calls should be OK.
|
|
|
|
const CEI *lastCEI = ceb.getPrevious(targetIx);
|
|
|
|
if (lastCEI == NULL) {
|
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
// Inner loop checks for a match beginning at each
|
|
|
|
// position from the outer loop.
|
2010-02-27 01:01:45 +00:00
|
|
|
int32_t targetIxOffset = 0;
|
2009-01-22 00:24:48 +00:00
|
|
|
for (patIx = strsrch->pattern.PCELength - 1; patIx >= 0; patIx -= 1) {
|
2008-05-23 04:22:28 +00:00
|
|
|
int64_t patCE = strsrch->pattern.PCE[patIx];
|
|
|
|
|
2010-02-27 01:01:45 +00:00
|
|
|
targetCEI = ceb.getPrevious(targetIx + strsrch->pattern.PCELength - 1 - patIx + targetIxOffset);
|
2008-05-23 04:22:28 +00:00
|
|
|
// Compare CE from target string with CE from the pattern.
|
|
|
|
// Note that the target CE will be UCOL_NULLORDER if we reach the end of input,
|
|
|
|
// which will fail the compare, below.
|
2010-02-27 01:01:45 +00:00
|
|
|
UCompareCEsResult ceMatch = compareCE64s(targetCEI->ce, patCE, strsrch->search->elementComparisonType);
|
|
|
|
if ( ceMatch == U_CE_NO_MATCH ) {
|
2008-05-23 04:22:28 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
2010-02-27 01:01:45 +00:00
|
|
|
} else if ( ceMatch > U_CE_NO_MATCH ) {
|
|
|
|
if ( ceMatch == U_CE_SKIP_TARG ) {
|
|
|
|
// redo with same patCE, next targCE
|
|
|
|
patIx++;
|
|
|
|
targetIxOffset++;
|
|
|
|
} else { // ceMatch == U_CE_SKIP_PATN
|
|
|
|
// redo with same targCE, next patCE
|
|
|
|
targetIxOffset--;
|
|
|
|
}
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-27 07:43:41 +00:00
|
|
|
if (!found && ((targetCEI == NULL) || (targetCEI->ce != UCOL_PROCESSED_NULLORDER))) {
|
2008-05-23 04:22:28 +00:00
|
|
|
// No match at this targetIx. Try again at the next.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
// No match at all, we have run off the end of the target text.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We have found a match in CE space.
|
|
|
|
// Now determine the bounds in string index space.
|
|
|
|
// There still is a chance of match failure if the CE range not correspond to
|
|
|
|
// an acceptable character range.
|
|
|
|
//
|
2010-02-27 01:01:45 +00:00
|
|
|
const CEI *firstCEI = ceb.getPrevious(targetIx + strsrch->pattern.PCELength - 1 + targetIxOffset);
|
2008-05-23 04:22:28 +00:00
|
|
|
mStart = firstCEI->lowIndex;
|
|
|
|
|
|
|
|
// Check for the start of the match being within a combining sequence.
|
|
|
|
// This can happen if the pattern itself begins with a combining char, and
|
|
|
|
// the match found combining marks in the target text that were attached
|
|
|
|
// to something else.
|
|
|
|
// This type of match should be rejected for not completely consuming a
|
|
|
|
// combining sequence.
|
2011-04-22 05:37:46 +00:00
|
|
|
if (!isBreakBoundary(strsrch, mStart)) {
|
2008-05-23 04:22:28 +00:00
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look at the high index of the first CE in the match. If it's the same as the
|
|
|
|
// low index, the first CE in the match is in the middle of an expansion.
|
|
|
|
if (mStart == firstCEI->highIndex) {
|
|
|
|
found = FALSE;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2011-03-22 03:50:26 +00:00
|
|
|
minLimit = lastCEI->lowIndex;
|
|
|
|
|
|
|
|
if (targetIx > 0) {
|
|
|
|
// Look at the CE following the match. If it is UCOL_NULLORDER the match
|
|
|
|
// extended to the end of input, and the match is good.
|
|
|
|
|
|
|
|
// Look at the high and low indices of the CE following the match. If
|
|
|
|
// they are the same it means one of two things:
|
|
|
|
// 1. The match extended to the last CE from the target text, which is OK, or
|
|
|
|
// 2. The last CE that was part of the match is in an expansion that extends
|
|
|
|
// to the first CE after the match. In this case, we reject the match.
|
|
|
|
const CEI *nextCEI = ceb.getPrevious(targetIx - 1);
|
|
|
|
|
|
|
|
if (nextCEI->lowIndex == nextCEI->highIndex && nextCEI->ce != UCOL_PROCESSED_NULLORDER) {
|
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLimit = maxLimit = nextCEI->lowIndex;
|
|
|
|
|
|
|
|
// Advance the match end position to the first acceptable match boundary.
|
|
|
|
// This advances the index over any combining charcters.
|
|
|
|
if (minLimit < maxLimit) {
|
|
|
|
int32_t nba = nextBoundaryAfter(strsrch, minLimit);
|
|
|
|
|
|
|
|
if (nba >= lastCEI->highIndex) {
|
|
|
|
mLimit = nba;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If advancing to the end of a combining sequence in character indexing space
|
|
|
|
// advanced us beyond the end of the match in CE space, reject this match.
|
|
|
|
if (mLimit > maxLimit) {
|
|
|
|
found = FALSE;
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
2011-03-22 03:50:26 +00:00
|
|
|
|
|
|
|
// Make sure the end of the match is on a break boundary
|
2011-04-22 05:37:46 +00:00
|
|
|
if (!isBreakBoundary(strsrch, mLimit)) {
|
2011-03-22 03:50:26 +00:00
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// No non-ignorable CEs after this point.
|
|
|
|
// The maximum position is detected by boundary after
|
|
|
|
// the last non-ignorable CE. Combining sequence
|
|
|
|
// across the start index will be truncated.
|
|
|
|
int32_t nba = nextBoundaryAfter(strsrch, minLimit);
|
|
|
|
mLimit = maxLimit = (nba > 0) && (startIdx > nba) ? nba : startIdx;
|
2008-05-23 04:22:28 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
if (getenv("USEARCH_DEBUG") != NULL) {
|
|
|
|
printf("minLimit, maxLimit, mLimit = %d, %d, %d\n", minLimit, maxLimit, mLimit);
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
|
2009-01-22 00:24:48 +00:00
|
|
|
if (! checkIdentical(strsrch, mStart, mLimit)) {
|
|
|
|
found = FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
if (found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USEARCH_DEBUG
|
|
|
|
if (getenv("USEARCH_DEBUG") != NULL) {
|
|
|
|
printf("Target CEs [%d .. %d]\n", ceb.firstIx, ceb.limitIx);
|
|
|
|
int32_t lastToPrint = ceb.limitIx+2;
|
|
|
|
for (int ii=ceb.firstIx; ii<lastToPrint; ii++) {
|
|
|
|
printf("%8x@%d ", ceb.get(ii)->ce, ceb.get(ii)->srcIndex);
|
|
|
|
}
|
|
|
|
printf("\n%s\n", found? "match found" : "no match");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// All Done. Store back the match bounds to the caller.
|
|
|
|
//
|
|
|
|
if (found==FALSE) {
|
|
|
|
mLimit = -1;
|
|
|
|
mStart = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchStart != NULL) {
|
|
|
|
*matchStart= mStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchLimit != NULL) {
|
|
|
|
*matchLimit = mLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// internal use methods declared in usrchimp.h -----------------------------
|
|
|
|
|
|
|
|
UBool usearch_handleNextExact(UStringSearch *strsrch, UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-11-09 17:39:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2003-08-31 20:53:46 +00:00
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
2002-08-26 16:52:05 +00:00
|
|
|
int32_t textoffset = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
// status used in setting coleiter offset, since offset is checked in
|
2009-04-22 23:10:07 +00:00
|
|
|
// shiftForward before setting the coleiter offset, status never
|
2003-08-31 20:53:46 +00:00
|
|
|
// a failure
|
2009-04-22 23:10:07 +00:00
|
|
|
textoffset = shiftForward(strsrch, textoffset, UCOL_NULLORDER,
|
2002-08-26 16:52:05 +00:00
|
|
|
patterncelength);
|
|
|
|
while (textoffset <= textlength)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
uint32_t patternceindex = patterncelength - 1;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t targetce;
|
2001-08-25 02:03:53 +00:00
|
|
|
UBool found = FALSE;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t lastce = UCOL_NULLORDER;
|
2003-08-31 20:53:46 +00:00
|
|
|
|
|
|
|
setColEIterOffset(coleiter, textoffset);
|
|
|
|
|
2006-12-19 06:06:30 +00:00
|
|
|
for (;;) {
|
2001-09-07 21:56:18 +00:00
|
|
|
// finding the last pattern ce match, imagine composite characters
|
|
|
|
// for example: search for pattern A in text \u00C0
|
|
|
|
// we'll have to skip \u0300 the grave first before we get to A
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_previous(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
2009-04-22 23:10:07 +00:00
|
|
|
if (targetce == UCOL_IGNORABLE && inNormBuf(coleiter)) {
|
|
|
|
// this is for the text \u0315\u0300 that requires
|
2001-09-07 21:56:18 +00:00
|
|
|
// normalization and pattern \u0300, where \u0315 is ignorable
|
2001-08-25 02:03:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (lastce == UCOL_NULLORDER || lastce == UCOL_IGNORABLE) {
|
|
|
|
lastce = targetce;
|
|
|
|
}
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2001-08-25 02:03:53 +00:00
|
|
|
if (targetce == patternce[patternceindex]) {
|
|
|
|
// the first ce can be a contraction
|
2001-09-07 21:56:18 +00:00
|
|
|
found = TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (!hasExpansion(coleiter)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 19:04:25 +00:00
|
|
|
//targetce = lastce;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (found && patternceindex > 0) {
|
2011-03-22 03:50:26 +00:00
|
|
|
lastce = targetce;
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_previous(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
2001-11-09 17:39:42 +00:00
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = getCE(strsrch, targetce);
|
|
|
|
if (targetce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
patternceindex --;
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2009-04-22 23:10:07 +00:00
|
|
|
found = found && targetce == patternce[patternceindex];
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-10-19 19:04:25 +00:00
|
|
|
targetce = lastce;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
if (!found) {
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
textoffset = shiftForward(strsrch, textoffset, lastce,
|
2002-08-26 16:52:05 +00:00
|
|
|
patternceindex);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked at loop.
|
2001-08-25 02:03:53 +00:00
|
|
|
patternceindex = patterncelength;
|
|
|
|
continue;
|
|
|
|
}
|
2003-08-31 20:53:46 +00:00
|
|
|
|
|
|
|
if (checkNextExactMatch(strsrch, &textoffset, status)) {
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked in ucol_setOffset
|
2003-08-31 20:53:46 +00:00
|
|
|
setColEIterOffset(coleiter, strsrch->search->matchedIndex);
|
|
|
|
return TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2003-08-31 20:53:46 +00:00
|
|
|
return FALSE;
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
|
|
|
int32_t textOffset = ucol_getOffset(strsrch->textIter);
|
|
|
|
int32_t start = -1;
|
|
|
|
int32_t end = -1;
|
|
|
|
|
|
|
|
if (usearch_search(strsrch, textOffset, &start, &end, status)) {
|
|
|
|
strsrch->search->matchedIndex = start;
|
|
|
|
strsrch->search->matchedLength = end - start;
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool usearch_handleNextCanonical(UStringSearch *strsrch, UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-11-09 17:39:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
|
|
|
int32_t textlength = strsrch->search->textLength;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
2003-07-03 21:48:39 +00:00
|
|
|
int32_t textoffset = ucol_getOffset(coleiter);
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool hasPatternAccents =
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->pattern.hasSuffixAccents || strsrch->pattern.hasPrefixAccents;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
textoffset = shiftForward(strsrch, textoffset, UCOL_NULLORDER,
|
2002-08-26 16:52:05 +00:00
|
|
|
patterncelength);
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->canonicalPrefixAccents[0] = 0;
|
|
|
|
strsrch->canonicalSuffixAccents[0] = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (textoffset <= textlength)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
int32_t patternceindex = patterncelength - 1;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t targetce;
|
2001-08-25 02:03:53 +00:00
|
|
|
UBool found = FALSE;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t lastce = UCOL_NULLORDER;
|
2003-08-31 20:53:46 +00:00
|
|
|
|
|
|
|
setColEIterOffset(coleiter, textoffset);
|
|
|
|
|
|
|
|
for (;;) {
|
2001-09-07 21:56:18 +00:00
|
|
|
// finding the last pattern ce match, imagine composite characters
|
|
|
|
// for example: search for pattern A in text \u00C0
|
|
|
|
// we'll have to skip \u0300 the grave first before we get to A
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_previous(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
2001-09-07 21:56:18 +00:00
|
|
|
if (lastce == UCOL_NULLORDER || lastce == UCOL_IGNORABLE) {
|
|
|
|
lastce = targetce;
|
|
|
|
}
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2001-08-25 02:03:53 +00:00
|
|
|
if (targetce == patternce[patternceindex]) {
|
|
|
|
// the first ce can be a contraction
|
2001-09-07 21:56:18 +00:00
|
|
|
found = TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (!hasExpansion(coleiter)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (found && patternceindex > 0) {
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_previous(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
|
|
|
if (targetce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
patternceindex --;
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2009-04-22 23:10:07 +00:00
|
|
|
found = found && targetce == patternce[patternceindex];
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initializing the rearranged accent array
|
|
|
|
if (hasPatternAccents && !found) {
|
|
|
|
strsrch->canonicalPrefixAccents[0] = 0;
|
|
|
|
strsrch->canonicalSuffixAccents[0] = 0;
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
break;
|
|
|
|
}
|
2001-08-25 02:03:53 +00:00
|
|
|
found = doNextCanonicalMatch(strsrch, textoffset, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
textoffset = shiftForward(strsrch, textoffset, lastce,
|
2002-08-26 16:52:05 +00:00
|
|
|
patternceindex);
|
2001-11-09 17:39:42 +00:00
|
|
|
// status checked at loop
|
2001-08-25 02:03:53 +00:00
|
|
|
patternceindex = patterncelength;
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
if (checkNextCanonicalMatch(strsrch, &textoffset, status)) {
|
2002-12-10 06:02:50 +00:00
|
|
|
setColEIterOffset(coleiter, strsrch->search->matchedIndex);
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
|
|
|
int32_t textOffset = ucol_getOffset(strsrch->textIter);
|
|
|
|
int32_t start = -1;
|
|
|
|
int32_t end = -1;
|
|
|
|
|
|
|
|
if (usearch_search(strsrch, textOffset, &start, &end, status)) {
|
|
|
|
strsrch->search->matchedIndex = start;
|
|
|
|
strsrch->search->matchedLength = end - start;
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool usearch_handlePreviousExact(UStringSearch *strsrch, UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-11-09 17:39:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t textoffset = ucol_getOffset(coleiter);
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
// shifting it check for setting offset
|
|
|
|
// if setOffset is called previously or there was no previous match, we
|
|
|
|
// leave the offset as it is.
|
|
|
|
if (strsrch->search->matchedIndex != USEARCH_DONE) {
|
|
|
|
textoffset = strsrch->search->matchedIndex;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
textoffset = reverseShift(strsrch, textoffset, UCOL_NULLORDER,
|
2002-08-26 16:52:05 +00:00
|
|
|
patterncelength);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (textoffset >= 0)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
2001-09-07 21:56:18 +00:00
|
|
|
int32_t patternceindex = 1;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t targetce;
|
2001-08-25 02:03:53 +00:00
|
|
|
UBool found = FALSE;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t firstce = UCOL_NULLORDER;
|
2002-08-26 16:52:05 +00:00
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
// if status is a failure, ucol_setOffset does nothing
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, textoffset);
|
2003-08-31 20:53:46 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2009-04-22 23:10:07 +00:00
|
|
|
// finding the first pattern ce match, imagine composite
|
|
|
|
// characters. for example: search for pattern \u0300 in text
|
|
|
|
// \u00C0, we'll have to skip A first before we get to
|
2001-09-07 21:56:18 +00:00
|
|
|
// \u0300 the grave accent
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_next(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
2001-09-07 21:56:18 +00:00
|
|
|
if (firstce == UCOL_NULLORDER || firstce == UCOL_IGNORABLE) {
|
|
|
|
firstce = targetce;
|
|
|
|
}
|
2007-10-25 22:02:54 +00:00
|
|
|
if (targetce == UCOL_IGNORABLE && strsrch->strength != UCOL_PRIMARY) {
|
2001-08-25 02:03:53 +00:00
|
|
|
continue;
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2001-08-25 02:03:53 +00:00
|
|
|
if (targetce == patternce[0]) {
|
2001-09-07 21:56:18 +00:00
|
|
|
found = TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (!hasExpansion(coleiter)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// checking for accents in composite character
|
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 19:04:25 +00:00
|
|
|
//targetce = firstce;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (found && (patternceindex < patterncelength)) {
|
2011-03-22 03:50:26 +00:00
|
|
|
firstce = targetce;
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_next(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
|
|
|
if (targetce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2009-04-22 23:10:07 +00:00
|
|
|
found = found && targetce == patternce[patternceindex];
|
2001-08-25 02:03:53 +00:00
|
|
|
patternceindex ++;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2007-10-19 19:04:25 +00:00
|
|
|
targetce = firstce;
|
2001-08-25 02:03:53 +00:00
|
|
|
|
|
|
|
if (!found) {
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
textoffset = reverseShift(strsrch, textoffset, targetce,
|
2002-08-26 16:52:05 +00:00
|
|
|
patternceindex);
|
2001-08-25 02:03:53 +00:00
|
|
|
patternceindex = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
if (checkPreviousExactMatch(strsrch, &textoffset, status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, textoffset);
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
2011-03-22 03:50:26 +00:00
|
|
|
int32_t textOffset;
|
|
|
|
|
|
|
|
if (strsrch->search->isOverlap) {
|
|
|
|
if (strsrch->search->matchedIndex != USEARCH_DONE) {
|
|
|
|
textOffset = strsrch->search->matchedIndex + strsrch->search->matchedLength - 1;
|
|
|
|
} else {
|
|
|
|
// move the start position at the end of possible match
|
|
|
|
initializePatternPCETable(strsrch, status);
|
|
|
|
for (int32_t nPCEs = 0; nPCEs < strsrch->pattern.PCELength - 1; nPCEs++) {
|
|
|
|
int64_t pce = ucol_nextProcessed(strsrch->textIter, NULL, NULL, status);
|
|
|
|
if (pce == UCOL_PROCESSED_NULLORDER) {
|
|
|
|
// at the end of the text
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
textOffset = ucol_getOffset(strsrch->textIter);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
textOffset = ucol_getOffset(strsrch->textIter);
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
int32_t start = -1;
|
|
|
|
int32_t end = -1;
|
|
|
|
|
|
|
|
if (usearch_searchBackwards(strsrch, textOffset, &start, &end, status)) {
|
|
|
|
strsrch->search->matchedIndex = start;
|
|
|
|
strsrch->search->matchedLength = end - start;
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool usearch_handlePreviousCanonical(UStringSearch *strsrch,
|
2001-08-25 02:03:53 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-11-09 17:39:42 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-11-09 17:39:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
#if BOYER_MOORE
|
2001-08-25 02:03:53 +00:00
|
|
|
UCollationElements *coleiter = strsrch->textIter;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t *patternce = strsrch->pattern.CE;
|
2001-08-25 02:03:53 +00:00
|
|
|
int32_t patterncelength = strsrch->pattern.CELength;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t textoffset = ucol_getOffset(coleiter);
|
2009-04-22 23:10:07 +00:00
|
|
|
UBool hasPatternAccents =
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->pattern.hasSuffixAccents || strsrch->pattern.hasPrefixAccents;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2001-08-25 02:03:53 +00:00
|
|
|
// shifting it check for setting offset
|
|
|
|
// if setOffset is called previously or there was no previous match, we
|
|
|
|
// leave the offset as it is.
|
|
|
|
if (strsrch->search->matchedIndex != USEARCH_DONE) {
|
|
|
|
textoffset = strsrch->search->matchedIndex;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
|
|
|
textoffset = reverseShift(strsrch, textoffset, UCOL_NULLORDER,
|
2002-08-26 16:52:05 +00:00
|
|
|
patterncelength);
|
2001-08-25 02:03:53 +00:00
|
|
|
strsrch->canonicalPrefixAccents[0] = 0;
|
|
|
|
strsrch->canonicalSuffixAccents[0] = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (textoffset >= 0)
|
2001-08-25 02:03:53 +00:00
|
|
|
{
|
|
|
|
int32_t patternceindex = 1;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t targetce;
|
2001-08-25 02:03:53 +00:00
|
|
|
UBool found = FALSE;
|
2004-04-01 22:04:52 +00:00
|
|
|
int32_t firstce = UCOL_NULLORDER;
|
2002-08-26 16:52:05 +00:00
|
|
|
|
|
|
|
setColEIterOffset(coleiter, textoffset);
|
2006-12-19 06:06:30 +00:00
|
|
|
for (;;) {
|
2009-04-22 23:10:07 +00:00
|
|
|
// finding the first pattern ce match, imagine composite
|
|
|
|
// characters. for example: search for pattern \u0300 in text
|
|
|
|
// \u00C0, we'll have to skip A first before we get to
|
2001-09-07 21:56:18 +00:00
|
|
|
// \u0300 the grave accent
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_next(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
2001-09-07 21:56:18 +00:00
|
|
|
if (firstce == UCOL_NULLORDER || firstce == UCOL_IGNORABLE) {
|
|
|
|
firstce = targetce;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2001-08-25 02:03:53 +00:00
|
|
|
if (targetce == patternce[0]) {
|
|
|
|
// the first ce can be a contraction
|
2001-09-07 21:56:18 +00:00
|
|
|
found = TRUE;
|
2001-08-25 02:03:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 21:56:18 +00:00
|
|
|
if (!hasExpansion(coleiter)) {
|
2001-08-25 02:03:53 +00:00
|
|
|
// checking for accents in composite character
|
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
targetce = firstce;
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2002-08-26 16:52:05 +00:00
|
|
|
while (found && patternceindex < patterncelength) {
|
2001-08-25 02:03:53 +00:00
|
|
|
targetce = ucol_next(coleiter, status);
|
2002-08-26 16:52:05 +00:00
|
|
|
if (U_FAILURE(*status) || targetce == UCOL_NULLORDER) {
|
2001-08-25 02:03:53 +00:00
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetce = getCE(strsrch, targetce);
|
|
|
|
if (targetce == UCOL_IGNORABLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-27 01:01:45 +00:00
|
|
|
// TODO: #if BOYER_MOORE, replace with code using 32-bit version of compareCE64s
|
2009-04-22 23:10:07 +00:00
|
|
|
found = found && targetce == patternce[patternceindex];
|
2001-08-25 02:03:53 +00:00
|
|
|
patternceindex ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initializing the rearranged accent array
|
|
|
|
if (hasPatternAccents && !found) {
|
|
|
|
strsrch->canonicalPrefixAccents[0] = 0;
|
|
|
|
strsrch->canonicalSuffixAccents[0] = 0;
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2001-11-09 17:39:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
found = doPreviousCanonicalMatch(strsrch, textoffset, status);
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2003-08-31 20:53:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-22 23:10:07 +00:00
|
|
|
textoffset = reverseShift(strsrch, textoffset, targetce,
|
2002-08-26 16:52:05 +00:00
|
|
|
patternceindex);
|
2001-08-25 02:03:53 +00:00
|
|
|
patternceindex = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkPreviousCanonicalMatch(strsrch, &textoffset, status)) {
|
2002-08-26 16:52:05 +00:00
|
|
|
setColEIterOffset(coleiter, textoffset);
|
2001-08-25 02:03:53 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2002-08-26 16:52:05 +00:00
|
|
|
setMatchNotFound(strsrch);
|
2001-08-25 02:03:53 +00:00
|
|
|
return FALSE;
|
2008-05-23 04:22:28 +00:00
|
|
|
#else
|
2011-03-22 03:50:26 +00:00
|
|
|
int32_t textOffset;
|
|
|
|
|
|
|
|
if (strsrch->search->isOverlap) {
|
|
|
|
if (strsrch->search->matchedIndex != USEARCH_DONE) {
|
|
|
|
textOffset = strsrch->search->matchedIndex + strsrch->search->matchedLength - 1;
|
|
|
|
} else {
|
|
|
|
// move the start position at the end of possible match
|
|
|
|
initializePatternPCETable(strsrch, status);
|
|
|
|
for (int32_t nPCEs = 0; nPCEs < strsrch->pattern.PCELength - 1; nPCEs++) {
|
|
|
|
int64_t pce = ucol_nextProcessed(strsrch->textIter, NULL, NULL, status);
|
|
|
|
if (pce == UCOL_PROCESSED_NULLORDER) {
|
|
|
|
// at the end of the text
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
textOffset = ucol_getOffset(strsrch->textIter);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
textOffset = ucol_getOffset(strsrch->textIter);
|
|
|
|
}
|
|
|
|
|
2008-05-23 04:22:28 +00:00
|
|
|
int32_t start = -1;
|
|
|
|
int32_t end = -1;
|
|
|
|
|
|
|
|
if (usearch_searchBackwards(strsrch, textOffset, &start, &end, status)) {
|
|
|
|
strsrch->search->matchedIndex = start;
|
|
|
|
strsrch->search->matchedLength = end - start;
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
setMatchNotFound(strsrch);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_COLLATION */
|