File: icu.c

package info (click to toggle)
mame 0.277%2Bdfsg.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 907,524 kB
  • sloc: cpp: 5,198,779; xml: 2,214,830; ansic: 750,334; sh: 34,449; lisp: 19,643; python: 16,298; makefile: 13,238; java: 8,492; yacc: 8,152; javascript: 7,083; cs: 6,013; asm: 4,786; ada: 1,681; pascal: 1,195; lex: 1,174; perl: 585; ruby: 373
file content (61 lines) | stat: -rw-r--r-- 1,674 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>

/* ICU4C */
#include <unicode/utypes.h>
#include <unicode/ustring.h>
#include <unicode/ucnv.h>
#include <unicode/unorm2.h>

#include "util.h"

int main(int argc, char **argv)
{
	 int i;

	 UErrorCode err;
	 UConverter *uc = ucnv_open("UTF8", &err);
	 if (U_FAILURE(err)) return EXIT_FAILURE;

	 const UNormalizer2 *NFKC = unorm2_getNFKCInstance(&err);
	 if (U_FAILURE(err)) return EXIT_FAILURE;
	 
	 for (i = 1; i < argc; ++i) {
		  if (argv[i][0] == '-') {
			   fprintf(stderr, "unrecognized option: %s\n", argv[i]);
			   return EXIT_FAILURE;
		  }

		  size_t len;
		  uint8_t *src = readfile(argv[i], &len);
		  if (!src) {
			   fprintf(stderr, "error reading %s\n", argv[i]);
			   return EXIT_FAILURE;
		  }

		  /* convert UTF8 data to ICU's UTF16 */
		  UChar *usrc = (UChar*) malloc(2*len * sizeof(UChar));
		  ucnv_toUChars(uc, usrc, 2*len, (char*) src, len, &err);
		  if (U_FAILURE(err)) return EXIT_FAILURE;
		  size_t ulen = u_strlen(usrc);

		  /* ICU's insane normalization API requires you to
			 know the size of the destination buffer in advance,
			 or alternatively to repeatedly try normalizing and
			 double the buffer size until it succeeds.  Here, I just
			 allocate a huge destination buffer to avoid the issue. */
		  UChar *udest = (UChar*) malloc(10*ulen * sizeof(UChar));

		  mytime start = gettime();
		  for (int i = 0; i < 100; ++i) {
			   unorm2_normalize(NFKC, usrc, ulen, udest, 10*ulen, &err);
			   if (U_FAILURE(err)) return EXIT_FAILURE;
		  }
		  printf("%s: %g\n", argv[i], elapsed(gettime(), start) / 100);
		  free(udest);
		  free(usrc);
		  free(src);
	 }

	 return EXIT_SUCCESS;
}