]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/ustring/ustring.cpp
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / samples / ustring / ustring.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3*
f3c0d7a5
A
4* © 2016 and later: Unicode, Inc. and others.
5* License & terms of use: http://www.unicode.org/copyright.html#License
6*
7*******************************************************************************
8*******************************************************************************
9*
b331163b 10* Copyright (C) 2000-2014, International Business Machines
b75a7d8f
A
11* Corporation and others. All Rights Reserved.
12*
13*******************************************************************************
14* file name: ustring.c
f3c0d7a5 15* encoding: UTF-8
b75a7d8f
A
16* tab size: 8 (not used)
17* indentation:4
18*
19* created on: 2000aug15
20* created by: Markus W. Scherer
21*
22* This file contains sample code that illustrates the use of Unicode strings
23* with ICU.
24*/
25
26#include <stdio.h>
27#include "unicode/utypes.h"
28#include "unicode/uchar.h"
29#include "unicode/locid.h"
30#include "unicode/ustring.h"
31#include "unicode/ucnv.h"
32#include "unicode/unistr.h"
33
f3c0d7a5
A
34#ifndef UPRV_LENGTHOF
35#define UPRV_LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
36#endif
37
b75a7d8f
A
38// helper functions -------------------------------------------------------- ***
39
40// default converter for the platform encoding
41static UConverter *cnv=NULL;
42
43static void
44printUString(const char *announce, const UChar *s, int32_t length) {
45 static char out[200];
46 UChar32 c;
47 int32_t i;
48 UErrorCode errorCode=U_ZERO_ERROR;
49
50 /*
51 * Convert to the "platform encoding". See notes in printUnicodeString().
52 * ucnv_fromUChars(), like most ICU APIs understands length==-1
53 * to mean that the string is NUL-terminated.
54 */
55 ucnv_fromUChars(cnv, out, sizeof(out), s, length, &errorCode);
56 if(U_FAILURE(errorCode) || errorCode==U_STRING_NOT_TERMINATED_WARNING) {
57 printf("%sproblem converting string from Unicode: %s\n", announce, u_errorName(errorCode));
58 return;
59 }
60
61 printf("%s%s {", announce, out);
62
63 /* output the code points (not code units) */
64 if(length>=0) {
65 /* s is not NUL-terminated */
66 for(i=0; i<length; /* U16_NEXT post-increments */) {
67 U16_NEXT(s, i, length, c);
68 printf(" %04x", c);
69 }
70 } else {
71 /* s is NUL-terminated */
72 for(i=0; /* condition in loop body */; /* U16_NEXT post-increments */) {
73 U16_NEXT(s, i, length, c);
74 if(c==0) {
75 break;
76 }
77 printf(" %04x", c);
78 }
79 }
80 printf(" }\n");
81}
82
83static void
84printUnicodeString(const char *announce, const UnicodeString &s) {
85 static char out[200];
86 int32_t i, length;
87
88 // output the string, converted to the platform encoding
89
90 // Note for Windows: The "platform encoding" defaults to the "ANSI codepage",
91 // which is different from the "OEM codepage" in the console window.
92 // However, if you pipe the output into a file and look at it with Notepad
93 // or similar, then "ANSI" characters will show correctly.
94 // Production code should be aware of what encoding is required,
95 // and use a UConverter or at least a charset name explicitly.
96 out[s.extract(0, 99, out)]=0;
97 printf("%s%s {", announce, out);
98
99 // output the code units (not code points)
100 length=s.length();
101 for(i=0; i<length; ++i) {
102 printf(" %04x", s.charAt(i));
103 }
104 printf(" }\n");
105}
106
107// sample code for utf.h macros -------------------------------------------- ***
108
109static void
110demo_utf_h_macros() {
111 static UChar input[]={ 0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062 };
112 UChar32 c;
113 int32_t i;
114 UBool isError;
115
116 printf("\n* demo_utf_h_macros() -------------- ***\n\n");
117
b331163b
A
118 printUString("iterate forward through: ", input, UPRV_LENGTHOF(input));
119 for(i=0; i<UPRV_LENGTHOF(input); /* U16_NEXT post-increments */) {
b75a7d8f
A
120 /* Iterating forwards
121 Codepoint at offset 0: U+0061
122 Codepoint at offset 1: U+10000
123 Codepoint at offset 3: U+10ffff
124 Codepoint at offset 5: U+0062
125 */
126 printf("Codepoint at offset %d: U+", i);
b331163b 127 U16_NEXT(input, i, UPRV_LENGTHOF(input), c);
b75a7d8f
A
128 printf("%04x\n", c);
129 }
130
131 puts("");
132
133 isError=FALSE;
134 i=1; /* write position, gets post-incremented so needs to be in an l-value */
b331163b 135 U16_APPEND(input, i, UPRV_LENGTHOF(input), 0x0062, isError);
b75a7d8f 136
b331163b
A
137 printUString("iterate backward through: ", input, UPRV_LENGTHOF(input));
138 for(i=UPRV_LENGTHOF(input); i>0; /* U16_PREV pre-decrements */) {
b75a7d8f
A
139 U16_PREV(input, 0, i, c);
140 /* Iterating backwards
141 Codepoint at offset 5: U+0062
142 Codepoint at offset 3: U+10ffff
143 Codepoint at offset 2: U+dc00 -- unpaired surrogate because lead surr. overwritten
144 Codepoint at offset 1: U+0062 -- by this BMP code point
145 Codepoint at offset 0: U+0061
146 */
147 printf("Codepoint at offset %d: U+%04x\n", i, c);
148 }
149}
150
151// sample code for Unicode strings in C ------------------------------------ ***
152
153static void demo_C_Unicode_strings() {
154 printf("\n* demo_C_Unicode_strings() --------- ***\n\n");
155
156 static const UChar text[]={ 0x41, 0x42, 0x43, 0 }; /* "ABC" */
157 static const UChar appendText[]={ 0x61, 0x62, 0x63, 0 }; /* "abc" */
158 static const UChar cmpText[]={ 0x61, 0x53, 0x73, 0x43, 0 }; /* "aSsC" */
159 UChar buffer[32];
160 int32_t compare;
161 int32_t length=u_strlen(text); /* length=3 */
162
163 /* simple ANSI C-style functions */
164 buffer[0]=0; /* empty, NUL-terminated string */
165 u_strncat(buffer, text, 1); /* append just n=1 character ('A') */
166 u_strcat(buffer, appendText); /* buffer=="Aabc" */
167 length=u_strlen(buffer); /* length=4 */
168 printUString("should be \"Aabc\": ", buffer, -1);
169
170 /* bitwise comparing buffer with text */
171 compare=u_strcmp(buffer, text);
172 if(compare<=0) {
173 printf("String comparison error, expected \"Aabc\" > \"ABC\"\n");
174 }
175
176 /* Build "A<sharp s>C" in the buffer... */
177 u_strcpy(buffer, text);
178 buffer[1]=0xdf; /* sharp s, case-compares equal to "ss" */
179 printUString("should be \"A<sharp s>C\": ", buffer, -1);
180
181 /* Compare two strings case-insensitively using full case folding */
182 compare=u_strcasecmp(buffer, cmpText, U_FOLD_CASE_DEFAULT);
183 if(compare!=0) {
184 printf("String case insensitive comparison error, expected \"AbC\" to be equal to \"ABC\"\n");
185 }
186}
187
188// sample code for case mappings with C APIs -------------------------------- ***
189
190static void demoCaseMapInC() {
191 /*
192 * input=
193 * "aB<capital sigma>"
194 * "iI<small dotless i><capital dotted I> "
195 * "<sharp s> <small lig. ffi>"
196 * "<small final sigma><small sigma><capital sigma>"
197 */
198 static const UChar input[]={
199 0x61, 0x42, 0x3a3,
200 0x69, 0x49, 0x131, 0x130, 0x20,
201 0xdf, 0x20, 0xfb03,
202 0x3c2, 0x3c3, 0x3a3, 0
203 };
204 UChar buffer[32];
205
206 UErrorCode errorCode;
207 UChar32 c;
208 int32_t i, j, length;
209 UBool isError;
210
211 printf("\n* demoCaseMapInC() ----------------- ***\n\n");
212
213 /*
214 * First, use simple case mapping functions which provide
215 * 1:1 code point mappings without context/locale ID.
216 *
217 * Note that some mappings will not be "right" because some "real"
218 * case mappings require context, depend on the locale ID,
219 * and/or result in a change in the number of code points.
220 */
221 printUString("input string: ", input, -1);
222
223 /* uppercase */
224 isError=FALSE;
b331163b 225 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) {
b75a7d8f
A
226 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminated */
227 if(c==0) {
228 break; /* stop at terminating NUL, no need to terminate buffer */
229 }
230 c=u_toupper(c);
b331163b 231 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
b75a7d8f
A
232 }
233 printUString("simple-uppercased: ", buffer, j);
234 /* lowercase */
235 isError=FALSE;
b331163b 236 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) {
b75a7d8f
A
237 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminated */
238 if(c==0) {
239 break; /* stop at terminating NUL, no need to terminate buffer */
240 }
241 c=u_tolower(c);
b331163b 242 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
b75a7d8f
A
243 }
244 printUString("simple-lowercased: ", buffer, j);
245 /* titlecase */
246 isError=FALSE;
b331163b 247 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) {
b75a7d8f
A
248 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminated */
249 if(c==0) {
250 break; /* stop at terminating NUL, no need to terminate buffer */
251 }
252 c=u_totitle(c);
b331163b 253 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
b75a7d8f
A
254 }
255 printUString("simple-titlecased: ", buffer, j);
256 /* case-fold/default */
257 isError=FALSE;
b331163b 258 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) {
b75a7d8f
A
259 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminated */
260 if(c==0) {
261 break; /* stop at terminating NUL, no need to terminate buffer */
262 }
263 c=u_foldCase(c, U_FOLD_CASE_DEFAULT);
b331163b 264 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
b75a7d8f
A
265 }
266 printUString("simple-case-folded/default: ", buffer, j);
267 /* case-fold/Turkic */
268 isError=FALSE;
b331163b 269 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) {
b75a7d8f
A
270 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminated */
271 if(c==0) {
272 break; /* stop at terminating NUL, no need to terminate buffer */
273 }
274 c=u_foldCase(c, U_FOLD_CASE_EXCLUDE_SPECIAL_I);
b331163b 275 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
b75a7d8f
A
276 }
277 printUString("simple-case-folded/Turkic: ", buffer, j);
278
279 /*
280 * Second, use full case mapping functions which provide
281 * 1:n code point mappings (n can be 0!) and are sensitive to context and locale ID.
282 *
283 * Note that lower/upper/titlecasing take a locale ID while case-folding
284 * has bit flag options instead, by design of the Unicode SpecialCasing.txt UCD file.
285 *
286 * Also, string titlecasing requires a BreakIterator to find starts of words.
287 * The sample code here passes in a NULL pointer; u_strToTitle() will open and close a default
288 * titlecasing BreakIterator automatically.
289 * For production code where many strings are titlecased it would be more efficient
290 * to open a BreakIterator externally and pass it in.
291 */
292 printUString("\ninput string: ", input, -1);
293
294 /* lowercase/English */
295 errorCode=U_ZERO_ERROR;
b331163b 296 length=u_strToLower(buffer, UPRV_LENGTHOF(buffer), input, -1, "en", &errorCode);
b75a7d8f
A
297 if(U_SUCCESS(errorCode)) {
298 printUString("full-lowercased/en: ", buffer, length);
299 } else {
300 printf("error in u_strToLower(en)=%ld error=%s\n", length, u_errorName(errorCode));
301 }
302 /* lowercase/Turkish */
303 errorCode=U_ZERO_ERROR;
b331163b 304 length=u_strToLower(buffer, UPRV_LENGTHOF(buffer), input, -1, "tr", &errorCode);
b75a7d8f
A
305 if(U_SUCCESS(errorCode)) {
306 printUString("full-lowercased/tr: ", buffer, length);
307 } else {
308 printf("error in u_strToLower(tr)=%ld error=%s\n", length, u_errorName(errorCode));
309 }
310 /* uppercase/English */
311 errorCode=U_ZERO_ERROR;
b331163b 312 length=u_strToUpper(buffer, UPRV_LENGTHOF(buffer), input, -1, "en", &errorCode);
b75a7d8f
A
313 if(U_SUCCESS(errorCode)) {
314 printUString("full-uppercased/en: ", buffer, length);
315 } else {
316 printf("error in u_strToUpper(en)=%ld error=%s\n", length, u_errorName(errorCode));
317 }
318 /* uppercase/Turkish */
319 errorCode=U_ZERO_ERROR;
b331163b 320 length=u_strToUpper(buffer, UPRV_LENGTHOF(buffer), input, -1, "tr", &errorCode);
b75a7d8f
A
321 if(U_SUCCESS(errorCode)) {
322 printUString("full-uppercased/tr: ", buffer, length);
323 } else {
324 printf("error in u_strToUpper(tr)=%ld error=%s\n", length, u_errorName(errorCode));
325 }
326 /* titlecase/English */
327 errorCode=U_ZERO_ERROR;
b331163b 328 length=u_strToTitle(buffer, UPRV_LENGTHOF(buffer), input, -1, NULL, "en", &errorCode);
b75a7d8f
A
329 if(U_SUCCESS(errorCode)) {
330 printUString("full-titlecased/en: ", buffer, length);
331 } else {
332 printf("error in u_strToTitle(en)=%ld error=%s\n", length, u_errorName(errorCode));
333 }
334 /* titlecase/Turkish */
335 errorCode=U_ZERO_ERROR;
b331163b 336 length=u_strToTitle(buffer, UPRV_LENGTHOF(buffer), input, -1, NULL, "tr", &errorCode);
b75a7d8f
A
337 if(U_SUCCESS(errorCode)) {
338 printUString("full-titlecased/tr: ", buffer, length);
339 } else {
340 printf("error in u_strToTitle(tr)=%ld error=%s\n", length, u_errorName(errorCode));
341 }
342 /* case-fold/default */
343 errorCode=U_ZERO_ERROR;
b331163b 344 length=u_strFoldCase(buffer, UPRV_LENGTHOF(buffer), input, -1, U_FOLD_CASE_DEFAULT, &errorCode);
b75a7d8f
A
345 if(U_SUCCESS(errorCode)) {
346 printUString("full-case-folded/default: ", buffer, length);
347 } else {
348 printf("error in u_strFoldCase(default)=%ld error=%s\n", length, u_errorName(errorCode));
349 }
350 /* case-fold/Turkic */
351 errorCode=U_ZERO_ERROR;
b331163b 352 length=u_strFoldCase(buffer, UPRV_LENGTHOF(buffer), input, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &errorCode);
b75a7d8f
A
353 if(U_SUCCESS(errorCode)) {
354 printUString("full-case-folded/Turkic: ", buffer, length);
355 } else {
356 printf("error in u_strFoldCase(Turkic)=%ld error=%s\n", length, u_errorName(errorCode));
357 }
358}
359
360// sample code for case mappings with C++ APIs ------------------------------ ***
361
362static void demoCaseMapInCPlusPlus() {
363 /*
364 * input=
365 * "aB<capital sigma>"
366 * "iI<small dotless i><capital dotted I> "
367 * "<sharp s> <small lig. ffi>"
368 * "<small final sigma><small sigma><capital sigma>"
369 */
370 static const UChar input[]={
371 0x61, 0x42, 0x3a3,
372 0x69, 0x49, 0x131, 0x130, 0x20,
373 0xdf, 0x20, 0xfb03,
374 0x3c2, 0x3c3, 0x3a3, 0
375 };
376
377 printf("\n* demoCaseMapInCPlusPlus() --------- ***\n\n");
378
379 UnicodeString s(input), t;
380 const Locale &en=Locale::getEnglish();
381 Locale tr("tr");
382
383 /*
384 * Full case mappings as in demoCaseMapInC(), using UnicodeString functions.
385 * These functions modify the string object itself.
386 * Since we want to keep the input string around, we copy it each time
387 * and case-map the copy.
388 */
389 printUnicodeString("input string: ", s);
390
391 /* lowercase/English */
392 printUnicodeString("full-lowercased/en: ", (t=s).toLower(en));
393 /* lowercase/Turkish */
394 printUnicodeString("full-lowercased/tr: ", (t=s).toLower(tr));
395 /* uppercase/English */
396 printUnicodeString("full-uppercased/en: ", (t=s).toUpper(en));
397 /* uppercase/Turkish */
398 printUnicodeString("full-uppercased/tr: ", (t=s).toUpper(tr));
399 /* titlecase/English */
400 printUnicodeString("full-titlecased/en: ", (t=s).toTitle(NULL, en));
401 /* titlecase/Turkish */
402 printUnicodeString("full-titlecased/tr: ", (t=s).toTitle(NULL, tr));
403 /* case-folde/default */
404 printUnicodeString("full-case-folded/default: ", (t=s).foldCase(U_FOLD_CASE_DEFAULT));
405 /* case-folde/Turkic */
406 printUnicodeString("full-case-folded/Turkic: ", (t=s).foldCase(U_FOLD_CASE_EXCLUDE_SPECIAL_I));
407}
408
409// sample code for UnicodeString storage models ----------------------------- ***
410
411static const UChar readonly[]={
412 0x61, 0x31, 0x20ac
413};
414static UChar writeable[]={
415 0x62, 0x32, 0xdbc0, 0xdc01 // includes a surrogate pair for a supplementary code point
416};
417static char out[100];
418
419static void
420demoUnicodeStringStorage() {
421 // These sample code lines illustrate how to use UnicodeString, and the
422 // comments tell what happens internally. There are no APIs to observe
423 // most of this programmatically, except for stepping into the code
424 // with a debugger.
425 // This is by design to hide such details from the user.
426 int32_t i;
427
428 printf("\n* demoUnicodeStringStorage() ------- ***\n\n");
429
430 // * UnicodeString with internally stored contents
431 // instantiate a UnicodeString from a single code point
432 // the few (2) UChars will be stored in the object itself
433 UnicodeString one((UChar32)0x24001);
434 // this copies the few UChars into the "two" object
435 UnicodeString two=one;
436 printf("length of short string copy: %d\n", two.length());
437 // set "one" to contain the 3 UChars from readonly
438 // this setTo() variant copies the characters
b331163b 439 one.setTo(readonly, UPRV_LENGTHOF(readonly));
b75a7d8f
A
440
441 // * UnicodeString with allocated contents
442 // build a longer string that will not fit into the object's buffer
b331163b 443 one+=UnicodeString(writeable, UPRV_LENGTHOF(writeable));
b75a7d8f
A
444 one+=one;
445 one+=one;
446 printf("length of longer string: %d\n", one.length());
447 // copying will use the same allocated buffer and increment the reference
448 // counter
449 two=one;
450 printf("length of longer string copy: %d\n", two.length());
451
452 // * UnicodeString using readonly-alias to a const UChar array
453 // construct a string that aliases a readonly buffer
b331163b 454 UnicodeString three(FALSE, readonly, UPRV_LENGTHOF(readonly));
b75a7d8f
A
455 printUnicodeString("readonly-alias string: ", three);
456 // copy-on-write: any modification to the string results in
457 // a copy to either the internal buffer or to a newly allocated one
458 three.setCharAt(1, 0x39);
459 printUnicodeString("readonly-aliasing string after modification: ", three);
460 // the aliased array is not modified
461 for(i=0; i<three.length(); ++i) {
462 printf("readonly buffer[%d] after modifying its string: 0x%lx\n",
463 i, readonly[i]);
464 }
465 // setTo() readonly alias
b331163b 466 one.setTo(FALSE, writeable, UPRV_LENGTHOF(writeable));
b75a7d8f
A
467 // copying the readonly-alias object with fastCopyFrom() (new in ICU 2.4)
468 // will readonly-alias the same buffer
469 two.fastCopyFrom(one);
470 printUnicodeString("fastCopyFrom(readonly alias of \"writeable\" array): ", two);
471 printf("verify that a fastCopyFrom(readonly alias) uses the same buffer pointer: %d (should be 1)\n",
472 one.getBuffer()==two.getBuffer());
473 // a normal assignment will clone the contents (new in ICU 2.4)
474 two=one;
475 printf("verify that a regular copy of a readonly alias uses a different buffer pointer: %d (should be 0)\n",
476 one.getBuffer()==two.getBuffer());
477
478 // * UnicodeString using writeable-alias to a non-const UChar array
b331163b 479 UnicodeString four(writeable, UPRV_LENGTHOF(writeable), UPRV_LENGTHOF(writeable));
b75a7d8f
A
480 printUnicodeString("writeable-alias string: ", four);
481 // a modification writes through to the buffer
482 four.setCharAt(1, 0x39);
483 for(i=0; i<four.length(); ++i) {
484 printf("writeable-alias backing buffer[%d]=0x%lx "
485 "after modification\n", i, writeable[i]);
486 }
487 // a copy will not alias any more;
488 // instead, it will get a copy of the contents into allocated memory
489 two=four;
490 two.setCharAt(1, 0x21);
491 for(i=0; i<two.length(); ++i) {
492 printf("writeable-alias backing buffer[%d]=0x%lx after "
493 "modification of string copy\n", i, writeable[i]);
494 }
495 // setTo() writeable alias, capacity==length
b331163b 496 one.setTo(writeable, UPRV_LENGTHOF(writeable), UPRV_LENGTHOF(writeable));
b75a7d8f
A
497 // grow the string - it will not fit into the backing buffer any more
498 // and will get copied before modification
499 one.append((UChar)0x40);
500 // shrink it back so it would fit
501 one.truncate(one.length()-1);
502 // we still operate on the copy
503 one.setCharAt(1, 0x25);
504 printf("string after growing too much and then shrinking[1]=0x%lx\n"
505 " backing store for this[1]=0x%lx\n",
506 one.charAt(1), writeable[1]);
507 // if we need it in the original buffer, then extract() to it
508 // extract() does not do anything if the string aliases that same buffer
509 // i=min(one.length(), length of array)
b331163b 510 if(one.length()<UPRV_LENGTHOF(writeable)) {
b75a7d8f
A
511 i=one.length();
512 } else {
b331163b 513 i=UPRV_LENGTHOF(writeable);
b75a7d8f
A
514 }
515 one.extract(0, i, writeable);
b331163b 516 for(i=0; i<UPRV_LENGTHOF(writeable); ++i) {
b75a7d8f
A
517 printf("writeable-alias backing buffer[%d]=0x%lx after re-extract\n",
518 i, writeable[i]);
519 }
520}
521
522// sample code for UnicodeString instantiations ----------------------------- ***
523
524static void
525demoUnicodeStringInit() {
526 // *** Make sure to read about invariant characters in utypes.h! ***
527 // Initialization of Unicode strings from C literals works _only_ for
528 // invariant characters!
529
530 printf("\n* demoUnicodeStringInit() ---------- ***\n\n");
531
532 // the string literal is 32 chars long - this must be counted for the macro
533 UnicodeString invariantOnly=UNICODE_STRING("such characters are safe 123 %-.", 32);
534
535 /*
536 * In C, we need two macros: one to declare the UChar[] array, and
537 * one to populate it; the second one is a noop on platforms where
538 * wchar_t is compatible with UChar and ASCII-based.
539 * The length of the string literal must be counted for both macros.
540 */
541 /* declare the invString array for the string */
542 U_STRING_DECL(invString, "such characters are safe 123 %-.", 32);
543 /* populate it with the characters */
544 U_STRING_INIT(invString, "such characters are safe 123 %-.", 32);
545
546 // compare the C and C++ strings
547 printf("C and C++ Unicode strings are equal: %d\n", invariantOnly==UnicodeString(TRUE, invString, 32));
548
549 /*
550 * convert between char * and UChar * strings that
551 * contain only invariant characters
552 */
553 static const char *cs1="such characters are safe 123 %-.";
554 static UChar us1[40];
555 static char cs2[40];
556 u_charsToUChars(cs1, us1, 33); /* include the terminating NUL */
557 u_UCharsToChars(us1, cs2, 33);
558 printf("char * -> UChar * -> char * with only "
559 "invariant characters: \"%s\"\n",
560 cs2);
561
562 // initialize a UnicodeString from a string literal that contains
563 // escape sequences written with invariant characters
564 // do not forget to duplicate the backslashes for ICU to see them
565 // then, count each double backslash only once!
566 UnicodeString german=UNICODE_STRING(
567 "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", 64).
568 unescape();
569 printUnicodeString("german UnicodeString from unescaping:\n ", german);
570
571 /*
572 * C: convert and unescape a char * string with only invariant
573 * characters to fill a UChar * string
574 */
575 UChar buffer[200];
576 int32_t length;
577 length=u_unescape(
578 "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n",
b331163b 579 buffer, UPRV_LENGTHOF(buffer));
b75a7d8f
A
580 printf("german C Unicode string from char * unescaping: (length %d)\n ", length);
581 printUnicodeString("", UnicodeString(buffer));
582}
583
584extern int
585main(int argc, const char *argv[]) {
586 UErrorCode errorCode=U_ZERO_ERROR;
587
588 // Note: Using a global variable for any object is not exactly thread-safe...
589
590 // You can change this call to e.g. ucnv_open("UTF-8", &errorCode) if you pipe
591 // the output to a file and look at it with a Unicode-capable editor.
592 // This will currently affect only the printUString() function, see the code above.
593 // printUnicodeString() could use this, too, by changing to an extract() overload
594 // that takes a UConverter argument.
595 cnv=ucnv_open(NULL, &errorCode);
596 if(U_FAILURE(errorCode)) {
597 fprintf(stderr, "error %s opening the default converter\n", u_errorName(errorCode));
598 return errorCode;
599 }
600
601 ucnv_setFromUCallBack(cnv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, NULL, NULL, &errorCode);
602 if(U_FAILURE(errorCode)) {
603 fprintf(stderr, "error %s setting the escape callback in the default converter\n", u_errorName(errorCode));
604 ucnv_close(cnv);
605 return errorCode;
606 }
607
608 demo_utf_h_macros();
609 demo_C_Unicode_strings();
610 demoCaseMapInC();
611 demoCaseMapInCPlusPlus();
612 demoUnicodeStringStorage();
613 demoUnicodeStringInit();
614
615 ucnv_close(cnv);
616 return 0;
617}