2002-02-13 19:24:02 +00:00
|
|
|
/*
|
2002-03-29 20:10:05 +00:00
|
|
|
*****************************************************************************
|
2004-08-26 22:51:40 +00:00
|
|
|
* Copyright (C) 1996-2004, International Business Machines Corporation and *
|
2002-03-29 20:10:05 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*****************************************************************************
|
2002-02-13 19:24:02 +00:00
|
|
|
*/
|
|
|
|
|
2003-05-06 01:22:23 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
|
2002-05-29 18:36:09 +00:00
|
|
|
#include "unicode/uset.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "hash.h"
|
2002-03-11 17:48:06 +00:00
|
|
|
#include "unormimp.h"
|
2002-09-17 05:06:04 +00:00
|
|
|
#include "unicode/caniter.h"
|
2002-08-21 21:33:16 +00:00
|
|
|
#include "unicode/normlzr.h"
|
|
|
|
#include "unicode/uchar.h"
|
2002-03-20 05:08:57 +00:00
|
|
|
#include "cmemory.h"
|
2002-02-13 19:24:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class allows one to iterate through all the strings that are canonically equivalent to a given
|
|
|
|
* string. For example, here are some sample results:
|
|
|
|
Results for: {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
1: \u0041\u030A\u0064\u0307\u0327
|
|
|
|
= {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
2: \u0041\u030A\u0064\u0327\u0307
|
|
|
|
= {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
|
|
|
|
3: \u0041\u030A\u1E0B\u0327
|
|
|
|
= {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
4: \u0041\u030A\u1E11\u0307
|
|
|
|
= {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
|
|
|
|
5: \u00C5\u0064\u0307\u0327
|
|
|
|
= {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
6: \u00C5\u0064\u0327\u0307
|
|
|
|
= {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
|
|
|
|
7: \u00C5\u1E0B\u0327
|
|
|
|
= {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
8: \u00C5\u1E11\u0307
|
|
|
|
= {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
|
|
|
|
9: \u212B\u0064\u0307\u0327
|
|
|
|
= {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
10: \u212B\u0064\u0327\u0307
|
|
|
|
= {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
|
|
|
|
11: \u212B\u1E0B\u0327
|
|
|
|
= {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
|
|
|
|
12: \u212B\u1E11\u0307
|
|
|
|
= {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
|
|
|
|
*<br>Note: the code is intended for use with small strings, and is not suitable for larger ones,
|
|
|
|
* since it has not been optimized for that situation.
|
|
|
|
*@author M. Davis
|
|
|
|
*@draft
|
|
|
|
*/
|
2002-02-27 21:47:05 +00:00
|
|
|
#if 0
|
2002-02-13 19:24:02 +00:00
|
|
|
static UBool PROGRESS = FALSE;
|
|
|
|
|
2002-03-19 07:16:01 +00:00
|
|
|
#include <stdio.h>
|
2002-02-13 19:24:02 +00:00
|
|
|
#include "unicode/translit.h"
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
2002-04-02 01:25:44 +00:00
|
|
|
// Just for testing - remove, not thread safe.
|
2002-02-13 19:24:02 +00:00
|
|
|
static const char* UToS(const UnicodeString &source) {
|
|
|
|
static char buffer[256];
|
|
|
|
buffer[source.extract(0, source.length(), buffer)] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const UnicodeString &Tr(const UnicodeString &source) {
|
|
|
|
static Transliterator *NAME = Transliterator::createInstance("name", UTRANS_FORWARD, status);
|
|
|
|
static UnicodeString result;
|
|
|
|
result = source;
|
|
|
|
NAME->transliterate(result);
|
|
|
|
return result;
|
|
|
|
}
|
2002-02-27 21:47:05 +00:00
|
|
|
#endif
|
2002-02-13 19:24:02 +00:00
|
|
|
// public
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2002-03-19 07:16:01 +00:00
|
|
|
// TODO: add boilerplate methods.
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CanonicalIterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
/**
|
|
|
|
*@param source string to get results for
|
|
|
|
*/
|
2002-09-17 05:06:04 +00:00
|
|
|
CanonicalIterator::CanonicalIterator(const UnicodeString &sourceStr, UErrorCode &status) :
|
2002-02-13 19:24:02 +00:00
|
|
|
pieces(NULL),
|
2002-04-04 02:45:18 +00:00
|
|
|
pieces_length(0),
|
2002-02-13 19:24:02 +00:00
|
|
|
pieces_lengths(NULL),
|
2002-03-27 06:27:19 +00:00
|
|
|
current(NULL),
|
2002-04-04 02:45:18 +00:00
|
|
|
current_length(0)
|
2002-02-13 19:24:02 +00:00
|
|
|
{
|
2002-03-27 06:27:19 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
2002-03-29 20:10:05 +00:00
|
|
|
setSource(sourceStr, status);
|
2002-03-27 06:27:19 +00:00
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CanonicalIterator::~CanonicalIterator() {
|
|
|
|
cleanPieces();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanonicalIterator::cleanPieces() {
|
2004-12-23 19:51:08 +00:00
|
|
|
int32_t i = 0;
|
|
|
|
if(pieces != NULL) {
|
|
|
|
for(i = 0; i < pieces_length; i++) {
|
|
|
|
if(pieces[i] != NULL) {
|
|
|
|
delete[] pieces[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uprv_free(pieces);
|
|
|
|
pieces = NULL;
|
|
|
|
if(pieces_lengths != NULL) {
|
|
|
|
uprv_free(pieces_lengths);
|
|
|
|
}
|
|
|
|
pieces_lengths = NULL;
|
|
|
|
if(current != NULL) {
|
|
|
|
uprv_free(current);
|
|
|
|
}
|
|
|
|
current = NULL;
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*@return gets the source: NOTE: it is the NFD form of source
|
|
|
|
*/
|
|
|
|
UnicodeString CanonicalIterator::getSource() {
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the iterator so that one can start again from the beginning.
|
|
|
|
*/
|
|
|
|
void CanonicalIterator::reset() {
|
2002-03-12 23:21:52 +00:00
|
|
|
done = FALSE;
|
2002-02-13 19:24:02 +00:00
|
|
|
for (int i = 0; i < current_length; ++i) {
|
|
|
|
current[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*@return the next string that is canonically equivalent. The value null is returned when
|
|
|
|
* the iteration is done.
|
|
|
|
*/
|
|
|
|
UnicodeString CanonicalIterator::next() {
|
2002-04-02 01:25:44 +00:00
|
|
|
int32_t i = 0;
|
2002-09-17 05:06:04 +00:00
|
|
|
|
|
|
|
if (done) {
|
|
|
|
buffer.setToBogus();
|
|
|
|
return buffer;
|
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-11-16 01:10:52 +00:00
|
|
|
// delete old contents
|
2002-11-19 02:12:39 +00:00
|
|
|
buffer.remove();
|
2002-11-16 01:10:52 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// construct return value
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
for (i = 0; i < pieces_length; ++i) {
|
|
|
|
buffer.append(pieces[i][current[i]]);
|
|
|
|
}
|
|
|
|
//String result = buffer.toString(); // not needed
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// find next value for next time
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
for (i = current_length - 1; ; --i) {
|
|
|
|
if (i < 0) {
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current[i]++;
|
|
|
|
if (current[i] < pieces_lengths[i]) break; // got sequence
|
|
|
|
current[i] = 0;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*@param set the source string to iterate against. This allows the same iterator to be used
|
|
|
|
* while changing the source string, saving object creation.
|
|
|
|
*/
|
2002-03-19 07:16:01 +00:00
|
|
|
void CanonicalIterator::setSource(const UnicodeString &newSource, UErrorCode &status) {
|
2004-12-23 19:51:08 +00:00
|
|
|
int32_t list_length = 0;
|
|
|
|
UChar32 cp = 0;
|
|
|
|
int32_t start = 0;
|
|
|
|
int32_t i = 0;
|
|
|
|
UnicodeString *list = NULL;
|
|
|
|
|
2003-06-06 05:05:22 +00:00
|
|
|
Normalizer::normalize(newSource, UNORM_NFD, 0, source, status);
|
2002-03-27 06:27:19 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
done = FALSE;
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
cleanPieces();
|
|
|
|
|
2002-03-16 06:15:58 +00:00
|
|
|
// catch degenerate case
|
|
|
|
if (newSource.length() == 0) {
|
2002-07-16 16:30:15 +00:00
|
|
|
pieces = (UnicodeString **)uprv_malloc(sizeof(UnicodeString *));
|
2004-12-28 05:42:37 +00:00
|
|
|
pieces_lengths = (int32_t*)uprv_malloc(1 * sizeof(int32_t));
|
|
|
|
pieces_length = 1;
|
2002-04-02 01:47:07 +00:00
|
|
|
current = (int32_t*)uprv_malloc(1 * sizeof(int32_t));
|
2004-12-28 05:42:37 +00:00
|
|
|
current_length = 1;
|
|
|
|
if (pieces == NULL || pieces_lengths == NULL || current == NULL) {
|
2002-06-29 09:31:05 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2004-12-23 19:51:08 +00:00
|
|
|
goto CleanPartialInitialization;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
2002-03-16 06:15:58 +00:00
|
|
|
current[0] = 0;
|
2002-04-02 01:25:44 +00:00
|
|
|
pieces[0] = new UnicodeString[1];
|
2004-12-28 05:42:37 +00:00
|
|
|
pieces_lengths[0] = 1;
|
2002-06-29 09:31:05 +00:00
|
|
|
if (pieces[0] == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2004-12-23 19:51:08 +00:00
|
|
|
goto CleanPartialInitialization;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
return;
|
2002-03-16 06:15:58 +00:00
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-03-16 06:15:58 +00:00
|
|
|
|
2004-12-23 19:51:08 +00:00
|
|
|
list = new UnicodeString[source.length()];
|
2002-06-29 09:31:05 +00:00
|
|
|
if (list == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2004-12-23 19:51:08 +00:00
|
|
|
goto CleanPartialInitialization;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
|
|
|
|
2002-03-12 20:19:34 +00:00
|
|
|
// i should initialy be the number of code units at the
|
|
|
|
// start of the string
|
2004-12-23 19:51:08 +00:00
|
|
|
i = UTF16_CHAR_LENGTH(source.char32At(0));
|
2002-03-12 20:19:34 +00:00
|
|
|
//int32_t i = 1;
|
2002-02-27 21:47:05 +00:00
|
|
|
// find the segments
|
|
|
|
// This code iterates through the source string and
|
|
|
|
// extracts segments that end up on a codepoint that
|
|
|
|
// doesn't start any decompositions. (Analysis is done
|
|
|
|
// on the NFD form - see above).
|
2002-02-13 19:24:02 +00:00
|
|
|
for (; i < source.length(); i += UTF16_CHAR_LENGTH(cp)) {
|
|
|
|
cp = source.char32At(i);
|
2002-03-11 17:48:06 +00:00
|
|
|
if (unorm_isCanonSafeStart(cp)) {
|
2002-03-12 20:19:34 +00:00
|
|
|
source.extract(start, i-start, list[list_length++]); // add up to i
|
2002-02-13 19:24:02 +00:00
|
|
|
start = i;
|
|
|
|
}
|
|
|
|
}
|
2002-03-12 20:19:34 +00:00
|
|
|
source.extract(start, i-start, list[list_length++]); // add last one
|
2002-02-13 19:24:02 +00:00
|
|
|
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// allocate the arrays, and find the strings that are CE to each segment
|
2002-07-16 16:30:15 +00:00
|
|
|
pieces = (UnicodeString **)uprv_malloc(list_length * sizeof(UnicodeString *));
|
2002-02-13 19:24:02 +00:00
|
|
|
pieces_length = list_length;
|
2002-04-02 01:47:07 +00:00
|
|
|
pieces_lengths = (int32_t*)uprv_malloc(list_length * sizeof(int32_t));
|
|
|
|
current = (int32_t*)uprv_malloc(list_length * sizeof(int32_t));
|
2004-12-28 05:42:37 +00:00
|
|
|
current_length = list_length;
|
|
|
|
if (pieces == NULL || pieces_lengths == NULL || current == NULL) {
|
2002-06-29 09:31:05 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2004-12-23 19:51:08 +00:00
|
|
|
goto CleanPartialInitialization;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
2004-12-28 05:42:37 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
for (i = 0; i < current_length; i++) {
|
2004-12-28 05:42:37 +00:00
|
|
|
current[i] = 0;
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
2002-02-27 21:47:05 +00:00
|
|
|
// for each segment, get all the combinations that can produce
|
|
|
|
// it after NFD normalization
|
2002-02-13 19:24:02 +00:00
|
|
|
for (i = 0; i < pieces_length; ++i) {
|
2002-02-27 21:47:05 +00:00
|
|
|
//if (PROGRESS) printf("SEGMENT\n");
|
2002-02-13 19:24:02 +00:00
|
|
|
pieces[i] = getEquivalents(list[i], pieces_lengths[i], status);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] list;
|
2004-12-23 19:51:08 +00:00
|
|
|
return;
|
|
|
|
// Common section to cleanup all local variables and reset object variables.
|
|
|
|
CleanPartialInitialization:
|
2004-12-28 05:42:37 +00:00
|
|
|
if (list != NULL) {
|
|
|
|
delete[] list;
|
|
|
|
}
|
2004-12-23 19:51:08 +00:00
|
|
|
if (pieces != NULL) {
|
|
|
|
uprv_free(pieces);
|
|
|
|
}
|
|
|
|
pieces = NULL;
|
|
|
|
pieces_length = 0;
|
|
|
|
if(pieces_lengths != NULL) {
|
|
|
|
uprv_free(pieces_lengths);
|
|
|
|
}
|
|
|
|
pieces_lengths = NULL;
|
|
|
|
if(current != NULL) {
|
|
|
|
uprv_free(current);
|
|
|
|
}
|
|
|
|
current = NULL;
|
|
|
|
current_length = 0;
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-04-02 01:25:44 +00:00
|
|
|
* Dumb recursive implementation of permutation.
|
2002-02-13 19:24:02 +00:00
|
|
|
* TODO: optimize
|
|
|
|
* @param source the string to find permutations for
|
|
|
|
* @return the results in a set.
|
|
|
|
*/
|
2004-08-24 17:21:14 +00:00
|
|
|
void U_EXPORT2 CanonicalIterator::permute(UnicodeString &source, UBool skipZeros, Hashtable *result, UErrorCode &status) {
|
2002-03-27 06:27:19 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
//if (PROGRESS) printf("Permute: %s\n", UToS(Tr(source)));
|
|
|
|
int32_t i = 0;
|
2002-02-27 21:47:05 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// optimization:
|
|
|
|
// if zero or one character, just return a set with it
|
|
|
|
// we check for length < 2 to keep from counting code points all the time
|
2002-03-19 07:16:01 +00:00
|
|
|
if (source.length() <= 2 && source.countChar32() <= 1) {
|
2002-02-13 19:24:02 +00:00
|
|
|
UnicodeString *toPut = new UnicodeString(source);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (toPut == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
result->put(source, toPut, status);
|
2002-03-19 07:16:01 +00:00
|
|
|
return;
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// otherwise iterate through the string, and recursively permute all the other characters
|
|
|
|
UChar32 cp;
|
2004-10-08 01:52:53 +00:00
|
|
|
Hashtable *subpermute = new Hashtable(status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (subpermute == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 22:10:46 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
subpermute->setValueDeleter(uhash_deleteUnicodeString);
|
|
|
|
}
|
2002-03-19 07:16:01 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
for (i = 0; i < source.length(); i += UTF16_CHAR_LENGTH(cp)) {
|
|
|
|
cp = source.char32At(i);
|
|
|
|
const UHashElement *ne = NULL;
|
|
|
|
int32_t el = -1;
|
|
|
|
UnicodeString subPermuteString = source;
|
2002-03-19 07:16:01 +00:00
|
|
|
|
|
|
|
// optimization:
|
|
|
|
// if the character is canonical combining class zero,
|
|
|
|
// don't permute it
|
|
|
|
if (skipZeros && i != 0 && u_getCombiningClass(cp) == 0) {
|
2002-04-02 01:25:44 +00:00
|
|
|
//System.out.println("Skipping " + Utility.hex(UTF16.valueOf(source, i)));
|
|
|
|
continue;
|
|
|
|
}
|
2002-03-19 07:16:01 +00:00
|
|
|
|
|
|
|
subpermute->removeAll();
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// see what the permutations of the characters before and after this one are
|
|
|
|
//Hashtable *subpermute = permute(source.substring(0,i) + source.substring(i + UTF16.getCharCount(cp)));
|
2002-03-19 07:16:01 +00:00
|
|
|
permute(subPermuteString.replace(i, UTF16_CHAR_LENGTH(cp), NULL, 0), skipZeros, subpermute, status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* Test for buffer overflows */
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
delete subpermute;
|
|
|
|
return;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
// The upper replace is destructive. The question is do we have to make a copy, or we don't care about the contents
|
|
|
|
// of source at this point.
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// prefix this character to all of them
|
|
|
|
ne = subpermute->nextElement(el);
|
|
|
|
while (ne != NULL) {
|
|
|
|
UnicodeString *permRes = (UnicodeString *)(ne->value.pointer);
|
|
|
|
UnicodeString *chStr = new UnicodeString(cp);
|
2002-06-29 09:31:05 +00:00
|
|
|
//test for NULL
|
|
|
|
if (chStr == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete subpermute;
|
|
|
|
return;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
chStr->append(*permRes); //*((UnicodeString *)(ne->value.pointer));
|
|
|
|
//if (PROGRESS) printf(" Piece: %s\n", UToS(*chStr));
|
2002-02-27 21:47:05 +00:00
|
|
|
result->put(*chStr, chStr, status);
|
2002-02-13 19:24:02 +00:00
|
|
|
ne = subpermute->nextElement(el);
|
|
|
|
}
|
|
|
|
}
|
2002-03-19 07:16:01 +00:00
|
|
|
delete subpermute;
|
|
|
|
//return result;
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// privates
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// we have a segment, in NFD. Find all the strings that are canonically equivalent to it.
|
2002-04-02 01:25:44 +00:00
|
|
|
UnicodeString* CanonicalIterator::getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status) {
|
2004-12-30 23:08:43 +00:00
|
|
|
Hashtable result(status);
|
|
|
|
Hashtable permutations(status);
|
|
|
|
if (U_FAILURE(status)) {
|
2002-06-29 09:31:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-12-30 23:08:43 +00:00
|
|
|
result.setValueDeleter(uhash_deleteUnicodeString);
|
|
|
|
permutations.setValueDeleter(uhash_deleteUnicodeString);
|
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
UChar USeg[256];
|
|
|
|
int32_t segLen = segment.extract(USeg, 256, status);
|
|
|
|
Hashtable *basic = getEquivalents2(USeg, segLen, status);
|
|
|
|
//Hashtable *basic = getEquivalents2(segment, segLen, status);
|
2004-12-30 23:08:43 +00:00
|
|
|
if (basic == 0) {
|
2002-06-29 09:31:05 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete basic;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-30 23:08:43 +00:00
|
|
|
|
|
|
|
// now get all the permutations
|
|
|
|
// add only the ones that are canonically equivalent
|
|
|
|
// TODO: optimize by not permuting any class zero.
|
2002-03-19 07:16:01 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
const UHashElement *ne = NULL;
|
|
|
|
int32_t el = -1;
|
|
|
|
//Iterator it = basic.iterator();
|
|
|
|
ne = basic->nextElement(el);
|
2002-04-02 01:25:44 +00:00
|
|
|
//while (it.hasNext())
|
2002-02-13 19:24:02 +00:00
|
|
|
while (ne != NULL) {
|
|
|
|
//String item = (String) it.next();
|
|
|
|
UnicodeString item = *((UnicodeString *)(ne->value.pointer));
|
2002-03-19 07:16:01 +00:00
|
|
|
|
2004-12-30 23:08:43 +00:00
|
|
|
permutations.removeAll();
|
|
|
|
permute(item, CANITER_SKIP_ZEROES, &permutations, status);
|
2002-02-13 19:24:02 +00:00
|
|
|
const UHashElement *ne2 = NULL;
|
|
|
|
int32_t el2 = -1;
|
|
|
|
//Iterator it2 = permutations.iterator();
|
2004-12-30 23:08:43 +00:00
|
|
|
ne2 = permutations.nextElement(el2);
|
2002-04-02 01:25:44 +00:00
|
|
|
//while (it2.hasNext())
|
2002-02-13 19:24:02 +00:00
|
|
|
while (ne2 != NULL) {
|
|
|
|
//String possible = (String) it2.next();
|
2002-03-21 19:37:25 +00:00
|
|
|
//UnicodeString *possible = new UnicodeString(*((UnicodeString *)(ne2->value.pointer)));
|
|
|
|
UnicodeString possible(*((UnicodeString *)(ne2->value.pointer)));
|
2002-02-13 19:24:02 +00:00
|
|
|
UnicodeString attempt;
|
2002-03-21 19:37:25 +00:00
|
|
|
Normalizer::normalize(possible, UNORM_NFD, 0, attempt, status);
|
2002-02-13 19:24:02 +00:00
|
|
|
|
|
|
|
// TODO: check if operator == is semanticaly the same as attempt.equals(segment)
|
|
|
|
if (attempt==segment) {
|
2002-02-27 21:47:05 +00:00
|
|
|
//if (PROGRESS) printf("Adding Permutation: %s\n", UToS(Tr(*possible)));
|
|
|
|
// TODO: use the hashtable just to catch duplicates - store strings directly (somehow).
|
2004-12-30 23:08:43 +00:00
|
|
|
result.put(possible, new UnicodeString(possible), status); //add(possible);
|
2002-02-13 19:24:02 +00:00
|
|
|
} else {
|
2002-02-27 21:47:05 +00:00
|
|
|
//if (PROGRESS) printf("-Skipping Permutation: %s\n", UToS(Tr(*possible)));
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2004-12-30 23:08:43 +00:00
|
|
|
ne2 = permutations.nextElement(el2);
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
ne = basic->nextElement(el);
|
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-07-02 15:10:30 +00:00
|
|
|
/* Test for buffer overflows */
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
delete basic;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
// convert into a String[] to clean up storage
|
|
|
|
//String[] finalResult = new String[result.size()];
|
2003-06-09 21:21:36 +00:00
|
|
|
UnicodeString *finalResult = NULL;
|
|
|
|
int32_t resultCount;
|
2004-12-30 23:08:43 +00:00
|
|
|
if((resultCount = result.count())) {
|
|
|
|
finalResult = new UnicodeString[resultCount];
|
2003-06-09 21:21:36 +00:00
|
|
|
} else {
|
2004-12-30 23:08:43 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2003-06-09 21:21:36 +00:00
|
|
|
}
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (finalResult == 0) {
|
2004-12-30 23:08:43 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
delete basic;
|
|
|
|
return 0;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
//result.toArray(finalResult);
|
|
|
|
result_len = 0;
|
|
|
|
el = -1;
|
2004-12-30 23:08:43 +00:00
|
|
|
ne = result.nextElement(el);
|
2002-02-13 19:24:02 +00:00
|
|
|
while(ne != NULL) {
|
2004-12-30 23:08:43 +00:00
|
|
|
UnicodeString finResult = *((UnicodeString *)(ne->value.pointer));
|
|
|
|
finalResult[result_len++] = finResult;
|
|
|
|
ne = result.nextElement(el);
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-21 19:37:25 +00:00
|
|
|
delete basic;
|
2002-02-13 19:24:02 +00:00
|
|
|
return finalResult;
|
|
|
|
}
|
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
Hashtable *CanonicalIterator::getEquivalents2(const UChar *segment, int32_t segLen, UErrorCode &status) {
|
|
|
|
//Hashtable *CanonicalIterator::getEquivalents2(const UnicodeString &segment, int32_t segLen, UErrorCode &status) {
|
|
|
|
|
2004-10-08 01:52:53 +00:00
|
|
|
Hashtable *result = new Hashtable(status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (result == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-03-21 22:09:03 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
result->setValueDeleter(uhash_deleteUnicodeString);
|
|
|
|
}
|
2002-02-27 21:47:05 +00:00
|
|
|
|
|
|
|
//if (PROGRESS) printf("Adding: %s\n", UToS(Tr(segment)));
|
|
|
|
|
2002-03-21 19:37:25 +00:00
|
|
|
UnicodeString toPut(segment, segLen);
|
|
|
|
|
|
|
|
result->put(toPut, new UnicodeString(toPut), status);
|
2002-02-27 21:47:05 +00:00
|
|
|
|
2002-03-11 17:48:06 +00:00
|
|
|
USerializedSet starts;
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// cycle through all the characters
|
2002-05-29 18:36:09 +00:00
|
|
|
UChar32 cp, end = 0;
|
2002-03-14 23:53:29 +00:00
|
|
|
int32_t i = 0, j;
|
2002-03-20 05:08:57 +00:00
|
|
|
for (i = 0; i < segLen; i += UTF16_CHAR_LENGTH(cp)) {
|
2002-02-13 19:24:02 +00:00
|
|
|
// see if any character is at the start of some decomposition
|
2002-03-20 05:08:57 +00:00
|
|
|
UTF_GET_CHAR(segment, 0, i, segLen, cp);
|
2002-03-11 17:48:06 +00:00
|
|
|
if (!unorm_getCanonStartSet(cp, &starts)) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
// if so, see which decompositions match
|
2002-05-29 18:36:09 +00:00
|
|
|
for(j = 0, cp = end+1; cp <= end || uset_getSerializedRange(&starts, j++, &cp, &end); ++cp) {
|
2002-03-20 05:08:57 +00:00
|
|
|
//Hashtable *remainder = extract(cp, segment, segLen, i, status);
|
|
|
|
Hashtable *remainder = extract(cp, segment, segLen, i, status);
|
2002-02-13 19:24:02 +00:00
|
|
|
if (remainder == NULL) continue;
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// there were some matches, so add all the possibilities to the set.
|
2002-03-20 05:08:57 +00:00
|
|
|
UnicodeString prefix(segment, i);
|
2002-03-19 07:16:01 +00:00
|
|
|
prefix += cp;
|
2002-02-13 19:24:02 +00:00
|
|
|
|
|
|
|
const UHashElement *ne = NULL;
|
|
|
|
int32_t el = -1;
|
|
|
|
ne = remainder->nextElement(el);
|
|
|
|
while (ne != NULL) {
|
|
|
|
UnicodeString item = *((UnicodeString *)(ne->value.pointer));
|
2002-03-19 07:16:01 +00:00
|
|
|
UnicodeString *toAdd = new UnicodeString(prefix);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (toAdd == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete result;
|
|
|
|
delete remainder;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-03-19 07:16:01 +00:00
|
|
|
*toAdd += item;
|
|
|
|
result->put(*toAdd, toAdd, status);
|
2002-02-27 21:47:05 +00:00
|
|
|
|
2002-03-19 07:16:01 +00:00
|
|
|
//if (PROGRESS) printf("Adding: %s\n", UToS(Tr(*toAdd)));
|
2002-02-27 21:47:05 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
ne = remainder->nextElement(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete remainder;
|
|
|
|
}
|
|
|
|
}
|
2002-07-02 15:10:30 +00:00
|
|
|
|
|
|
|
/* Test for buffer overflows */
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if the decomposition of cp2 is at segment starting at segmentPos
|
|
|
|
* (with canonical rearrangment!)
|
|
|
|
* If so, take the remainder, and return the equivalents
|
|
|
|
*/
|
2002-03-20 05:08:57 +00:00
|
|
|
Hashtable *CanonicalIterator::extract(UChar32 comp, const UChar *segment, int32_t segLen, int32_t segmentPos, UErrorCode &status) {
|
|
|
|
//Hashtable *CanonicalIterator::extract(UChar32 comp, const UnicodeString &segment, int32_t segLen, int32_t segmentPos, UErrorCode &status) {
|
2002-02-27 21:47:05 +00:00
|
|
|
//if (PROGRESS) printf(" extract: %s, ", UToS(Tr(UnicodeString(comp))));
|
|
|
|
//if (PROGRESS) printf("%s, %i\n", UToS(Tr(segment)), segmentPos);
|
2002-02-13 19:24:02 +00:00
|
|
|
|
2002-04-02 01:25:44 +00:00
|
|
|
const int32_t bufSize = 256;
|
2002-03-20 05:08:57 +00:00
|
|
|
int32_t bufLen = 0;
|
|
|
|
UChar temp[bufSize];
|
|
|
|
|
2004-09-13 23:37:58 +00:00
|
|
|
int32_t inputLen = 0, decompLen;
|
2004-11-04 06:41:39 +00:00
|
|
|
UChar stackBuffer[4];
|
2004-09-13 23:37:58 +00:00
|
|
|
const UChar *decomp;
|
2002-03-20 18:23:40 +00:00
|
|
|
|
2002-09-30 04:00:17 +00:00
|
|
|
U16_APPEND_UNSAFE(temp, inputLen, comp);
|
2004-11-04 06:41:39 +00:00
|
|
|
decomp = unorm_getCanonicalDecomposition(comp, stackBuffer, &decompLen);
|
2004-09-13 23:37:58 +00:00
|
|
|
if(decomp == NULL) {
|
|
|
|
/* copy temp */
|
2004-11-04 06:41:39 +00:00
|
|
|
stackBuffer[0] = temp[0];
|
2004-09-13 23:37:58 +00:00
|
|
|
if(inputLen > 1) {
|
2004-11-04 06:41:39 +00:00
|
|
|
stackBuffer[1] = temp[1];
|
2004-09-13 23:37:58 +00:00
|
|
|
}
|
2004-11-04 06:41:39 +00:00
|
|
|
decomp = stackBuffer;
|
2004-09-13 23:37:58 +00:00
|
|
|
decompLen = inputLen;
|
2002-03-20 18:23:40 +00:00
|
|
|
}
|
2002-03-20 05:08:57 +00:00
|
|
|
|
|
|
|
UChar *buff = temp+inputLen;
|
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// See if it matches the start of segment (at segmentPos)
|
|
|
|
UBool ok = FALSE;
|
|
|
|
UChar32 cp;
|
|
|
|
int32_t decompPos = 0;
|
2002-03-20 05:08:57 +00:00
|
|
|
UChar32 decompCp;
|
|
|
|
UTF_NEXT_CHAR(decomp, decompPos, decompLen, decompCp);
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-09-30 04:00:17 +00:00
|
|
|
int32_t i;
|
|
|
|
UBool overflow = FALSE;
|
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
i = segmentPos;
|
|
|
|
while(i < segLen) {
|
|
|
|
UTF_NEXT_CHAR(segment, i, segLen, cp);
|
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
if (cp == decompCp) { // if equal, eat another cp from decomp
|
2002-02-27 21:47:05 +00:00
|
|
|
|
|
|
|
//if (PROGRESS) printf(" matches: %s\n", UToS(Tr(UnicodeString(cp))));
|
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
if (decompPos == decompLen) { // done, have all decomp characters!
|
|
|
|
//u_strcat(buff+bufLen, segment+i);
|
2003-06-10 23:05:58 +00:00
|
|
|
uprv_memcpy(buff+bufLen, segment+i, (segLen-i)*sizeof(UChar));
|
2002-03-20 05:08:57 +00:00
|
|
|
bufLen+=segLen-i;
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
ok = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-20 05:08:57 +00:00
|
|
|
UTF_NEXT_CHAR(decomp, decompPos, decompLen, decompCp);
|
2002-02-13 19:24:02 +00:00
|
|
|
} else {
|
2002-02-27 21:47:05 +00:00
|
|
|
//if (PROGRESS) printf(" buffer: %s\n", UToS(Tr(UnicodeString(cp))));
|
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
// brute force approach
|
|
|
|
|
2002-09-30 04:00:17 +00:00
|
|
|
U16_APPEND(buff, bufLen, bufSize, cp, overflow);
|
|
|
|
|
|
|
|
if(overflow) {
|
|
|
|
/*
|
|
|
|
* ### TODO handle buffer overflow
|
|
|
|
* The buffer is large, but an overflow may still happen with
|
|
|
|
* unusual input (many combining marks?).
|
|
|
|
* Reallocate buffer and continue.
|
|
|
|
* markus 20020929
|
|
|
|
*/
|
|
|
|
|
|
|
|
overflow = FALSE;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
|
|
|
|
/* TODO: optimize
|
|
|
|
// since we know that the classes are monotonically increasing, after zero
|
|
|
|
// e.g. 0 5 7 9 0 3
|
|
|
|
// we can do an optimization
|
|
|
|
// there are only a few cases that work: zero, less, same, greater
|
|
|
|
// if both classes are the same, we fail
|
|
|
|
// if the decomp class < the segment class, we fail
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-02-13 19:24:02 +00:00
|
|
|
segClass = getClass(cp);
|
|
|
|
if (decompClass <= segClass) return null;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok) return NULL; // we failed, characters left over
|
2002-02-27 21:47:05 +00:00
|
|
|
|
|
|
|
//if (PROGRESS) printf("Matches\n");
|
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
if (bufLen == 0) {
|
2004-10-08 01:52:53 +00:00
|
|
|
Hashtable *result = new Hashtable(status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (result == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-13 19:24:02 +00:00
|
|
|
result->setValueDeleter(uhash_deleteUnicodeString);
|
2002-11-16 01:10:52 +00:00
|
|
|
result->put(UnicodeString(), new UnicodeString(), status);
|
2002-02-13 19:24:02 +00:00
|
|
|
return result; // succeed, but no remainder
|
|
|
|
}
|
|
|
|
|
|
|
|
// brute force approach
|
|
|
|
// check to make sure result is canonically equivalent
|
2002-03-20 05:08:57 +00:00
|
|
|
int32_t tempLen = inputLen + bufLen;
|
|
|
|
|
|
|
|
UChar trial[bufSize];
|
2003-02-15 01:54:25 +00:00
|
|
|
unorm_decompose(trial, bufSize, temp, tempLen, FALSE, 0, &status);
|
2002-03-20 05:08:57 +00:00
|
|
|
|
2002-07-02 15:10:30 +00:00
|
|
|
/* Test for buffer overflows */
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
if(uprv_memcmp(segment+segmentPos, trial, (segLen - segmentPos)*sizeof(UChar)) != 0) {
|
2002-02-13 19:24:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-04-02 01:25:44 +00:00
|
|
|
|
2002-03-20 05:08:57 +00:00
|
|
|
return getEquivalents2(buff, bufLen, status);
|
2002-02-13 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
U_NAMESPACE_END
|
2003-05-06 01:22:23 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_NORMALIZATION */
|