2 ***********************************************************************
3 * © 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ***********************************************************************
6 ***********************************************************************
7 * Copyright (c) 2002-2011, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 ***********************************************************************
10 ***********************************************************************
15 #include "unicode/unorm.h"
16 #include "unicode/ustring.h"
18 #include "unicode/uperf.h"
21 // Stubs for Windows API functions when building on UNIXes.
23 #if U_PLATFORM_USES_ONLY_WIN32_API
28 inline int FoldStringW(DWORD dwMapFlags
, const UChar
* lpSrcStr
,int cchSrc
, UChar
* lpDestStr
,int cchDest
);
31 #define DEST_BUFFER_CAPACITY 6000
32 typedef int32_t (*NormFn
)(const UChar
* src
,int32_t srcLen
, UChar
* dest
,int32_t dstLen
, int32_t options
, UErrorCode
* status
);
33 typedef int32_t (*QuickCheckFn
)(const UChar
* src
,int32_t srcLen
, UNormalizationMode mode
, int32_t options
, UErrorCode
* status
);
35 class QuickCheckPerfFunction
: public UPerfFunction
{
40 UNormalizationMode mode
;
49 virtual void call(UErrorCode
* status
){
52 for(int32_t i
= 0; i
< numLines
; i
++){
53 retVal
= (*fn
)(lines
[i
].name
,lines
[i
].len
,mode
, options
, status
);
56 for(int32_t i
= 0; i
< numLines
; i
++){
57 retVal
= (*fn
)(lines
[i
].name
,-1,mode
, options
, status
);
63 retVal
= (*fn
)(src
,srcLen
,mode
, options
, status
);
65 retVal
= (*fn
)(src
,-1,mode
, options
, status
);
70 virtual long getOperationsPerIteration(){
73 for(int32_t i
=0; i
< numLines
; i
++){
74 totalChars
+= lines
[i
].len
;
81 QuickCheckPerfFunction(QuickCheckFn func
, ULine
* srcLines
,int32_t srcNumLines
, UNormalizationMode _mode
, int32_t opts
, UBool _uselen
) : options(opts
) {
84 numLines
= srcNumLines
;
91 QuickCheckPerfFunction(QuickCheckFn func
, const UChar
* source
,int32_t sourceLen
, UNormalizationMode _mode
, int32_t opts
, UBool _uselen
) : options(opts
) {
104 class NormPerfFunction
: public UPerfFunction
{
108 UChar dest
[DEST_BUFFER_CAPACITY
];
120 virtual void call(UErrorCode
* status
){
123 for(int32_t i
= 0; i
< numLines
; i
++){
124 retVal
= (*fn
)(lines
[i
].name
,lines
[i
].len
,pDest
,destLen
, options
, status
);
127 for(int32_t i
= 0; i
< numLines
; i
++){
128 retVal
= (*fn
)(lines
[i
].name
,-1,pDest
,destLen
, options
, status
);
133 retVal
= (*fn
)(src
,srcLen
,pDest
,destLen
, options
, status
);
135 retVal
= (*fn
)(src
,-1,pDest
,destLen
, options
, status
);
139 virtual long getOperationsPerIteration(){
140 if(line_mode
==TRUE
){
141 int32_t totalChars
=0;
142 for(int32_t i
=0; i
< numLines
; i
++){
143 totalChars
+= lines
[i
].len
;
150 NormPerfFunction(NormFn func
, int32_t opts
, ULine
* srcLines
,int32_t srcNumLines
,UBool _uselen
) : options(opts
) {
153 numLines
= srcNumLines
;
155 destLen
= DEST_BUFFER_CAPACITY
;
161 NormPerfFunction(NormFn func
, int32_t opts
, const UChar
* source
,int32_t sourceLen
,UBool _uselen
) : options(opts
) {
166 destLen
= sourceLen
*3;
167 pDest
= (UChar
*) malloc(destLen
* U_SIZEOF_UCHAR
);
181 class NormalizerPerformanceTest
: public UPerfTest
{
188 int32_t origBufferLen
;
189 int32_t NFDBufferLen
;
190 int32_t NFCBufferLen
;
193 void normalizeInput(ULine
* dest
,const UChar
* src
,int32_t srcLen
,UNormalizationMode mode
, int32_t options
);
194 UChar
* normalizeInput(int32_t& len
, const UChar
* src
,int32_t srcLen
,UNormalizationMode mode
, int32_t options
);
198 NormalizerPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
199 ~NormalizerPerformanceTest();
200 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,const char* &name
, char* par
= NULL
);
201 /* NFC performance */
202 UPerfFunction
* TestICU_NFC_NFD_Text();
203 UPerfFunction
* TestICU_NFC_NFC_Text();
204 UPerfFunction
* TestICU_NFC_Orig_Text();
206 /* NFD performance */
207 UPerfFunction
* TestICU_NFD_NFD_Text();
208 UPerfFunction
* TestICU_NFD_NFC_Text();
209 UPerfFunction
* TestICU_NFD_Orig_Text();
211 /* FCD performance */
212 UPerfFunction
* TestICU_FCD_NFD_Text();
213 UPerfFunction
* TestICU_FCD_NFC_Text();
214 UPerfFunction
* TestICU_FCD_Orig_Text();
216 /*Win NFC performance */
217 UPerfFunction
* TestWin_NFC_NFD_Text();
218 UPerfFunction
* TestWin_NFC_NFC_Text();
219 UPerfFunction
* TestWin_NFC_Orig_Text();
221 /* Win NFD performance */
222 UPerfFunction
* TestWin_NFD_NFD_Text();
223 UPerfFunction
* TestWin_NFD_NFC_Text();
224 UPerfFunction
* TestWin_NFD_Orig_Text();
226 /* Quick check performance */
227 UPerfFunction
* TestQC_NFC_NFD_Text();
228 UPerfFunction
* TestQC_NFC_NFC_Text();
229 UPerfFunction
* TestQC_NFC_Orig_Text();
231 UPerfFunction
* TestQC_NFD_NFD_Text();
232 UPerfFunction
* TestQC_NFD_NFC_Text();
233 UPerfFunction
* TestQC_NFD_Orig_Text();
235 UPerfFunction
* TestQC_FCD_NFD_Text();
236 UPerfFunction
* TestQC_FCD_NFC_Text();
237 UPerfFunction
* TestQC_FCD_Orig_Text();
239 /* IsNormalized performnace */
240 UPerfFunction
* TestIsNormalized_NFC_NFD_Text();
241 UPerfFunction
* TestIsNormalized_NFC_NFC_Text();
242 UPerfFunction
* TestIsNormalized_NFC_Orig_Text();
244 UPerfFunction
* TestIsNormalized_NFD_NFD_Text();
245 UPerfFunction
* TestIsNormalized_NFD_NFC_Text();
246 UPerfFunction
* TestIsNormalized_NFD_Orig_Text();
248 UPerfFunction
* TestIsNormalized_FCD_NFD_Text();
249 UPerfFunction
* TestIsNormalized_FCD_NFC_Text();
250 UPerfFunction
* TestIsNormalized_FCD_Orig_Text();
254 //---------------------------------------------------------------------------------------
255 // Platform / ICU version specific proto-types
256 //---------------------------------------------------------------------------------------
259 #if (U_ICU_VERSION_MAJOR_NUM > 1 ) || ((U_ICU_VERSION_MAJOR_NUM == 1 )&&(U_ICU_VERSION_MINOR_NUM > 8) && (U_ICU_VERSION_PATCHLEVEL_NUM >=1))
261 int32_t ICUNormNFD(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
262 return unorm_normalize(src
,srcLen
,UNORM_NFD
, options
,dest
,dstLen
,status
);
265 int32_t ICUNormNFC(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
266 return unorm_normalize(src
,srcLen
,UNORM_NFC
, options
,dest
,dstLen
,status
);
269 int32_t ICUNormNFKD(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
270 return unorm_normalize(src
,srcLen
,UNORM_NFKD
, options
,dest
,dstLen
,status
);
272 int32_t ICUNormNFKC(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
273 return unorm_normalize(src
,srcLen
,UNORM_NFKC
, options
,dest
,dstLen
,status
);
276 int32_t ICUNormFCD(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
277 return unorm_normalize(src
,srcLen
,UNORM_FCD
, options
,dest
,dstLen
,status
);
280 int32_t ICUQuickCheck(const UChar
* src
,int32_t srcLen
, UNormalizationMode mode
, int32_t options
, UErrorCode
* status
){
281 #if (U_ICU_VERSION_MAJOR_NUM > 2 ) || ((U_ICU_VERSION_MAJOR_NUM == 2 )&&(U_ICU_VERSION_MINOR_NUM >= 6))
282 return unorm_quickCheckWithOptions(src
,srcLen
,mode
, options
, status
);
284 return unorm_quickCheck(src
,srcLen
,mode
,status
);
287 int32_t ICUIsNormalized(const UChar
* src
,int32_t srcLen
, UNormalizationMode mode
, int32_t options
, UErrorCode
* status
){
288 return unorm_isNormalized(src
,srcLen
,mode
,status
);
294 int32_t ICUNormNFD(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
295 return unorm_normalize(src
,srcLen
,UCOL_DECOMP_CAN
, options
,dest
,dstLen
,status
);
298 int32_t ICUNormNFC(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
299 return unorm_normalize(src
,srcLen
,UCOL_COMPOSE_CAN
, options
,dest
,dstLen
,status
);
302 int32_t ICUNormNFKD(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
303 return unorm_normalize(src
,srcLen
,UCOL_DECOMP_COMPAT
, options
,dest
,dstLen
,status
);
305 int32_t ICUNormNFKC(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
306 return unorm_normalize(src
,srcLen
,UCOL_COMPOSE_COMPAT
, options
,dest
,dstLen
,status
);
309 int32_t ICUNormFCD(const UChar
* src
, int32_t srcLen
,UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
310 return unorm_normalize(src
,srcLen
,UNORM_FCD
, options
,dest
,dstLen
,status
);
313 int32_t ICUQuickCheck(const UChar
* src
,int32_t srcLen
, UNormalizationMode mode
, int32_t options
, UErrorCode
* status
){
314 return unorm_quickCheck(src
,srcLen
,mode
,status
);
317 int32_t ICUIsNormalized(const UChar
* src
,int32_t srcLen
, UNormalizationMode mode
, int32_t options
, UErrorCode
* status
){
322 #if U_PLATFORM_HAS_WIN32_API
324 int32_t WinNormNFD(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
325 return FoldStringW(MAP_COMPOSITE
,src
,srcLen
,dest
,dstLen
);
328 int32_t WinNormNFC(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
329 return FoldStringW(MAP_PRECOMPOSED
,src
,srcLen
,dest
,dstLen
);
332 int32_t WinNormNFKD(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
333 return FoldStringW(MAP_COMPOSITE
+MAP_FOLDCZONE
,src
,srcLen
,dest
,dstLen
);
335 int32_t WinNormNFKC(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
336 return FoldStringW(MAP_FOLDCZONE
,src
,srcLen
,dest
,dstLen
);
339 int32_t WinNormNFD(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
343 int32_t WinNormNFC(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
347 int32_t WinNormNFKD(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {
350 int32_t WinNormNFKC(const UChar
* src
, int32_t srcLen
, UChar
* dest
, int32_t dstLen
, int32_t options
, UErrorCode
* status
) {