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"
|
2002-03-04 01:24:54 +00:00
|
|
|
#include "unicode/uchar.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-01-06 23:50:03 +00:00
|
|
|
#include "normalizer2impl.h"
|
2004-10-06 23:10:53 +00:00
|
|
|
#include "ucln_cmn.h"
|
2004-09-11 14:24:31 +00:00
|
|
|
#include "unormimp.h"
|
2010-01-06 23:50:03 +00:00
|
|
|
#include "uprops.h"
|
2002-02-10 00:11:16 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "umutex.h"
|
2008-10-22 19:50:07 +00:00
|
|
|
#include "utrie2.h"
|
2002-05-29 18:36:09 +00:00
|
|
|
#include "unicode/uset.h"
|
2004-10-18 02:43:33 +00:00
|
|
|
#include "putilimp.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
|
|
|
|
|
2001-08-30 22:29:47 +00:00
|
|
|
/*
|
|
|
|
* This new implementation of the normalization code loads its data from
|
|
|
|
* unorm.dat, which is generated with the gennorm tool.
|
|
|
|
* The format of that file is described in unormimp.h .
|
|
|
|
*/
|
|
|
|
|
2001-06-20 22:24:42 +00:00
|
|
|
/* load unorm.dat ----------------------------------------------------------- */
|
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
#define UNORM_HARDCODE_DATA 1
|
|
|
|
|
|
|
|
#if UNORM_HARDCODE_DATA
|
|
|
|
|
|
|
|
/* unorm_props_data.c is machine-generated by gennorm --csource */
|
|
|
|
#include "unorm_props_data.c"
|
|
|
|
|
2006-03-20 07:53:07 +00:00
|
|
|
static const UBool formatVersion_2_2=TRUE;
|
2005-05-03 22:47:46 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-06-20 22:24:42 +00:00
|
|
|
#define DATA_NAME "unorm"
|
2002-07-17 19:25:18 +00:00
|
|
|
#define DATA_TYPE "icu"
|
2001-06-20 22:24:42 +00:00
|
|
|
|
|
|
|
static UDataMemory *normData=NULL;
|
|
|
|
static UErrorCode dataErrorCode=U_ZERO_ERROR;
|
|
|
|
static int8_t haveNormData=0;
|
|
|
|
|
2001-12-19 20:14:25 +00:00
|
|
|
static int32_t indexes[_NORM_INDEX_TOP]={ 0 };
|
2002-03-07 19:56:30 +00:00
|
|
|
static UTrie normTrie={ 0,0,0,0,0,0,0 }, fcdTrie={ 0,0,0,0,0,0,0 }, auxTrie={ 0,0,0,0,0,0,0 };
|
2001-06-20 22:24:42 +00:00
|
|
|
|
|
|
|
/*
|
2002-07-17 19:25:18 +00:00
|
|
|
* pointers into the memory-mapped unorm.icu
|
2001-06-20 22:24:42 +00:00
|
|
|
*/
|
2001-12-19 20:14:25 +00:00
|
|
|
static const uint16_t *extraData=NULL,
|
2002-03-11 17:43:52 +00:00
|
|
|
*combiningTable=NULL,
|
|
|
|
*canonStartSets=NULL;
|
2001-06-20 22:24:42 +00:00
|
|
|
|
2002-03-07 19:56:30 +00:00
|
|
|
static uint8_t formatVersion[4]={ 0, 0, 0, 0 };
|
2002-11-05 00:57:52 +00:00
|
|
|
static UBool formatVersion_2_1=FALSE, formatVersion_2_2=FALSE;
|
2002-03-07 19:56:30 +00:00
|
|
|
|
2001-06-20 22:24:42 +00:00
|
|
|
/* the Unicode version of the normalization data */
|
2003-05-28 00:47:58 +00:00
|
|
|
static UVersionInfo dataVersion={ 0, 0, 0, 0 };
|
2001-06-20 22:24:42 +00:00
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
#endif
|
|
|
|
|
2001-08-29 23:50:18 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
|
2004-10-06 23:10:53 +00:00
|
|
|
static UBool U_CALLCONV
|
2005-07-21 23:27:41 +00:00
|
|
|
unorm_cleanup(void) {
|
2005-05-03 22:47:46 +00:00
|
|
|
#if !UNORM_HARDCODE_DATA
|
2001-09-06 01:20:43 +00:00
|
|
|
if(normData!=NULL) {
|
|
|
|
udata_close(normData);
|
|
|
|
normData=NULL;
|
|
|
|
}
|
|
|
|
dataErrorCode=U_ZERO_ERROR;
|
|
|
|
haveNormData=0;
|
2005-05-03 22:47:46 +00:00
|
|
|
#endif
|
2001-09-06 01:20:43 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
#if !UNORM_HARDCODE_DATA
|
2002-03-07 19:56:30 +00:00
|
|
|
|
2001-06-20 22:24:42 +00:00
|
|
|
static UBool U_CALLCONV
|
2001-06-28 20:46:46 +00:00
|
|
|
isAcceptable(void * /* context */,
|
|
|
|
const char * /* type */, const char * /* name */,
|
2001-06-20 22:24:42 +00:00
|
|
|
const UDataInfo *pInfo) {
|
|
|
|
if(
|
|
|
|
pInfo->size>=20 &&
|
|
|
|
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
|
|
|
|
pInfo->charsetFamily==U_CHARSET_FAMILY &&
|
|
|
|
pInfo->dataFormat[0]==0x4e && /* dataFormat="Norm" */
|
|
|
|
pInfo->dataFormat[1]==0x6f &&
|
|
|
|
pInfo->dataFormat[2]==0x72 &&
|
|
|
|
pInfo->dataFormat[3]==0x6d &&
|
2001-12-19 20:14:25 +00:00
|
|
|
pInfo->formatVersion[0]==2 &&
|
|
|
|
pInfo->formatVersion[2]==UTRIE_SHIFT &&
|
|
|
|
pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT
|
2001-06-20 22:24:42 +00:00
|
|
|
) {
|
2002-03-07 19:56:30 +00:00
|
|
|
uprv_memcpy(formatVersion, pInfo->formatVersion, 4);
|
2001-06-20 22:24:42 +00:00
|
|
|
uprv_memcpy(dataVersion, pInfo->dataVersion, 4);
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
#endif
|
|
|
|
|
2002-11-15 17:36:49 +00:00
|
|
|
static UBool U_CALLCONV
|
2008-10-22 19:50:07 +00:00
|
|
|
_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 /*end*/, uint32_t /*value*/) {
|
2002-11-15 17:36:49 +00:00
|
|
|
/* add the start code point to the USet */
|
2004-12-02 04:18:35 +00:00
|
|
|
const USetAdder *sa=(const USetAdder *)context;
|
2004-09-07 17:59:53 +00:00
|
|
|
sa->add(sa->set, start);
|
2002-11-15 17:36:49 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-08-29 23:50:18 +00:00
|
|
|
U_CDECL_END
|
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
#if !UNORM_HARDCODE_DATA
|
|
|
|
|
2001-06-20 22:24:42 +00:00
|
|
|
static int8_t
|
|
|
|
loadNormData(UErrorCode &errorCode) {
|
|
|
|
/* load Unicode normalization data from file */
|
2003-05-07 00:02:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This lazy intialization with double-checked locking (without mutex protection for
|
|
|
|
* haveNormData==0) is transiently unsafe under certain circumstances.
|
|
|
|
* Check the readme and use u_init() if necessary.
|
|
|
|
*
|
|
|
|
* While u_init() initializes the main normalization data via this functions,
|
|
|
|
* it does not do so for exclusion sets (which are fully mutexed).
|
|
|
|
* This is because
|
|
|
|
* - there can be many exclusion sets
|
|
|
|
* - they are rarely used
|
|
|
|
* - they are not usually used in execution paths that are
|
|
|
|
* as performance-sensitive as others
|
|
|
|
* (e.g., IDNA takes more time than unorm_quickCheck() anyway)
|
2009-07-09 22:50:01 +00:00
|
|
|
*
|
|
|
|
* TODO: Remove code in support for non-hardcoded data. u_init() is now advertised
|
|
|
|
* as not being required for thread safety, and we can't reasonably
|
|
|
|
* revert to requiring it.
|
2003-05-07 00:02:11 +00:00
|
|
|
*/
|
2001-06-20 22:24:42 +00:00
|
|
|
if(haveNormData==0) {
|
2002-03-07 19:56:30 +00:00
|
|
|
UTrie _normTrie={ 0,0,0,0,0,0,0 }, _fcdTrie={ 0,0,0,0,0,0,0 }, _auxTrie={ 0,0,0,0,0,0,0 };
|
2001-06-20 22:24:42 +00:00
|
|
|
UDataMemory *data;
|
2005-01-04 00:47:51 +00:00
|
|
|
|
2001-12-19 20:14:25 +00:00
|
|
|
const int32_t *p=NULL;
|
2002-03-07 19:56:30 +00:00
|
|
|
const uint8_t *pb;
|
2001-06-20 22:24:42 +00:00
|
|
|
|
|
|
|
if(&errorCode==NULL || U_FAILURE(errorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the data outside the mutex block */
|
|
|
|
data=udata_openChoice(NULL, DATA_TYPE, DATA_NAME, isAcceptable, NULL, &errorCode);
|
|
|
|
dataErrorCode=errorCode;
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return haveNormData=-1;
|
|
|
|
}
|
|
|
|
|
2001-12-19 20:14:25 +00:00
|
|
|
p=(const int32_t *)udata_getMemory(data);
|
2002-03-07 19:56:30 +00:00
|
|
|
pb=(const uint8_t *)(p+_NORM_INDEX_TOP);
|
|
|
|
utrie_unserialize(&_normTrie, pb, p[_NORM_INDEX_TRIE_SIZE], &errorCode);
|
|
|
|
_normTrie.getFoldingOffset=getFoldingNormOffset;
|
|
|
|
|
|
|
|
pb+=p[_NORM_INDEX_TRIE_SIZE]+p[_NORM_INDEX_UCHAR_COUNT]*2+p[_NORM_INDEX_COMBINE_DATA_COUNT]*2;
|
|
|
|
if(p[_NORM_INDEX_FCD_TRIE_SIZE]!=0) {
|
2005-01-04 00:47:51 +00:00
|
|
|
utrie_unserialize(&_fcdTrie, pb, p[_NORM_INDEX_FCD_TRIE_SIZE], &errorCode);
|
|
|
|
}
|
|
|
|
pb+=p[_NORM_INDEX_FCD_TRIE_SIZE];
|
|
|
|
|
|
|
|
if(p[_NORM_INDEX_AUX_TRIE_SIZE]!=0) {
|
2002-03-07 19:56:30 +00:00
|
|
|
utrie_unserialize(&_auxTrie, pb, p[_NORM_INDEX_AUX_TRIE_SIZE], &errorCode);
|
|
|
|
_auxTrie.getFoldingOffset=getFoldingAuxOffset;
|
|
|
|
}
|
2001-12-19 20:14:25 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
dataErrorCode=errorCode;
|
|
|
|
udata_close(data);
|
|
|
|
return haveNormData=-1;
|
|
|
|
}
|
2001-06-20 22:24:42 +00:00
|
|
|
|
|
|
|
/* in the mutex block, set the data for this process */
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if(normData==NULL) {
|
|
|
|
normData=data;
|
|
|
|
data=NULL;
|
2001-12-19 20:14:25 +00:00
|
|
|
|
|
|
|
uprv_memcpy(&indexes, p, sizeof(indexes));
|
|
|
|
uprv_memcpy(&normTrie, &_normTrie, sizeof(UTrie));
|
|
|
|
uprv_memcpy(&fcdTrie, &_fcdTrie, sizeof(UTrie));
|
2002-03-07 19:56:30 +00:00
|
|
|
uprv_memcpy(&auxTrie, &_auxTrie, sizeof(UTrie));
|
2001-12-19 20:14:25 +00:00
|
|
|
} else {
|
|
|
|
p=(const int32_t *)udata_getMemory(normData);
|
2001-06-20 22:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize some variables */
|
2001-12-19 20:14:25 +00:00
|
|
|
extraData=(uint16_t *)((uint8_t *)(p+_NORM_INDEX_TOP)+indexes[_NORM_INDEX_TRIE_SIZE]);
|
2001-06-20 22:24:42 +00:00
|
|
|
combiningTable=extraData+indexes[_NORM_INDEX_UCHAR_COUNT];
|
2002-03-07 19:56:30 +00:00
|
|
|
formatVersion_2_1=formatVersion[0]>2 || (formatVersion[0]==2 && formatVersion[1]>=1);
|
2002-11-05 00:57:52 +00:00
|
|
|
formatVersion_2_2=formatVersion[0]>2 || (formatVersion[0]==2 && formatVersion[1]>=2);
|
2002-03-11 17:43:52 +00:00
|
|
|
if(formatVersion_2_1) {
|
2002-03-12 00:49:48 +00:00
|
|
|
canonStartSets=combiningTable+
|
|
|
|
indexes[_NORM_INDEX_COMBINE_DATA_COUNT]+
|
|
|
|
(indexes[_NORM_INDEX_FCD_TRIE_SIZE]+indexes[_NORM_INDEX_AUX_TRIE_SIZE])/2;
|
2002-03-11 17:43:52 +00:00
|
|
|
}
|
2001-06-20 22:24:42 +00:00
|
|
|
haveNormData=1;
|
2004-10-06 23:10:53 +00:00
|
|
|
ucln_common_registerCleanup(UCLN_COMMON_UNORM, unorm_cleanup);
|
2004-02-24 00:13:01 +00:00
|
|
|
umtx_unlock(NULL);
|
2001-06-20 22:24:42 +00:00
|
|
|
|
|
|
|
/* if a different thread set it first, then close the extra data */
|
|
|
|
if(data!=NULL) {
|
|
|
|
udata_close(data); /* NULL if it was set correctly */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return haveNormData;
|
|
|
|
}
|
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
#endif
|
|
|
|
|
2001-10-04 20:42:25 +00:00
|
|
|
static inline UBool
|
2001-06-20 22:24:42 +00:00
|
|
|
_haveData(UErrorCode &errorCode) {
|
2005-05-03 22:47:46 +00:00
|
|
|
#if UNORM_HARDCODE_DATA
|
|
|
|
return U_SUCCESS(errorCode);
|
|
|
|
#else
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return FALSE;
|
|
|
|
} else if(haveNormData>0) {
|
|
|
|
return TRUE;
|
|
|
|
} else if(haveNormData<0) {
|
2001-06-20 22:24:42 +00:00
|
|
|
errorCode=dataErrorCode;
|
2005-05-03 22:47:46 +00:00
|
|
|
return FALSE;
|
|
|
|
} else /* haveNormData==0 */ {
|
2001-06-20 22:24:42 +00:00
|
|
|
return (UBool)(loadNormData(errorCode)>0);
|
|
|
|
}
|
2005-05-03 22:47:46 +00:00
|
|
|
#endif
|
2001-06-20 22:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
unorm_haveData(UErrorCode *pErrorCode) {
|
|
|
|
return _haveData(*pErrorCode);
|
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
/* normalization properties ------------------------------------------------- */
|
2002-03-07 19:56:30 +00:00
|
|
|
|
2007-06-03 06:08:46 +00:00
|
|
|
U_CFUNC UBool U_EXPORT2
|
2002-03-11 17:43:52 +00:00
|
|
|
unorm_isCanonSafeStart(UChar32 c) {
|
2005-05-03 22:47:46 +00:00
|
|
|
#if UNORM_HARDCODE_DATA
|
|
|
|
if(auxTrie.index!=NULL) {
|
|
|
|
#else
|
2002-03-11 17:43:52 +00:00
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
2005-01-04 00:47:51 +00:00
|
|
|
if(_haveData(errorCode) && auxTrie.index!=NULL) {
|
2005-05-03 22:47:46 +00:00
|
|
|
#endif
|
2008-10-22 19:50:07 +00:00
|
|
|
uint16_t aux=UTRIE2_GET16(&auxTrie, c);
|
2002-03-14 23:54:09 +00:00
|
|
|
return (UBool)((aux&_NORM_AUX_UNSAFE_MASK)==0);
|
2002-03-11 17:43:52 +00:00
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-06 01:51:37 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
unorm_getUnicodeVersion(UVersionInfo *versionInfo, UErrorCode *pErrorCode){
|
|
|
|
if(unorm_haveData(pErrorCode)){
|
|
|
|
uprv_memcpy(*versionInfo, dataVersion, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-11 17:43:52 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
unorm_getCanonStartSet(UChar32 c, USerializedSet *fillSet) {
|
2005-07-13 21:28:52 +00:00
|
|
|
#if !UNORM_HARDCODE_DATA
|
2002-03-11 17:43:52 +00:00
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
2005-07-13 21:28:52 +00:00
|
|
|
#endif
|
2002-03-14 23:54:09 +00:00
|
|
|
if( fillSet!=NULL && (uint32_t)c<=0x10ffff &&
|
2005-05-03 22:47:46 +00:00
|
|
|
#if !UNORM_HARDCODE_DATA
|
|
|
|
_haveData(errorCode) &&
|
|
|
|
#endif
|
|
|
|
canonStartSets!=NULL
|
2002-03-14 23:54:09 +00:00
|
|
|
) {
|
|
|
|
const uint16_t *table;
|
|
|
|
int32_t i, start, limit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* binary search for c
|
|
|
|
*
|
|
|
|
* There are two search tables,
|
|
|
|
* one for BMP code points and one for supplementary ones.
|
|
|
|
* See unormimp.h for details.
|
|
|
|
*/
|
|
|
|
if(c<=0xffff) {
|
|
|
|
table=canonStartSets+canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH];
|
|
|
|
start=0;
|
|
|
|
limit=canonStartSets[_NORM_SET_INDEX_CANON_BMP_TABLE_LENGTH];
|
|
|
|
|
|
|
|
/* each entry is a pair { c, result } */
|
|
|
|
while(start<limit-2) {
|
|
|
|
i=(uint16_t)(((start+limit)/4)*2); /* (start+limit)/2 and address pairs */
|
|
|
|
if(c<table[i]) {
|
|
|
|
limit=i;
|
|
|
|
} else {
|
|
|
|
start=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* found? */
|
|
|
|
if(c==table[start]) {
|
|
|
|
i=table[start+1];
|
|
|
|
if((i&_NORM_CANON_SET_BMP_MASK)==_NORM_CANON_SET_BMP_IS_INDEX) {
|
|
|
|
/* result 01xxxxxx xxxxxx contains index x to a USerializedSet */
|
|
|
|
i&=(_NORM_MAX_CANON_SETS-1);
|
|
|
|
return uset_getSerializedSet(fillSet,
|
|
|
|
canonStartSets+i,
|
|
|
|
canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH]-i);
|
|
|
|
} else {
|
|
|
|
/* other result values are BMP code points for single-code point sets */
|
|
|
|
uset_setSerializedToOne(fillSet, (UChar32)i);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint16_t high, low, h;
|
|
|
|
|
|
|
|
table=canonStartSets+canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH]+
|
|
|
|
canonStartSets[_NORM_SET_INDEX_CANON_BMP_TABLE_LENGTH];
|
|
|
|
start=0;
|
|
|
|
limit=canonStartSets[_NORM_SET_INDEX_CANON_SUPP_TABLE_LENGTH];
|
|
|
|
|
|
|
|
high=(uint16_t)(c>>16);
|
|
|
|
low=(uint16_t)c;
|
|
|
|
|
|
|
|
/* each entry is a triplet { high(c), low(c), result } */
|
|
|
|
while(start<limit-3) {
|
|
|
|
i=(uint16_t)(((start+limit)/6)*3); /* (start+limit)/2 and address triplets */
|
|
|
|
h=table[i]&0x1f; /* high word */
|
|
|
|
if(high<h || (high==h && low<table[i+1])) {
|
|
|
|
limit=i;
|
|
|
|
} else {
|
|
|
|
start=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* found? */
|
|
|
|
h=table[start];
|
|
|
|
if(high==(h&0x1f) && low==table[start+1]) {
|
|
|
|
i=table[start+2];
|
|
|
|
if((h&0x8000)==0) {
|
|
|
|
/* the result is an index to a USerializedSet */
|
|
|
|
return uset_getSerializedSet(fillSet,
|
|
|
|
canonStartSets+i,
|
|
|
|
canonStartSets[_NORM_SET_INDEX_CANON_SETS_LENGTH]-i);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* single-code point set {x} in
|
|
|
|
* triplet { 100xxxxx 000hhhhh llllllll llllllll xxxxxxxx xxxxxxxx }
|
|
|
|
*/
|
|
|
|
i|=((int32_t)h&0x1f00)<<8; /* add high bits from high(c) */
|
|
|
|
uset_setSerializedToOne(fillSet, (UChar32)i);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-11 17:43:52 +00:00
|
|
|
}
|
2002-03-14 23:54:09 +00:00
|
|
|
|
|
|
|
return FALSE; /* not found */
|
2002-03-11 17:43:52 +00:00
|
|
|
}
|
|
|
|
|
2002-07-04 00:18:35 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_getFC_NFKC_Closure(UChar32 c, UChar *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
|
|
|
|
uint16_t aux;
|
|
|
|
|
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(destCapacity<0 || (dest==NULL && destCapacity>0)) {
|
|
|
|
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-10-22 19:50:07 +00:00
|
|
|
if(_haveData(*pErrorCode) && auxTrie.index!=NULL) {
|
|
|
|
aux=UTRIE2_GET16(&auxTrie, c);
|
|
|
|
aux&=_NORM_AUX_FNC_MASK;
|
|
|
|
} else {
|
|
|
|
aux=0;
|
2002-07-04 00:18:35 +00:00
|
|
|
}
|
|
|
|
if(aux!=0) {
|
|
|
|
const UChar *s;
|
|
|
|
int32_t length;
|
|
|
|
|
2002-07-11 17:15:08 +00:00
|
|
|
s=(const UChar *)(extraData+aux);
|
2002-07-04 00:18:35 +00:00
|
|
|
if(*s<0xff00) {
|
|
|
|
/* s points to the single-unit string */
|
|
|
|
length=1;
|
|
|
|
} else {
|
|
|
|
length=*s&0xff;
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
if(0<length && length<=destCapacity) {
|
|
|
|
uprv_memcpy(dest, s, length*U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
return u_terminateUChars(dest, destCapacity, length, pErrorCode);
|
|
|
|
} else {
|
2002-07-15 16:40:49 +00:00
|
|
|
return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
|
2002-07-04 00:18:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
unorm_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
|
|
|
|
UChar c;
|
2002-11-05 00:57:52 +00:00
|
|
|
|
2005-05-03 22:47:46 +00:00
|
|
|
if(!_haveData(*pErrorCode)) {
|
2002-11-05 00:57:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the start code point of each same-value range of each trie */
|
2005-01-04 00:47:51 +00:00
|
|
|
if(auxTrie.index!=NULL) {
|
2010-01-06 23:50:03 +00:00
|
|
|
utrie2_enum(&auxTrie, NULL, _enumPropertyStartsRange, sa);
|
|
|
|
}
|
2001-06-28 00:34:35 +00:00
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
/* add Hangul LV syllables and LV+1 because of skippables */
|
|
|
|
for(c=Hangul::HANGUL_BASE; c<Hangul::HANGUL_LIMIT; c+=Hangul::JAMO_T_COUNT) {
|
|
|
|
sa->add(sa->set, c);
|
|
|
|
sa->add(sa->set, c+1);
|
2001-06-28 00:34:35 +00:00
|
|
|
}
|
2010-01-06 23:50:03 +00:00
|
|
|
sa->add(sa->set, Hangul::HANGUL_LIMIT); /* add Hangul+1 to continue with other properties */
|
2001-09-26 01:22:00 +00:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
return unorm2_quickCheck((const UNormalizer2 *)&fn2, src, srcLength, pErrorCode);
|
|
|
|
} 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));
|
|
|
|
return unorm2_isNormalized((const UNormalizer2 *)&fn2, src, srcLength, pErrorCode);
|
|
|
|
} 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));
|
|
|
|
return unorm2_normalize((const UNormalizer2 *)&fn2,
|
|
|
|
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 */
|