2001-07-03 23:44:45 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2006-09-08 03:40:11 +00:00
|
|
|
* Copyright (C) 2001-2006, International Business Machines
|
2001-07-03 23:44:45 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 07/03/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2001-12-03 20:50:50 +00:00
|
|
|
#include "unicode/uniset.h"
|
2002-02-09 21:59:27 +00:00
|
|
|
#include "unicode/uiter.h"
|
2001-11-16 23:51:15 +00:00
|
|
|
#include "nortrans.h"
|
2001-10-19 23:26:48 +00:00
|
|
|
#include "unormimp.h"
|
2001-12-03 20:50:50 +00:00
|
|
|
#include "ucln_in.h"
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NormalizationTransliterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2001-07-03 23:44:45 +00:00
|
|
|
/**
|
|
|
|
* System registration hook.
|
|
|
|
*/
|
|
|
|
void NormalizationTransliterator::registerIDs() {
|
2001-10-19 23:26:48 +00:00
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
if(!unorm_haveData(&errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-01-22 00:27:49 +00:00
|
|
|
Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFC"),
|
2001-10-17 17:29:34 +00:00
|
|
|
_create, integerToken(UNORM_NFC));
|
2002-01-22 00:27:49 +00:00
|
|
|
Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFKC"),
|
2001-10-17 17:29:34 +00:00
|
|
|
_create, integerToken(UNORM_NFKC));
|
2002-01-22 00:27:49 +00:00
|
|
|
Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFD"),
|
2001-10-17 17:29:34 +00:00
|
|
|
_create, integerToken(UNORM_NFD));
|
2002-01-22 00:27:49 +00:00
|
|
|
Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFKD"),
|
2001-10-17 17:29:34 +00:00
|
|
|
_create, integerToken(UNORM_NFKD));
|
2002-01-22 00:27:49 +00:00
|
|
|
Transliterator::_registerSpecialInverse(UNICODE_STRING_SIMPLE("NFC"),
|
|
|
|
UNICODE_STRING_SIMPLE("NFD"), TRUE);
|
|
|
|
Transliterator::_registerSpecialInverse(UNICODE_STRING_SIMPLE("NFKC"),
|
|
|
|
UNICODE_STRING_SIMPLE("NFKD"), TRUE);
|
2001-07-03 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory methods
|
|
|
|
*/
|
2001-10-17 17:29:34 +00:00
|
|
|
Transliterator* NormalizationTransliterator::_create(const UnicodeString& ID,
|
|
|
|
Token context) {
|
|
|
|
return new NormalizationTransliterator(ID, (UNormalizationMode) context.integer, 0);
|
2001-07-03 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
|
|
|
NormalizationTransliterator::NormalizationTransliterator(
|
|
|
|
const UnicodeString& id,
|
2001-09-27 23:19:22 +00:00
|
|
|
UNormalizationMode mode, int32_t opt) :
|
2001-07-03 23:44:45 +00:00
|
|
|
Transliterator(id, 0) {
|
2001-09-27 23:19:22 +00:00
|
|
|
fMode = mode;
|
2001-07-03 23:44:45 +00:00
|
|
|
options = opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
NormalizationTransliterator::~NormalizationTransliterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
NormalizationTransliterator::NormalizationTransliterator(const NormalizationTransliterator& o) :
|
|
|
|
Transliterator(o) {
|
2001-09-27 23:19:22 +00:00
|
|
|
fMode = o.fMode;
|
2001-07-03 23:44:45 +00:00
|
|
|
options = o.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
NormalizationTransliterator& NormalizationTransliterator::operator=(const NormalizationTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
2001-09-27 23:19:22 +00:00
|
|
|
fMode = o.fMode;
|
2001-07-03 23:44:45 +00:00
|
|
|
options = o.options;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* NormalizationTransliterator::clone(void) const {
|
|
|
|
return new NormalizationTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
|
|
|
*/
|
|
|
|
void NormalizationTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
|
|
|
|
UBool isIncremental) const {
|
2001-10-19 23:26:48 +00:00
|
|
|
// start and limit of the input range
|
2001-07-03 23:44:45 +00:00
|
|
|
int32_t start = offsets.start;
|
|
|
|
int32_t limit = offsets.limit;
|
2001-10-19 23:26:48 +00:00
|
|
|
int32_t length, delta;
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
if(start >= limit) {
|
|
|
|
return;
|
2001-07-03 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
// a C code unit iterator, implemented around the Replaceable
|
2002-02-09 21:59:27 +00:00
|
|
|
UCharIterator iter;
|
|
|
|
uiter_setReplaceable(&iter, &text);
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
// the output string and buffer pointer
|
|
|
|
UnicodeString output;
|
|
|
|
UChar *buffer;
|
2002-02-10 00:11:16 +00:00
|
|
|
UBool neededToNormalize;
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
UErrorCode errorCode;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize as short chunks at a time as possible even in
|
|
|
|
* bulk mode, so that styled text is minimally disrupted.
|
|
|
|
* In incremental mode, a chunk that ends with offsets.limit
|
|
|
|
* must not be normalized.
|
|
|
|
*
|
|
|
|
* If it was known that the input text is not styled, then
|
|
|
|
* a bulk mode normalization could look like this:
|
|
|
|
*
|
|
|
|
|
|
|
|
UChar staticChars[256];
|
|
|
|
UnicodeString input;
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
length = limit - start;
|
|
|
|
input.setTo(staticChars, 0, sizeof(staticChars)/U_SIZEOF_UCHAR); // writable alias
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
_Replaceable_extractBetween(text, start, limit, input.getBuffer(length));
|
|
|
|
input.releaseBuffer(length);
|
2001-07-03 23:44:45 +00:00
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Normalizer::normalize(input, fMode, options, output, status);
|
|
|
|
|
|
|
|
text.handleReplaceBetween(start, limit, output);
|
|
|
|
|
|
|
|
int32_t delta = output.length() - length;
|
|
|
|
offsets.contextLimit += delta;
|
|
|
|
offsets.limit += delta;
|
|
|
|
offsets.start = limit + delta;
|
|
|
|
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
while(start < limit) {
|
|
|
|
// set the iterator limits for the remaining input range
|
|
|
|
// this is a moving target because of the replacements in the text object
|
|
|
|
iter.start = iter.index = start;
|
|
|
|
iter.limit = limit;
|
|
|
|
|
|
|
|
// incrementally normalize a small chunk of the input
|
|
|
|
buffer = output.getBuffer(-1);
|
|
|
|
errorCode = U_ZERO_ERROR;
|
2002-02-10 00:11:16 +00:00
|
|
|
length = unorm_next(&iter, buffer, output.getCapacity(),
|
|
|
|
fMode, 0,
|
|
|
|
TRUE, &neededToNormalize,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
output.releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2001-10-19 23:26:48 +00:00
|
|
|
|
|
|
|
if(errorCode == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
// use a larger output string buffer and do it again from the start
|
|
|
|
iter.index = start;
|
|
|
|
buffer = output.getBuffer(length);
|
|
|
|
errorCode = U_ZERO_ERROR;
|
2002-02-10 00:11:16 +00:00
|
|
|
length = unorm_next(&iter, buffer, output.getCapacity(),
|
|
|
|
fMode, 0,
|
|
|
|
TRUE, &neededToNormalize,
|
|
|
|
&errorCode);
|
2005-06-22 02:40:25 +00:00
|
|
|
output.releaseBuffer(U_SUCCESS(errorCode) ? length : 0);
|
2001-07-03 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 23:26:48 +00:00
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
limit = iter.index;
|
|
|
|
if(isIncremental && limit == iter.limit) {
|
|
|
|
// stop in incremental mode when we reach the input limit
|
|
|
|
// in case there are additional characters that could change the
|
|
|
|
// normalization result
|
2001-12-03 20:50:50 +00:00
|
|
|
|
|
|
|
// UNLESS all characters in the result of the normalization of
|
|
|
|
// the last run are in the skippable set
|
2002-11-05 00:53:42 +00:00
|
|
|
const UChar *s=output.getBuffer();
|
|
|
|
int32_t i=0, outLength=output.length();
|
|
|
|
UChar32 c;
|
|
|
|
|
|
|
|
while(i<outLength) {
|
|
|
|
U16_NEXT(s, i, outLength, c);
|
|
|
|
if(!unorm_isNFSkippable(c, fMode)) {
|
|
|
|
outLength=-1; // I wish C++ had labeled loops and break outer; ...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outLength<0) {
|
2001-12-03 20:50:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-10-19 23:26:48 +00:00
|
|
|
}
|
|
|
|
|
2002-02-10 00:11:16 +00:00
|
|
|
if(neededToNormalize) {
|
|
|
|
// replace the input chunk with its normalized form
|
|
|
|
text.handleReplaceBetween(start, limit, output);
|
|
|
|
|
|
|
|
// update all necessary indexes accordingly
|
|
|
|
delta = length - (limit - start); // length change in the text object
|
|
|
|
start = limit += delta; // the next chunk starts where this one ends, with adjustment
|
|
|
|
limit = offsets.limit += delta; // set the iteration limit to the adjusted end of the input range
|
|
|
|
offsets.contextLimit += delta;
|
|
|
|
} else {
|
|
|
|
// delta == 0
|
|
|
|
start = limit;
|
|
|
|
limit = offsets.limit;
|
|
|
|
}
|
2001-07-03 23:44:45 +00:00
|
|
|
}
|
2001-10-19 23:26:48 +00:00
|
|
|
|
|
|
|
offsets.start = start;
|
2001-07-03 23:44:45 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|