• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   © 2016 and later: Unicode, Inc. and others.
5 *   License & terms of use: http://www.unicode.org/copyright.html
6 *
7 *******************************************************************************
8 *******************************************************************************
9 *
10 *   Copyright (C) 2003-2004, International Business Machines
11 *   Corporation and others.  All Rights Reserved.
12 *
13 ****
14 *
15 *   Case folding examples, in C
16 *
17 *******************************************************************************
18 */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include "unicode/uchar.h"
24 #include "unicode/ustring.h"
25 #include "unicode/utypes.h"
26 #include "unicode/ustdio.h"
27 
28 /* Note: don't use 'k' or 'K' because we might be on EBCDIC */
29 
c_main(UFILE * out)30 int c_main(UFILE *out)
31 {
32   UChar32 ch;
33   UErrorCode errorCode = U_ZERO_ERROR;
34   static const UChar upper[] = {0x61, 0x42, 0x49, 0}; /* upper = "aBI" */
35   static const UChar lower[] = {0x61, 0x42, 0x69, 0}; /* lower = "abi" */
36   /* unfold = "aB LATIN SMALL LETTER DOTLESS I" */
37   static const UChar unfold[] = {0x61, 0x42, 0x131, 0} ;
38   UChar buffer[32];
39   const UChar char_k = 0x006b; /* 'k' */
40   const UChar char_K = 0x004b; /* 'K' */
41 
42   int length;
43 
44   printf("** C Case Mapping Sample\n");
45 
46   /* uchar.h APIs, single character case mapping */
47   ch = u_toupper(char_k); /* ch = 'K' */
48   u_fprintf(out, "toupper(%C) = %C\n", char_k, ch);
49   ch = u_tolower(ch); /* ch = 'k' */
50   u_fprintf(out, "tolower() = %C\n", ch);
51   ch = u_totitle(char_k); /* ch = 'K' */
52   u_fprintf(out, "totitle(%C) = %C\n", char_k, ch);
53   ch = u_foldCase(char_K, U_FOLD_CASE_DEFAULT); /* ch = 'k' */
54   u_fprintf(out, "u_foldCase(%C, U_FOLD_CASE_DEFAULT) = %C\n", char_K, (UChar) ch);
55 
56   /* ustring.h APIs, UChar * string case mapping with a Turkish locale */
57   /* result buffer = "ab?" latin small letter a, latin small letter b, latin
58      small letter dotless i */
59   length = u_strToLower(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
60                         sizeof(upper)/sizeof(upper[0]), "tr", &errorCode);
61   if(U_FAILURE(errorCode) || buffer[length]!=0) {
62     u_fprintf(out, "error in u_strToLower(Turkish locale)=%ld error=%s\n", length,
63            u_errorName(errorCode));
64   }
65 
66   u_fprintf(out, "u_strToLower(%S, turkish) -> %S\n", upper, buffer);
67 
68 
69   /* ustring.h APIs, UChar * string case mapping with a English locale */
70   /* result buffer = "ABI" latin CAPITAL letter A, latin capital letter B,
71      latin capital letter I */
72   length = u_strToUpper(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
73                         sizeof(upper)/sizeof(upper[0]), "en", &errorCode);
74   if(U_FAILURE(errorCode) || buffer[length]!=0) {
75     u_fprintf(out, "error in u_strToLower(English locale)=%ld error=%s\n", length,
76               u_errorName(errorCode));
77   }
78   u_fprintf(out, "u_strToUpper(%S, english) -> %S\n", lower, buffer);
79 
80 
81   /* ustring.h APIs, UChar * string case folding */
82   /* result buffer = "abi" */
83   length = u_strFoldCase(buffer, sizeof(buffer)/sizeof(buffer[0]), unfold,
84                          sizeof(unfold)/sizeof(unfold[0]), U_FOLD_CASE_DEFAULT,
85                          &errorCode);
86   if(U_FAILURE(errorCode) || buffer[length]!=0) {
87     u_fprintf(out, "error in u_strFoldCase()=%ld error=%s\n", length,
88            u_errorName(errorCode));
89   }
90   u_fprintf(out, "u_strFoldCase(%S, U_FOLD_CASE_DEFAULT) -> %S\n", unfold, buffer);
91   u_fprintf(out, "\n** end of C sample\n");
92 
93   return 0;
94 }
95