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:
|
2010-01-06 23:50:03 +00:00
|
|
|
* Copyright (c) 1996-2010, 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
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "unicode/uniset.h"
|
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"
|
2001-09-11 01:29:36 +00:00
|
|
|
#include "unicode/normlzr.h"
|
2001-10-19 17:36:02 +00:00
|
|
|
#include "cmemory.h"
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "normalizer2impl.h"
|
|
|
|
#include "uprops.h" // for uniset_getUnicode32Instance()
|
2001-06-20 22:24:42 +00:00
|
|
|
|
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) :
|
2010-01-06 23:50:03 +00:00
|
|
|
UObject(), fFilteredNorm2(NULL), fNorm2(NULL), fUMode(mode), fOptions(0),
|
|
|
|
text(new StringCharacterIterator(str)),
|
2001-09-25 22:45:26 +00:00
|
|
|
currentIndex(0), nextIndex(0),
|
|
|
|
buffer(), bufferPos(0)
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
init();
|
2001-09-25 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Normalizer::Normalizer(const UChar *str, int32_t length, UNormalizationMode mode) :
|
2010-01-06 23:50:03 +00:00
|
|
|
UObject(), fFilteredNorm2(NULL), fNorm2(NULL), fUMode(mode), fOptions(0),
|
|
|
|
text(new UCharCharacterIterator(str, length)),
|
2001-09-25 22:45:26 +00:00
|
|
|
currentIndex(0), nextIndex(0),
|
|
|
|
buffer(), bufferPos(0)
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
init();
|
2001-09-25 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Normalizer::Normalizer(const CharacterIterator& iter, UNormalizationMode mode) :
|
2010-01-06 23:50:03 +00:00
|
|
|
UObject(), fFilteredNorm2(NULL), fNorm2(NULL), fUMode(mode), fOptions(0),
|
|
|
|
text(iter.clone()),
|
2001-09-25 22:45:26 +00:00
|
|
|
currentIndex(0), nextIndex(0),
|
|
|
|
buffer(), bufferPos(0)
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
init();
|
2001-09-25 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
2001-08-29 23:57:15 +00:00
|
|
|
Normalizer::Normalizer(const Normalizer ©) :
|
2010-01-06 23:50:03 +00:00
|
|
|
UObject(copy), fFilteredNorm2(NULL), fNorm2(NULL), fUMode(copy.fUMode), fOptions(copy.fOptions),
|
|
|
|
text(copy.text->clone()),
|
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
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
init();
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const UChar _NUL=0;
|
|
|
|
|
|
|
|
void
|
2010-01-06 23:50:03 +00:00
|
|
|
Normalizer::init() {
|
2001-08-31 21:37:39 +00:00
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
2010-01-06 23:50:03 +00:00
|
|
|
fNorm2=Normalizer2Factory::getInstance(fUMode, errorCode);
|
|
|
|
if(fOptions&UNORM_UNICODE_3_2) {
|
|
|
|
delete fFilteredNorm2;
|
|
|
|
fNorm2=fFilteredNorm2=
|
|
|
|
new FilteredNormalizer2(*fNorm2, *uniset_getUnicode32Instance(errorCode));
|
|
|
|
}
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
fNorm2=Normalizer2Factory::getNoopInstance(errorCode);
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
Normalizer::~Normalizer()
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
delete fFilteredNorm2;
|
|
|
|
delete 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
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
return new Normalizer(*this);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a hash code for this iterator.
|
|
|
|
*/
|
|
|
|
int32_t Normalizer::hashCode() const
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
return text->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 ||
|
2010-06-01 06:10:26 +00:00
|
|
|
(fUMode==that.fUMode &&
|
2001-08-29 23:57:15 +00:00
|
|
|
fOptions==that.fOptions &&
|
2010-01-06 23:50:03 +00:00
|
|
|
*text==*that.text &&
|
2001-08-29 23:57:15 +00:00
|
|
|
buffer==that.buffer &&
|
|
|
|
bufferPos==that.bufferPos &&
|
2010-06-01 06:10:26 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
FilteredNormalizer2(*n2, *uniset_getUnicode32Instance(status)).
|
|
|
|
normalize(source, *dest, status);
|
|
|
|
} else {
|
|
|
|
n2->normalize(source, *dest, status);
|
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
if(dest==&localDest && U_SUCCESS(status)) {
|
2002-10-05 00:29:14 +00:00
|
|
|
result=*dest;
|
|
|
|
}
|
2001-06-28 00:34:35 +00:00
|
|
|
}
|
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) {
|
2010-01-06 23:50:03 +00:00
|
|
|
normalize(source, compat ? UNORM_NFKC : UNORM_NFC, options, result, status);
|
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) {
|
2010-01-06 23:50:03 +00:00
|
|
|
normalize(source, compat ? UNORM_NFKD : UNORM_NFD, options, result, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNormalizationCheckResult
|
|
|
|
Normalizer::quickCheck(const UnicodeString& source,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UErrorCode &status) {
|
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
return FilteredNormalizer2(*n2, *uniset_getUnicode32Instance(status)).
|
|
|
|
quickCheck(source, status);
|
|
|
|
} else {
|
|
|
|
return n2->quickCheck(source, status);
|
2002-03-12 23:00:16 +00:00
|
|
|
}
|
2001-08-17 00:21:18 +00:00
|
|
|
} else {
|
2010-01-06 23:50:03 +00:00
|
|
|
return UNORM_MAYBE;
|
|
|
|
}
|
|
|
|
}
|
2002-10-05 00:29:14 +00:00
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
UBool
|
|
|
|
Normalizer::isNormalized(const UnicodeString& source,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UErrorCode &status) {
|
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
return FilteredNormalizer2(*n2, *uniset_getUnicode32Instance(status)).
|
|
|
|
isNormalized(source, status);
|
2002-10-05 00:29:14 +00:00
|
|
|
} else {
|
2010-01-06 23:50:03 +00:00
|
|
|
return n2->isNormalized(source, status);
|
2000-07-22 00:10:49 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
} else {
|
|
|
|
return FALSE;
|
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;
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
if(&right!=&result) {
|
2002-10-05 00:29:14 +00:00
|
|
|
dest=&result;
|
|
|
|
} else {
|
2010-01-06 23:50:03 +00:00
|
|
|
// the right and result strings are the same object, use a temporary one
|
2002-10-05 00:29:14 +00:00
|
|
|
dest=&localDest;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
*dest=left;
|
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
FilteredNormalizer2(*n2, *uniset_getUnicode32Instance(errorCode)).
|
|
|
|
append(*dest, right, errorCode);
|
|
|
|
} else {
|
|
|
|
n2->append(*dest, right, errorCode);
|
|
|
|
}
|
2002-03-12 23:00:16 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
if(dest==&localDest && U_SUCCESS(errorCode)) {
|
2002-10-05 00:29:14 +00:00
|
|
|
result=*dest;
|
|
|
|
}
|
2002-03-12 23:00:16 +00:00
|
|
|
}
|
|
|
|
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() {
|
2010-01-06 23:50:03 +00:00
|
|
|
currentIndex=nextIndex=text->setToStart();
|
2001-08-29 23:57:15 +00:00
|
|
|
clearBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-12 01:32:42 +00:00
|
|
|
Normalizer::setIndexOnly(int32_t index) {
|
2010-01-06 23:50:03 +00:00
|
|
|
text->setIndex(index); // pins index
|
|
|
|
currentIndex=nextIndex=text->getIndex();
|
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
|
|
|
/**
|
2010-01-06 23:50:03 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-06 23:50:03 +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() {
|
2010-01-06 23:50:03 +00:00
|
|
|
currentIndex=nextIndex=text->setToEnd();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-06 23:50:03 +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 {
|
2010-01-06 23:50:03 +00:00
|
|
|
return text->startIndex();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-06 23:50:03 +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 {
|
2010-01-06 23:50:03 +00:00
|
|
|
return text->endIndex();
|
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;
|
2010-01-06 23:50:03 +00:00
|
|
|
init();
|
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);
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
init();
|
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.
|
2010-01-06 23:50:03 +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;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
delete text;
|
|
|
|
text = 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;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
delete text;
|
|
|
|
text = 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;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
delete text;
|
|
|
|
text = 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)
|
|
|
|
{
|
2010-01-06 23:50:03 +00:00
|
|
|
text->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() {
|
|
|
|
clearBuffer();
|
2001-09-11 01:29:36 +00:00
|
|
|
currentIndex=nextIndex;
|
2010-01-06 23:50:03 +00:00
|
|
|
text->setIndex(nextIndex);
|
|
|
|
if(!text->hasNext()) {
|
2001-09-11 01:29:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
// Skip at least one character so we make progress.
|
|
|
|
UnicodeString segment(text->next32PostInc());
|
|
|
|
while(text->hasNext()) {
|
|
|
|
UChar32 c;
|
|
|
|
if(fNorm2->hasBoundaryBefore(c=text->next32PostInc())) {
|
|
|
|
text->move32(-1, CharacterIterator::kCurrent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
segment.append(c);
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
nextIndex=text->getIndex();
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
fNorm2->normalize(segment, buffer, errorCode);
|
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() {
|
|
|
|
clearBuffer();
|
2001-09-11 01:29:36 +00:00
|
|
|
nextIndex=currentIndex;
|
2010-01-06 23:50:03 +00:00
|
|
|
text->setIndex(currentIndex);
|
|
|
|
if(!text->hasPrevious()) {
|
2001-09-11 01:29:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
UnicodeString segment;
|
|
|
|
while(text->hasPrevious()) {
|
|
|
|
UChar32 c=text->previous32();
|
|
|
|
segment.insert(0, c);
|
|
|
|
if(fNorm2->hasBoundaryBefore(c)) {
|
|
|
|
break;
|
|
|
|
}
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
currentIndex=text->getIndex();
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
fNorm2->normalize(segment, buffer, errorCode);
|
2001-08-31 21:37:39 +00:00
|
|
|
bufferPos=buffer.length();
|
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 */
|