• 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#License
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 "unicode/uchar.h"
21 #include "unicode/ustring.h"
22 #include "unicode/utypes.h"
23 #include "unicode/ustdio.h"
24 
25 /* Note: don't use 'k' or 'K' because we might be on EBCDIC */
26 
c_main(UFILE * out)27 int c_main(UFILE *out)
28 {
29   UChar32 ch;
30   UErrorCode errorCode = U_ZERO_ERROR;
31   static const UChar upper[] = {0x61, 0x42, 0x49, 0}; /* upper = "aBI" */
32   static const UChar lower[] = {0x61, 0x42, 0x69, 0}; /* lower = "abi" */
33   /* unfold = "aB LATIN SMALL LETTER DOTLESS I" */
34   static const UChar unfold[] = {0x61, 0x42, 0x131, 0} ;
35   UChar buffer[32];
36   const UChar char_k = 0x006b; /* 'k' */
37   const UChar char_K = 0x004b; /* 'K' */
38 
39   int length;
40 
41   printf("** C Case Mapping Sample\n");
42 
43   /* uchar.h APIs, single character case mapping */
44   ch = u_toupper(char_k); /* ch = 'K' */
45   u_fprintf(out, "toupper(%C) = %C\n", char_k, ch);
46   ch = u_tolower(ch); /* ch = 'k' */
47   u_fprintf(out, "tolower() = %C\n", ch);
48   ch = u_totitle(char_k); /* ch = 'K' */
49   u_fprintf(out, "totitle(%C) = %C\n", char_k, ch);
50   ch = u_foldCase(char_K, U_FOLD_CASE_DEFAULT); /* ch = 'k' */
51   u_fprintf(out, "u_foldCase(%C, U_FOLD_CASE_DEFAULT) = %C\n", char_K, (UChar) ch);
52 
53   /* ustring.h APIs, UChar * string case mapping with a Turkish locale */
54   /* result buffer = "ab?" latin small letter a, latin small letter b, latin
55      small letter dotless i */
56   length = u_strToLower(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
57                         sizeof(upper)/sizeof(upper[0]), "tr", &errorCode);
58   if(U_FAILURE(errorCode) || buffer[length]!=0) {
59     u_fprintf(out, "error in u_strToLower(Turkish locale)=%ld error=%s\n", length,
60            u_errorName(errorCode));
61   }
62 
63   u_fprintf(out, "u_strToLower(%S, turkish) -> %S\n", upper, buffer);
64 
65 
66   /* ustring.h APIs, UChar * string case mapping with a Engish locale */
67   /* result buffer = "ABI" latin CAPITAL letter A, latin capital letter B,
68      latin capital letter I */
69   length = u_strToUpper(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
70                         sizeof(upper)/sizeof(upper[0]), "en", &errorCode);
71   if(U_FAILURE(errorCode) || buffer[length]!=0) {
72     u_fprintf(out, "error in u_strToLower(English locale)=%ld error=%s\n", length,
73               u_errorName(errorCode));
74   }
75   u_fprintf(out, "u_strToUpper(%S, english) -> %S\n", lower, buffer);
76 
77 
78   /* ustring.h APIs, UChar * string case folding */
79   /* result buffer = "abi" */
80   length = u_strFoldCase(buffer, sizeof(buffer)/sizeof(buffer[0]), unfold,
81                          sizeof(unfold)/sizeof(unfold[0]), U_FOLD_CASE_DEFAULT,
82                          &errorCode);
83   if(U_FAILURE(errorCode) || buffer[length]!=0) {
84     u_fprintf(out, "error in u_strFoldCase()=%ld error=%s\n", length,
85            u_errorName(errorCode));
86   }
87   u_fprintf(out, "u_strFoldCase(%S, U_FOLD_CASE_DEFAULT) -> %S\n", unfold, buffer);
88   u_fprintf(out, "\n** end of C sample\n");
89 
90   return 0;
91 }
92