2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
1999-10-26 17:15:44 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1999-2015, International Business Machines
|
|
|
|
* 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
|
|
|
|
2004-08-26 22:51:40 +00:00
|
|
|
#include "unicode/utypes.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/parsepos.h"
|
2003-10-14 21:47:59 +00:00
|
|
|
#include "unicode/symtable.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/utf8.h"
|
|
|
|
#include "unicode/utf16.h"
|
2003-09-29 23:00:39 +00:00
|
|
|
#include "ruleiter.h"
|
2000-05-25 19:27:14 +00:00
|
|
|
#include "cmemory.h"
|
2007-07-19 04:33:20 +00:00
|
|
|
#include "cstring.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "patternprops.h"
|
2011-07-04 23:51:56 +00:00
|
|
|
#include "uelement.h"
|
2001-11-21 22:43:21 +00:00
|
|
|
#include "util.h"
|
2002-04-25 23:39:57 +00:00
|
|
|
#include "uvector.h"
|
2002-10-30 18:24:00 +00:00
|
|
|
#include "charstr.h"
|
|
|
|
#include "ustrfmt.h"
|
|
|
|
#include "uassert.h"
|
2007-07-19 04:33:20 +00:00
|
|
|
#include "bmpset.h"
|
|
|
|
#include "unisetspan.h"
|
2000-05-25 19:27:14 +00:00
|
|
|
|
2004-10-15 22:56:26 +00:00
|
|
|
// 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*/
|
|
|
|
#define OPEN_BRACE ((UChar)123) /*{*/
|
|
|
|
#define CLOSE_BRACE ((UChar)125) /*}*/
|
|
|
|
#define UPPER_P ((UChar)0x0050) /*P*/
|
|
|
|
#define LOWER_P ((UChar)0x0070) /*p*/
|
|
|
|
#define UPPER_N ((UChar)78) /*N*/
|
|
|
|
#define EQUALS ((UChar)0x003D) /*=*/
|
|
|
|
|
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
|
|
|
|
|
2001-10-12 05:54:15 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-29 16:23:22 +00:00
|
|
|
SymbolTable::~SymbolTable() {}
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeSet)
|
2001-11-09 01:22:33 +00:00
|
|
|
|
2003-05-14 22:30:21 +00:00
|
|
|
/**
|
2003-05-23 20:32:33 +00:00
|
|
|
* Modify the given UChar32 variable so that it is in range, by
|
|
|
|
* pinning values < UNICODESET_LOW to UNICODESET_LOW, and
|
|
|
|
* pinning values > UNICODESET_HIGH-1 to UNICODESET_HIGH-1.
|
|
|
|
* It modifies its argument in-place and also returns it.
|
2003-05-14 22:30:21 +00:00
|
|
|
*/
|
2003-06-10 00:39:15 +00:00
|
|
|
static inline UChar32 pinCodePoint(UChar32& c) {
|
2003-05-23 20:32:33 +00:00
|
|
|
if (c < UNICODESET_LOW) {
|
|
|
|
c = UNICODESET_LOW;
|
|
|
|
} else if (c > (UNICODESET_HIGH-1)) {
|
|
|
|
c = (UNICODESET_HIGH-1);
|
2003-05-14 22:30:21 +00:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-07-04 23:51:56 +00:00
|
|
|
static void U_CALLCONV cloneUnicodeString(UElement *dst, UElement *src) {
|
2002-08-29 22:12:49 +00:00
|
|
|
dst->pointer = new UnicodeString(*(UnicodeString*)src->pointer);
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
|
2011-07-04 23:51:56 +00:00
|
|
|
static int8_t U_CALLCONV compareUnicodeString(UElement t1, UElement 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() :
|
2007-07-19 04:33:20 +00:00
|
|
|
len(1), capacity(1 + START_EXTRA), list(0), bmpSet(0), buffer(0),
|
2008-01-22 17:46:22 +00:00
|
|
|
bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
|
|
|
|
fFlags(0)
|
2001-03-06 20:20:47 +00:00
|
|
|
{
|
2007-12-14 10:17:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
allocateStrings(status);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
setToBogus(); // If memory allocation failed, set to bogus state.
|
2008-01-22 17:46:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-08-07 02:48:28 +00:00
|
|
|
if(list!=NULL){
|
|
|
|
list[0] = UNICODESET_HIGH;
|
2008-01-22 17:46:22 +00:00
|
|
|
} else { // If memory allocation failed, set to bogus state.
|
|
|
|
setToBogus();
|
|
|
|
return;
|
2002-08-07 02:48:28 +00:00
|
|
|
}
|
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) :
|
2007-07-19 04:33:20 +00:00
|
|
|
len(1), capacity(1 + START_EXTRA), list(0), bmpSet(0), buffer(0),
|
2008-01-22 17:46:22 +00:00
|
|
|
bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
|
|
|
|
fFlags(0)
|
2001-03-06 20:20:47 +00:00
|
|
|
{
|
2007-12-14 10:17:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
allocateStrings(status);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
setToBogus(); // If memory allocation failed, set to bogus state.
|
2008-01-22 17:46:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-08-07 02:48:28 +00:00
|
|
|
if(list!=NULL){
|
|
|
|
list[0] = UNICODESET_HIGH;
|
2006-09-29 21:36:59 +00:00
|
|
|
complement(start, end);
|
2008-01-22 17:46:22 +00:00
|
|
|
} else { // If memory allocation failed, set to bogus state.
|
|
|
|
setToBogus();
|
|
|
|
return;
|
2002-08-07 02:48:28 +00:00
|
|
|
}
|
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
|
|
|
|
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),
|
2007-07-19 04:33:20 +00:00
|
|
|
len(0), capacity(o.isFrozen() ? o.len : o.len + GROW_EXTRA), list(0),
|
|
|
|
bmpSet(0),
|
|
|
|
buffer(0), bufferCapacity(0),
|
2008-01-22 17:46:22 +00:00
|
|
|
patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
|
|
|
|
fFlags(0)
|
2000-08-23 23:48:04 +00:00
|
|
|
{
|
2007-12-14 10:17:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
allocateStrings(status);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
setToBogus(); // If memory allocation failed, set to bogus state.
|
2008-01-22 17:46:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
2002-08-07 02:48:28 +00:00
|
|
|
if(list!=NULL){
|
|
|
|
*this = o;
|
2008-01-22 17:46:22 +00:00
|
|
|
} else { // If memory allocation failed, set to bogus state.
|
|
|
|
setToBogus();
|
|
|
|
return;
|
2002-08-07 02:48:28 +00:00
|
|
|
}
|
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
|
|
|
|
2007-07-19 04:33:20 +00:00
|
|
|
// Copy-construct as thawed.
|
|
|
|
UnicodeSet::UnicodeSet(const UnicodeSet& o, UBool /* asThawed */) :
|
|
|
|
UnicodeFilter(o),
|
|
|
|
len(0), capacity(o.len + GROW_EXTRA), list(0),
|
|
|
|
bmpSet(0),
|
|
|
|
buffer(0), bufferCapacity(0),
|
2008-01-22 17:46:22 +00:00
|
|
|
patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
|
|
|
|
fFlags(0)
|
2007-07-19 04:33:20 +00:00
|
|
|
{
|
2007-12-14 10:17:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
allocateStrings(status);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
setToBogus(); // If memory allocation failed, set to bogus state.
|
2008-01-22 17:46:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-07-19 04:33:20 +00:00
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
|
|
|
if(list!=NULL){
|
|
|
|
// *this = o except for bmpSet and stringSpan
|
|
|
|
len = o.len;
|
2016-09-20 01:10:36 +00:00
|
|
|
uprv_memcpy(list, o.list, (size_t)len*sizeof(UChar32));
|
2008-01-22 17:46:22 +00:00
|
|
|
if (strings != NULL && o.strings != NULL) {
|
|
|
|
strings->assign(*o.strings, cloneUnicodeString, status);
|
|
|
|
} else { // Invalid strings.
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
2007-07-19 04:33:20 +00:00
|
|
|
if (o.pat) {
|
|
|
|
setPattern(UnicodeString(o.pat, o.patLen));
|
|
|
|
}
|
2008-01-22 17:46:22 +00:00
|
|
|
} else { // If memory allocation failed, set to bogus state.
|
|
|
|
setToBogus();
|
|
|
|
return;
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
|
|
|
_dbgct(this);
|
|
|
|
}
|
|
|
|
|
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);
|
2007-07-19 04:33:20 +00:00
|
|
|
delete bmpSet;
|
2002-08-09 18:29:37 +00:00
|
|
|
if (buffer) {
|
|
|
|
uprv_free(buffer);
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
delete strings;
|
2007-07-19 04:33:20 +00:00
|
|
|
delete stringSpan;
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
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) {
|
2018-09-07 20:40:19 +00:00
|
|
|
return copyFrom(o, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeSet& UnicodeSet::copyFrom(const UnicodeSet& o, UBool asThawed) {
|
2007-07-25 03:58:02 +00:00
|
|
|
if (this == &o) {
|
|
|
|
return *this;
|
|
|
|
}
|
2007-07-24 18:08:22 +00:00
|
|
|
if (isFrozen()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2008-02-08 03:39:40 +00:00
|
|
|
if (o.isBogus()) {
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
ensureCapacity(o.len, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
// ensureCapacity will mark the UnicodeSet as Bogus if OOM failure happens.
|
|
|
|
return *this;
|
2007-12-14 09:23:07 +00:00
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
len = o.len;
|
2016-09-20 01:10:36 +00:00
|
|
|
uprv_memcpy(list, o.list, (size_t)len*sizeof(UChar32));
|
2018-09-07 20:40:19 +00:00
|
|
|
if (o.bmpSet == NULL || asThawed) {
|
2007-07-19 04:33:20 +00:00
|
|
|
bmpSet = NULL;
|
|
|
|
} else {
|
|
|
|
bmpSet = new BMPSet(*o.bmpSet, list, len);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (bmpSet == NULL) { // Check for memory allocation error.
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strings != NULL && o.strings != NULL) {
|
|
|
|
strings->assign(*o.strings, cloneUnicodeString, ec);
|
|
|
|
} else { // Invalid strings.
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
2018-09-07 20:40:19 +00:00
|
|
|
if (o.stringSpan == NULL || asThawed) {
|
2007-07-19 04:33:20 +00:00
|
|
|
stringSpan = NULL;
|
|
|
|
} else {
|
|
|
|
stringSpan = new UnicodeSetStringSpan(*o.stringSpan, *strings);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (stringSpan == NULL) { // Check for memory allocation error.
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
|
|
|
if (o.pat) {
|
|
|
|
setPattern(UnicodeString(o.pat, o.patLen));
|
|
|
|
}
|
1999-10-26 17:15:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-07-19 04:33:20 +00:00
|
|
|
/**
|
|
|
|
* Returns a copy of this object. All UnicodeMatcher objects have
|
|
|
|
* to support cloning in order to allow classes using
|
|
|
|
* UnicodeMatchers, such as Transliterator, to implement cloning.
|
|
|
|
*/
|
|
|
|
UnicodeFunctor* UnicodeSet::clone() const {
|
|
|
|
return new UnicodeSet(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeFunctor *UnicodeSet::cloneAsThawed() const {
|
|
|
|
return new UnicodeSet(*this, TRUE);
|
|
|
|
}
|
|
|
|
|
1999-10-26 17:15:44 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2018-08-20 23:40:46 +00:00
|
|
|
uint32_t result = static_cast<uint32_t>(len);
|
2000-05-25 19:27:14 +00:00
|
|
|
for (int32_t i = 0; i < len; ++i) {
|
2018-08-20 23:40:46 +00:00
|
|
|
result *= 1000003u;
|
2000-05-25 19:27:14 +00:00
|
|
|
result += list[i];
|
|
|
|
}
|
2018-08-20 23:40:46 +00:00
|
|
|
return static_cast<int32_t>(result);
|
1999-10-26 17:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Public API
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
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),
|
2004-11-04 22:05:00 +00:00
|
|
|
* Note than the elements of a set may include both individual
|
|
|
|
* codepoints and strings.
|
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
|
2002-07-29 19:36:55 +00:00
|
|
|
//int32_t i = -1;
|
|
|
|
//for (;;) {
|
|
|
|
// if (c < list[++i]) break;
|
|
|
|
//}
|
2007-07-19 04:33:20 +00:00
|
|
|
if (bmpSet != NULL) {
|
|
|
|
return bmpSet->contains(c);
|
|
|
|
}
|
|
|
|
if (stringSpan != NULL) {
|
|
|
|
return stringSpan->contains(c);
|
|
|
|
}
|
2003-05-14 22:30:21 +00:00
|
|
|
if (c >= UNICODESET_HIGH) { // Don't need to check LOW bound
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-07-29 19:36:55 +00:00
|
|
|
int32_t i = findCodePoint(c);
|
2007-07-19 04:33:20 +00:00
|
|
|
return (UBool)(i & 1); // return true if odd
|
2002-07-29 19:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the smallest value i such that c < list[i]. Caller
|
|
|
|
* must ensure that c is a legal value or this method will enter
|
|
|
|
* an infinite loop. This method performs a binary search.
|
|
|
|
* @param c a character in the range MIN_VALUE..MAX_VALUE
|
|
|
|
* inclusive
|
|
|
|
* @return the smallest integer i in the range 0..len-1,
|
|
|
|
* inclusive, such that c < list[i]
|
|
|
|
*/
|
|
|
|
int32_t UnicodeSet::findCodePoint(UChar32 c) const {
|
2002-11-14 22:26:41 +00:00
|
|
|
/* Examples:
|
|
|
|
findCodePoint(c)
|
|
|
|
set list[] c=0 1 3 4 7 8
|
|
|
|
=== ============== ===========
|
|
|
|
[] [110000] 0 0 0 0 0 0
|
|
|
|
[\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2
|
|
|
|
[\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2
|
2003-05-14 22:30:21 +00:00
|
|
|
[:Any:] [0, 110000] 1 1 1 1 1 1
|
2002-11-14 22:26:41 +00:00
|
|
|
*/
|
|
|
|
|
2002-07-29 19:36:55 +00:00
|
|
|
// Return the smallest i such that c < list[i]. Assume
|
|
|
|
// list[len - 1] == HIGH and that c is legal (0..HIGH-1).
|
2006-03-20 07:53:07 +00:00
|
|
|
if (c < list[0])
|
|
|
|
return 0;
|
2002-10-10 21:18:49 +00:00
|
|
|
// High runner test. c is often after the last range, so an
|
2004-11-04 22:05:00 +00:00
|
|
|
// initial check for this condition pays off.
|
2002-11-14 22:42:13 +00:00
|
|
|
int32_t lo = 0;
|
|
|
|
int32_t hi = len - 1;
|
2007-07-19 04:33:20 +00:00
|
|
|
if (lo >= hi || c >= list[hi-1])
|
|
|
|
return hi;
|
2002-07-29 19:36:55 +00:00
|
|
|
// invariant: c >= list[lo]
|
|
|
|
// invariant: c < list[hi]
|
2002-04-25 23:39:57 +00:00
|
|
|
for (;;) {
|
2002-10-10 21:18:49 +00:00
|
|
|
int32_t i = (lo + hi) >> 1;
|
2006-03-20 07:53:07 +00:00
|
|
|
if (i == lo) {
|
|
|
|
break; // Found!
|
|
|
|
} else if (c < list[i]) {
|
2002-07-29 19:36:55 +00:00
|
|
|
hi = i;
|
|
|
|
} else {
|
|
|
|
lo = i;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
2006-03-20 07:53:07 +00:00
|
|
|
return hi;
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2002-07-29 19:36:55 +00:00
|
|
|
//int32_t i = -1;
|
|
|
|
//for (;;) {
|
|
|
|
// if (start < list[++i]) break;
|
|
|
|
//}
|
|
|
|
int32_t i = findCodePoint(start);
|
2000-05-25 19:27:14 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2007-07-19 04:33:20 +00:00
|
|
|
return (UBool)(span(s.getBuffer(), s.length(), USET_SPAN_CONTAINED) ==
|
|
|
|
s.length());
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2003-06-09 18:55:55 +00:00
|
|
|
//int32_t i = -1;
|
|
|
|
//for (;;) {
|
|
|
|
// if (start < list[++i]) break;
|
|
|
|
//}
|
|
|
|
int32_t i = findCodePoint(start);
|
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;
|
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2007-07-19 04:33:20 +00:00
|
|
|
return (UBool)(span(s.getBuffer(), s.length(), USET_SPAN_NOT_CONTAINED) ==
|
|
|
|
s.length());
|
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;
|
2007-06-15 23:32:12 +00:00
|
|
|
int32_t rangeCount=getRangeCount();
|
|
|
|
for (i=0; i<rangeCount; ++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
|
|
|
/**
|
2004-11-04 22:05:00 +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
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
// 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;
|
2004-11-04 22:05:00 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Implement of UnicodeMatcher
|
|
|
|
*/
|
|
|
|
void UnicodeSet::addMatchSetTo(UnicodeSet& toUnionTo) const {
|
|
|
|
toUnionTo.addAll(*this);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-07-19 04:33:20 +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
|
|
|
|
*/
|
|
|
|
UnicodeSet& UnicodeSet::set(UChar32 start, UChar32 end) {
|
|
|
|
clear();
|
|
|
|
complement(start, end);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
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) {
|
2003-05-14 22:30:21 +00:00
|
|
|
if (pinCodePoint(start) < pinCodePoint(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);
|
2002-10-10 21:18:49 +00:00
|
|
|
} else if (start == end) {
|
|
|
|
add(start);
|
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
|
|
|
}
|
|
|
|
|
2002-10-10 21:18:49 +00:00
|
|
|
// #define DEBUG_US_ADD
|
|
|
|
|
|
|
|
#ifdef DEBUG_US_ADD
|
|
|
|
#include <stdio.h>
|
|
|
|
void dump(UChar32 c) {
|
|
|
|
if (c <= 0xFF) {
|
|
|
|
printf("%c", (char)c);
|
|
|
|
} else {
|
2002-11-14 22:26:41 +00:00
|
|
|
printf("U+%04X", c);
|
2002-10-10 21:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void dump(const UChar32* list, int32_t len) {
|
|
|
|
printf("[");
|
|
|
|
for (int32_t i=0; i<len; ++i) {
|
|
|
|
if (i != 0) printf(", ");
|
|
|
|
dump(list[i]);
|
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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) {
|
2002-10-10 21:18:49 +00:00
|
|
|
// find smallest i such that c < list[i]
|
|
|
|
// if odd, then it is IN the set
|
|
|
|
// if even, then it is OUT of the set
|
2003-05-14 22:30:21 +00:00
|
|
|
int32_t i = findCodePoint(pinCodePoint(c));
|
2002-10-10 21:18:49 +00:00
|
|
|
|
|
|
|
// already in set?
|
2008-02-08 03:39:40 +00:00
|
|
|
if ((i & 1) != 0 || isFrozen() || isBogus()) return *this;
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-10-10 21:18:49 +00:00
|
|
|
// HIGH is 0x110000
|
|
|
|
// assert(list[len-1] == HIGH);
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-10-10 21:18:49 +00:00
|
|
|
// empty = [HIGH]
|
|
|
|
// [start_0, limit_0, start_1, limit_1, HIGH]
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-11-14 22:26:41 +00:00
|
|
|
// [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]
|
2002-10-10 21:18:49 +00:00
|
|
|
// ^
|
|
|
|
// list[i]
|
|
|
|
|
|
|
|
// i == 0 means c is before the first range
|
|
|
|
|
|
|
|
#ifdef DEBUG_US_ADD
|
|
|
|
printf("Add of ");
|
|
|
|
dump(c);
|
|
|
|
printf(" found at %d", i);
|
|
|
|
printf(": ");
|
|
|
|
dump(list, len);
|
|
|
|
printf(" => ");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (c == list[i]-1) {
|
|
|
|
// c is before start of next range
|
|
|
|
list[i] = c;
|
|
|
|
// if we touched the HIGH mark, then add a new one
|
|
|
|
if (c == (UNICODESET_HIGH - 1)) {
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ensureCapacity(len+1, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
// ensureCapacity will mark the object as Bogus if OOM failure happens.
|
|
|
|
return *this;
|
2007-12-14 09:23:07 +00:00
|
|
|
}
|
2002-10-10 21:18:49 +00:00
|
|
|
list[len++] = UNICODESET_HIGH;
|
|
|
|
}
|
|
|
|
if (i > 0 && c == list[i-1]) {
|
|
|
|
// collapse adjacent ranges
|
|
|
|
|
2002-11-14 22:26:41 +00:00
|
|
|
// [..., start_k-1, c, c, limit_k, ..., HIGH]
|
2002-10-10 21:18:49 +00:00
|
|
|
// ^
|
|
|
|
// list[i]
|
|
|
|
|
|
|
|
//for (int32_t k=i-1; k<len-2; ++k) {
|
|
|
|
// list[k] = list[k+2];
|
|
|
|
//}
|
|
|
|
UChar32* dst = list + i - 1;
|
|
|
|
UChar32* src = dst + 2;
|
|
|
|
UChar32* srclimit = list + len;
|
|
|
|
while (src < srclimit) *(dst++) = *(src++);
|
|
|
|
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (i > 0 && c == list[i-1]) {
|
|
|
|
// c is after end of prior range
|
|
|
|
list[i-1]++;
|
2002-10-30 18:24:00 +00:00
|
|
|
// no need to check for collapse here
|
2002-10-10 21:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// At this point we know the new char is not adjacent to
|
|
|
|
// any existing ranges, and it is not 10FFFF.
|
|
|
|
|
|
|
|
|
2002-11-14 22:26:41 +00:00
|
|
|
// [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]
|
2002-10-10 21:18:49 +00:00
|
|
|
// ^
|
|
|
|
// list[i]
|
|
|
|
|
2002-11-14 22:26:41 +00:00
|
|
|
// [..., start_k-1, limit_k-1, c, c+1, start_k, limit_k, ..., HIGH]
|
2002-10-10 21:18:49 +00:00
|
|
|
// ^
|
|
|
|
// list[i]
|
|
|
|
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ensureCapacity(len+2, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2018-08-02 06:10:47 +00:00
|
|
|
// ensureCapacity will mark the object as Bogus if OOM failure happens.
|
|
|
|
return *this;
|
2007-12-14 09:23:07 +00:00
|
|
|
}
|
2002-10-10 21:18:49 +00:00
|
|
|
|
|
|
|
//for (int32_t k=len-1; k>=i; --k) {
|
|
|
|
// list[k+2] = list[k];
|
|
|
|
//}
|
|
|
|
UChar32* src = list + len;
|
|
|
|
UChar32* dst = src + 2;
|
|
|
|
UChar32* srclimit = list + i;
|
|
|
|
while (src > srclimit) *(--dst) = *(--src);
|
|
|
|
|
|
|
|
list[i] = c;
|
|
|
|
list[i+1] = c+1;
|
|
|
|
len += 2;
|
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2002-10-10 21:18:49 +00:00
|
|
|
#ifdef DEBUG_US_ADD
|
|
|
|
dump(list, len);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
for (i=1; i<len; ++i) {
|
|
|
|
if (list[i] <= list[i-1]) {
|
|
|
|
// Corrupt array!
|
|
|
|
printf("ERROR: list has been corrupted\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
}
|
2002-10-10 21:18:49 +00:00
|
|
|
#endif
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2002-10-10 21:18:49 +00:00
|
|
|
return *this;
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (s.length() == 0 || isFrozen() || isBogus()) return *this;
|
2002-04-25 23:39:57 +00:00
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
2002-08-29 04:58:00 +00:00
|
|
|
if (!strings->contains((void*) &s)) {
|
|
|
|
_add(s);
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2002-08-29 04:58:00 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
} else {
|
2007-07-19 04:33:20 +00:00
|
|
|
add((UChar32)cp);
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeString* t = new UnicodeString(s);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (t == NULL) { // Check for memory allocation error.
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
strings->sortedInsert(t, compareUnicodeString, ec);
|
2008-02-17 09:16:12 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
setToBogus();
|
|
|
|
delete t;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
2011-07-27 05:53:56 +00:00
|
|
|
for (int32_t i = 0; i < s.length(); i += U16_LENGTH(cp)) {
|
2002-04-25 23:39:57 +00:00
|
|
|
cp = s.char32At(i);
|
2007-07-19 04:33:20 +00:00
|
|
|
add(cp);
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-12-11 21:30:10 +00:00
|
|
|
UnicodeSet& UnicodeSet::removeAllStrings() {
|
|
|
|
strings->removeAllElements();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2004-08-24 17:21:14 +00:00
|
|
|
UnicodeSet* U_EXPORT2 UnicodeSet::createFrom(const UnicodeString& s) {
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet *set = new UnicodeSet();
|
2008-01-22 17:46:22 +00:00
|
|
|
if (set != NULL) { // Check for memory allocation error.
|
|
|
|
set->add(s);
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
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
|
|
|
|
*/
|
2004-08-24 17:21:14 +00:00
|
|
|
UnicodeSet* U_EXPORT2 UnicodeSet::createFromAll(const UnicodeString& s) {
|
2002-04-25 23:39:57 +00:00
|
|
|
UnicodeSet *set = new UnicodeSet();
|
2008-01-22 17:46:22 +00:00
|
|
|
if (set != NULL) { // Check for memory allocation error.
|
|
|
|
set->addAll(s);
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
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) {
|
2003-05-14 22:30:21 +00:00
|
|
|
if (pinCodePoint(start) <= pinCodePoint(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.
|
2004-11-04 22:05:00 +00:00
|
|
|
*
|
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) {
|
2003-05-14 22:30:21 +00:00
|
|
|
if (pinCodePoint(start) <= pinCodePoint(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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (s.length() == 0 || isFrozen() || isBogus()) return *this;
|
2002-04-25 23:39:57 +00:00
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
|
|
|
strings->removeElement((void*) &s);
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2002-04-25 23:39:57 +00:00
|
|
|
} 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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2003-05-14 22:30:21 +00:00
|
|
|
if (pinCodePoint(start) <= pinCodePoint(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
|
|
|
}
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
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::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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2004-11-04 22:05:00 +00:00
|
|
|
if (list[0] == UNICODESET_LOW) {
|
2007-12-14 09:23:07 +00:00
|
|
|
ensureBufferCapacity(len-1, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return *this;
|
|
|
|
}
|
2016-09-20 01:10:36 +00:00
|
|
|
uprv_memcpy(buffer, list + 1, (size_t)(len-1)*sizeof(UChar32));
|
2002-04-25 23:39:57 +00:00
|
|
|
--len;
|
|
|
|
} else {
|
2007-12-14 09:23:07 +00:00
|
|
|
ensureBufferCapacity(len+1, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return *this;
|
|
|
|
}
|
2016-09-20 01:10:36 +00:00
|
|
|
uprv_memcpy(buffer + 1, list, (size_t)len*sizeof(UChar32));
|
2002-04-25 23:39:57 +00:00
|
|
|
buffer[0] = UNICODESET_LOW;
|
|
|
|
++len;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
swapBuffers();
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2002-04-25 23:39:57 +00:00
|
|
|
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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (s.length() == 0 || isFrozen() || isBogus()) return *this;
|
2002-04-25 23:39:57 +00:00
|
|
|
int32_t cp = getSingleCP(s);
|
|
|
|
if (cp < 0) {
|
|
|
|
if (strings->contains((void*) &s)) {
|
|
|
|
strings->removeElement((void*) &s);
|
|
|
|
} else {
|
|
|
|
_add(s);
|
|
|
|
}
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2002-04-25 23:39:57 +00:00
|
|
|
} else {
|
|
|
|
complement((UChar32)cp, (UChar32)cp);
|
2001-12-03 20:50:50 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
2004-11-04 22:05:00 +00:00
|
|
|
}
|
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) {
|
2008-06-17 22:27:18 +00:00
|
|
|
if ( c.len>0 && c.list!=NULL ) {
|
|
|
|
add(c.list, c.len, 0);
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
|
2004-11-04 22:05:00 +00:00
|
|
|
// Add strings in order
|
2008-06-17 22:27:18 +00:00
|
|
|
if ( c.strings!=NULL ) {
|
|
|
|
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);
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
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) {
|
2007-07-19 04:33:20 +00:00
|
|
|
if (isFrozen()) {
|
|
|
|
return *this;
|
|
|
|
}
|
2008-02-08 03:39:40 +00:00
|
|
|
if (list != NULL) {
|
|
|
|
list[0] = UNICODESET_HIGH;
|
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
len = 1;
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2008-02-08 03:39:40 +00:00
|
|
|
if (strings != NULL) {
|
|
|
|
strings->removeAllElements();
|
|
|
|
}
|
|
|
|
if (list != NULL && strings != NULL) {
|
|
|
|
// Remove bogus
|
|
|
|
fFlags = 0;
|
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
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() {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2006-09-29 23:56:11 +00:00
|
|
|
// Delete buffer first to defragment memory less.
|
2007-07-19 04:33:20 +00:00
|
|
|
if (buffer != NULL) {
|
|
|
|
uprv_free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
2006-09-29 23:56:11 +00:00
|
|
|
if (len < capacity) {
|
|
|
|
// Make the capacity equal to len or 1.
|
|
|
|
// We don't want to realloc of 0 size.
|
2008-02-16 11:26:53 +00:00
|
|
|
int32_t newCapacity = len + (len == 0);
|
|
|
|
UChar32* temp = (UChar32*) uprv_realloc(list, sizeof(UChar32) * newCapacity);
|
|
|
|
if (temp) {
|
|
|
|
list = temp;
|
|
|
|
capacity = newCapacity;
|
|
|
|
}
|
|
|
|
// else what the heck happened?! We allocated less memory!
|
|
|
|
// Oh well. We'll keep our original array.
|
2006-09-29 23:56:11 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return *this;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 01:39:06 +00:00
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize constructor.
|
|
|
|
*/
|
|
|
|
UnicodeSet::UnicodeSet(const uint16_t data[], int32_t dataLen, ESerialization serialization, UErrorCode &ec)
|
|
|
|
: len(1), capacity(1+START_EXTRA), list(0), bmpSet(0), buffer(0),
|
|
|
|
bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
|
|
|
|
fFlags(0) {
|
|
|
|
|
|
|
|
if(U_FAILURE(ec)) {
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (serialization != kSerialized)
|
|
|
|
|| (data==NULL)
|
|
|
|
|| (dataLen < 1)) {
|
|
|
|
ec = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocateStrings(ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
2015-09-15 21:42:56 +00:00
|
|
|
|
2015-09-10 01:39:06 +00:00
|
|
|
// bmp?
|
|
|
|
int32_t headerSize = ((data[0]&0x8000)) ?2:1;
|
|
|
|
int32_t bmpLength = (headerSize==1)?data[0]:data[1];
|
|
|
|
|
|
|
|
len = (((data[0]&0x7FFF)-bmpLength)/2)+bmpLength;
|
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf("dataLen %d headerSize %d bmpLen %d len %d. data[0]=%X/%X/%X/%X\n", dataLen,headerSize,bmpLength,len, data[0],data[1],data[2],data[3]);
|
|
|
|
#endif
|
|
|
|
capacity = len+1;
|
|
|
|
list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
|
|
|
|
if(!list || U_FAILURE(ec)) {
|
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// copy bmp
|
|
|
|
int32_t i;
|
|
|
|
for(i = 0; i< bmpLength;i++) {
|
|
|
|
list[i] = data[i+headerSize];
|
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf("<<16@%d[%d] %X\n", i+headerSize, i, list[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// copy smp
|
|
|
|
for(i=bmpLength;i<len;i++) {
|
|
|
|
list[i] = ((UChar32)data[headerSize+bmpLength+(i-bmpLength)*2+0] << 16) +
|
|
|
|
((UChar32)data[headerSize+bmpLength+(i-bmpLength)*2+1]);
|
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf("<<32@%d+[%d] %lX\n", headerSize+bmpLength+i, i, list[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// terminator
|
|
|
|
list[len++]=UNICODESET_HIGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-09-10 01:39:06 +00:00
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf(">> bmpLength%d length%d len%d\n", bmpLength, length, len);
|
|
|
|
#endif
|
2002-05-29 18:36:09 +00:00
|
|
|
/* 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;
|
|
|
|
|
2015-09-10 01:39:06 +00:00
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf("writeHdr\n");
|
|
|
|
#endif
|
2002-05-29 18:36:09 +00:00
|
|
|
*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) {
|
2015-09-10 01:39:06 +00:00
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf("writebmp: %x\n", (int)*p);
|
|
|
|
#endif
|
2002-05-29 18:36:09 +00:00
|
|
|
*dest++=(uint16_t)*p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the supplementary part of the array */
|
|
|
|
for (; i<length; i+=2) {
|
2015-09-10 01:39:06 +00:00
|
|
|
#ifdef DEBUG_SERIALIZE
|
|
|
|
printf("write32: %x\n", (int)*p);
|
|
|
|
#endif
|
2002-05-29 18:36:09 +00:00
|
|
|
*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: Utility methods
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2002-04-25 23:39:57 +00:00
|
|
|
/**
|
|
|
|
* Allocate our strings vector and return TRUE if successful.
|
|
|
|
*/
|
2006-09-29 21:36:59 +00:00
|
|
|
UBool UnicodeSet::allocateStrings(UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-06-03 05:23:57 +00:00
|
|
|
strings = new UVector(uprv_deleteUObject,
|
2006-09-29 21:36:59 +00:00
|
|
|
uhash_compareUnicodeString, 1, status);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (strings == NULL) { // Check for memory allocation error.
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-09-29 21:36:59 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-04-25 23:39:57 +00:00
|
|
|
delete strings;
|
|
|
|
strings = NULL;
|
|
|
|
return FALSE;
|
2008-01-22 17:46:22 +00:00
|
|
|
}
|
2002-04-25 23:39:57 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-12-14 09:23:07 +00:00
|
|
|
void UnicodeSet::ensureCapacity(int32_t newLen, UErrorCode& ec) {
|
2018-08-02 06:10:47 +00:00
|
|
|
if (newLen <= capacity) {
|
2002-02-28 01:42:40 +00:00
|
|
|
return;
|
2018-08-02 06:10:47 +00:00
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
UChar32* temp = (UChar32*) uprv_realloc(list, sizeof(UChar32) * (newLen + GROW_EXTRA));
|
|
|
|
if (temp == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
2018-08-02 06:10:47 +00:00
|
|
|
setToBogus(); // set the object to bogus state if an OOM failure occurred.
|
2008-02-08 03:39:40 +00:00
|
|
|
return;
|
2007-12-14 09:23:07 +00:00
|
|
|
}
|
2000-05-25 19:27:14 +00:00
|
|
|
list = temp;
|
2007-12-14 09:23:07 +00:00
|
|
|
capacity = newLen + GROW_EXTRA;
|
|
|
|
// else we keep the original contents on the memory failure.
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
|
|
|
|
2007-12-14 09:23:07 +00:00
|
|
|
void UnicodeSet::ensureBufferCapacity(int32_t newLen, UErrorCode& ec) {
|
2002-02-28 01:42:40 +00:00
|
|
|
if (buffer != NULL && newLen <= bufferCapacity)
|
|
|
|
return;
|
2007-12-14 09:23:07 +00:00
|
|
|
UChar32* temp = (UChar32*) uprv_realloc(buffer, sizeof(UChar32) * (newLen + GROW_EXTRA));
|
|
|
|
if (temp == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
2008-02-08 03:39:40 +00:00
|
|
|
setToBogus();
|
|
|
|
return;
|
2002-08-09 18:29:37 +00:00
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
buffer = temp;
|
2000-05-25 19:27:14 +00:00
|
|
|
bufferCapacity = newLen + GROW_EXTRA;
|
2007-12-14 09:23:07 +00:00
|
|
|
// else we keep the original contents on the memory failure.
|
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;
|
|
|
|
}
|
|
|
|
|
2008-01-22 17:46:22 +00:00
|
|
|
void UnicodeSet::setToBogus() {
|
|
|
|
clear(); // Remove everything in the set.
|
|
|
|
fFlags = kIsBogus;
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// 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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ensureBufferCapacity(len + otherLen, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
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();
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
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) {
|
2008-06-17 22:27:18 +00:00
|
|
|
if (isFrozen() || isBogus() || other==NULL) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ensureBufferCapacity(len + otherLen, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
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();
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
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) {
|
2008-02-08 03:39:40 +00:00
|
|
|
if (isFrozen() || isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-12-14 09:23:07 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ensureBufferCapacity(len + otherLen, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-05-25 19:27:14 +00:00
|
|
|
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();
|
2006-10-03 17:41:23 +00:00
|
|
|
releasePattern();
|
2000-05-25 19:27:14 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2004-10-15 22:56:26 +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
|
|
|
|
escapeUnprintable) {
|
|
|
|
UChar32 cp;
|
2011-07-27 05:53:56 +00:00
|
|
|
for (int32_t i = 0; i < s.length(); i += U16_LENGTH(cp)) {
|
2004-10-15 22:56:26 +00:00
|
|
|
_appendToPat(buf, cp = s.char32At(i), escapeUnprintable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the <code>toPattern()</code> representation of a
|
|
|
|
* character to the given <code>StringBuffer</code>.
|
|
|
|
*/
|
|
|
|
void UnicodeSet::_appendToPat(UnicodeString& buf, UChar32 c, UBool
|
|
|
|
escapeUnprintable) {
|
|
|
|
if (escapeUnprintable && ICU_Utility::isUnprintable(c)) {
|
|
|
|
// Use hex escape notation (\uxxxx or \Uxxxxxxxx) for anything
|
|
|
|
// unprintable
|
|
|
|
if (ICU_Utility::escapeUnprintable(buf, c)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Okay to let ':' pass through
|
|
|
|
switch (c) {
|
|
|
|
case SET_OPEN:
|
|
|
|
case SET_CLOSE:
|
|
|
|
case HYPHEN:
|
|
|
|
case COMPLEMENT:
|
|
|
|
case INTERSECTION:
|
|
|
|
case BACKSLASH:
|
|
|
|
case OPEN_BRACE:
|
|
|
|
case CLOSE_BRACE:
|
|
|
|
case COLON:
|
|
|
|
case SymbolTable::SYMBOL_REF:
|
|
|
|
buf.append(BACKSLASH);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Escape whitespace
|
2011-04-25 20:47:32 +00:00
|
|
|
if (PatternProps::isWhiteSpace(c)) {
|
2004-10-15 22:56:26 +00:00
|
|
|
buf.append(BACKSLASH);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf.append(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2006-10-03 17:41:23 +00:00
|
|
|
UBool escapeUnprintable) const
|
|
|
|
{
|
|
|
|
if (pat != NULL) {
|
2004-10-15 22:56:26 +00:00
|
|
|
int32_t i;
|
|
|
|
int32_t backslashCount = 0;
|
2006-10-03 17:41:23 +00:00
|
|
|
for (i=0; i<patLen; ) {
|
|
|
|
UChar32 c;
|
|
|
|
U16_NEXT(pat, i, patLen, c);
|
2004-10-15 22:56:26 +00:00
|
|
|
if (escapeUnprintable && ICU_Utility::isUnprintable(c)) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
ICU_Utility::escapeUnprintable(result, c);
|
|
|
|
backslashCount = 0;
|
|
|
|
} else {
|
|
|
|
result.append(c);
|
|
|
|
if (c == BACKSLASH) {
|
|
|
|
++backslashCount;
|
|
|
|
} else {
|
|
|
|
backslashCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2004-10-15 22:56:26 +00:00
|
|
|
return _generatePattern(result, escapeUnprintable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
UnicodeString& UnicodeSet::toPattern(UnicodeString& result,
|
2006-10-03 17:41:23 +00:00
|
|
|
UBool escapeUnprintable) const
|
|
|
|
{
|
2004-10-15 22:56:26 +00:00
|
|
|
result.truncate(0);
|
|
|
|
return _toPattern(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,
|
2006-10-03 17:41:23 +00:00
|
|
|
UBool escapeUnprintable) const
|
|
|
|
{
|
2004-10-15 22:56:26 +00:00
|
|
|
result.append(SET_OPEN);
|
|
|
|
|
|
|
|
// // 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);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
int32_t count = getRangeCount();
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if ((start+1) != 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) {
|
|
|
|
if ((start+1) != end) {
|
|
|
|
result.append(HYPHEN);
|
|
|
|
}
|
|
|
|
_appendToPat(result, end, escapeUnprintable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-11-04 22:05:00 +00:00
|
|
|
|
2004-10-15 22:56:26 +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);
|
|
|
|
}
|
|
|
|
return result.append(SET_CLOSE);
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:41:23 +00:00
|
|
|
/**
|
|
|
|
* Release existing cached pattern
|
|
|
|
*/
|
|
|
|
void UnicodeSet::releasePattern() {
|
|
|
|
if (pat) {
|
|
|
|
uprv_free(pat);
|
|
|
|
pat = NULL;
|
|
|
|
patLen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the new pattern to cache.
|
|
|
|
*/
|
|
|
|
void UnicodeSet::setPattern(const UnicodeString& newPat) {
|
|
|
|
releasePattern();
|
2006-10-03 19:11:36 +00:00
|
|
|
int32_t newPatLen = newPat.length();
|
|
|
|
pat = (UChar *)uprv_malloc((newPatLen + 1) * sizeof(UChar));
|
|
|
|
if (pat) {
|
|
|
|
patLen = newPatLen;
|
|
|
|
newPat.extractBetween(0, patLen, pat);
|
|
|
|
pat[patLen] = 0;
|
|
|
|
}
|
|
|
|
// else we don't care if malloc failed. This was just a nice cache.
|
|
|
|
// We can regenerate an equivalent pattern later when requested.
|
2006-10-03 17:41:23 +00:00
|
|
|
}
|
2004-10-15 22:56:26 +00:00
|
|
|
|
2007-07-19 04:33:20 +00:00
|
|
|
UnicodeFunctor *UnicodeSet::freeze() {
|
2008-02-08 03:39:40 +00:00
|
|
|
if(!isFrozen() && !isBogus()) {
|
2007-07-19 04:33:20 +00:00
|
|
|
// Do most of what compact() does before freezing because
|
|
|
|
// compact() will not work when the set is frozen.
|
|
|
|
// Small modification: Don't shrink if the savings would be tiny (<=GROW_EXTRA).
|
|
|
|
|
|
|
|
// Delete buffer first to defragment memory less.
|
|
|
|
if (buffer != NULL) {
|
|
|
|
uprv_free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
if (capacity > (len + GROW_EXTRA)) {
|
|
|
|
// Make the capacity equal to len or 1.
|
|
|
|
// We don't want to realloc of 0 size.
|
|
|
|
capacity = len + (len == 0);
|
|
|
|
list = (UChar32*) uprv_realloc(list, sizeof(UChar32) * capacity);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (list == NULL) { // Check for memory allocation error.
|
|
|
|
setToBogus();
|
|
|
|
return this;
|
|
|
|
}
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize contains() and span() and similar functions.
|
|
|
|
if (!strings->isEmpty()) {
|
|
|
|
stringSpan = new UnicodeSetStringSpan(*this, *strings, UnicodeSetStringSpan::ALL);
|
|
|
|
if (stringSpan != NULL && !stringSpan->needsStringSpanUTF16()) {
|
|
|
|
// All strings are irrelevant for span() etc. because
|
|
|
|
// all of each string's code points are contained in this set.
|
|
|
|
// Do not check needsStringSpanUTF8() because UTF-8 has at most as
|
|
|
|
// many relevant strings as UTF-16.
|
|
|
|
// (Thus needsStringSpanUTF8() implies needsStringSpanUTF16().)
|
|
|
|
delete stringSpan;
|
|
|
|
stringSpan = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stringSpan == NULL) {
|
|
|
|
// No span-relevant strings: Optimize for code point spans.
|
|
|
|
bmpSet=new BMPSet(list, len);
|
2008-01-22 17:46:22 +00:00
|
|
|
if (bmpSet == NULL) { // Check for memory allocation error.
|
|
|
|
setToBogus();
|
|
|
|
}
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t UnicodeSet::span(const UChar *s, int32_t length, USetSpanCondition spanCondition) const {
|
|
|
|
if(length>0 && bmpSet!=NULL) {
|
|
|
|
return (int32_t)(bmpSet->span(s, s+length, spanCondition)-s);
|
|
|
|
}
|
|
|
|
if(length<0) {
|
|
|
|
length=u_strlen(s);
|
|
|
|
}
|
|
|
|
if(length==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(stringSpan!=NULL) {
|
|
|
|
return stringSpan->span(s, length, spanCondition);
|
|
|
|
} else if(!strings->isEmpty()) {
|
|
|
|
uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
|
|
|
|
UnicodeSetStringSpan::FWD_UTF16_NOT_CONTAINED :
|
|
|
|
UnicodeSetStringSpan::FWD_UTF16_CONTAINED;
|
|
|
|
UnicodeSetStringSpan strSpan(*this, *strings, which);
|
|
|
|
if(strSpan.needsStringSpanUTF16()) {
|
|
|
|
return strSpan.span(s, length, spanCondition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(spanCondition!=USET_SPAN_NOT_CONTAINED) {
|
|
|
|
spanCondition=USET_SPAN_CONTAINED; // Pin to 0/1 values.
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
int32_t start=0, prev=0;
|
|
|
|
do {
|
|
|
|
U16_NEXT(s, start, length, c);
|
|
|
|
if(spanCondition!=contains(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while((prev=start)<length);
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t UnicodeSet::spanBack(const UChar *s, int32_t length, USetSpanCondition spanCondition) const {
|
|
|
|
if(length>0 && bmpSet!=NULL) {
|
|
|
|
return (int32_t)(bmpSet->spanBack(s, s+length, spanCondition)-s);
|
|
|
|
}
|
|
|
|
if(length<0) {
|
|
|
|
length=u_strlen(s);
|
|
|
|
}
|
|
|
|
if(length==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(stringSpan!=NULL) {
|
|
|
|
return stringSpan->spanBack(s, length, spanCondition);
|
|
|
|
} else if(!strings->isEmpty()) {
|
|
|
|
uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
|
|
|
|
UnicodeSetStringSpan::BACK_UTF16_NOT_CONTAINED :
|
|
|
|
UnicodeSetStringSpan::BACK_UTF16_CONTAINED;
|
|
|
|
UnicodeSetStringSpan strSpan(*this, *strings, which);
|
|
|
|
if(strSpan.needsStringSpanUTF16()) {
|
|
|
|
return strSpan.spanBack(s, length, spanCondition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(spanCondition!=USET_SPAN_NOT_CONTAINED) {
|
|
|
|
spanCondition=USET_SPAN_CONTAINED; // Pin to 0/1 values.
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
int32_t prev=length;
|
|
|
|
do {
|
|
|
|
U16_PREV(s, 0, length, c);
|
|
|
|
if(spanCondition!=contains(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while((prev=length)>0);
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t UnicodeSet::spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const {
|
|
|
|
if(length>0 && bmpSet!=NULL) {
|
|
|
|
const uint8_t *s0=(const uint8_t *)s;
|
|
|
|
return (int32_t)(bmpSet->spanUTF8(s0, length, spanCondition)-s0);
|
|
|
|
}
|
|
|
|
if(length<0) {
|
2009-11-11 15:47:22 +00:00
|
|
|
length=(int32_t)uprv_strlen(s);
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
|
|
|
if(length==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(stringSpan!=NULL) {
|
|
|
|
return stringSpan->spanUTF8((const uint8_t *)s, length, spanCondition);
|
|
|
|
} else if(!strings->isEmpty()) {
|
|
|
|
uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
|
|
|
|
UnicodeSetStringSpan::FWD_UTF8_NOT_CONTAINED :
|
|
|
|
UnicodeSetStringSpan::FWD_UTF8_CONTAINED;
|
|
|
|
UnicodeSetStringSpan strSpan(*this, *strings, which);
|
|
|
|
if(strSpan.needsStringSpanUTF8()) {
|
|
|
|
return strSpan.spanUTF8((const uint8_t *)s, length, spanCondition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(spanCondition!=USET_SPAN_NOT_CONTAINED) {
|
|
|
|
spanCondition=USET_SPAN_CONTAINED; // Pin to 0/1 values.
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
int32_t start=0, prev=0;
|
|
|
|
do {
|
2012-11-11 06:14:18 +00:00
|
|
|
U8_NEXT_OR_FFFD(s, start, length, c);
|
2007-07-19 04:33:20 +00:00
|
|
|
if(spanCondition!=contains(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while((prev=start)<length);
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t UnicodeSet::spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const {
|
|
|
|
if(length>0 && bmpSet!=NULL) {
|
|
|
|
const uint8_t *s0=(const uint8_t *)s;
|
|
|
|
return bmpSet->spanBackUTF8(s0, length, spanCondition);
|
|
|
|
}
|
|
|
|
if(length<0) {
|
2009-11-11 15:47:22 +00:00
|
|
|
length=(int32_t)uprv_strlen(s);
|
2007-07-19 04:33:20 +00:00
|
|
|
}
|
|
|
|
if(length==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(stringSpan!=NULL) {
|
|
|
|
return stringSpan->spanBackUTF8((const uint8_t *)s, length, spanCondition);
|
|
|
|
} else if(!strings->isEmpty()) {
|
|
|
|
uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
|
|
|
|
UnicodeSetStringSpan::BACK_UTF8_NOT_CONTAINED :
|
|
|
|
UnicodeSetStringSpan::BACK_UTF8_CONTAINED;
|
|
|
|
UnicodeSetStringSpan strSpan(*this, *strings, which);
|
|
|
|
if(strSpan.needsStringSpanUTF8()) {
|
|
|
|
return strSpan.spanBackUTF8((const uint8_t *)s, length, spanCondition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(spanCondition!=USET_SPAN_NOT_CONTAINED) {
|
|
|
|
spanCondition=USET_SPAN_CONTAINED; // Pin to 0/1 values.
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
int32_t prev=length;
|
|
|
|
do {
|
2012-11-11 06:14:18 +00:00
|
|
|
U8_PREV_OR_FFFD(s, 0, length, c);
|
2007-07-19 04:33:20 +00:00
|
|
|
if(spanCondition!=contains(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while((prev=length)>0);
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
2002-10-30 18:24:00 +00:00
|
|
|
U_NAMESPACE_END
|