2000-12-06 00:52:58 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2010-01-06 23:50:03 +00:00
|
|
|
* Copyright (c) 1996-2010, International Business Machines
|
2001-03-21 20:44:20 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
2000-12-06 00:52:58 +00:00
|
|
|
* File unorm.cpp
|
|
|
|
*
|
|
|
|
* Created by: Vladimir Weinstein 12052000
|
|
|
|
*
|
2001-02-03 01:25:27 +00:00
|
|
|
* Modification history :
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 02/01/01 synwee Added normalization quickcheck enum and method.
|
2001-02-15 19:29:05 +00:00
|
|
|
* 02/12/01 synwee Commented out quickcheck util api has been approved
|
|
|
|
* Added private method for doing FCD checks
|
2001-02-24 02:49:31 +00:00
|
|
|
* 02/23/01 synwee Modified quickcheck and checkFCE to run through
|
|
|
|
* string for codepoints < 0x300 for the normalization
|
|
|
|
* mode NFC.
|
2001-08-29 23:50:18 +00:00
|
|
|
* 05/25/01+ Markus Scherer total rewrite, implement all normalization here
|
2001-08-17 00:21:18 +00:00
|
|
|
* instead of just wrappers around normlzr.cpp,
|
|
|
|
* load unorm.dat, support Unicode 3.1 with
|
|
|
|
* supplementary code points, etc.
|
2010-01-06 23:50:03 +00:00
|
|
|
* 2009-nov..2010-jan Markus Scherer total rewrite, new Normalizer2 API & code
|
2000-12-06 00:52:58 +00:00
|
|
|
*/
|
|
|
|
|
2001-06-20 22:24:42 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-05-06 01:37:52 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
|
2001-04-04 02:31:28 +00:00
|
|
|
#include "unicode/udata.h"
|
2004-09-11 14:24:31 +00:00
|
|
|
#include "unicode/ustring.h"
|
2002-02-10 00:11:16 +00:00
|
|
|
#include "unicode/uiter.h"
|
2001-08-29 23:50:18 +00:00
|
|
|
#include "unicode/unorm.h"
|
2010-06-08 23:32:11 +00:00
|
|
|
#include "unicode/unorm2.h"
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "normalizer2impl.h"
|
2004-09-11 14:24:31 +00:00
|
|
|
#include "unormimp.h"
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "uprops.h"
|
2010-06-01 06:10:26 +00:00
|
|
|
#include "ustr_imp.h"
|
2000-12-06 00:52:58 +00:00
|
|
|
|
2004-09-12 16:59:20 +00:00
|
|
|
#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
|
2003-02-15 01:54:25 +00:00
|
|
|
|
2006-09-03 17:08:23 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
/* quick check functions ---------------------------------------------------- */
|
|
|
|
|
2003-08-14 02:32:31 +00:00
|
|
|
U_CAPI UNormalizationCheckResult U_EXPORT2
|
|
|
|
unorm_quickCheck(const UChar *src,
|
|
|
|
int32_t srcLength,
|
|
|
|
UNormalizationMode mode,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
return unorm2_quickCheck((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
|
2003-08-14 02:32:31 +00:00
|
|
|
}
|
2001-09-26 01:22:00 +00:00
|
|
|
|
2003-08-14 02:32:31 +00:00
|
|
|
U_CAPI UNormalizationCheckResult U_EXPORT2
|
|
|
|
unorm_quickCheckWithOptions(const UChar *src, int32_t srcLength,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*pErrorCode));
|
2010-06-08 23:32:11 +00:00
|
|
|
return unorm2_quickCheck(
|
|
|
|
reinterpret_cast<const UNormalizer2 *>(static_cast<Normalizer2 *>(&fn2)),
|
|
|
|
src, srcLength, pErrorCode);
|
2010-01-06 23:50:03 +00:00
|
|
|
} else {
|
|
|
|
return unorm2_quickCheck((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
|
|
|
|
}
|
2004-09-13 23:37:58 +00:00
|
|
|
}
|
|
|
|
|
2003-08-14 02:32:31 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
unorm_isNormalized(const UChar *src, int32_t srcLength,
|
|
|
|
UNormalizationMode mode,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
return unorm2_isNormalized((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
|
2003-08-14 02:32:31 +00:00
|
|
|
}
|
2001-09-26 01:22:00 +00:00
|
|
|
|
2003-08-14 02:32:31 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
unorm_isNormalizedWithOptions(const UChar *src, int32_t srcLength,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*pErrorCode));
|
2010-06-08 23:32:11 +00:00
|
|
|
return unorm2_isNormalized(
|
|
|
|
reinterpret_cast<const UNormalizer2 *>(static_cast<Normalizer2 *>(&fn2)),
|
|
|
|
src, srcLength, pErrorCode);
|
2010-01-06 23:50:03 +00:00
|
|
|
} else {
|
|
|
|
return unorm2_isNormalized((const UNormalizer2 *)n2, src, srcLength, pErrorCode);
|
2001-06-28 00:34:35 +00:00
|
|
|
}
|
2003-03-01 00:13:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
/* normalize() API ---------------------------------------------------------- */
|
2001-06-20 22:24:42 +00:00
|
|
|
|
2001-08-17 00:21:18 +00:00
|
|
|
/** Public API for normalizing. */
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-08-17 00:21:18 +00:00
|
|
|
unorm_normalize(const UChar *src, int32_t srcLength,
|
2003-02-15 01:54:25 +00:00
|
|
|
UNormalizationMode mode, int32_t options,
|
2001-08-17 00:21:18 +00:00
|
|
|
UChar *dest, int32_t destCapacity,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*pErrorCode));
|
2010-06-08 23:32:11 +00:00
|
|
|
return unorm2_normalize(
|
|
|
|
reinterpret_cast<const UNormalizer2 *>(static_cast<Normalizer2 *>(&fn2)),
|
2010-01-06 23:50:03 +00:00
|
|
|
src, srcLength, dest, destCapacity, pErrorCode);
|
|
|
|
} else {
|
|
|
|
return unorm2_normalize((const UNormalizer2 *)n2,
|
|
|
|
src, srcLength, dest, destCapacity, pErrorCode);
|
2001-06-20 22:24:42 +00:00
|
|
|
}
|
2001-02-15 19:29:05 +00:00
|
|
|
}
|
2001-08-29 23:50:18 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2001-08-29 23:50:18 +00:00
|
|
|
/* iteration functions ------------------------------------------------------ */
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
static int32_t
|
|
|
|
unorm_iterate(UCharIterator *src, UBool forward,
|
|
|
|
UChar *dest, int32_t destCapacity,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UBool doNormalize, UBool *pNeededToNormalize,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
const UnicodeSet *uni32;
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
uni32=uniset_getUnicode32Instance(*pErrorCode);
|
2001-08-29 23:50:18 +00:00
|
|
|
} else {
|
2010-01-06 23:50:03 +00:00
|
|
|
uni32=NULL; // unused
|
2001-08-29 23:50:18 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
FilteredNormalizer2 fn2(*n2, *uni32);
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
n2=&fn2;
|
2001-08-29 23:50:18 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
2002-02-10 01:35:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( destCapacity<0 || (dest==NULL && destCapacity>0) ||
|
|
|
|
src==NULL
|
|
|
|
) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-02-10 00:11:16 +00:00
|
|
|
if(pNeededToNormalize!=NULL) {
|
|
|
|
*pNeededToNormalize=FALSE;
|
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
if(!(forward ? src->hasNext(src) : src->hasPrevious(src))) {
|
|
|
|
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
2001-08-29 23:50:18 +00:00
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
UnicodeString buffer;
|
|
|
|
UChar32 c;
|
|
|
|
if(forward) {
|
|
|
|
/* get one character and ignore its properties */
|
|
|
|
buffer.append(uiter_next32(src));
|
|
|
|
/* get all following characters until we see a boundary */
|
|
|
|
while((c=uiter_next32(src))>=0) {
|
|
|
|
if(n2->hasBoundaryBefore(c)) {
|
|
|
|
/* back out the latest movement to stop at the boundary */
|
|
|
|
src->move(src, -U16_LENGTH(c), UITER_CURRENT);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
buffer.append(c);
|
2002-02-10 00:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-29 23:50:18 +00:00
|
|
|
} else {
|
2010-01-06 23:50:03 +00:00
|
|
|
while((c=uiter_previous32(src))>=0) {
|
|
|
|
/* always write this character to the front of the buffer */
|
|
|
|
buffer.insert(0, c);
|
|
|
|
/* stop if this just-copied character is a boundary */
|
|
|
|
if(n2->hasBoundaryBefore(c)) {
|
|
|
|
break;
|
2001-10-19 17:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-29 23:50:18 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
UnicodeString destString(dest, 0, destCapacity);
|
|
|
|
if(buffer.length()>0 && doNormalize) {
|
|
|
|
n2->normalize(buffer, destString, *pErrorCode).extract(dest, destCapacity, *pErrorCode);
|
|
|
|
if(pNeededToNormalize!=NULL && U_SUCCESS(*pErrorCode)) {
|
|
|
|
*pNeededToNormalize= destString!=buffer;
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
return destString.length();
|
|
|
|
} else {
|
|
|
|
/* just copy the source characters */
|
|
|
|
return buffer.extract(dest, destCapacity, *pErrorCode);
|
2001-08-31 21:37:39 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
}
|
2001-08-31 21:37:39 +00:00
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
unorm_previous(UCharIterator *src,
|
|
|
|
UChar *dest, int32_t destCapacity,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UBool doNormalize, UBool *pNeededToNormalize,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
return unorm_iterate(src, FALSE,
|
|
|
|
dest, destCapacity,
|
|
|
|
mode, options,
|
|
|
|
doNormalize, pNeededToNormalize,
|
|
|
|
pErrorCode);
|
2001-08-29 23:50:18 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 23:25:07 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2002-02-10 00:11:16 +00:00
|
|
|
unorm_next(UCharIterator *src,
|
|
|
|
UChar *dest, int32_t destCapacity,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UBool doNormalize, UBool *pNeededToNormalize,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
return unorm_iterate(src, TRUE,
|
|
|
|
dest, destCapacity,
|
|
|
|
mode, options,
|
|
|
|
doNormalize, pNeededToNormalize,
|
|
|
|
pErrorCode);
|
2001-08-29 23:50:18 +00:00
|
|
|
}
|
|
|
|
|
2002-02-10 01:35:54 +00:00
|
|
|
/* Concatenation of normalized strings -------------------------------------- */
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
unorm_concatenate(const UChar *left, int32_t leftLength,
|
|
|
|
const UChar *right, int32_t rightLength,
|
|
|
|
UChar *dest, int32_t destCapacity,
|
|
|
|
UNormalizationMode mode, int32_t options,
|
|
|
|
UErrorCode *pErrorCode) {
|
2010-01-06 23:50:03 +00:00
|
|
|
const Normalizer2 *n2=Normalizer2Factory::getInstance(mode, *pErrorCode);
|
|
|
|
const UnicodeSet *uni32;
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
uni32=uniset_getUnicode32Instance(*pErrorCode);
|
|
|
|
} else {
|
|
|
|
uni32=NULL; // unused
|
|
|
|
}
|
|
|
|
FilteredNormalizer2 fn2(*n2, *uni32);
|
|
|
|
if(options&UNORM_UNICODE_3_2) {
|
|
|
|
n2=&fn2;
|
|
|
|
}
|
|
|
|
if(U_FAILURE(*pErrorCode)) {
|
2002-02-10 01:35:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( destCapacity<0 || (dest==NULL && destCapacity>0) ||
|
|
|
|
left==NULL || leftLength<-1 ||
|
|
|
|
right==NULL || rightLength<-1
|
|
|
|
) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for overlapping right and destination */
|
|
|
|
if( dest!=NULL &&
|
|
|
|
((right>=dest && right<(dest+destCapacity)) ||
|
|
|
|
(rightLength>0 && dest>=right && dest<(right+rightLength)))
|
|
|
|
) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allow left==dest */
|
2010-01-06 23:50:03 +00:00
|
|
|
UnicodeString destString;
|
|
|
|
if(left==dest) {
|
|
|
|
destString.setTo(dest, leftLength, destCapacity);
|
2002-02-10 01:35:54 +00:00
|
|
|
} else {
|
2010-01-06 23:50:03 +00:00
|
|
|
destString.setTo(dest, 0, destCapacity);
|
|
|
|
destString.append(left, leftLength);
|
2002-02-10 01:35:54 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
return n2->append(destString, UnicodeString(rightLength<0, right, rightLength), *pErrorCode).
|
|
|
|
extract(dest, destCapacity, *pErrorCode);
|
2002-02-10 01:35:54 +00:00
|
|
|
}
|
2002-05-22 00:29:58 +00:00
|
|
|
|
2003-05-06 01:37:52 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_NORMALIZATION */
|