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