1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
*************************************************************************
|
1999-11-22 21:59:05 +00:00
|
|
|
* COPYRIGHT:
|
2005-06-22 02:40:25 +00:00
|
|
|
* Copyright (c) 1996-2005, International Business Machines Corporation and
|
1999-11-22 21:59:05 +00:00
|
|
|
* others. All Rights Reserved.
|
2001-03-21 20:44:20 +00:00
|
|
|
*************************************************************************
|
1999-11-22 21:59:05 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-05-06 01:22:23 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/chariter.h"
|
|
|
|
#include "unicode/schriter.h"
|
2001-08-29 23:57:15 +00:00
|
|
|
#include "unicode/uchriter.h"
|
2002-02-09 21:59:27 +00:00
|
|
|
#include "unicode/uiter.h"
|
2001-09-11 01:29:36 +00:00
|
|
|
#include "unicode/normlzr.h"
|
2001-10-19 17:36:02 +00:00
|
|
|
#include "cmemory.h"
|
2001-06-20 22:24:42 +00:00
|
|
|
#include "unormimp.h"
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Normalizer)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Constructors and other boilerplate
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2001-09-25 22:45:26 +00:00
|
|
|
Normalizer::Normalizer(const UnicodeString& str, UNormalizationMode mode) :
|
2002-07-02 23:58:34 +00:00
|
|
|
UObject(), fUMode(mode), fOptions(0),
|
2001-09-25 22:45:26 +00:00
|
|
|
currentIndex(0), nextIndex(0),
|
|
|
|
buffer(), bufferPos(0)
|
|
|
|
{
|
2001-10-19 17:36:02 +00:00
|
|
|
init(new StringCharacterIterator(str));
|
2001-09-25 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Normalizer::Normalizer(const UChar *str, int32_t length, UNormalizationMode mode) :
|
2002-07-02 23:58:34 +00:00
|
|
|
UObject(), fUMode(mode), fOptions(0),
|
2001-09-25 22:45:26 +00:00
|
|
|
currentIndex(0), nextIndex(0),
|
|
|
|
buffer(), bufferPos(0)
|
|
|
|
{
|
2001-10-19 17:36:02 +00:00
|
|
|
init(new UCharCharacterIterator(str, length));
|
2001-09-25 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Normalizer::Normalizer(const CharacterIterator& iter, UNormalizationMode mode) :
|
2002-07-02 23:58:34 +00:00
|
|
|
UObject(), fUMode(mode), fOptions(0),
|
2001-09-25 22:45:26 +00:00
|
|
|
currentIndex(0), nextIndex(0),
|
|
|
|
buffer(), bufferPos(0)
|
|
|
|
{
|
2001-10-19 17:36:02 +00:00
|
|
|
init(iter.clone());
|
2001-09-25 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
Normalizer::Normalizer(const Normalizer ©) :
|
2002-07-02 23:58:34 +00:00
|
|
|
UObject(copy), fUMode(copy.fUMode), fOptions(copy.fOptions),
|
2001-11-09 21:53:33 +00:00
|
|
|
currentIndex(copy.currentIndex), nextIndex(copy.nextIndex),
|
2001-08-29 23:57:15 +00:00
|
|
|
buffer(copy.buffer), bufferPos(copy.bufferPos)
|
2001-08-31 21:37:39 +00:00
|
|
|
{
|
2001-10-19 17:36:02 +00:00
|
|
|
init(((CharacterIterator *)(copy.text->context))->clone());
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const UChar _NUL=0;
|
|
|
|
|
|
|
|
void
|
2001-10-19 17:36:02 +00:00
|
|
|
Normalizer::init(CharacterIterator *iter) {
|
2001-08-31 21:37:39 +00:00
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
2001-10-19 17:36:02 +00:00
|
|
|
|
2002-07-16 01:55:55 +00:00
|
|
|
text=(UCharIterator *)uprv_malloc(sizeof(UCharIterator));
|
|
|
|
if(text!=NULL) {
|
|
|
|
if(unorm_haveData(&errorCode)) {
|
|
|
|
uiter_setCharacterIterator(text, iter);
|
|
|
|
} else {
|
|
|
|
delete iter;
|
|
|
|
uiter_setCharacterIterator(text, new UCharCharacterIterator(&_NUL, 0));
|
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
} else {
|
|
|
|
delete iter;
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
Normalizer::~Normalizer()
|
|
|
|
{
|
2002-07-16 01:55:55 +00:00
|
|
|
if(text!=NULL) {
|
|
|
|
delete (CharacterIterator *)text->context;
|
|
|
|
uprv_free(text);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-08-08 22:15:50 +00:00
|
|
|
Normalizer*
|
1999-08-16 21:50:52 +00:00
|
|
|
Normalizer::clone() const
|
|
|
|
{
|
2000-07-22 00:10:49 +00:00
|
|
|
if(this!=0) {
|
|
|
|
return new Normalizer(*this);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a hash code for this iterator.
|
|
|
|
*/
|
|
|
|
int32_t Normalizer::hashCode() const
|
|
|
|
{
|
2001-10-19 17:36:02 +00:00
|
|
|
return ((CharacterIterator *)(text->context))->hashCode() + fUMode + fOptions + buffer.hashCode() + bufferPos + currentIndex + nextIndex;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool Normalizer::operator==(const Normalizer& that) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-29 23:57:15 +00:00
|
|
|
return
|
|
|
|
this==&that ||
|
2001-09-25 22:45:26 +00:00
|
|
|
fUMode==that.fUMode &&
|
2001-08-29 23:57:15 +00:00
|
|
|
fOptions==that.fOptions &&
|
2001-10-19 17:36:02 +00:00
|
|
|
*((CharacterIterator *)(text->context))==*((CharacterIterator *)(that.text->context)) &&
|
2001-08-29 23:57:15 +00:00
|
|
|
buffer==that.buffer &&
|
|
|
|
bufferPos==that.bufferPos &&
|
|
|
|
nextIndex==that.nextIndex;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Static utility methods
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2004-08-24 17:21:14 +00:00
|
|
|
void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
Normalizer::normalize(const UnicodeString& source,
|
2001-09-25 22:45:26 +00:00
|
|
|
UNormalizationMode mode, int32_t options,
|
2000-07-22 00:10:49 +00:00
|
|
|
UnicodeString& result,
|
2001-08-17 00:21:18 +00:00
|
|
|
UErrorCode &status) {
|
2001-08-29 23:57:15 +00:00
|
|
|
if(source.isBogus() || U_FAILURE(status)) {
|
2001-08-17 00:21:18 +00:00
|
|
|
result.setToBogus();
|
2002-03-12 23:00:16 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
} else {
|
2002-10-05 00:29:14 +00:00
|
|
|
UnicodeString localDest;
|
|
|
|
UnicodeString *dest;
|
|
|
|
|
|
|
|
if(&source!=&result) {
|
|
|
|
dest=&result;
|
|
|
|
} else {
|
|
|
|
// the source and result strings are the same object, use a temporary one
|
|
|
|
dest=&localDest;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar *buffer=dest->getBuffer(source.length());
|
|
|
|
int32_t length=unorm_internalNormalize(buffer, dest->getCapacity(),
|
2001-10-19 17:36:02 +00:00
|
|
|
source.getBuffer(), source.length(),
|
2003-02-15 01:54:25 +00:00
|
|
|
mode, options,
|
2001-08-17 00:21:18 +00:00
|
|
|
&status);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(status) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(status==U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
status=U_ZERO_ERROR;
|
2002-10-05 00:29:14 +00:00
|
|
|
buffer=dest->getBuffer(length);
|
|
|
|
length=unorm_internalNormalize(buffer, dest->getCapacity(),
|
2001-10-19 17:36:02 +00:00
|
|
|
source.getBuffer(), source.length(),
|
2003-02-15 01:54:25 +00:00
|
|
|
mode, options,
|
2001-10-19 17:36:02 +00:00
|
|
|
&status);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(status) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-05 00:29:14 +00:00
|
|
|
if(dest==&localDest) {
|
|
|
|
result=*dest;
|
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
if(U_FAILURE(status)) {
|
2001-06-28 00:34:35 +00:00
|
|
|
result.setToBogus();
|
|
|
|
}
|
|
|
|
}
|
2001-02-03 01:25:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:21:14 +00:00
|
|
|
void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
Normalizer::compose(const UnicodeString& source,
|
2001-09-25 22:45:26 +00:00
|
|
|
UBool compat, int32_t options,
|
2000-07-22 00:10:49 +00:00
|
|
|
UnicodeString& result,
|
2001-08-17 00:21:18 +00:00
|
|
|
UErrorCode &status) {
|
2001-08-30 22:25:26 +00:00
|
|
|
if(source.isBogus() || U_FAILURE(status)) {
|
2001-08-17 00:21:18 +00:00
|
|
|
result.setToBogus();
|
2002-03-12 23:00:16 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
} else {
|
2002-10-05 00:29:14 +00:00
|
|
|
UnicodeString localDest;
|
|
|
|
UnicodeString *dest;
|
|
|
|
|
|
|
|
if(&source!=&result) {
|
|
|
|
dest=&result;
|
|
|
|
} else {
|
|
|
|
// the source and result strings are the same object, use a temporary one
|
|
|
|
dest=&localDest;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar *buffer=dest->getBuffer(source.length());
|
|
|
|
int32_t length=unorm_compose(buffer, dest->getCapacity(),
|
2001-10-19 17:36:02 +00:00
|
|
|
source.getBuffer(), source.length(),
|
2003-02-15 01:54:25 +00:00
|
|
|
compat, options,
|
2001-08-17 00:21:18 +00:00
|
|
|
&status);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(status) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(status==U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
status=U_ZERO_ERROR;
|
2002-10-05 00:29:14 +00:00
|
|
|
buffer=dest->getBuffer(length);
|
|
|
|
length=unorm_compose(buffer, dest->getCapacity(),
|
2001-10-19 17:36:02 +00:00
|
|
|
source.getBuffer(), source.length(),
|
2003-02-15 01:54:25 +00:00
|
|
|
compat, options,
|
2001-10-19 17:36:02 +00:00
|
|
|
&status);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(status) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-05 00:29:14 +00:00
|
|
|
if(dest==&localDest) {
|
|
|
|
result=*dest;
|
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
if(U_FAILURE(status)) {
|
2001-06-28 00:34:35 +00:00
|
|
|
result.setToBogus();
|
2000-07-22 00:10:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-24 17:21:14 +00:00
|
|
|
void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
Normalizer::decompose(const UnicodeString& source,
|
2001-09-25 22:45:26 +00:00
|
|
|
UBool compat, int32_t options,
|
2000-07-22 00:10:49 +00:00
|
|
|
UnicodeString& result,
|
2001-08-17 00:21:18 +00:00
|
|
|
UErrorCode &status) {
|
2001-08-30 22:25:26 +00:00
|
|
|
if(source.isBogus() || U_FAILURE(status)) {
|
2001-08-17 00:21:18 +00:00
|
|
|
result.setToBogus();
|
2002-03-12 23:00:16 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
} else {
|
2002-10-05 00:29:14 +00:00
|
|
|
UnicodeString localDest;
|
|
|
|
UnicodeString *dest;
|
|
|
|
|
|
|
|
if(&source!=&result) {
|
|
|
|
dest=&result;
|
|
|
|
} else {
|
|
|
|
// the source and result strings are the same object, use a temporary one
|
|
|
|
dest=&localDest;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar *buffer=dest->getBuffer(source.length());
|
|
|
|
int32_t length=unorm_decompose(buffer, dest->getCapacity(),
|
2001-10-19 17:36:02 +00:00
|
|
|
source.getBuffer(), source.length(),
|
2003-02-15 01:54:25 +00:00
|
|
|
compat, options,
|
2001-10-19 17:36:02 +00:00
|
|
|
&status);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(status) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(status==U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
status=U_ZERO_ERROR;
|
2002-10-05 00:29:14 +00:00
|
|
|
buffer=dest->getBuffer(length);
|
|
|
|
length=unorm_decompose(buffer, dest->getCapacity(),
|
2001-10-19 17:36:02 +00:00
|
|
|
source.getBuffer(), source.length(),
|
2003-02-15 01:54:25 +00:00
|
|
|
compat, options,
|
2001-10-19 17:36:02 +00:00
|
|
|
&status);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(status) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-05 00:29:14 +00:00
|
|
|
if(dest==&localDest) {
|
|
|
|
result=*dest;
|
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
if(U_FAILURE(status)) {
|
2001-06-20 22:24:42 +00:00
|
|
|
result.setToBogus();
|
2000-07-22 00:10:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-24 17:21:14 +00:00
|
|
|
UnicodeString & U_EXPORT2
|
2002-03-12 23:00:16 +00:00
|
|
|
Normalizer::concatenate(UnicodeString &left, UnicodeString &right,
|
|
|
|
UnicodeString &result,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UErrorCode &errorCode) {
|
|
|
|
if(left.isBogus() || right.isBogus() || U_FAILURE(errorCode)) {
|
|
|
|
result.setToBogus();
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
2002-10-05 00:29:14 +00:00
|
|
|
UnicodeString localDest;
|
|
|
|
UnicodeString *dest;
|
|
|
|
|
|
|
|
if(&left!=&result && &right!=&result) {
|
|
|
|
dest=&result;
|
|
|
|
} else {
|
|
|
|
// the source and result strings are the same object, use a temporary one
|
|
|
|
dest=&localDest;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar *buffer=dest->getBuffer(left.length()+right.length());
|
2002-03-12 23:00:16 +00:00
|
|
|
int32_t length=unorm_concatenate(left.getBuffer(), left.length(),
|
|
|
|
right.getBuffer(), right.length(),
|
2002-10-05 00:29:14 +00:00
|
|
|
buffer, dest->getCapacity(),
|
2002-03-12 23:00:16 +00:00
|
|
|
mode, options,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2002-03-12 23:00:16 +00:00
|
|
|
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
errorCode=U_ZERO_ERROR;
|
2002-10-05 00:29:14 +00:00
|
|
|
buffer=dest->getBuffer(length);
|
2002-03-12 23:00:16 +00:00
|
|
|
int32_t length=unorm_concatenate(left.getBuffer(), left.length(),
|
|
|
|
right.getBuffer(), right.length(),
|
2002-10-05 00:29:14 +00:00
|
|
|
buffer, dest->getCapacity(),
|
2002-03-12 23:00:16 +00:00
|
|
|
mode, options,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
dest->releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2002-03-12 23:00:16 +00:00
|
|
|
}
|
|
|
|
|
2002-10-05 00:29:14 +00:00
|
|
|
if(dest==&localDest) {
|
|
|
|
result=*dest;
|
|
|
|
}
|
2002-03-12 23:00:16 +00:00
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
result.setToBogus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//-------------------------------------------------------------------------
|
2001-08-29 23:57:15 +00:00
|
|
|
// Iteration API
|
1999-08-16 21:50:52 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2002-07-16 01:55:55 +00:00
|
|
|
* Return the current character in the normalized text.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2001-08-29 23:57:15 +00:00
|
|
|
UChar32 Normalizer::current() {
|
2001-09-11 01:29:36 +00:00
|
|
|
if(bufferPos<buffer.length() || nextNormalize()) {
|
2001-08-29 23:57:15 +00:00
|
|
|
return buffer.char32At(bufferPos);
|
|
|
|
} else {
|
2001-09-11 01:29:36 +00:00
|
|
|
return DONE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next character in the normalized text and advance
|
|
|
|
* the iteration position by one. If the end
|
|
|
|
* of the text has already been reached, {@link #DONE} is returned.
|
|
|
|
*/
|
2000-05-24 20:56:19 +00:00
|
|
|
UChar32 Normalizer::next() {
|
2001-09-11 01:29:36 +00:00
|
|
|
if(bufferPos<buffer.length() || nextNormalize()) {
|
|
|
|
UChar32 c=buffer.char32At(bufferPos);
|
2001-08-29 23:57:15 +00:00
|
|
|
bufferPos+=UTF_CHAR_LENGTH(c);
|
|
|
|
return c;
|
|
|
|
} else {
|
2001-09-11 01:29:36 +00:00
|
|
|
return DONE;
|
2000-07-22 00:10:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the previous character in the normalized text and decrement
|
|
|
|
* the iteration position by one. If the beginning
|
|
|
|
* of the text has already been reached, {@link #DONE} is returned.
|
|
|
|
*/
|
2001-08-29 23:57:15 +00:00
|
|
|
UChar32 Normalizer::previous() {
|
2001-09-11 01:29:36 +00:00
|
|
|
if(bufferPos>0 || previousNormalize()) {
|
|
|
|
UChar32 c=buffer.char32At(bufferPos-1);
|
2001-08-29 23:57:15 +00:00
|
|
|
bufferPos-=UTF_CHAR_LENGTH(c);
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
return DONE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
void Normalizer::reset() {
|
2002-02-20 02:04:23 +00:00
|
|
|
currentIndex=nextIndex=text->move(text, 0, UITER_START);
|
2001-08-29 23:57:15 +00:00
|
|
|
clearBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-12 01:32:42 +00:00
|
|
|
Normalizer::setIndexOnly(int32_t index) {
|
2002-03-20 17:04:56 +00:00
|
|
|
currentIndex=nextIndex=text->move(text, index, UITER_ZERO); // validates index
|
2001-08-29 23:57:15 +00:00
|
|
|
clearBuffer();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
/**
|
2001-10-19 17:36:02 +00:00
|
|
|
* Return the first character in the normalized text-> This resets
|
|
|
|
* the <tt>Normalizer's</tt> position to the beginning of the text->
|
2001-08-29 23:57:15 +00:00
|
|
|
*/
|
|
|
|
UChar32 Normalizer::first() {
|
2001-09-11 01:29:36 +00:00
|
|
|
reset();
|
2001-08-29 23:57:15 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-10-19 17:36:02 +00:00
|
|
|
* Return the last character in the normalized text-> This resets
|
2001-08-29 23:57:15 +00:00
|
|
|
* the <tt>Normalizer's</tt> position to be just before the
|
|
|
|
* the input text corresponding to that normalized character.
|
|
|
|
*/
|
|
|
|
UChar32 Normalizer::last() {
|
2002-02-20 02:04:23 +00:00
|
|
|
currentIndex=nextIndex=text->move(text, 0, UITER_LIMIT);
|
2001-08-29 23:57:15 +00:00
|
|
|
clearBuffer();
|
|
|
|
return previous();
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the current iteration position in the input text that is
|
|
|
|
* being normalized. This method is useful in applications such as
|
|
|
|
* searching, where you need to be able to determine the position in
|
|
|
|
* the input text that corresponds to a given normalized output character.
|
|
|
|
* <p>
|
|
|
|
* <b>Note:</b> This method sets the position in the <em>input</em>, while
|
|
|
|
* {@link #next} and {@link #previous} iterate through characters in the
|
|
|
|
* <em>output</em>. This means that there is not necessarily a one-to-one
|
|
|
|
* correspondence between characters returned by <tt>next</tt> and
|
|
|
|
* <tt>previous</tt> and the indices passed to and returned from
|
|
|
|
* <tt>setIndex</tt> and {@link #getIndex}.
|
|
|
|
*
|
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t Normalizer::getIndex() const {
|
2001-09-11 01:29:36 +00:00
|
|
|
if(bufferPos<buffer.length()) {
|
|
|
|
return currentIndex;
|
|
|
|
} else {
|
|
|
|
return nextIndex;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-10-19 17:36:02 +00:00
|
|
|
* Retrieve the index of the start of the input text-> This is the begin index
|
1999-08-16 21:50:52 +00:00
|
|
|
* of the <tt>CharacterIterator</tt> or the start (i.e. 0) of the <tt>String</tt>
|
|
|
|
* over which this <tt>Normalizer</tt> is iterating
|
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t Normalizer::startIndex() const {
|
2002-03-20 17:04:56 +00:00
|
|
|
return text->getIndex(text, UITER_START);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-10-19 17:36:02 +00:00
|
|
|
* Retrieve the index of the end of the input text-> This is the end index
|
1999-08-16 21:50:52 +00:00
|
|
|
* of the <tt>CharacterIterator</tt> or the length of the <tt>String</tt>
|
|
|
|
* over which this <tt>Normalizer</tt> is iterating
|
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t Normalizer::endIndex() const {
|
2002-03-20 17:04:56 +00:00
|
|
|
return text->getIndex(text, UITER_LIMIT);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Property access methods
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2001-09-25 22:45:26 +00:00
|
|
|
Normalizer::setMode(UNormalizationMode newMode)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-09-25 22:45:26 +00:00
|
|
|
fUMode = newMode;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-09-25 22:45:26 +00:00
|
|
|
UNormalizationMode
|
|
|
|
Normalizer::getUMode() const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-09-25 22:45:26 +00:00
|
|
|
return fUMode;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Normalizer::setOption(int32_t option,
|
2000-07-22 00:10:49 +00:00
|
|
|
UBool value)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-07-22 00:10:49 +00:00
|
|
|
if (value) {
|
|
|
|
fOptions |= option;
|
|
|
|
} else {
|
|
|
|
fOptions &= (~option);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
Normalizer::getOption(int32_t option) const
|
|
|
|
{
|
|
|
|
return (fOptions & option) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the input text over which this <tt>Normalizer</tt> will iterate.
|
2001-10-19 17:36:02 +00:00
|
|
|
* The iteration position is set to the beginning of the input text->
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Normalizer::setText(const UnicodeString& newText,
|
2000-07-22 00:10:49 +00:00
|
|
|
UErrorCode &status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-07-22 00:10:49 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CharacterIterator *newIter = new StringCharacterIterator(newText);
|
|
|
|
if (newIter == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
delete (CharacterIterator *)(text->context);
|
|
|
|
text->context = newIter;
|
2000-07-22 00:10:49 +00:00
|
|
|
reset();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the input text over which this <tt>Normalizer</tt> will iterate.
|
|
|
|
* The iteration position is set to the beginning of the string.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Normalizer::setText(const CharacterIterator& newText,
|
2000-07-22 00:10:49 +00:00
|
|
|
UErrorCode &status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-07-22 00:10:49 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CharacterIterator *newIter = newText.clone();
|
|
|
|
if (newIter == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
delete (CharacterIterator *)(text->context);
|
|
|
|
text->context = newIter;
|
2000-07-22 00:10:49 +00:00
|
|
|
reset();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-11-22 21:59:05 +00:00
|
|
|
void
|
|
|
|
Normalizer::setText(const UChar* newText,
|
|
|
|
int32_t length,
|
2000-07-22 00:10:49 +00:00
|
|
|
UErrorCode &status)
|
1999-11-22 21:59:05 +00:00
|
|
|
{
|
2001-08-29 23:57:15 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CharacterIterator *newIter = new UCharCharacterIterator(newText, length);
|
|
|
|
if (newIter == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
delete (CharacterIterator *)(text->context);
|
|
|
|
text->context = newIter;
|
2001-08-29 23:57:15 +00:00
|
|
|
reset();
|
1999-11-22 21:59:05 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the text under iteration into the UnicodeString referred to by "result".
|
|
|
|
* @param result Receives a copy of the text under iteration.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Normalizer::getText(UnicodeString& result)
|
|
|
|
{
|
2001-10-19 17:36:02 +00:00
|
|
|
((CharacterIterator *)(text->context))->getText(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Private utility methods
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Normalizer::clearBuffer() {
|
2001-08-29 23:57:15 +00:00
|
|
|
buffer.remove();
|
|
|
|
bufferPos=0;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
UBool
|
|
|
|
Normalizer::nextNormalize() {
|
2001-10-19 17:36:02 +00:00
|
|
|
UChar *p;
|
|
|
|
int32_t length;
|
|
|
|
UErrorCode errorCode;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
clearBuffer();
|
2001-09-11 01:29:36 +00:00
|
|
|
currentIndex=nextIndex;
|
2002-03-20 17:04:56 +00:00
|
|
|
text->move(text, nextIndex, UITER_ZERO);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(!text->hasNext(text)) {
|
2001-09-11 01:29:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-10-19 17:36:02 +00:00
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
p=buffer.getBuffer(-1);
|
2002-02-10 00:11:16 +00:00
|
|
|
length=unorm_next(text, p, buffer.getCapacity(),
|
2003-02-26 01:45:06 +00:00
|
|
|
fUMode, fOptions,
|
2002-02-10 00:11:16 +00:00
|
|
|
TRUE, 0,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
buffer.releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
errorCode=U_ZERO_ERROR;
|
2002-03-20 17:04:56 +00:00
|
|
|
text->move(text, nextIndex, UITER_ZERO);
|
2001-10-19 17:36:02 +00:00
|
|
|
p=buffer.getBuffer(length);
|
2002-02-10 00:11:16 +00:00
|
|
|
length=unorm_next(text, p, buffer.getCapacity(),
|
2003-02-26 01:45:06 +00:00
|
|
|
fUMode, fOptions,
|
2002-02-10 00:11:16 +00:00
|
|
|
TRUE, 0,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
buffer.releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
2002-03-20 17:04:56 +00:00
|
|
|
nextIndex=text->getIndex(text, UITER_CURRENT);
|
2001-10-19 17:36:02 +00:00
|
|
|
return U_SUCCESS(errorCode) && !buffer.isEmpty();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
UBool
|
|
|
|
Normalizer::previousNormalize() {
|
2001-10-19 17:36:02 +00:00
|
|
|
UChar *p;
|
|
|
|
int32_t length;
|
|
|
|
UErrorCode errorCode;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
clearBuffer();
|
2001-09-11 01:29:36 +00:00
|
|
|
nextIndex=currentIndex;
|
2002-03-20 17:04:56 +00:00
|
|
|
text->move(text, currentIndex, UITER_ZERO);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(!text->hasPrevious(text)) {
|
2001-09-11 01:29:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-10-19 17:36:02 +00:00
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
p=buffer.getBuffer(-1);
|
2002-02-10 00:11:16 +00:00
|
|
|
length=unorm_previous(text, p, buffer.getCapacity(),
|
|
|
|
fUMode, fOptions,
|
|
|
|
TRUE, 0,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
buffer.releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
errorCode=U_ZERO_ERROR;
|
2002-03-20 17:04:56 +00:00
|
|
|
text->move(text, currentIndex, UITER_ZERO);
|
2001-10-19 17:36:02 +00:00
|
|
|
p=buffer.getBuffer(length);
|
2002-02-10 00:11:16 +00:00
|
|
|
length=unorm_previous(text, p, buffer.getCapacity(),
|
|
|
|
fUMode, fOptions,
|
|
|
|
TRUE, 0,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
buffer.releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
2001-08-31 21:37:39 +00:00
|
|
|
bufferPos=buffer.length();
|
2002-03-20 17:04:56 +00:00
|
|
|
currentIndex=text->getIndex(text, UITER_CURRENT);
|
2001-10-19 17:36:02 +00:00
|
|
|
return U_SUCCESS(errorCode) && !buffer.isEmpty();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
2003-05-06 01:22:23 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_NORMALIZATION */
|