1999-10-26 17:15:44 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2001-03-21 20:31:13 +00:00
|
|
|
* Copyright (C) 1999-2001, International Business Machines
|
1999-11-23 01:31:13 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
1999-10-26 17:15:44 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 10/20/99 alan Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
1999-10-20 22:08:09 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/parsepos.h"
|
2002-03-19 03:01:28 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-10-10 21:36:26 +00:00
|
|
|
#include "unicode/uscript.h"
|
2000-02-08 02:49:15 +00:00
|
|
|
#include "symtable.h"
|
2000-05-25 19:27:14 +00:00
|
|
|
#include "cmemory.h"
|
2002-04-25 23:39:57 +00:00
|
|
|
#include "uhash.h"
|
2001-10-17 19:21:12 +00:00
|
|
|
#include "upropset.h"
|
2001-11-21 22:43:21 +00:00
|
|
|
#include "util.h"
|
2002-04-25 23:39:57 +00:00
|
|
|
#include "uvector.h"
|
2000-05-25 19:27:14 +00:00
|
|
|
|
2001-06-21 18:27:52 +00:00
|
|
|
// HIGH_VALUE > all valid values. 110000 for codepoints
|
|
|
|
#define UNICODESET_HIGH 0x0110000
|
|
|
|
|
|
|
|
// LOW <= all valid values. ZERO for codepoints
|
|
|
|
#define UNICODESET_LOW 0x000000
|
2000-05-31 22:18:22 +00:00
|
|
|
|
2001-07-02 20:09:52 +00:00
|
|
|
// initial storage. Must be >= 0
|
|
|
|
#define START_EXTRA 16
|
|
|
|
|
|
|
|
// extra amount for growth. Must be >= 0
|
|
|
|
#define GROW_EXTRA START_EXTRA
|
|
|
|
|
|
|
|
// Define UChar constants using hex for EBCDIC compatibility
|
|
|
|
// Used #define to reduce private static exports and memory access time.
|
|
|
|
#define SET_OPEN ((UChar)0x005B) /*[*/
|
|
|
|
#define SET_CLOSE ((UChar)0x005D) /*]*/
|
|
|
|
#define HYPHEN ((UChar)0x002D) /*-*/
|
|
|
|
#define COMPLEMENT ((UChar)0x005E) /*^*/
|
|
|
|
#define COLON ((UChar)0x003A) /*:*/
|
|
|
|
#define BACKSLASH ((UChar)0x005C) /*\*/
|
|
|
|
#define INTERSECTION ((UChar)0x0026) /*&*/
|
|
|
|
#define UPPER_U ((UChar)0x0055) /*U*/
|
|
|
|
#define LOWER_U ((UChar)0x0075) /*u*/
|
2002-04-25 23:39:57 +00:00
|
|
|
#define OPEN_BRACE ((UChar)123) /*{*/
|
|
|
|
#define CLOSE_BRACE ((UChar)125) /*}*/
|
2000-05-25 19:27:14 +00:00
|
|
|
|
2001-10-18 04:16:16 +00:00
|
|
|
// TEMPORARY: Remove when deprecated category code constructor is removed.
|
|
|
|
static const UChar CATEGORY_NAMES[] = {
|
|
|
|
// Must be kept in sync with uchar.h/UCharCategory
|
|
|
|
0x43, 0x6E, /* "Cn" */
|
|
|
|
0x4C, 0x75, /* "Lu" */
|
|
|
|
0x4C, 0x6C, /* "Ll" */
|
|
|
|
0x4C, 0x74, /* "Lt" */
|
|
|
|
0x4C, 0x6D, /* "Lm" */
|
|
|
|
0x4C, 0x6F, /* "Lo" */
|
|
|
|
0x4D, 0x6E, /* "Mn" */
|
|
|
|
0x4D, 0x65, /* "Me" */
|
|
|
|
0x4D, 0x63, /* "Mc" */
|
|
|
|
0x4E, 0x64, /* "Nd" */
|
|
|
|
0x4E, 0x6C, /* "Nl" */
|
|
|
|
0x4E, 0x6F, /* "No" */
|
|
|
|
0x5A, 0x73, /* "Zs" */
|
|
|
|
0x5A, 0x6C, /* "Zl" */
|
|
|
|
0x5A, 0x70, /* "Zp" */
|
|
|
|
0x43, 0x63, /* "Cc" */
|
|
|
|
0x43, 0x66, /* "Cf" */
|
|
|
|
0x43, 0x6F, /* "Co" */
|
|
|
|
0x43, 0x73, /* "Cs" */
|
|
|
|
0x50, 0x64, /* "Pd" */
|
|
|
|
0x50, 0x73, /* "Ps" */
|
|
|
|
0x50, 0x65, /* "Pe" */
|
|
|
|
0x50, 0x63, /* "Pc" */
|
|
|
|
0x50, 0x6F, /* "Po" */
|
|
|
|
0x53, 0x6D, /* "Sm" */
|
|
|
|
0x53, 0x63, /* "Sc" */
|
|
|
|
0x53, 0x6B, /* "Sk" */
|
|
|
|
0x53, 0x6F, /* "So" */
|
|
|
|
0x50, 0x69, /* "Pi" */
|
|
|
|
0x50, 0x66, /* "Pf" */
|
|
|
|
0x00
|
|
|
|
};
|
1999-10-20 22:08:09 +00:00
|
|
|
|
2000-01-12 18:03:53 +00:00
|
|
|
/**
|
|
|
|
* Delimiter string used in patterns to close a category reference:
|
|
|
|
* ":]". Example: "[:Lu:]".
|
|
|
|
*/
|
2001-07-02 20:09:52 +00:00
|
|
|
static const UChar CATEGORY_CLOSE[] = {COLON, SET_CLOSE, 0x0000}; /* ":]" */
|
2000-01-12 18:03:53 +00:00
|
|
|
|
2001-10-12 05:54:15 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum value that can be stored in a UnicodeSet.
|
|
|
|
*/
|
|
|
|
const UChar32 UnicodeSet::MIN_VALUE = UNICODESET_LOW;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum value that can be stored in a UnicodeSet.
|
|
|
|
*/
|
|
|
|
const UChar32 UnicodeSet::MAX_VALUE = UNICODESET_HIGH - 1;
|
|
|
|
|
2001-11-09 01:22:33 +00:00
|
|
|
const char UnicodeSet::fgClassID = 0;
|
|
|
|
|
2001-11-14 20:16:39 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Debugging
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
// DO NOT DELETE THIS CODE. This code is used to debug memory leaks.
|
|
|
|
// To enable the debugging, define the symbol DEBUG_MEM in the line
|
|
|
|
// below. This will result in text being sent to stdout that looks
|
|
|
|
// like this:
|
|
|
|
// DEBUG UnicodeSet: ct 0x00A39B20; 397 [\u0A81-\u0A83\u0A85-
|
|
|
|
// DEBUG UnicodeSet: dt 0x00A39B20; 396 [\u0A81-\u0A83\u0A85-
|
|
|
|
// Each line lists a construction (ct) or destruction (dt) event, the
|
2001-11-20 00:47:12 +00:00
|
|
|
// object address, the number of outstanding objects after the event,
|
2001-11-14 20:16:39 +00:00
|
|
|
// and the pattern of the object in question.
|
|
|
|
|
|
|
|
// #define DEBUG_MEM
|
|
|
|
|
|
|
|
#ifdef DEBUG_MEM
|
|
|
|
#include <stdio.h>
|
|
|
|
static int32_t _dbgCount = 0;
|
|
|
|
|
2001-11-15 01:28:13 +00:00
|
|
|
static inline void _dbgct(UnicodeSet* set) {
|
2001-11-14 20:16:39 +00:00
|
|
|
UnicodeString str;
|
|
|
|
set->toPattern(str, TRUE);
|
|
|
|
char buf[40];
|
|
|
|
str.extract(0, 39, buf, "");
|
|
|
|
printf("DEBUG UnicodeSet: ct 0x%08X; %d %s\n", set, ++_dbgCount, buf);
|
|
|
|
}
|
|
|
|
|
2001-11-15 01:28:13 +00:00
|
|
|
static inline void _dbgdt(UnicodeSet* set) {
|
2001-11-14 20:16:39 +00:00
|
|
|
UnicodeString str;
|
|
|
|
set->toPattern(str, TRUE);
|
|
|
|
char buf[40];
|
|
|
|
str.extract(0, 39, buf, "");
|
|
|
|
printf("DEBUG UnicodeSet: dt 0x%08X; %d %s\n", set, --_dbgCount, buf);
|
|
|
|
}
|
|
|
|
|
2002-05-14 21:21:03 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define _dbgct(set)
|
|
|
|
#define _dbgdt(set)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// UnicodeString in UVector support
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2002-05-14 21:21:03 +00:00
|
|
|
static void U_CALLCONV cloneUnicodeString(UHashTok dst, UHashTok src) {
|
2002-04-25 23:39:57 +00:00
|
|
|
dst.pointer = new UnicodeString(*(UnicodeString*)src.pointer);
|
|
|
|
}
|
|
|
|
|
2002-05-14 21:21:03 +00:00
|
|
|
static int8_t U_CALLCONV compareUnicodeString(UHashTok t1, UHashTok t2) {
|
2002-04-25 23:39:57 +00:00
|
|
|
const UnicodeString &a = *(const UnicodeString*)t1.pointer;
|
|
|
|
const UnicodeString &b = *(const UnicodeString*)t2.pointer;
|
|
|
|
return a.compare(b);
|
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
//----------------------------------------------------------------
|
2000-05-25 19:27:14 +00:00
|
|
|
// Constructors &c
|
1999-10-20 22:08:09 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2000-05-25 19:27:14 +00:00
|
|
|
* Constructs an empty set.
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
UnicodeSet::UnicodeSet() :
|
|
|
|
len(1), capacity(1 + START_EXTRA), bufferCapacity(0),
|
2001-03-06 20:20:47 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2001-06-21 18:27:52 +00:00
|
|
|
list[0] = UNICODESET_HIGH;
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgct(this);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
|
|
|
|
/**
|
2000-05-25 19:27:14 +00:00
|
|
|
* Constructs a set containing the given range. If <code>end >
|
|
|
|
* start</code> then an empty set is created.
|
|
|
|
*
|
|
|
|
* @param start first character, inclusive, of range
|
|
|
|
* @param end last character, inclusive, of range
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
UnicodeSet::UnicodeSet(UChar32 start, UChar32 end) :
|
|
|
|
len(1), capacity(1 + START_EXTRA), bufferCapacity(0),
|
2001-03-06 20:20:47 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2001-06-21 18:27:52 +00:00
|
|
|
list[0] = UNICODESET_HIGH;
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
2000-05-26 18:32:15 +00:00
|
|
|
complement(start, end);
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgct(this);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a set from the given pattern, optionally ignoring
|
|
|
|
* white space. See the class description for the syntax of the
|
|
|
|
* pattern language.
|
|
|
|
* @param pattern a string specifying what characters are in the set
|
|
|
|
*/
|
|
|
|
UnicodeSet::UnicodeSet(const UnicodeString& pattern,
|
2000-05-25 19:27:14 +00:00
|
|
|
UErrorCode& status) :
|
|
|
|
len(0), capacity(START_EXTRA), bufferCapacity(0),
|
2001-03-06 20:20:47 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
1999-10-20 22:08:09 +00:00
|
|
|
applyPattern(pattern, status);
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgct(this);
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2000-02-08 02:49:15 +00:00
|
|
|
// For internal use by RuleBasedTransliterator
|
2000-01-13 07:28:08 +00:00
|
|
|
UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
|
2000-02-08 02:49:15 +00:00
|
|
|
const SymbolTable& symbols,
|
2000-05-25 19:27:14 +00:00
|
|
|
UErrorCode& status) :
|
|
|
|
len(0), capacity(START_EXTRA), bufferCapacity(0),
|
2001-03-06 20:20:47 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
2000-05-25 19:27:14 +00:00
|
|
|
applyPattern(pattern, pos, &symbols, status);
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgct(this);
|
2000-01-13 07:28:08 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
// For internal use by TransliteratorIDParser
|
|
|
|
UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
|
|
|
|
UErrorCode& status) :
|
|
|
|
len(0), capacity(START_EXTRA), bufferCapacity(0),
|
|
|
|
buffer(0)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
2002-02-07 01:07:55 +00:00
|
|
|
applyPattern(pattern, pos, NULL, status);
|
|
|
|
_dbgct(this);
|
|
|
|
}
|
|
|
|
|
2001-10-18 04:16:16 +00:00
|
|
|
/**
|
|
|
|
* DEPRECATED Constructs a set from the given Unicode character category.
|
|
|
|
* @param category an integer indicating the character category as
|
|
|
|
* defined in uchar.h.
|
|
|
|
* @deprecated To be removed after 2002-DEC-31
|
|
|
|
*/
|
|
|
|
UnicodeSet::UnicodeSet(int8_t category, UErrorCode& status) :
|
|
|
|
len(0), capacity(START_EXTRA), bufferCapacity(0), list(0),
|
2002-04-25 23:39:57 +00:00
|
|
|
buffer(0), strings(0)
|
2001-10-18 04:16:16 +00:00
|
|
|
{
|
|
|
|
static const UChar OPEN[] = { 91, 58, 0 }; // "[:"
|
|
|
|
static const UChar CLOSE[]= { 58, 93, 0 }; // ":]"
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (category < 0 || category >= U_CHAR_CATEGORY_COUNT) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
2001-10-19 23:57:38 +00:00
|
|
|
UnicodeString pattern(FALSE, CATEGORY_NAMES + category*2, 2);
|
|
|
|
pattern.insert(0, OPEN);
|
|
|
|
pattern.append(CLOSE);
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
2001-10-19 23:57:38 +00:00
|
|
|
applyPattern(pattern, status);
|
2001-10-18 04:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgct(this);
|
2001-10-18 04:16:16 +00:00
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
|
1999-10-26 17:15:44 +00:00
|
|
|
/**
|
|
|
|
* Constructs a set that is identical to the given UnicodeSet.
|
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
UnicodeSet::UnicodeSet(const UnicodeSet& o) :
|
2001-03-06 20:20:47 +00:00
|
|
|
UnicodeFilter(o),
|
2000-08-23 23:48:04 +00:00
|
|
|
capacity(o.len + GROW_EXTRA), bufferCapacity(0),
|
|
|
|
buffer(0)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-04-25 23:39:57 +00:00
|
|
|
allocateStrings();
|
2000-05-25 19:27:14 +00:00
|
|
|
*this = o;
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgct(this);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
1999-10-26 17:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructs the set.
|
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
UnicodeSet::~UnicodeSet() {
|
2001-11-14 20:16:39 +00:00
|
|
|
_dbgdt(this); // first!
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(list);
|
|
|
|
uprv_free(buffer);
|
2002-04-25 23:39:57 +00:00
|
|
|
delete strings;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
1999-10-26 17:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns this object to be a copy of another.
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::operator=(const UnicodeSet& o) {
|
2000-05-25 19:27:14 +00:00
|
|
|
ensureCapacity(o.len);
|
|
|
|
len = o.len;
|
|
|
|
uprv_memcpy(list, o.list, len*sizeof(UChar32));
|
2002-04-25 23:39:57 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
strings->assign(*o.strings, cloneUnicodeString, ec);
|
2001-06-12 18:07:15 +00:00
|
|
|
pat = o.pat;
|
1999-10-26 17:15:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the specified object with this set for equality. Returns
|
|
|
|
* <tt>true</tt> if the two sets
|
|
|
|
* have the same size, and every member of the specified set is
|
|
|
|
* contained in this set (or equivalently, every member of this set is
|
|
|
|
* contained in the specified set).
|
|
|
|
*
|
|
|
|
* @param o set to be compared for equality with this set.
|
|
|
|
* @return <tt>true</tt> if the specified set is equal to this set.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool UnicodeSet::operator==(const UnicodeSet& o) const {
|
2000-05-25 19:27:14 +00:00
|
|
|
if (len != o.len) return FALSE;
|
|
|
|
for (int32_t i = 0; i < len; ++i) {
|
|
|
|
if (list[i] != o.list[i]) return FALSE;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
if (*strings != *o.strings) return FALSE;
|
2000-05-25 19:27:14 +00:00
|
|
|
return TRUE;
|
1999-10-26 17:15:44 +00:00
|
|
|
}
|
|
|
|
|
2000-01-18 20:00:56 +00:00
|
|
|
/**
|
2001-07-27 00:18:53 +00:00
|
|
|
* Returns a copy of this object. All UnicodeMatcher objects have
|
2000-01-18 20:00:56 +00:00
|
|
|
* to support cloning in order to allow classes using
|
2001-07-27 00:18:53 +00:00
|
|
|
* UnicodeMatchers, such as Transliterator, to implement cloning.
|
2000-01-18 20:00:56 +00:00
|
|
|
*/
|
2002-02-07 01:07:55 +00:00
|
|
|
UnicodeFunctor* UnicodeSet::clone() const {
|
2000-01-18 20:00:56 +00:00
|
|
|
return new UnicodeSet(*this);
|
|
|
|
}
|
|
|
|
|
1999-10-26 17:15:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the hash code value for this set.
|
|
|
|
*
|
|
|
|
* @return the hash code value for this set.
|
|
|
|
* @see Object#hashCode()
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t UnicodeSet::hashCode(void) const {
|
2000-05-25 19:27:14 +00:00
|
|
|
int32_t result = len;
|
|
|
|
for (int32_t i = 0; i < len; ++i) {
|
|
|
|
result *= 1000003;
|
|
|
|
result += list[i];
|
|
|
|
}
|
|
|
|
return result;
|
1999-10-26 17:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Public API
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
/**
|
|
|
|
* Make this object represent the range <code>start - end</code>.
|
|
|
|
* If <code>end > start</code> then this object is set to an
|
|
|
|
* an empty range.
|
|
|
|
*
|
|
|
|
* @param start first character in the set, inclusive
|
|
|
|
* @rparam end last character in the set, inclusive
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::set(UChar32 start, UChar32 end) {
|
2000-05-25 19:27:14 +00:00
|
|
|
clear();
|
2000-05-26 18:32:15 +00:00
|
|
|
complement(start, end);
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
|
|
|
* Modifies this set to represent the set specified by the given
|
|
|
|
* pattern, optionally ignoring white space. See the class
|
|
|
|
* description for the syntax of the pattern language.
|
|
|
|
* @param pattern a string specifying what characters are in the set
|
|
|
|
* @param ignoreSpaces if <code>true</code>, all spaces in the
|
|
|
|
* pattern are ignored. Spaces are those characters for which
|
|
|
|
* <code>Character.isSpaceChar()</code> is <code>true</code>.
|
|
|
|
* Characters preceded by '\\' are escaped, losing any special
|
|
|
|
* meaning they otherwise have. Spaces may be included by
|
|
|
|
* escaping them.
|
|
|
|
* @exception <code>IllegalArgumentException</code> if the pattern
|
|
|
|
* contains a syntax error.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::applyPattern(const UnicodeString& pattern,
|
|
|
|
UErrorCode& status) {
|
1999-10-20 22:08:09 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ParsePosition pos(0);
|
2000-05-25 19:27:14 +00:00
|
|
|
applyPattern(pattern, pos, NULL, status);
|
2002-04-25 23:39:57 +00:00
|
|
|
if (U_FAILURE(status)) return *this;
|
2000-01-12 18:03:53 +00:00
|
|
|
|
2000-01-13 07:28:08 +00:00
|
|
|
// Skip over trailing whitespace
|
|
|
|
int32_t i = pos.getIndex();
|
|
|
|
int32_t n = pattern.length();
|
2002-03-19 03:01:28 +00:00
|
|
|
while (i<n && u_isWhitespace(pattern.charAt(i))) {
|
2000-01-13 07:28:08 +00:00
|
|
|
++i;
|
2000-01-12 18:03:53 +00:00
|
|
|
}
|
|
|
|
|
2000-01-13 07:28:08 +00:00
|
|
|
if (i != n) {
|
1999-10-20 22:08:09 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2001-10-17 19:21:12 +00:00
|
|
|
/**
|
|
|
|
* Return true if the given position, in the given pattern, appears
|
|
|
|
* to be the start of a UnicodeSet pattern.
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::resemblesPattern(const UnicodeString& pattern, int32_t pos) {
|
|
|
|
return ((pos+1) < pattern.length() &&
|
|
|
|
pattern.charAt(pos) == (UChar)91/*[*/) ||
|
|
|
|
UnicodePropertySet::resemblesPattern(pattern, pos);
|
|
|
|
}
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* Append the <code>toPattern()</code> representation of a
|
|
|
|
* string to the given <code>StringBuffer</code>.
|
|
|
|
*/
|
|
|
|
void UnicodeSet::_appendToPat(UnicodeString& buf, const UnicodeString& s, UBool useHexEscape) {
|
|
|
|
UChar32 cp;
|
|
|
|
for (int32_t i = 0; i < s.length(); i += UTF_CHAR_LENGTH(cp)) {
|
|
|
|
_appendToPat(buf, cp = s.char32At(i), useHexEscape);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
/**
|
|
|
|
* Append the <code>toPattern()</code> representation of a
|
|
|
|
* character to the given <code>StringBuffer</code>.
|
|
|
|
*/
|
2001-06-12 18:07:15 +00:00
|
|
|
void UnicodeSet::_appendToPat(UnicodeString& buf, UChar32 c, UBool useHexEscape) {
|
|
|
|
if (useHexEscape) {
|
|
|
|
// Use hex escape notation (\uxxxx or \Uxxxxxxxx) for anything
|
|
|
|
// unprintable
|
2001-12-03 20:19:08 +00:00
|
|
|
if (ICU_Utility::escapeUnprintable(buf, c)) {
|
2001-06-12 18:07:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
// Okay to let ':' pass through
|
|
|
|
switch (c) {
|
|
|
|
case SET_OPEN:
|
|
|
|
case SET_CLOSE:
|
|
|
|
case HYPHEN:
|
|
|
|
case COMPLEMENT:
|
|
|
|
case INTERSECTION:
|
|
|
|
case BACKSLASH:
|
2001-10-17 19:21:12 +00:00
|
|
|
case 123/*{*/:
|
|
|
|
case 125/*}*/:
|
2001-12-01 01:33:41 +00:00
|
|
|
case SymbolTable::SYMBOL_REF:
|
|
|
|
case COLON:
|
2000-05-25 19:27:14 +00:00
|
|
|
buf.append(BACKSLASH);
|
2001-06-12 18:07:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Escape whitespace
|
2001-12-01 01:33:41 +00:00
|
|
|
if (u_isspace(c)) {
|
2001-06-12 18:07:15 +00:00
|
|
|
buf.append(BACKSLASH);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
buf.append((UChar) c);
|
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
|
|
|
* Returns a string representation of this set. If the result of
|
|
|
|
* calling this function is passed to a UnicodeSet constructor, it
|
|
|
|
* will produce another set that is equal to this one.
|
|
|
|
*/
|
2001-06-12 18:07:15 +00:00
|
|
|
UnicodeString& UnicodeSet::toPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const {
|
|
|
|
result.truncate(0);
|
|
|
|
return _toPattern(result, escapeUnprintable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a string representation of this set to result. This will be
|
|
|
|
* a cleaned version of the string passed to applyPattern(), if there
|
|
|
|
* is one. Otherwise it will be generated.
|
|
|
|
*/
|
|
|
|
UnicodeString& UnicodeSet::_toPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const {
|
2001-07-18 18:38:00 +00:00
|
|
|
if (pat.length() > 0) {
|
|
|
|
int32_t i;
|
|
|
|
int32_t backslashCount = 0;
|
2001-12-01 01:33:41 +00:00
|
|
|
for (i=0; i<pat.length(); ) {
|
|
|
|
UChar32 c = pat.char32At(i);
|
|
|
|
i += UTF_CHAR_LENGTH(c);
|
2001-12-03 20:19:08 +00:00
|
|
|
if (escapeUnprintable && ICU_Utility::isUnprintable(c)) {
|
2001-07-18 18:38:00 +00:00
|
|
|
// If the unprintable character is preceded by an odd
|
|
|
|
// number of backslashes, then it has been escaped.
|
|
|
|
// Before unescaping it, we delete the final
|
|
|
|
// backslash.
|
|
|
|
if ((backslashCount % 2) == 1) {
|
|
|
|
result.truncate(result.length() - 1);
|
|
|
|
}
|
2001-12-03 20:19:08 +00:00
|
|
|
ICU_Utility::escapeUnprintable(result, c);
|
2001-07-18 18:38:00 +00:00
|
|
|
backslashCount = 0;
|
|
|
|
} else {
|
|
|
|
result.append(c);
|
|
|
|
if (c == BACKSLASH) {
|
|
|
|
++backslashCount;
|
|
|
|
} else {
|
|
|
|
backslashCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
|
2001-06-12 18:07:15 +00:00
|
|
|
return _generatePattern(result, escapeUnprintable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate and append a string representation of this set to result.
|
|
|
|
* This does not use this.pat, the cleaned up copy of the string
|
|
|
|
* passed to applyPattern().
|
|
|
|
*/
|
|
|
|
UnicodeString& UnicodeSet::_generatePattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const {
|
|
|
|
result.append(SET_OPEN);
|
|
|
|
|
2001-10-17 19:21:12 +00:00
|
|
|
// // Check against the predefined categories. We implicitly build
|
|
|
|
// // up ALL category sets the first time toPattern() is called.
|
|
|
|
// for (int8_t cat=0; cat<Unicode::GENERAL_TYPES_COUNT; ++cat) {
|
|
|
|
// if (*this == getCategorySet(cat)) {
|
|
|
|
// result.append(COLON);
|
|
|
|
// result.append(CATEGORY_NAMES, cat*2, 2);
|
|
|
|
// return result.append(CATEGORY_CLOSE);
|
|
|
|
// }
|
|
|
|
// }
|
2001-06-12 18:07:15 +00:00
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
int32_t count = getRangeCount();
|
2001-06-12 18:07:15 +00:00
|
|
|
|
|
|
|
// If the set contains at least 2 intervals and includes both
|
|
|
|
// MIN_VALUE and MAX_VALUE, then the inverse representation will
|
|
|
|
// be more economical.
|
|
|
|
if (count > 1 &&
|
|
|
|
getRangeStart(0) == MIN_VALUE &&
|
|
|
|
getRangeEnd(count-1) == MAX_VALUE) {
|
|
|
|
|
|
|
|
// Emit the inverse
|
|
|
|
result.append(COMPLEMENT);
|
|
|
|
|
|
|
|
for (int32_t i = 1; i < count; ++i) {
|
|
|
|
UChar32 start = getRangeEnd(i-1)+1;
|
|
|
|
UChar32 end = getRangeStart(i)-1;
|
|
|
|
_appendToPat(result, start, escapeUnprintable);
|
|
|
|
if (start != end) {
|
|
|
|
result.append(HYPHEN);
|
|
|
|
_appendToPat(result, end, escapeUnprintable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default; emit the ranges as pairs
|
|
|
|
else {
|
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
|
|
|
UChar32 start = getRangeStart(i);
|
|
|
|
UChar32 end = getRangeEnd(i);
|
|
|
|
_appendToPat(result, start, escapeUnprintable);
|
|
|
|
if (start != end) {
|
|
|
|
result.append(HYPHEN);
|
|
|
|
_appendToPat(result, end, escapeUnprintable);
|
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
for (int32_t i = 0; i<strings->size(); ++i) {
|
|
|
|
result.append(OPEN_BRACE);
|
|
|
|
_appendToPat(result,
|
|
|
|
*(const UnicodeString*) strings->elementAt(i),
|
|
|
|
escapeUnprintable);
|
|
|
|
result.append(CLOSE_BRACE);
|
|
|
|
}
|
2000-03-04 02:21:02 +00:00
|
|
|
return result.append(SET_CLOSE);
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
1999-10-26 17:15:44 +00:00
|
|
|
* Returns the number of elements in this set (its cardinality),
|
|
|
|
* <em>n</em>, where <code>0 <= </code><em>n</em><code> <= 65536</code>.
|
1999-10-20 22:08:09 +00:00
|
|
|
*
|
|
|
|
* @return the number of elements in this set (its cardinality).
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t UnicodeSet::size(void) const {
|
1999-10-20 22:08:09 +00:00
|
|
|
int32_t n = 0;
|
2000-05-25 19:27:14 +00:00
|
|
|
int32_t count = getRangeCount();
|
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
|
|
|
n += getRangeEnd(i) - getRangeStart(i) + 1;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return n + strings->size();
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns <tt>true</tt> if this set contains no elements.
|
|
|
|
*
|
|
|
|
* @return <tt>true</tt> if this set contains no elements.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool UnicodeSet::isEmpty(void) const {
|
2002-04-25 23:39:57 +00:00
|
|
|
return len == 1 && strings->size() == 0;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-04-25 23:39:57 +00:00
|
|
|
* Returns true if this set contains the given character.
|
|
|
|
* @param c character to be checked for containment
|
|
|
|
* @return true if the test condition is met
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::contains(UChar32 c) const {
|
|
|
|
// Set i to the index of the start item greater than ch
|
|
|
|
// We know we will terminate without length test!
|
|
|
|
// LATER: for large sets, add binary search
|
|
|
|
int32_t i = -1;
|
|
|
|
for (;;) {
|
|
|
|
if (c < list[++i]) break;
|
|
|
|
}
|
|
|
|
return ((i & 1) != 0); // return true if odd
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this set contains every character
|
|
|
|
* of the given range.
|
|
|
|
* @param start first character, inclusive, of the range
|
|
|
|
* @param end last character, inclusive, of the range
|
|
|
|
* @return true if the test condition is met
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
UBool UnicodeSet::contains(UChar32 start, UChar32 end) const {
|
|
|
|
int32_t i = -1;
|
|
|
|
for (;;) {
|
|
|
|
if (start < list[++i]) break;
|
|
|
|
}
|
|
|
|
return ((i & 1) != 0 && end < list[i]);
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-04-25 23:39:57 +00:00
|
|
|
* Returns <tt>true</tt> if this set contains the given
|
|
|
|
* multicharacter string.
|
|
|
|
* @param s string to be checked for containment
|
|
|
|
* @return <tt>true</tt> if this set contains the specified string
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UBool UnicodeSet::contains(const UnicodeString& s) const {
|
|
|
|
if (s.length() == 0) return FALSE;
|
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
|
|
|
return strings->contains((void*) &s);
|
|
|
|
} else {
|
|
|
|
return contains((UChar32) cp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this set contains all the characters and strings
|
|
|
|
* of the given set.
|
|
|
|
* @param c set to be checked for containment
|
|
|
|
* @return true if the test condition is met
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::containsAll(const UnicodeSet& c) const {
|
|
|
|
// The specified set is a subset if all of its pairs are contained in
|
|
|
|
// this set. It's possible to code this more efficiently in terms of
|
|
|
|
// direct manipulation of the inversion lists if the need arises.
|
|
|
|
int32_t n = c.getRangeCount();
|
|
|
|
for (int i=0; i<n; ++i) {
|
|
|
|
if (!contains(c.getRangeStart(i), c.getRangeEnd(i))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strings->containsAll(*c.strings)) return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this set contains all the characters
|
|
|
|
* of the given string.
|
|
|
|
* @param s string containing characters to be checked for containment
|
|
|
|
* @return true if the test condition is met
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::containsAll(const UnicodeString& s) const {
|
|
|
|
UChar32 cp;
|
|
|
|
for (int32_t i = 0; i < s.length(); i += UTF_CHAR_LENGTH(cp)) {
|
|
|
|
cp = s.char32At(i);
|
|
|
|
if (!contains(cp)) return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this set contains none of the characters
|
|
|
|
* of the given range.
|
|
|
|
* @param start first character, inclusive, of the range
|
|
|
|
* @param end last character, inclusive, of the range
|
|
|
|
* @return true if the test condition is met
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::containsNone(UChar32 start, UChar32 end) const {
|
2000-05-25 19:27:14 +00:00
|
|
|
int32_t i = -1;
|
|
|
|
for (;;) {
|
2002-04-25 23:39:57 +00:00
|
|
|
if (start < list[++i]) break;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return ((i & 1) == 0 && end < list[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this set contains none of the characters and strings
|
|
|
|
* of the given set.
|
|
|
|
* @param c set to be checked for containment
|
|
|
|
* @return true if the test condition is met
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::containsNone(const UnicodeSet& c) const {
|
|
|
|
// The specified set is a subset if all of its pairs are contained in
|
|
|
|
// this set. It's possible to code this more efficiently in terms of
|
|
|
|
// direct manipulation of the inversion lists if the need arises.
|
|
|
|
int32_t n = c.getRangeCount();
|
|
|
|
for (int32_t i=0; i<n; ++i) {
|
|
|
|
if (!containsNone(c.getRangeStart(i), c.getRangeEnd(i))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strings->containsNone(*c.strings)) return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this set contains none of the characters
|
|
|
|
* of the given string.
|
|
|
|
* @param s string containing characters to be checked for containment
|
|
|
|
* @return true if the test condition is met
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::containsNone(const UnicodeString& s) const {
|
|
|
|
UChar32 cp;
|
|
|
|
for (int32_t i = 0; i < s.length(); i += UTF_CHAR_LENGTH(cp)) {
|
|
|
|
cp = s.char32At(i);
|
|
|
|
if (contains(cp)) return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
2000-01-13 07:28:08 +00:00
|
|
|
/**
|
|
|
|
* Returns <tt>true</tt> if this set contains any character whose low byte
|
|
|
|
* is the given value. This is used by <tt>RuleBasedTransliterator</tt> for
|
|
|
|
* indexing.
|
|
|
|
*/
|
2001-07-27 00:18:53 +00:00
|
|
|
UBool UnicodeSet::matchesIndexValue(uint8_t v) const {
|
2000-01-13 07:28:08 +00:00
|
|
|
/* The index value v, in the range [0,255], is contained in this set if
|
|
|
|
* it is contained in any pair of this set. Pairs either have the high
|
|
|
|
* bytes equal, or unequal. If the high bytes are equal, then we have
|
|
|
|
* aaxx..aayy, where aa is the high byte. Then v is contained if xx <=
|
|
|
|
* v <= yy. If the high bytes are unequal we have aaxx..bbyy, bb>aa.
|
|
|
|
* Then v is contained if xx <= v || v <= yy. (This is identical to the
|
|
|
|
* time zone month containment logic.)
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<getRangeCount(); ++i) {
|
2000-05-25 19:27:14 +00:00
|
|
|
UChar32 low = getRangeStart(i);
|
|
|
|
UChar32 high = getRangeEnd(i);
|
|
|
|
if ((low & ~0xFF) == (high & ~0xFF)) {
|
|
|
|
if ((low & 0xFF) <= v && v <= (high & 0xFF)) {
|
2000-01-13 07:28:08 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
} else if ((low & 0xFF) <= v || v <= (high & 0xFF)) {
|
2000-01-13 07:28:08 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
if (strings->size() != 0) {
|
|
|
|
for (i=0; i<strings->size(); ++i) {
|
|
|
|
const UnicodeString& s = *(const UnicodeString*)strings->elementAt(i);
|
|
|
|
//if (s.length() == 0) {
|
|
|
|
// // Empty strings match everything
|
|
|
|
// return TRUE;
|
|
|
|
//}
|
|
|
|
// assert(s.length() != 0); // We enforce this elsewhere
|
|
|
|
UChar32 c = s.char32At(0);
|
|
|
|
if ((c & 0xFF) == v) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-01-13 07:28:08 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
/**
|
2002-04-25 23:39:57 +00:00
|
|
|
* Implementation of UnicodeMatcher::matches(). Always matches the
|
|
|
|
* longest possible multichar string.
|
2001-07-25 19:11:02 +00:00
|
|
|
*/
|
|
|
|
UMatchDegree UnicodeSet::matches(const Replaceable& text,
|
|
|
|
int32_t& offset,
|
|
|
|
int32_t limit,
|
2001-10-30 23:55:09 +00:00
|
|
|
UBool incremental) {
|
2001-07-25 19:11:02 +00:00
|
|
|
if (offset == limit) {
|
2002-04-25 23:39:57 +00:00
|
|
|
// Strings, if any, have length != 0, so we don't worry
|
|
|
|
// about them here. If we ever allow zero-length strings
|
|
|
|
// we much check for them here.
|
2002-05-29 18:36:09 +00:00
|
|
|
if (contains(U_ETHER)) {
|
2001-07-25 19:11:02 +00:00
|
|
|
return incremental ? U_PARTIAL_MATCH : U_MATCH;
|
|
|
|
} else {
|
|
|
|
return U_MISMATCH;
|
|
|
|
}
|
|
|
|
} else {
|
2002-04-25 23:39:57 +00:00
|
|
|
if (strings->size() != 0) { // try strings first
|
|
|
|
|
|
|
|
// might separate forward and backward loops later
|
|
|
|
// for now they are combined
|
|
|
|
|
|
|
|
// TODO Improve efficiency of this, at least in the forward
|
|
|
|
// direction, if not in both. In the forward direction we
|
|
|
|
// can assume the strings are sorted.
|
|
|
|
|
|
|
|
int32_t i;
|
|
|
|
UBool forward = offset < limit;
|
|
|
|
|
|
|
|
// firstChar is the leftmost char to match in the
|
|
|
|
// forward direction or the rightmost char to match in
|
|
|
|
// the reverse direction.
|
|
|
|
UChar firstChar = text.charAt(offset);
|
|
|
|
|
|
|
|
// If there are multiple strings that can match we
|
|
|
|
// return the longest match.
|
|
|
|
int32_t highWaterLength = 0;
|
|
|
|
|
|
|
|
for (i=0; i<strings->size(); ++i) {
|
|
|
|
const UnicodeString& trial = *(const UnicodeString*)strings->elementAt(i);
|
|
|
|
|
|
|
|
//if (trial.length() == 0) {
|
|
|
|
// return U_MATCH; // null-string always matches
|
|
|
|
//}
|
|
|
|
// assert(trial.length() != 0); // We ensure this elsewhere
|
|
|
|
|
|
|
|
UChar c = trial.charAt(forward ? 0 : trial.length() - 1);
|
|
|
|
|
|
|
|
// Strings are sorted, so we can optimize in the
|
|
|
|
// forward direction.
|
|
|
|
if (forward && c > firstChar) break;
|
|
|
|
if (c != firstChar) continue;
|
|
|
|
|
2002-05-14 21:21:03 +00:00
|
|
|
int32_t matchLen = matchRest(text, offset, limit, trial);
|
2002-04-25 23:39:57 +00:00
|
|
|
|
|
|
|
if (incremental) {
|
|
|
|
int32_t maxLen = forward ? limit-offset : offset-limit;
|
2002-05-14 21:21:03 +00:00
|
|
|
if (matchLen == maxLen) {
|
2002-04-25 23:39:57 +00:00
|
|
|
// We have successfully matched but only up to limit.
|
|
|
|
return U_PARTIAL_MATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-14 21:21:03 +00:00
|
|
|
if (matchLen == trial.length()) {
|
2002-04-25 23:39:57 +00:00
|
|
|
// We have successfully matched the whole string.
|
2002-05-14 21:21:03 +00:00
|
|
|
if (matchLen > highWaterLength) {
|
|
|
|
highWaterLength = matchLen;
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
// In the forward direction we know strings
|
|
|
|
// are sorted so we can bail early.
|
2002-05-14 21:21:03 +00:00
|
|
|
if (forward && matchLen < highWaterLength) {
|
2002-04-25 23:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've checked all strings without a partial match.
|
|
|
|
// If we have full matches, return the longest one.
|
|
|
|
if (highWaterLength != 0) {
|
|
|
|
offset += forward ? highWaterLength : -highWaterLength;
|
|
|
|
return U_MATCH;
|
|
|
|
}
|
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
return UnicodeFilter::matches(text, offset, limit, incremental);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the longest match for s in text at the given position.
|
|
|
|
* If limit > start then match forward from start+1 to limit
|
|
|
|
* matching all characters except s.charAt(0). If limit < start,
|
|
|
|
* go backward starting from start-1 matching all characters
|
|
|
|
* except s.charAt(s.length()-1). This method assumes that the
|
|
|
|
* first character, text.charAt(start), matches s, so it does not
|
|
|
|
* check it.
|
|
|
|
* @param text the text to match
|
|
|
|
* @param start the first character to match. In the forward
|
|
|
|
* direction, text.charAt(start) is matched against s.charAt(0).
|
|
|
|
* In the reverse direction, it is matched against
|
|
|
|
* s.charAt(s.length()-1).
|
|
|
|
* @param limit the limit offset for matching, either last+1 in
|
|
|
|
* the forward direction, or last-1 in the reverse direction,
|
|
|
|
* where last is the index of the last character to match.
|
|
|
|
* @return If part of s matches up to the limit, return |limit -
|
|
|
|
* start|. If all of s matches before reaching the limit, return
|
|
|
|
* s.length(). If there is a mismatch between s and text, return
|
|
|
|
* 0
|
|
|
|
*/
|
|
|
|
int32_t UnicodeSet::matchRest(const Replaceable& text,
|
|
|
|
int32_t start, int32_t limit,
|
|
|
|
const UnicodeString& s) {
|
|
|
|
int32_t i;
|
|
|
|
int32_t maxLen;
|
|
|
|
int32_t slen = s.length();
|
|
|
|
if (start < limit) {
|
|
|
|
maxLen = limit - start;
|
|
|
|
if (maxLen > slen) maxLen = slen;
|
|
|
|
for (i = 1; i < maxLen; ++i) {
|
|
|
|
if (text.charAt(start + i) != s.charAt(i)) return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
maxLen = start - limit;
|
|
|
|
if (maxLen > slen) maxLen = slen;
|
|
|
|
--slen; // <=> slen = s.length() - 1;
|
|
|
|
for (i = 1; i < maxLen; ++i) {
|
|
|
|
if (text.charAt(start - i) != s.charAt(slen - i)) return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxLen;
|
|
|
|
}
|
|
|
|
|
2001-12-11 01:13:33 +00:00
|
|
|
/**
|
|
|
|
* Returns the index of the given character within this set, where
|
|
|
|
* the set is ordered by ascending code point. If the character
|
|
|
|
* is not in this set, return -1. The inverse of this method is
|
|
|
|
* <code>charAt()</code>.
|
|
|
|
* @return an index from 0..size()-1, or -1
|
|
|
|
*/
|
|
|
|
int32_t UnicodeSet::indexOf(UChar32 c) const {
|
|
|
|
if (c < MIN_VALUE || c > MAX_VALUE) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int32_t i = 0;
|
|
|
|
int32_t n = 0;
|
|
|
|
for (;;) {
|
|
|
|
UChar32 start = list[i++];
|
|
|
|
if (c < start) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
UChar32 limit = list[i++];
|
|
|
|
if (c < limit) {
|
|
|
|
return n + c - start;
|
|
|
|
}
|
|
|
|
n += limit - start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the character at the given index within this set, where
|
|
|
|
* the set is ordered by ascending code point. If the index is
|
|
|
|
* out of range, return (UChar32)-1. The inverse of this method is
|
|
|
|
* <code>indexOf()</code>.
|
|
|
|
* @param index an index from 0..size()-1
|
|
|
|
* @return the character at the given index, or (UChar32)-1.
|
|
|
|
*/
|
|
|
|
UChar32 UnicodeSet::charAt(int32_t index) const {
|
|
|
|
if (index >= 0) {
|
2001-12-13 22:57:08 +00:00
|
|
|
// len2 is the largest even integer <= len, that is, it is len
|
|
|
|
// for even values and len-1 for odd values. With odd values
|
|
|
|
// the last entry is UNICODESET_HIGH.
|
|
|
|
int32_t len2 = len & ~1;
|
|
|
|
for (int32_t i=0; i < len2;) {
|
2001-12-11 01:13:33 +00:00
|
|
|
UChar32 start = list[i++];
|
|
|
|
int32_t count = list[i++] - start;
|
|
|
|
if (index < count) {
|
|
|
|
return (UChar32)(start + index);
|
|
|
|
}
|
|
|
|
index -= count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (UChar32)-1;
|
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
|
|
|
* Adds the specified range to this set if it is not already
|
|
|
|
* present. If this set already contains the specified range,
|
2000-05-25 19:27:14 +00:00
|
|
|
* the call leaves this set unchanged. If <code>end > start</code>
|
1999-10-20 22:08:09 +00:00
|
|
|
* then an empty range is added, leaving the set unchanged.
|
|
|
|
*
|
2000-05-25 19:27:14 +00:00
|
|
|
* @param start first character, inclusive, of range to be added
|
1999-10-20 22:08:09 +00:00
|
|
|
* to this set.
|
2000-05-25 19:27:14 +00:00
|
|
|
* @param end last character, inclusive, of range to be added
|
1999-10-20 22:08:09 +00:00
|
|
|
* to this set.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::add(UChar32 start, UChar32 end) {
|
2000-05-25 19:27:14 +00:00
|
|
|
if (start <= end) {
|
2001-06-21 18:27:52 +00:00
|
|
|
UChar32 range[3] = { start, end+1, UNICODESET_HIGH };
|
2000-05-25 19:27:14 +00:00
|
|
|
add(range, 2, 0);
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the specified character to this set if it is not already
|
|
|
|
* present. If this set already contains the specified character,
|
|
|
|
* the call leaves this set unchanged.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::add(UChar32 c) {
|
|
|
|
return add(c, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the specified multicharacter to this set if it is not already
|
|
|
|
* present. If this set already contains the multicharacter,
|
|
|
|
* the call leaves this set unchanged.
|
|
|
|
* Thus "ch" => {"ch"}
|
|
|
|
* <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
|
|
|
|
* @param s the source string
|
|
|
|
* @return the modified set, for chaining
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::add(const UnicodeString& s) {
|
|
|
|
if (s.length() == 0) return *this;
|
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
|
|
|
_add(s);
|
|
|
|
pat.truncate(0);
|
|
|
|
} else {
|
|
|
|
add((UChar32)cp, (UChar32)cp);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the given string, in order, to 'strings'. The given string
|
|
|
|
* must have been checked by the caller to not be empty and to not
|
|
|
|
* already be in 'strings'.
|
|
|
|
*/
|
|
|
|
void UnicodeSet::_add(const UnicodeString& s) {
|
|
|
|
UnicodeString* t = new UnicodeString(s);
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
strings->sortedInsert(t, compareUnicodeString, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a code point IF the string consists of a single one.
|
|
|
|
* otherwise returns -1.
|
|
|
|
* @param string to test
|
|
|
|
*/
|
|
|
|
int32_t UnicodeSet::getSingleCP(const UnicodeString& s) {
|
|
|
|
//if (s.length() < 1) {
|
|
|
|
// throw new IllegalArgumentException("Can't use zero-length strings in UnicodeSet");
|
|
|
|
//}
|
|
|
|
if (s.length() > 2) return -1;
|
|
|
|
if (s.length() == 1) return s.charAt(0);
|
|
|
|
|
|
|
|
// at this point, len = 2
|
|
|
|
UChar32 cp = s.char32At(0);
|
|
|
|
if (cp > 0xFFFF) { // is surrogate pair
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}
|
|
|
|
* If this set already any particular character, it has no effect on that character.
|
|
|
|
* @param the source string
|
|
|
|
* @return the modified set, for chaining
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::addAll(const UnicodeString& s) {
|
|
|
|
UChar32 cp;
|
|
|
|
for (int32_t i = 0; i < s.length(); i += UTF_CHAR_LENGTH(cp)) {
|
|
|
|
cp = s.char32At(i);
|
|
|
|
add(cp, cp);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}
|
|
|
|
* If this set already any particular character, it has no effect on that character.
|
|
|
|
* @param the source string
|
|
|
|
* @return the modified set, for chaining
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::retainAll(const UnicodeString& s) {
|
|
|
|
UnicodeSet set;
|
|
|
|
set.addAll(s);
|
|
|
|
retainAll(set);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}
|
|
|
|
* If this set already any particular character, it has no effect on that character.
|
|
|
|
* @param the source string
|
|
|
|
* @return the modified set, for chaining
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::complementAll(const UnicodeString& s) {
|
|
|
|
UnicodeSet set;
|
|
|
|
set.addAll(s);
|
|
|
|
complementAll(set);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}
|
|
|
|
* If this set already any particular character, it has no effect on that character.
|
|
|
|
* @param the source string
|
|
|
|
* @return the modified set, for chaining
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::removeAll(const UnicodeString& s) {
|
|
|
|
UnicodeSet set;
|
|
|
|
set.addAll(s);
|
|
|
|
removeAll(set);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a set from a multicharacter string. Thus "ch" => {"ch"}
|
|
|
|
* <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
|
|
|
|
* @param the source string
|
|
|
|
* @return a newly created set containing the given string
|
|
|
|
*/
|
|
|
|
UnicodeSet* UnicodeSet::createFrom(const UnicodeString& s) {
|
|
|
|
UnicodeSet *set = new UnicodeSet();
|
|
|
|
set->add(s);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}
|
|
|
|
* @param the source string
|
|
|
|
* @return a newly created set containing the given characters
|
|
|
|
*/
|
|
|
|
UnicodeSet* UnicodeSet::createFromAll(const UnicodeString& s) {
|
|
|
|
UnicodeSet *set = new UnicodeSet();
|
|
|
|
set->addAll(s);
|
|
|
|
return set;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
/**
|
|
|
|
* Retain only the elements in this set that are contained in the
|
|
|
|
* specified range. If <code>end > start</code> then an empty range is
|
|
|
|
* retained, leaving the set empty.
|
|
|
|
*
|
|
|
|
* @param start first character, inclusive, of range to be retained
|
|
|
|
* to this set.
|
|
|
|
* @param end last character, inclusive, of range to be retained
|
|
|
|
* to this set.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::retain(UChar32 start, UChar32 end) {
|
2000-05-25 19:27:14 +00:00
|
|
|
if (start <= end) {
|
2001-06-21 18:27:52 +00:00
|
|
|
UChar32 range[3] = { start, end+1, UNICODESET_HIGH };
|
2000-05-25 19:27:14 +00:00
|
|
|
retain(range, 2, 0);
|
|
|
|
} else {
|
|
|
|
clear();
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::retain(UChar32 c) {
|
|
|
|
return retain(c, c);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
|
|
|
* Removes the specified range from this set if it is present.
|
|
|
|
* The set will not contain the specified range once the call
|
2000-05-25 19:27:14 +00:00
|
|
|
* returns. If <code>end > start</code> then an empty range is
|
1999-10-20 22:08:09 +00:00
|
|
|
* removed, leaving the set unchanged.
|
|
|
|
*
|
2000-05-25 19:27:14 +00:00
|
|
|
* @param start first character, inclusive, of range to be removed
|
1999-10-20 22:08:09 +00:00
|
|
|
* from this set.
|
2000-05-25 19:27:14 +00:00
|
|
|
* @param end last character, inclusive, of range to be removed
|
1999-10-20 22:08:09 +00:00
|
|
|
* from this set.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::remove(UChar32 start, UChar32 end) {
|
2000-05-25 19:27:14 +00:00
|
|
|
if (start <= end) {
|
2001-06-21 18:27:52 +00:00
|
|
|
UChar32 range[3] = { start, end+1, UNICODESET_HIGH };
|
2000-05-25 19:27:14 +00:00
|
|
|
retain(range, 2, 2);
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the specified character from this set if it is present.
|
|
|
|
* The set will not contain the specified range once the call
|
|
|
|
* returns.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::remove(UChar32 c) {
|
|
|
|
return remove(c, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the specified string from this set if it is present.
|
|
|
|
* The set will not contain the specified character once the call
|
|
|
|
* returns.
|
|
|
|
* @param the source string
|
|
|
|
* @return the modified set, for chaining
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::remove(const UnicodeString& s) {
|
|
|
|
if (s.length() == 0) return *this;
|
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
|
|
|
strings->removeElement((void*) &s);
|
|
|
|
pat.truncate(0);
|
|
|
|
} else {
|
|
|
|
remove((UChar32)cp, (UChar32)cp);
|
|
|
|
}
|
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
/**
|
|
|
|
* Complements the specified range in this set. Any character in
|
|
|
|
* the range will be removed if it is in this set, or will be
|
|
|
|
* added if it is not in this set. If <code>end > start</code>
|
|
|
|
* then an empty range is xor'ed, leaving the set unchanged.
|
|
|
|
*
|
|
|
|
* @param start first character, inclusive, of range to be removed
|
|
|
|
* from this set.
|
|
|
|
* @param end last character, inclusive, of range to be removed
|
|
|
|
* from this set.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::complement(UChar32 start, UChar32 end) {
|
2000-05-25 19:27:14 +00:00
|
|
|
if (start <= end) {
|
2001-06-21 18:27:52 +00:00
|
|
|
UChar32 range[3] = { start, end+1, UNICODESET_HIGH };
|
2000-05-26 01:18:36 +00:00
|
|
|
exclusiveOr(range, 2, 0);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
pat.truncate(0);
|
|
|
|
return *this;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::complement(UChar32 c) {
|
|
|
|
return complement(c, c);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
2002-04-25 23:39:57 +00:00
|
|
|
* This is equivalent to
|
|
|
|
* <code>complement(MIN_VALUE, MAX_VALUE)</code>.
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::complement(void) {
|
|
|
|
if (list[0] == UNICODESET_LOW) {
|
|
|
|
ensureBufferCapacity(len-1);
|
|
|
|
uprv_memcpy(buffer, list + 1, (len-1)*sizeof(UChar32));
|
|
|
|
--len;
|
|
|
|
} else {
|
|
|
|
ensureBufferCapacity(len+1);
|
|
|
|
uprv_memcpy(buffer + 1, list, len*sizeof(UChar32));
|
|
|
|
buffer[0] = UNICODESET_LOW;
|
|
|
|
++len;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
swapBuffers();
|
|
|
|
pat.truncate(0);
|
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2001-12-03 20:50:50 +00:00
|
|
|
/**
|
2002-04-25 23:39:57 +00:00
|
|
|
* Complement the specified string in this set.
|
|
|
|
* The set will not contain the specified string once the call
|
|
|
|
* returns.
|
|
|
|
* <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
|
|
|
|
* @param s the string to complement
|
|
|
|
* @return this object, for chaining
|
2001-12-03 20:50:50 +00:00
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::complement(const UnicodeString& s) {
|
|
|
|
if (s.length() == 0) return *this;
|
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
|
|
|
if (strings->contains((void*) &s)) {
|
|
|
|
strings->removeElement((void*) &s);
|
|
|
|
} else {
|
|
|
|
_add(s);
|
|
|
|
}
|
|
|
|
pat.truncate(0);
|
|
|
|
} else {
|
|
|
|
complement((UChar32)cp, (UChar32)cp);
|
2001-12-03 20:50:50 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2001-12-03 20:50:50 +00:00
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
|
|
|
* Adds all of the elements in the specified set to this set if
|
|
|
|
* they're not already present. This operation effectively
|
|
|
|
* modifies this set so that its value is the <i>union</i> of the two
|
|
|
|
* sets. The behavior of this operation is unspecified if the specified
|
|
|
|
* collection is modified while the operation is in progress.
|
|
|
|
*
|
|
|
|
* @param c set whose elements are to be added to this set.
|
|
|
|
* @see #add(char, char)
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::addAll(const UnicodeSet& c) {
|
2000-05-25 19:27:14 +00:00
|
|
|
add(c.list, c.len, 0);
|
2002-04-25 23:39:57 +00:00
|
|
|
|
|
|
|
// Add strings in order
|
|
|
|
for (int32_t i=0; i<c.strings->size(); ++i) {
|
|
|
|
const UnicodeString* s = (const UnicodeString*)c.strings->elementAt(i);
|
|
|
|
if (!strings->contains((void*) s)) {
|
|
|
|
_add(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retains only the elements in this set that are contained in the
|
|
|
|
* specified set. In other words, removes from this set all of
|
|
|
|
* its elements that are not contained in the specified set. This
|
|
|
|
* operation effectively modifies this set so that its value is
|
|
|
|
* the <i>intersection</i> of the two sets.
|
|
|
|
*
|
|
|
|
* @param c set that defines which elements this set will retain.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::retainAll(const UnicodeSet& c) {
|
2000-05-25 19:27:14 +00:00
|
|
|
retain(c.list, c.len, 0);
|
2002-04-25 23:39:57 +00:00
|
|
|
strings->retainAll(*c.strings);
|
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes from this set all of its elements that are contained in the
|
|
|
|
* specified set. This operation effectively modifies this
|
|
|
|
* set so that its value is the <i>asymmetric set difference</i> of
|
|
|
|
* the two sets.
|
|
|
|
*
|
|
|
|
* @param c set that defines which elements will be removed from
|
|
|
|
* this set.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::removeAll(const UnicodeSet& c) {
|
2000-05-25 19:27:14 +00:00
|
|
|
retain(c.list, c.len, 2);
|
2002-04-25 23:39:57 +00:00
|
|
|
strings->removeAll(*c.strings);
|
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-05-25 19:27:14 +00:00
|
|
|
* Complements in this set all elements contained in the specified
|
|
|
|
* set. Any character in the other set will be removed if it is
|
|
|
|
* in this set, or will be added if it is not in this set.
|
|
|
|
*
|
|
|
|
* @param c set that defines which elements will be xor'ed from
|
|
|
|
* this set.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::complementAll(const UnicodeSet& c) {
|
2000-05-26 01:18:36 +00:00
|
|
|
exclusiveOr(c.list, c.len, 0);
|
2000-05-25 19:27:14 +00:00
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
for (int32_t i=0; i<c.strings->size(); ++i) {
|
|
|
|
void* e = c.strings->elementAt(i);
|
|
|
|
if (!strings->removeElement(e)) {
|
|
|
|
_add(*(const UnicodeString*)e);
|
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all of the elements from this set. This set will be
|
|
|
|
* empty after this call returns.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::clear(void) {
|
2001-06-21 18:27:52 +00:00
|
|
|
list[0] = UNICODESET_HIGH;
|
2000-05-25 19:27:14 +00:00
|
|
|
len = 1;
|
2001-06-12 18:07:15 +00:00
|
|
|
pat.truncate(0);
|
2002-04-25 23:39:57 +00:00
|
|
|
strings->removeAllElements();
|
|
|
|
return *this;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iteration method that returns the number of ranges contained in
|
|
|
|
* this set.
|
|
|
|
* @see #getRangeStart
|
|
|
|
* @see #getRangeEnd
|
|
|
|
*/
|
|
|
|
int32_t UnicodeSet::getRangeCount() const {
|
|
|
|
return len/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iteration method that returns the first character in the
|
|
|
|
* specified range of this set.
|
|
|
|
* @see #getRangeCount
|
|
|
|
* @see #getRangeEnd
|
|
|
|
*/
|
|
|
|
UChar32 UnicodeSet::getRangeStart(int32_t index) const {
|
|
|
|
return list[index*2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iteration method that returns the last character in the
|
|
|
|
* specified range of this set.
|
|
|
|
* @see #getRangeStart
|
|
|
|
* @see #getRangeEnd
|
|
|
|
*/
|
|
|
|
UChar32 UnicodeSet::getRangeEnd(int32_t index) const {
|
|
|
|
return list[index*2 + 1] - 1;
|
|
|
|
}
|
|
|
|
|
2002-06-04 23:37:20 +00:00
|
|
|
int32_t UnicodeSet::getStringCount() const {
|
|
|
|
return strings->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString* UnicodeSet::getString(int32_t index) const {
|
|
|
|
return (const UnicodeString*) strings->elementAt(index);
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
/**
|
|
|
|
* Reallocate this objects internal structures to take up the least
|
|
|
|
* possible space, without changing this object's value.
|
|
|
|
*/
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet& UnicodeSet::compact() {
|
2000-05-25 19:27:14 +00:00
|
|
|
if (len != capacity) {
|
|
|
|
capacity = len;
|
2002-02-28 01:42:40 +00:00
|
|
|
UChar32* temp = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2000-05-25 19:27:14 +00:00
|
|
|
uprv_memcpy(temp, list, len*sizeof(UChar32));
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(list);
|
2000-05-25 19:27:14 +00:00
|
|
|
list = temp;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(buffer);
|
2000-05-25 19:27:14 +00:00
|
|
|
buffer = NULL;
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2002-05-29 18:36:09 +00:00
|
|
|
int32_t UnicodeSet::serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const {
|
|
|
|
int32_t bmpLength, length, destLength;
|
|
|
|
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destCapacity<0 || (destCapacity>0 && dest==NULL)) {
|
|
|
|
ec=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count necessary 16-bit units */
|
|
|
|
length=this->len-1; // Subtract 1 to ignore final UNICODESET_HIGH
|
|
|
|
// assert(length>=0);
|
|
|
|
if (length==0) {
|
|
|
|
/* empty set */
|
|
|
|
if (destCapacity>0) {
|
|
|
|
*dest=0;
|
|
|
|
} else {
|
|
|
|
ec=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* now length>0 */
|
|
|
|
|
|
|
|
if (this->list[length-1]<=0xffff) {
|
|
|
|
/* all BMP */
|
|
|
|
bmpLength=length;
|
|
|
|
} else if (this->list[0]>=0x10000) {
|
|
|
|
/* all supplementary */
|
|
|
|
bmpLength=0;
|
|
|
|
length*=2;
|
|
|
|
} else {
|
|
|
|
/* some BMP, some supplementary */
|
|
|
|
for (bmpLength=0; bmpLength<length && this->list[bmpLength]<=0xffff; ++bmpLength) {}
|
|
|
|
length=bmpLength+2*(length-bmpLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* length: number of 16-bit array units */
|
|
|
|
if (length>0x7fff) {
|
|
|
|
/* there are only 15 bits for the length in the first serialized word */
|
|
|
|
ec=U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* total serialized length:
|
|
|
|
* number of 16-bit array units (length) +
|
|
|
|
* 1 length unit (always) +
|
|
|
|
* 1 bmpLength unit (if there are supplementary values)
|
|
|
|
*/
|
|
|
|
destLength=length+((length>bmpLength)?2:1);
|
|
|
|
if (destLength<=destCapacity) {
|
|
|
|
const UChar32 *p;
|
|
|
|
int32_t i;
|
|
|
|
|
|
|
|
*dest=(uint16_t)length;
|
|
|
|
if (length>bmpLength) {
|
|
|
|
*dest|=0x8000;
|
|
|
|
*++dest=(uint16_t)bmpLength;
|
|
|
|
}
|
|
|
|
++dest;
|
|
|
|
|
|
|
|
/* write the BMP part of the array */
|
|
|
|
p=this->list;
|
|
|
|
for (i=0; i<bmpLength; ++i) {
|
|
|
|
*dest++=(uint16_t)*p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the supplementary part of the array */
|
|
|
|
for (; i<length; i+=2) {
|
|
|
|
*dest++=(uint16_t)(*p>>16);
|
|
|
|
*dest++=(uint16_t)*p++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ec=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
return destLength;
|
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Implementation: Pattern parsing
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the given pattern, starting at the given position. The
|
|
|
|
* character at pattern.charAt(pos.getIndex()) must be '[', or the
|
|
|
|
* parse fails. Parsing continues until the corresponding closing
|
|
|
|
* ']'. If a syntax error is encountered between the opening and
|
2001-06-12 18:07:15 +00:00
|
|
|
* closing brace, the parse fails. Upon return from a successful
|
1999-10-20 22:08:09 +00:00
|
|
|
* parse, the ParsePosition is updated to point to the character
|
|
|
|
* following the closing ']', and a StringBuffer containing a
|
|
|
|
* pairs list for the parsed pattern is returned. This method calls
|
|
|
|
* itself recursively to parse embedded subpatterns.
|
|
|
|
*
|
|
|
|
* @param pattern the string containing the pattern to be parsed.
|
|
|
|
* The portion of the string from pos.getIndex(), which must be a
|
|
|
|
* '[', to the corresponding closing ']', is parsed.
|
|
|
|
* @param pos upon entry, the position at which to being parsing.
|
|
|
|
* The character at pattern.charAt(pos.getIndex()) must be a '['.
|
|
|
|
* Upon return from a U_SUCCESSful parse, pos.getIndex() is either
|
|
|
|
* the character after the closing ']' of the parsed pattern, or
|
|
|
|
* pattern.length() if the closing ']' is the last character of
|
|
|
|
* the pattern string.
|
|
|
|
* @return a StringBuffer containing a pairs list for the parsed
|
|
|
|
* substring of <code>pattern</code>
|
|
|
|
* @exception IllegalArgumentException if the parse fails.
|
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
void UnicodeSet::applyPattern(const UnicodeString& pattern,
|
|
|
|
ParsePosition& pos,
|
|
|
|
const SymbolTable* symbols,
|
|
|
|
UErrorCode& status) {
|
1999-10-20 22:08:09 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2001-06-12 18:07:15 +00:00
|
|
|
// Need to build the pattern in a temporary string because
|
|
|
|
// _applyPattern calls add() etc., which set pat to empty.
|
|
|
|
UnicodeString rebuiltPat;
|
|
|
|
_applyPattern(pattern, pos, symbols, rebuiltPat, status);
|
|
|
|
pat = rebuiltPat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnicodeSet::_applyPattern(const UnicodeString& pattern,
|
|
|
|
ParsePosition& pos,
|
|
|
|
const SymbolTable* symbols,
|
|
|
|
UnicodeString& rebuiltPat,
|
|
|
|
UErrorCode& status) {
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2001-06-12 18:07:15 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the pattern contains any of the following, we save a
|
|
|
|
// rebuilt (variable-substituted) copy of the source pattern:
|
|
|
|
// - a category
|
|
|
|
// - an intersection or subtraction operator
|
|
|
|
// - an anchor (trailing '$', indicating RBT ether)
|
|
|
|
UBool rebuildPattern = FALSE;
|
2001-07-18 18:38:00 +00:00
|
|
|
UnicodeString newPat(SET_OPEN);
|
2001-11-20 03:27:44 +00:00
|
|
|
int32_t nestedPatStart = - 1; // see below for usage
|
|
|
|
UBool nestedPatDone = FALSE; // see below for usage
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeString multiCharBuffer;
|
2001-06-12 18:07:15 +00:00
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool invert = FALSE;
|
2000-05-25 19:27:14 +00:00
|
|
|
clear();
|
1999-10-20 22:08:09 +00:00
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
const UChar32 NONE = (UChar32) -1;
|
|
|
|
UChar32 lastChar = NONE; // This is either a char (0..10FFFF) or NONE
|
2001-12-01 01:33:41 +00:00
|
|
|
UBool isLastLiteral = FALSE; // TRUE if lastChar was a literal
|
2000-01-12 18:03:53 +00:00
|
|
|
UChar lastOp = 0;
|
|
|
|
|
|
|
|
/* This loop iterates over the characters in the pattern. We start at
|
|
|
|
* the position specified by pos. We exit the loop when either a
|
|
|
|
* matching closing ']' is seen, or we read all characters of the
|
|
|
|
* pattern. In the latter case an error will be thrown.
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-01-12 18:03:53 +00:00
|
|
|
|
|
|
|
/* Pattern syntax:
|
|
|
|
* pat := '[' '^'? elem* ']'
|
|
|
|
* elem := a | a '-' a | set | set op set
|
|
|
|
* set := pat | (a set variable)
|
|
|
|
* op := '&' | '-'
|
|
|
|
* a := (a character, possibly defined by a var)
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-01-12 18:03:53 +00:00
|
|
|
|
|
|
|
// mode 0: No chars parsed yet; next must be '['
|
|
|
|
// mode 1: '[' seen; if next is '^' or ':' then special
|
|
|
|
// mode 2: '[' '^'? seen; parse pattern and close with ']'
|
|
|
|
// mode 3: '[:' seen; parse category and close with ':]'
|
2001-10-17 19:21:12 +00:00
|
|
|
// mode 4: ']' seen; parse complete
|
|
|
|
// mode 5: Top-level property pattern seen
|
2000-01-12 18:03:53 +00:00
|
|
|
int8_t mode = 0;
|
|
|
|
int32_t i = pos.getIndex();
|
|
|
|
int32_t limit = pattern.length();
|
2000-05-25 19:27:14 +00:00
|
|
|
UnicodeSet nestedAux;
|
|
|
|
const UnicodeSet* nestedSet; // never owned
|
2000-01-12 18:03:53 +00:00
|
|
|
UnicodeString scratch;
|
2000-05-20 04:40:29 +00:00
|
|
|
/* In the case of an embedded SymbolTable variable, we look it up and
|
|
|
|
* then take characters from the resultant char[] array. These chars
|
|
|
|
* are subjected to an extra level of lookup in the SymbolTable in case
|
|
|
|
* they are stand-ins for a nested UnicodeSet. */
|
|
|
|
const UnicodeString* varValueBuffer = NULL;
|
|
|
|
int32_t ivarValueBuffer = 0;
|
2000-08-30 20:43:25 +00:00
|
|
|
int32_t anchor = 0;
|
2001-07-25 19:11:02 +00:00
|
|
|
UChar32 c;
|
|
|
|
while (i<limit) {
|
2000-01-12 18:03:53 +00:00
|
|
|
/* If the next element is a single character, c will be set to it,
|
2000-05-25 19:27:14 +00:00
|
|
|
* and nestedSet will be null. In this case isLiteral indicates
|
2000-01-12 18:03:53 +00:00
|
|
|
* whether the character should assume special meaning if it has
|
|
|
|
* one. If the next element is a nested set, either via a variable
|
|
|
|
* reference, or via an embedded "[..]" or "[:..:]" pattern, then
|
2000-05-25 19:27:14 +00:00
|
|
|
* nestedSet will be set to the pairs list for the nested set, and
|
2000-01-12 18:03:53 +00:00
|
|
|
* c's value should be ignored.
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
nestedSet = NULL;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isLiteral = FALSE;
|
2000-05-20 04:40:29 +00:00
|
|
|
if (varValueBuffer != NULL) {
|
|
|
|
if (ivarValueBuffer < varValueBuffer->length()) {
|
2001-07-25 19:11:02 +00:00
|
|
|
c = varValueBuffer->char32At(ivarValueBuffer);
|
|
|
|
ivarValueBuffer += UTF_CHAR_LENGTH(c);
|
2002-02-07 01:07:55 +00:00
|
|
|
const UnicodeFunctor *m = symbols->lookupMatcher(c); // may be NULL
|
2001-11-09 01:22:33 +00:00
|
|
|
if (m != NULL && m->getDynamicClassID() != UnicodeSet::getStaticClassID()) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nestedSet = (UnicodeSet*) m;
|
2001-07-18 19:52:28 +00:00
|
|
|
nestedPatDone = FALSE;
|
2000-05-20 04:40:29 +00:00
|
|
|
} else {
|
|
|
|
varValueBuffer = NULL;
|
2001-07-25 19:11:02 +00:00
|
|
|
c = pattern.char32At(i);
|
|
|
|
i += UTF_CHAR_LENGTH(c);
|
2000-05-20 04:40:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-07-25 19:11:02 +00:00
|
|
|
c = pattern.char32At(i);
|
|
|
|
i += UTF_CHAR_LENGTH(c);
|
2000-05-20 04:40:29 +00:00
|
|
|
}
|
2000-01-12 18:03:53 +00:00
|
|
|
|
|
|
|
// Ignore whitespace. This is not Unicode whitespace, but Java
|
|
|
|
// whitespace, a subset of Unicode whitespace.
|
2001-07-25 19:11:02 +00:00
|
|
|
if (u_isspace(c)) {
|
2000-01-12 18:03:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-08-30 20:43:25 +00:00
|
|
|
// Keep track of the count of characters after an alleged anchor
|
|
|
|
if (anchor > 0) {
|
|
|
|
++anchor;
|
|
|
|
}
|
|
|
|
|
2000-01-12 18:03:53 +00:00
|
|
|
// Parse the opening '[' and optional following '^'
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
2001-10-17 19:21:12 +00:00
|
|
|
if (UnicodePropertySet::resemblesPattern(pattern, i-1)) {
|
|
|
|
mode = 3;
|
|
|
|
break; // Fall through
|
|
|
|
} else if (c == SET_OPEN) {
|
2001-07-18 18:38:00 +00:00
|
|
|
mode = 1; // Next look for '^' or ':'
|
2000-01-12 18:03:53 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// throw new IllegalArgumentException("Missing opening '['");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
2000-01-12 18:03:53 +00:00
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
mode = 2;
|
|
|
|
switch (c) {
|
2000-03-04 02:21:02 +00:00
|
|
|
case COMPLEMENT:
|
2000-01-12 18:03:53 +00:00
|
|
|
invert = TRUE;
|
2001-07-18 18:38:00 +00:00
|
|
|
newPat.append(c);
|
2000-01-12 18:03:53 +00:00
|
|
|
continue; // Back to top to fetch next character
|
2000-03-04 02:21:02 +00:00
|
|
|
case HYPHEN:
|
2000-01-12 18:03:53 +00:00
|
|
|
isLiteral = TRUE; // Treat leading '-' as a literal
|
|
|
|
break; // Fall through
|
|
|
|
}
|
|
|
|
// else fall through and parse this character normally
|
|
|
|
}
|
|
|
|
|
|
|
|
// After opening matter is parsed ("[", "[^", or "[:"), the mode
|
|
|
|
// will be 2 if we want a closing ']', or 3 if we should parse a
|
|
|
|
// category and close with ":]".
|
|
|
|
|
2000-05-20 04:40:29 +00:00
|
|
|
// Only process escapes, variable references, and nested sets
|
|
|
|
// if we are _not_ retrieving characters from the variable
|
|
|
|
// buffer. Characters in the variable buffer have already
|
|
|
|
// benn through escape and variable reference processing.
|
|
|
|
if (varValueBuffer == NULL) {
|
2001-10-17 19:21:12 +00:00
|
|
|
/**
|
|
|
|
* Handle property set patterns.
|
|
|
|
*/
|
|
|
|
if (UnicodePropertySet::resemblesPattern(pattern, i-1)) {
|
|
|
|
ParsePosition pp(i-1);
|
2001-10-19 02:32:05 +00:00
|
|
|
UnicodeSet *s = UnicodePropertySet::createFromPattern(pattern, pp);
|
|
|
|
if (s == NULL) {
|
2001-10-17 19:21:12 +00:00
|
|
|
// assert(pp.getIndex() == i-1);
|
|
|
|
//throw new IllegalArgumentException("Invalid property pattern " +
|
|
|
|
// pattern.substring(i-1));
|
|
|
|
status = U_INVALID_PROPERTY_PATTERN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO This is very inefficient. We create a new UnicodeSet,
|
|
|
|
// then do an assignment, then delete it. Clean this up in
|
|
|
|
// the future so that either (1) we just use the new set
|
|
|
|
// directly, and delete it when we're done, or (2) even better,
|
|
|
|
// UnicodePropertySet takes an existing set.
|
2001-10-19 02:32:05 +00:00
|
|
|
nestedAux = *s;
|
|
|
|
delete s;
|
2001-10-17 19:21:12 +00:00
|
|
|
nestedSet = &nestedAux;
|
|
|
|
nestedPatStart = newPat.length();
|
|
|
|
nestedPatDone = TRUE; // we're going to do it just below
|
|
|
|
|
2001-12-01 21:46:53 +00:00
|
|
|
switch (lastOp) {
|
|
|
|
case HYPHEN:
|
|
|
|
case INTERSECTION:
|
|
|
|
newPat.append(lastOp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-10-17 19:21:12 +00:00
|
|
|
// If we have a top-level property pattern, then trim
|
|
|
|
// off the opening '[' and use the property pattern
|
|
|
|
// as the entire pattern.
|
|
|
|
if (mode == 3) {
|
|
|
|
newPat.truncate(0);
|
|
|
|
}
|
|
|
|
UnicodeString str;
|
|
|
|
pattern.extractBetween(i-1, pp.getIndex(), str);
|
|
|
|
newPat.append(str);
|
|
|
|
rebuildPattern = TRUE;
|
|
|
|
|
|
|
|
i = pp.getIndex(); // advance past property pattern
|
|
|
|
|
|
|
|
if (mode == 3) {
|
|
|
|
// Entire pattern is a category; leave parse
|
|
|
|
// loop. This is one of 2 ways we leave this
|
|
|
|
// loop if the pattern is well-formed.
|
|
|
|
*this = nestedAux;
|
|
|
|
mode = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-20 04:40:29 +00:00
|
|
|
/* Handle escapes. If a character is escaped, then it assumes its
|
|
|
|
* literal value. This is true for all characters, both special
|
|
|
|
* characters and characters with no special meaning. We also
|
|
|
|
* interpret '\\uxxxx' Unicode escapes here (as literals).
|
|
|
|
*/
|
2001-10-17 19:21:12 +00:00
|
|
|
else if (c == BACKSLASH) {
|
2000-07-16 13:42:38 +00:00
|
|
|
UChar32 escaped = pattern.unescapeAt(i);
|
|
|
|
if (escaped == (UChar32) -1) {
|
2000-05-20 04:40:29 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-07-16 13:42:38 +00:00
|
|
|
isLiteral = TRUE;
|
2001-07-25 19:11:02 +00:00
|
|
|
c = escaped;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-01-12 18:03:53 +00:00
|
|
|
|
2000-05-20 04:40:29 +00:00
|
|
|
/* Parse variable references. These are treated as literals. If a
|
|
|
|
* variable refers to a UnicodeSet, its stand in character is
|
|
|
|
* returned in the UChar[] buffer.
|
|
|
|
* Variable names are only parsed if varNameToChar is not null.
|
|
|
|
* Set variables are only looked up if varCharToSet is not null.
|
|
|
|
*/
|
|
|
|
else if (symbols != NULL && !isLiteral && c == SymbolTable::SYMBOL_REF) {
|
2001-07-25 19:11:02 +00:00
|
|
|
pos.setIndex(i);
|
2000-05-20 04:40:29 +00:00
|
|
|
UnicodeString name = symbols->parseReference(pattern, pos, limit);
|
2000-08-30 20:43:25 +00:00
|
|
|
if (name.length() != 0) {
|
|
|
|
varValueBuffer = symbols->lookup(name);
|
|
|
|
if (varValueBuffer == NULL) {
|
|
|
|
//throw new IllegalArgumentException("Undefined variable: "
|
|
|
|
// + name);
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ivarValueBuffer = 0;
|
|
|
|
i = pos.getIndex(); // Make i point PAST last char of var name
|
|
|
|
} else {
|
|
|
|
// Got a null; this means we have an isolated $.
|
|
|
|
// Tentatively assume this is an anchor.
|
|
|
|
anchor = 1;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-05-20 04:40:29 +00:00
|
|
|
continue; // Back to the top to get varValueBuffer[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An opening bracket indicates the first bracket of a nested
|
2001-10-17 19:21:12 +00:00
|
|
|
* subpattern.
|
2000-05-20 04:40:29 +00:00
|
|
|
*/
|
|
|
|
else if (!isLiteral && c == SET_OPEN) {
|
2001-07-18 19:52:28 +00:00
|
|
|
// Record position before nested pattern
|
|
|
|
nestedPatStart = newPat.length();
|
|
|
|
|
2001-10-17 19:21:12 +00:00
|
|
|
// Recurse to get the pairs for this nested set.
|
|
|
|
// Backup i to '['.
|
|
|
|
pos.setIndex(--i);
|
|
|
|
switch (lastOp) {
|
|
|
|
case HYPHEN:
|
|
|
|
case INTERSECTION:
|
|
|
|
newPat.append(lastOp);
|
|
|
|
break;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2001-10-17 19:21:12 +00:00
|
|
|
nestedAux._applyPattern(pattern, pos, symbols, newPat, status);
|
|
|
|
nestedSet = &nestedAux;
|
|
|
|
nestedPatDone = TRUE;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i = pos.getIndex();
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
|
|
|
|
else if (!isLiteral && c == OPEN_BRACE) {
|
|
|
|
// start of a string. find the rest.
|
|
|
|
int32_t length = 0;
|
|
|
|
int32_t st = i;
|
|
|
|
multiCharBuffer.truncate(0);
|
|
|
|
while (i < pattern.length()) {
|
|
|
|
UChar32 ch = pattern.char32At(i);
|
|
|
|
i += UTF_CHAR_LENGTH(ch);
|
|
|
|
if (ch == CLOSE_BRACE) {
|
|
|
|
length = -length; // signal that we saw '}'
|
|
|
|
break;
|
|
|
|
} else if (ch == BACKSLASH) {
|
|
|
|
ch = pattern.unescapeAt(i);
|
|
|
|
if (ch == (UChar32) -1) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--length; // sic; see above
|
|
|
|
multiCharBuffer.append(ch);
|
|
|
|
}
|
|
|
|
if (length < 1) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We have new string. Add it to set and continue;
|
|
|
|
// we don't need to drop through to the further
|
|
|
|
// processing
|
|
|
|
add(multiCharBuffer);
|
|
|
|
pattern.extractBetween(st, i, multiCharBuffer);
|
|
|
|
newPat.append(OPEN_BRACE).append(multiCharBuffer);
|
|
|
|
rebuildPattern = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-01-12 18:03:53 +00:00
|
|
|
|
|
|
|
/* At this point we have either a character c, or a nested set. If
|
|
|
|
* we have encountered a nested set, either embedded in the pattern,
|
2000-05-25 19:27:14 +00:00
|
|
|
* or as a variable, we have a non-null nestedSet, and c should be
|
2000-01-12 18:03:53 +00:00
|
|
|
* ignored. Otherwise c is the current character, and isLiteral
|
|
|
|
* indicates whether it is an escaped literal (or variable) or a
|
|
|
|
* normal unescaped character. Unescaped characters '-', '&', and
|
|
|
|
* ']' have special meanings.
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
2000-05-25 19:27:14 +00:00
|
|
|
if (nestedSet != NULL) {
|
2001-07-25 19:11:02 +00:00
|
|
|
if (lastChar != NONE) {
|
2000-01-12 18:03:53 +00:00
|
|
|
if (lastOp != 0) {
|
|
|
|
// throw new IllegalArgumentException("Illegal rhs for " + lastChar + lastOp);
|
1999-10-20 22:08:09 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
add(lastChar, lastChar);
|
2001-07-18 19:52:28 +00:00
|
|
|
if (nestedPatDone) {
|
|
|
|
// If there was a character before the nested set,
|
|
|
|
// then we need to insert it in newPat before the
|
|
|
|
// pattern for the nested set. This position was
|
|
|
|
// recorded in nestedPatStart.
|
|
|
|
UnicodeString s;
|
|
|
|
_appendToPat(s, lastChar, FALSE);
|
|
|
|
newPat.insert(nestedPatStart, s);
|
|
|
|
} else {
|
|
|
|
_appendToPat(newPat, lastChar, FALSE);
|
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
lastChar = NONE;
|
2000-01-12 18:03:53 +00:00
|
|
|
}
|
|
|
|
switch (lastOp) {
|
2000-03-04 02:21:02 +00:00
|
|
|
case HYPHEN:
|
2000-05-25 19:27:14 +00:00
|
|
|
removeAll(*nestedSet);
|
2000-01-12 18:03:53 +00:00
|
|
|
break;
|
2000-03-04 02:21:02 +00:00
|
|
|
case INTERSECTION:
|
2000-05-25 19:27:14 +00:00
|
|
|
retainAll(*nestedSet);
|
2000-01-12 18:03:53 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
2000-05-25 19:27:14 +00:00
|
|
|
addAll(*nestedSet);
|
2000-01-12 18:03:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-18 19:52:28 +00:00
|
|
|
|
|
|
|
// Get the pattern for the nested set, if we haven't done so
|
|
|
|
// already.
|
|
|
|
if (!nestedPatDone) {
|
|
|
|
if (lastOp != 0) {
|
|
|
|
newPat.append(lastOp);
|
|
|
|
}
|
|
|
|
nestedSet->_toPattern(newPat, FALSE);
|
|
|
|
}
|
|
|
|
rebuildPattern = TRUE;
|
|
|
|
|
2000-01-12 18:03:53 +00:00
|
|
|
lastOp = 0;
|
2001-07-18 19:52:28 +00:00
|
|
|
|
2000-03-04 02:21:02 +00:00
|
|
|
} else if (!isLiteral && c == SET_CLOSE) {
|
2001-06-12 18:07:15 +00:00
|
|
|
// Final closing delimiter. This is one of 2 ways we
|
|
|
|
// leave this loop if the pattern is well-formed.
|
2000-08-30 20:43:25 +00:00
|
|
|
if (anchor > 2 || anchor == 1) {
|
|
|
|
//throw new IllegalArgumentException("Syntax error near $" + pattern);
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (anchor == 2) {
|
2001-06-12 18:07:15 +00:00
|
|
|
rebuildPattern = TRUE;
|
2001-07-18 18:38:00 +00:00
|
|
|
newPat.append((UChar)SymbolTable::SYMBOL_REF);
|
2002-05-29 18:36:09 +00:00
|
|
|
add(U_ETHER);
|
2000-08-30 20:43:25 +00:00
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
mode = 4;
|
2000-01-12 18:03:53 +00:00
|
|
|
break;
|
2000-03-04 02:21:02 +00:00
|
|
|
} else if (lastOp == 0 && !isLiteral && (c == HYPHEN || c == INTERSECTION)) {
|
2001-07-25 19:11:02 +00:00
|
|
|
// assert(c <= 0xFFFF);
|
|
|
|
lastOp = (UChar) c;
|
2000-03-04 02:21:02 +00:00
|
|
|
} else if (lastOp == HYPHEN) {
|
2000-05-24 18:25:27 +00:00
|
|
|
if (lastChar >= c) {
|
|
|
|
// Don't allow redundant (a-a) or empty (b-a) ranges;
|
|
|
|
// these are most likely typos.
|
|
|
|
//throw new IllegalArgumentException("Invalid range " + lastChar +
|
|
|
|
// '-' + c);
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
2000-05-24 18:25:27 +00:00
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
add(lastChar, c);
|
2001-07-18 18:38:00 +00:00
|
|
|
_appendToPat(newPat, lastChar, FALSE);
|
|
|
|
newPat.append(HYPHEN);
|
|
|
|
_appendToPat(newPat, c, FALSE);
|
2000-01-12 18:03:53 +00:00
|
|
|
lastOp = 0;
|
2001-07-25 19:11:02 +00:00
|
|
|
lastChar = NONE;
|
2000-01-12 18:03:53 +00:00
|
|
|
} else if (lastOp != 0) {
|
|
|
|
// We have <set>&<char> or <char>&<char>
|
|
|
|
// throw new IllegalArgumentException("Unquoted " + lastOp);
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
2000-01-12 18:03:53 +00:00
|
|
|
} else {
|
2001-07-25 19:11:02 +00:00
|
|
|
if (lastChar != NONE) {
|
2000-01-12 18:03:53 +00:00
|
|
|
// We have <char><char>
|
2000-05-25 19:27:14 +00:00
|
|
|
add(lastChar, lastChar);
|
2001-07-18 18:38:00 +00:00
|
|
|
_appendToPat(newPat, lastChar, FALSE);
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-01-12 18:03:53 +00:00
|
|
|
lastChar = c;
|
2001-12-01 01:33:41 +00:00
|
|
|
isLastLiteral = isLiteral;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-17 19:21:12 +00:00
|
|
|
if (mode < 4) {
|
|
|
|
// throw new IllegalArgumentException("Missing ']'");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-05-29 18:36:09 +00:00
|
|
|
// Treat a trailing '$' as indicating U_ETHER. This code is only
|
2001-10-17 19:21:12 +00:00
|
|
|
// executed if symbols == NULL; otherwise other code parses the
|
|
|
|
// anchor.
|
2001-12-01 01:33:41 +00:00
|
|
|
if (lastChar == (UChar)SymbolTable::SYMBOL_REF && !isLastLiteral) {
|
2001-10-17 19:21:12 +00:00
|
|
|
rebuildPattern = TRUE;
|
|
|
|
newPat.append(lastChar);
|
2002-05-29 18:36:09 +00:00
|
|
|
add(U_ETHER);
|
2001-10-17 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (lastChar != NONE) {
|
2001-06-12 18:07:15 +00:00
|
|
|
add(lastChar, lastChar);
|
2001-07-18 18:38:00 +00:00
|
|
|
_appendToPat(newPat, lastChar, FALSE);
|
2001-06-12 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2000-01-12 18:03:53 +00:00
|
|
|
// Handle unprocessed stuff preceding the closing ']'
|
2000-03-04 02:21:02 +00:00
|
|
|
if (lastOp == HYPHEN) {
|
2000-01-12 18:03:53 +00:00
|
|
|
// Trailing '-' is treated as literal
|
2000-05-25 19:27:14 +00:00
|
|
|
add(lastOp, lastOp);
|
2001-07-18 18:38:00 +00:00
|
|
|
newPat.append(HYPHEN);
|
2000-03-04 02:21:02 +00:00
|
|
|
} else if (lastOp == INTERSECTION) {
|
2000-01-12 18:03:53 +00:00
|
|
|
// throw new IllegalArgumentException("Unquoted trailing " + lastOp);
|
1999-10-20 22:08:09 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-05-25 19:27:14 +00:00
|
|
|
return;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2000-01-12 18:03:53 +00:00
|
|
|
|
2001-10-17 19:21:12 +00:00
|
|
|
if (mode == 4) {
|
|
|
|
newPat.append(SET_CLOSE);
|
|
|
|
}
|
2001-07-18 18:38:00 +00:00
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
/**
|
2000-01-12 18:03:53 +00:00
|
|
|
* If we saw a '^' after the initial '[' of this pattern, then perform
|
|
|
|
* the complement. (Inversion after '[:' is handled elsewhere.)
|
1999-10-20 22:08:09 +00:00
|
|
|
*/
|
|
|
|
if (invert) {
|
2000-05-25 19:27:14 +00:00
|
|
|
complement();
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
pos.setIndex(i);
|
2001-06-12 18:07:15 +00:00
|
|
|
|
2001-07-18 18:38:00 +00:00
|
|
|
// Use the rebuilt pattern (newPat) only if necessary. Prefer the
|
|
|
|
// generated pattern.
|
2001-06-12 18:07:15 +00:00
|
|
|
if (rebuildPattern) {
|
2001-07-18 18:38:00 +00:00
|
|
|
rebuiltPat.append(newPat);
|
|
|
|
} else {
|
|
|
|
_generatePattern(rebuiltPat, FALSE);
|
2001-06-12 18:07:15 +00:00
|
|
|
}
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Implementation: Utility methods
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* Allocate our strings vector and return TRUE if successful.
|
|
|
|
*/
|
|
|
|
UBool UnicodeSet::allocateStrings() {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
strings = new UVector(uhash_deleteUnicodeString,
|
|
|
|
uhash_compareUnicodeString, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete strings;
|
|
|
|
strings = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
void UnicodeSet::ensureCapacity(int32_t newLen) {
|
2002-02-28 01:42:40 +00:00
|
|
|
if (newLen <= capacity)
|
|
|
|
return;
|
2000-05-25 19:27:14 +00:00
|
|
|
capacity = newLen + GROW_EXTRA;
|
2002-02-28 01:42:40 +00:00
|
|
|
UChar32* temp = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2000-05-25 19:27:14 +00:00
|
|
|
uprv_memcpy(temp, list, len*sizeof(UChar32));
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(list);
|
2000-05-25 19:27:14 +00:00
|
|
|
list = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnicodeSet::ensureBufferCapacity(int32_t newLen) {
|
2002-02-28 01:42:40 +00:00
|
|
|
if (buffer != NULL && newLen <= bufferCapacity)
|
|
|
|
return;
|
|
|
|
uprv_free(buffer);
|
2000-05-25 19:27:14 +00:00
|
|
|
bufferCapacity = newLen + GROW_EXTRA;
|
2002-02-28 01:42:40 +00:00
|
|
|
buffer = (UChar32*) uprv_malloc(sizeof(UChar32) * bufferCapacity);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap list and buffer.
|
|
|
|
*/
|
|
|
|
void UnicodeSet::swapBuffers(void) {
|
|
|
|
// swap list and buffer
|
|
|
|
UChar32* temp = list;
|
|
|
|
list = buffer;
|
|
|
|
buffer = temp;
|
|
|
|
|
|
|
|
int32_t c = capacity;
|
|
|
|
capacity = bufferCapacity;
|
|
|
|
bufferCapacity = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Implementation: Fundamental operators
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2001-10-23 00:54:07 +00:00
|
|
|
static inline UChar32 max(UChar32 a, UChar32 b) {
|
2000-05-25 19:27:14 +00:00
|
|
|
return (a > b) ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// polarity = 0, 3 is normal: x xor y
|
|
|
|
// polarity = 1, 2: x xor ~y == x === y
|
|
|
|
|
2000-05-26 01:18:36 +00:00
|
|
|
void UnicodeSet::exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity) {
|
2000-05-25 19:27:14 +00:00
|
|
|
ensureBufferCapacity(len + otherLen);
|
|
|
|
int32_t i = 0, j = 0, k = 0;
|
|
|
|
UChar32 a = list[i++];
|
|
|
|
UChar32 b;
|
|
|
|
if (polarity == 1 || polarity == 2) {
|
2001-06-21 18:27:52 +00:00
|
|
|
b = UNICODESET_LOW;
|
|
|
|
if (other[j] == UNICODESET_LOW) { // skip base if already LOW
|
2000-05-25 19:27:14 +00:00
|
|
|
++j;
|
|
|
|
b = other[j];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b = other[j++];
|
|
|
|
}
|
|
|
|
// simplest of all the routines
|
|
|
|
// sort the values, discarding identicals!
|
|
|
|
for (;;) {
|
|
|
|
if (a < b) {
|
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i++];
|
|
|
|
} else if (b < a) {
|
|
|
|
buffer[k++] = b;
|
|
|
|
b = other[j++];
|
2001-06-21 18:27:52 +00:00
|
|
|
} else if (a != UNICODESET_HIGH) { // at this point, a == b
|
2000-05-25 19:27:14 +00:00
|
|
|
// discard both values!
|
|
|
|
a = list[i++];
|
|
|
|
b = other[j++];
|
|
|
|
} else { // DONE!
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = UNICODESET_HIGH;
|
2000-05-25 19:27:14 +00:00
|
|
|
len = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swapBuffers();
|
2001-06-12 18:07:15 +00:00
|
|
|
pat.truncate(0);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// polarity = 0 is normal: x union y
|
|
|
|
// polarity = 2: x union ~y
|
|
|
|
// polarity = 1: ~x union y
|
|
|
|
// polarity = 3: ~x union ~y
|
|
|
|
|
|
|
|
void UnicodeSet::add(const UChar32* other, int32_t otherLen, int8_t polarity) {
|
|
|
|
ensureBufferCapacity(len + otherLen);
|
|
|
|
int32_t i = 0, j = 0, k = 0;
|
|
|
|
UChar32 a = list[i++];
|
|
|
|
UChar32 b = other[j++];
|
|
|
|
// change from xor is that we have to check overlapping pairs
|
|
|
|
// polarity bit 1 means a is second, bit 2 means b is.
|
|
|
|
for (;;) {
|
|
|
|
switch (polarity) {
|
|
|
|
case 0: // both first; take lower if unequal
|
|
|
|
if (a < b) { // take a
|
|
|
|
// Back up over overlapping ranges in buffer[]
|
|
|
|
if (k > 0 && a <= buffer[k-1]) {
|
|
|
|
// Pick latter end value in buffer[] vs. list[]
|
|
|
|
a = max(list[i], buffer[--k]);
|
|
|
|
} else {
|
|
|
|
// No overlap
|
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i];
|
|
|
|
}
|
|
|
|
i++; // Common if/else code factored out
|
|
|
|
polarity ^= 1;
|
|
|
|
} else if (b < a) { // take b
|
|
|
|
if (k > 0 && b <= buffer[k-1]) {
|
|
|
|
b = max(other[j], buffer[--k]);
|
|
|
|
} else {
|
|
|
|
buffer[k++] = b;
|
|
|
|
b = other[j];
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
polarity ^= 2;
|
|
|
|
} else { // a == b, take a, drop b
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
2000-05-25 19:27:14 +00:00
|
|
|
// This is symmetrical; it doesn't matter if
|
|
|
|
// we backtrack with a or b. - liu
|
|
|
|
if (k > 0 && a <= buffer[k-1]) {
|
|
|
|
a = max(list[i], buffer[--k]);
|
|
|
|
} else {
|
|
|
|
// No overlap
|
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i];
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
polarity ^= 1;
|
2001-06-21 18:27:52 +00:00
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // both second; take higher if unequal, and drop other
|
|
|
|
if (b <= a) { // take a
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
2000-05-25 19:27:14 +00:00
|
|
|
buffer[k++] = a;
|
|
|
|
} else { // take b
|
2001-06-21 18:27:52 +00:00
|
|
|
if (b == UNICODESET_HIGH) goto loop_end;
|
2000-05-25 19:27:14 +00:00
|
|
|
buffer[k++] = b;
|
|
|
|
}
|
2001-06-21 18:27:52 +00:00
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1; // factored common code
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
break;
|
|
|
|
case 1: // a second, b first; if b < a, overlap
|
|
|
|
if (a < b) { // no overlap, take a
|
|
|
|
buffer[k++] = a; a = list[i++]; polarity ^= 1;
|
|
|
|
} else if (b < a) { // OVERLAP, drop b
|
2001-06-21 18:27:52 +00:00
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else { // a == b, drop both!
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // a first, b second; if a < b, overlap
|
|
|
|
if (b < a) { // no overlap, take b
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = b;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else if (a < b) { // OVERLAP, drop a
|
2001-06-21 18:27:52 +00:00
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else { // a == b, drop both!
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loop_end:
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = UNICODESET_HIGH; // terminate
|
2000-05-25 19:27:14 +00:00
|
|
|
len = k;
|
|
|
|
swapBuffers();
|
2001-06-12 18:07:15 +00:00
|
|
|
pat.truncate(0);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// polarity = 0 is normal: x intersect y
|
|
|
|
// polarity = 2: x intersect ~y == set-minus
|
|
|
|
// polarity = 1: ~x intersect y
|
|
|
|
// polarity = 3: ~x intersect ~y
|
|
|
|
|
|
|
|
void UnicodeSet::retain(const UChar32* other, int32_t otherLen, int8_t polarity) {
|
|
|
|
ensureBufferCapacity(len + otherLen);
|
|
|
|
int32_t i = 0, j = 0, k = 0;
|
|
|
|
UChar32 a = list[i++];
|
|
|
|
UChar32 b = other[j++];
|
|
|
|
// change from xor is that we have to check overlapping pairs
|
|
|
|
// polarity bit 1 means a is second, bit 2 means b is.
|
|
|
|
for (;;) {
|
|
|
|
switch (polarity) {
|
|
|
|
case 0: // both first; drop the smaller
|
|
|
|
if (a < b) { // drop a
|
2001-06-21 18:27:52 +00:00
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else if (b < a) { // drop b
|
2001-06-21 18:27:52 +00:00
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else { // a == b, take one, drop other
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // both second; take lower if unequal
|
|
|
|
if (a < b) { // take a
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else if (b < a) { // take b
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = b;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else { // a == b, take one, drop other
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // a second, b first;
|
|
|
|
if (a < b) { // NO OVERLAP, drop a
|
2001-06-21 18:27:52 +00:00
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else if (b < a) { // OVERLAP, take b
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = b;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else { // a == b, drop both!
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // a first, b second; if a < b, overlap
|
|
|
|
if (b < a) { // no overlap, drop b
|
2001-06-21 18:27:52 +00:00
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else if (a < b) { // OVERLAP, take a
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = a;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
2000-05-25 19:27:14 +00:00
|
|
|
} else { // a == b, drop both!
|
2001-06-21 18:27:52 +00:00
|
|
|
if (a == UNICODESET_HIGH) goto loop_end;
|
|
|
|
a = list[i++];
|
|
|
|
polarity ^= 1;
|
|
|
|
b = other[j++];
|
|
|
|
polarity ^= 2;
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loop_end:
|
2001-06-21 18:27:52 +00:00
|
|
|
buffer[k++] = UNICODESET_HIGH; // terminate
|
2000-05-25 19:27:14 +00:00
|
|
|
len = k;
|
|
|
|
swapBuffers();
|
2001-06-12 18:07:15 +00:00
|
|
|
pat.truncate(0);
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|