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