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