2 *************************************************************************
4 * Copyright (c) 1996-2012, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 *************************************************************************
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_NORMALIZATION
13 #include "unicode/uniset.h"
14 #include "unicode/unistr.h"
15 #include "unicode/chariter.h"
16 #include "unicode/schriter.h"
17 #include "unicode/uchriter.h"
18 #include "unicode/normlzr.h"
19 #include "unicode/utf16.h"
21 #include "normalizer2impl.h"
22 #include "uprops.h" // for uniset_getUnicode32Instance()
26 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Normalizer
)
28 //-------------------------------------------------------------------------
29 // Constructors and other boilerplate
30 //-------------------------------------------------------------------------
32 Normalizer::Normalizer(const UnicodeString
& str
, UNormalizationMode mode
) :
33 UObject(), fFilteredNorm2(NULL
), fNorm2(NULL
), fUMode(mode
), fOptions(0),
34 text(new StringCharacterIterator(str
)),
35 currentIndex(0), nextIndex(0),
36 buffer(), bufferPos(0)
41 Normalizer::Normalizer(const UChar
*str
, int32_t length
, UNormalizationMode mode
) :
42 UObject(), fFilteredNorm2(NULL
), fNorm2(NULL
), fUMode(mode
), fOptions(0),
43 text(new UCharCharacterIterator(str
, length
)),
44 currentIndex(0), nextIndex(0),
45 buffer(), bufferPos(0)
50 Normalizer::Normalizer(const CharacterIterator
& iter
, UNormalizationMode mode
) :
51 UObject(), fFilteredNorm2(NULL
), fNorm2(NULL
), fUMode(mode
), fOptions(0),
53 currentIndex(0), nextIndex(0),
54 buffer(), bufferPos(0)
59 Normalizer::Normalizer(const Normalizer
©
) :
60 UObject(copy
), fFilteredNorm2(NULL
), fNorm2(NULL
), fUMode(copy
.fUMode
), fOptions(copy
.fOptions
),
61 text(copy
.text
->clone()),
62 currentIndex(copy
.currentIndex
), nextIndex(copy
.nextIndex
),
63 buffer(copy
.buffer
), bufferPos(copy
.bufferPos
)
70 UErrorCode errorCode
=U_ZERO_ERROR
;
71 fNorm2
=Normalizer2Factory::getInstance(fUMode
, errorCode
);
72 if(fOptions
&UNORM_UNICODE_3_2
) {
73 delete fFilteredNorm2
;
74 fNorm2
=fFilteredNorm2
=
75 new FilteredNormalizer2(*fNorm2
, *uniset_getUnicode32Instance(errorCode
));
77 if(U_FAILURE(errorCode
)) {
78 errorCode
=U_ZERO_ERROR
;
79 fNorm2
=Normalizer2Factory::getNoopInstance(errorCode
);
83 Normalizer::~Normalizer()
85 delete fFilteredNorm2
;
90 Normalizer::clone() const
92 return new Normalizer(*this);
96 * Generates a hash code for this iterator.
98 int32_t Normalizer::hashCode() const
100 return text
->hashCode() + fUMode
+ fOptions
+ buffer
.hashCode() + bufferPos
+ currentIndex
+ nextIndex
;
103 UBool
Normalizer::operator==(const Normalizer
& that
) const
107 (fUMode
==that
.fUMode
&&
108 fOptions
==that
.fOptions
&&
110 buffer
==that
.buffer
&&
111 bufferPos
==that
.bufferPos
&&
112 nextIndex
==that
.nextIndex
);
115 //-------------------------------------------------------------------------
116 // Static utility methods
117 //-------------------------------------------------------------------------
120 Normalizer::normalize(const UnicodeString
& source
,
121 UNormalizationMode mode
, int32_t options
,
122 UnicodeString
& result
,
123 UErrorCode
&status
) {
124 if(source
.isBogus() || U_FAILURE(status
)) {
126 if(U_SUCCESS(status
)) {
127 status
=U_ILLEGAL_ARGUMENT_ERROR
;
130 UnicodeString localDest
;
133 if(&source
!=&result
) {
136 // the source and result strings are the same object, use a temporary one
139 const Normalizer2
*n2
=Normalizer2Factory::getInstance(mode
, status
);
140 if(U_SUCCESS(status
)) {
141 if(options
&UNORM_UNICODE_3_2
) {
142 FilteredNormalizer2(*n2
, *uniset_getUnicode32Instance(status
)).
143 normalize(source
, *dest
, status
);
145 n2
->normalize(source
, *dest
, status
);
148 if(dest
==&localDest
&& U_SUCCESS(status
)) {
155 Normalizer::compose(const UnicodeString
& source
,
156 UBool compat
, int32_t options
,
157 UnicodeString
& result
,
158 UErrorCode
&status
) {
159 normalize(source
, compat
? UNORM_NFKC
: UNORM_NFC
, options
, result
, status
);
163 Normalizer::decompose(const UnicodeString
& source
,
164 UBool compat
, int32_t options
,
165 UnicodeString
& result
,
166 UErrorCode
&status
) {
167 normalize(source
, compat
? UNORM_NFKD
: UNORM_NFD
, options
, result
, status
);
170 UNormalizationCheckResult
171 Normalizer::quickCheck(const UnicodeString
& source
,
172 UNormalizationMode mode
, int32_t options
,
173 UErrorCode
&status
) {
174 const Normalizer2
*n2
=Normalizer2Factory::getInstance(mode
, status
);
175 if(U_SUCCESS(status
)) {
176 if(options
&UNORM_UNICODE_3_2
) {
177 return FilteredNormalizer2(*n2
, *uniset_getUnicode32Instance(status
)).
178 quickCheck(source
, status
);
180 return n2
->quickCheck(source
, status
);
188 Normalizer::isNormalized(const UnicodeString
& source
,
189 UNormalizationMode mode
, int32_t options
,
190 UErrorCode
&status
) {
191 const Normalizer2
*n2
=Normalizer2Factory::getInstance(mode
, status
);
192 if(U_SUCCESS(status
)) {
193 if(options
&UNORM_UNICODE_3_2
) {
194 return FilteredNormalizer2(*n2
, *uniset_getUnicode32Instance(status
)).
195 isNormalized(source
, status
);
197 return n2
->isNormalized(source
, status
);
204 UnicodeString
& U_EXPORT2
205 Normalizer::concatenate(const UnicodeString
&left
, const UnicodeString
&right
,
206 UnicodeString
&result
,
207 UNormalizationMode mode
, int32_t options
,
208 UErrorCode
&errorCode
) {
209 if(left
.isBogus() || right
.isBogus() || U_FAILURE(errorCode
)) {
211 if(U_SUCCESS(errorCode
)) {
212 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
215 UnicodeString localDest
;
218 if(&right
!=&result
) {
221 // the right and result strings are the same object, use a temporary one
225 const Normalizer2
*n2
=Normalizer2Factory::getInstance(mode
, errorCode
);
226 if(U_SUCCESS(errorCode
)) {
227 if(options
&UNORM_UNICODE_3_2
) {
228 FilteredNormalizer2(*n2
, *uniset_getUnicode32Instance(errorCode
)).
229 append(*dest
, right
, errorCode
);
231 n2
->append(*dest
, right
, errorCode
);
234 if(dest
==&localDest
&& U_SUCCESS(errorCode
)) {
241 //-------------------------------------------------------------------------
243 //-------------------------------------------------------------------------
246 * Return the current character in the normalized text.
248 UChar32
Normalizer::current() {
249 if(bufferPos
<buffer
.length() || nextNormalize()) {
250 return buffer
.char32At(bufferPos
);
257 * Return the next character in the normalized text and advance
258 * the iteration position by one. If the end
259 * of the text has already been reached, {@link #DONE} is returned.
261 UChar32
Normalizer::next() {
262 if(bufferPos
<buffer
.length() || nextNormalize()) {
263 UChar32 c
=buffer
.char32At(bufferPos
);
264 bufferPos
+=U16_LENGTH(c
);
272 * Return the previous character in the normalized text and decrement
273 * the iteration position by one. If the beginning
274 * of the text has already been reached, {@link #DONE} is returned.
276 UChar32
Normalizer::previous() {
277 if(bufferPos
>0 || previousNormalize()) {
278 UChar32 c
=buffer
.char32At(bufferPos
-1);
279 bufferPos
-=U16_LENGTH(c
);
286 void Normalizer::reset() {
287 currentIndex
=nextIndex
=text
->setToStart();
292 Normalizer::setIndexOnly(int32_t index
) {
293 text
->setIndex(index
); // pins index
294 currentIndex
=nextIndex
=text
->getIndex();
299 * Return the first character in the normalized text. This resets
300 * the <tt>Normalizer's</tt> position to the beginning of the text.
302 UChar32
Normalizer::first() {
308 * Return the last character in the normalized text. This resets
309 * the <tt>Normalizer's</tt> position to be just before the
310 * the input text corresponding to that normalized character.
312 UChar32
Normalizer::last() {
313 currentIndex
=nextIndex
=text
->setToEnd();
319 * Retrieve the current iteration position in the input text that is
320 * being normalized. This method is useful in applications such as
321 * searching, where you need to be able to determine the position in
322 * the input text that corresponds to a given normalized output character.
324 * <b>Note:</b> This method sets the position in the <em>input</em>, while
325 * {@link #next} and {@link #previous} iterate through characters in the
326 * <em>output</em>. This means that there is not necessarily a one-to-one
327 * correspondence between characters returned by <tt>next</tt> and
328 * <tt>previous</tt> and the indices passed to and returned from
329 * <tt>setIndex</tt> and {@link #getIndex}.
332 int32_t Normalizer::getIndex() const {
333 if(bufferPos
<buffer
.length()) {
341 * Retrieve the index of the start of the input text. This is the begin index
342 * of the <tt>CharacterIterator</tt> or the start (i.e. 0) of the <tt>String</tt>
343 * over which this <tt>Normalizer</tt> is iterating
345 int32_t Normalizer::startIndex() const {
346 return text
->startIndex();
350 * Retrieve the index of the end of the input text. This is the end index
351 * of the <tt>CharacterIterator</tt> or the length of the <tt>String</tt>
352 * over which this <tt>Normalizer</tt> is iterating
354 int32_t Normalizer::endIndex() const {
355 return text
->endIndex();
358 //-------------------------------------------------------------------------
359 // Property access methods
360 //-------------------------------------------------------------------------
363 Normalizer::setMode(UNormalizationMode newMode
)
370 Normalizer::getUMode() const
376 Normalizer::setOption(int32_t option
,
382 fOptions
&= (~option
);
388 Normalizer::getOption(int32_t option
) const
390 return (fOptions
& option
) != 0;
394 * Set the input text over which this <tt>Normalizer</tt> will iterate.
395 * The iteration position is set to the beginning of the input text.
398 Normalizer::setText(const UnicodeString
& newText
,
401 if (U_FAILURE(status
)) {
404 CharacterIterator
*newIter
= new StringCharacterIterator(newText
);
405 if (newIter
== NULL
) {
406 status
= U_MEMORY_ALLOCATION_ERROR
;
415 * Set the input text over which this <tt>Normalizer</tt> will iterate.
416 * The iteration position is set to the beginning of the string.
419 Normalizer::setText(const CharacterIterator
& newText
,
422 if (U_FAILURE(status
)) {
425 CharacterIterator
*newIter
= newText
.clone();
426 if (newIter
== NULL
) {
427 status
= U_MEMORY_ALLOCATION_ERROR
;
436 Normalizer::setText(const UChar
* newText
,
440 if (U_FAILURE(status
)) {
443 CharacterIterator
*newIter
= new UCharCharacterIterator(newText
, length
);
444 if (newIter
== NULL
) {
445 status
= U_MEMORY_ALLOCATION_ERROR
;
454 * Copies the text under iteration into the UnicodeString referred to by "result".
455 * @param result Receives a copy of the text under iteration.
458 Normalizer::getText(UnicodeString
& result
)
460 text
->getText(result
);
463 //-------------------------------------------------------------------------
464 // Private utility methods
465 //-------------------------------------------------------------------------
467 void Normalizer::clearBuffer() {
473 Normalizer::nextNormalize() {
475 currentIndex
=nextIndex
;
476 text
->setIndex(nextIndex
);
477 if(!text
->hasNext()) {
480 // Skip at least one character so we make progress.
481 UnicodeString
segment(text
->next32PostInc());
482 while(text
->hasNext()) {
484 if(fNorm2
->hasBoundaryBefore(c
=text
->next32PostInc())) {
485 text
->move32(-1, CharacterIterator::kCurrent
);
490 nextIndex
=text
->getIndex();
491 UErrorCode errorCode
=U_ZERO_ERROR
;
492 fNorm2
->normalize(segment
, buffer
, errorCode
);
493 return U_SUCCESS(errorCode
) && !buffer
.isEmpty();
497 Normalizer::previousNormalize() {
499 nextIndex
=currentIndex
;
500 text
->setIndex(currentIndex
);
501 if(!text
->hasPrevious()) {
504 UnicodeString segment
;
505 while(text
->hasPrevious()) {
506 UChar32 c
=text
->previous32();
507 segment
.insert(0, c
);
508 if(fNorm2
->hasBoundaryBefore(c
)) {
512 currentIndex
=text
->getIndex();
513 UErrorCode errorCode
=U_ZERO_ERROR
;
514 fNorm2
->normalize(segment
, buffer
, errorCode
);
515 bufferPos
=buffer
.length();
516 return U_SUCCESS(errorCode
) && !buffer
.isEmpty();
521 #endif /* #if !UCONFIG_NO_NORMALIZATION */