2 *******************************************************************************
4 * Copyright (C) 2000-2014, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2000aug15
14 * created by: Markus W. Scherer
16 * This file contains sample code that illustrates the use of Unicode strings
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"
28 // helper functions -------------------------------------------------------- ***
30 // default converter for the platform encoding
31 static UConverter
*cnv
=NULL
;
34 printUString(const char *announce
, const UChar
*s
, int32_t length
) {
38 UErrorCode errorCode
=U_ZERO_ERROR
;
41 * Convert to the "platform encoding". See notes in printUnicodeString().
42 * ucnv_fromUChars(), like most ICU APIs understands length==-1
43 * to mean that the string is NUL-terminated.
45 ucnv_fromUChars(cnv
, out
, sizeof(out
), s
, length
, &errorCode
);
46 if(U_FAILURE(errorCode
) || errorCode
==U_STRING_NOT_TERMINATED_WARNING
) {
47 printf("%sproblem converting string from Unicode: %s\n", announce
, u_errorName(errorCode
));
51 printf("%s%s {", announce
, out
);
53 /* output the code points (not code units) */
55 /* s is not NUL-terminated */
56 for(i
=0; i
<length
; /* U16_NEXT post-increments */) {
57 U16_NEXT(s
, i
, length
, c
);
61 /* s is NUL-terminated */
62 for(i
=0; /* condition in loop body */; /* U16_NEXT post-increments */) {
63 U16_NEXT(s
, i
, length
, c
);
74 printUnicodeString(const char *announce
, const UnicodeString
&s
) {
78 // output the string, converted to the platform encoding
80 // Note for Windows: The "platform encoding" defaults to the "ANSI codepage",
81 // which is different from the "OEM codepage" in the console window.
82 // However, if you pipe the output into a file and look at it with Notepad
83 // or similar, then "ANSI" characters will show correctly.
84 // Production code should be aware of what encoding is required,
85 // and use a UConverter or at least a charset name explicitly.
86 out
[s
.extract(0, 99, out
)]=0;
87 printf("%s%s {", announce
, out
);
89 // output the code units (not code points)
91 for(i
=0; i
<length
; ++i
) {
92 printf(" %04x", s
.charAt(i
));
97 // sample code for utf.h macros -------------------------------------------- ***
100 demo_utf_h_macros() {
101 static UChar input
[]={ 0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062 };
106 printf("\n* demo_utf_h_macros() -------------- ***\n\n");
108 printUString("iterate forward through: ", input
, UPRV_LENGTHOF(input
));
109 for(i
=0; i
<UPRV_LENGTHOF(input
); /* U16_NEXT post-increments */) {
110 /* Iterating forwards
111 Codepoint at offset 0: U+0061
112 Codepoint at offset 1: U+10000
113 Codepoint at offset 3: U+10ffff
114 Codepoint at offset 5: U+0062
116 printf("Codepoint at offset %d: U+", i
);
117 U16_NEXT(input
, i
, UPRV_LENGTHOF(input
), c
);
124 i
=1; /* write position, gets post-incremented so needs to be in an l-value */
125 U16_APPEND(input
, i
, UPRV_LENGTHOF(input
), 0x0062, isError
);
127 printUString("iterate backward through: ", input
, UPRV_LENGTHOF(input
));
128 for(i
=UPRV_LENGTHOF(input
); i
>0; /* U16_PREV pre-decrements */) {
129 U16_PREV(input
, 0, i
, c
);
130 /* Iterating backwards
131 Codepoint at offset 5: U+0062
132 Codepoint at offset 3: U+10ffff
133 Codepoint at offset 2: U+dc00 -- unpaired surrogate because lead surr. overwritten
134 Codepoint at offset 1: U+0062 -- by this BMP code point
135 Codepoint at offset 0: U+0061
137 printf("Codepoint at offset %d: U+%04x\n", i
, c
);
141 // sample code for Unicode strings in C ------------------------------------ ***
143 static void demo_C_Unicode_strings() {
144 printf("\n* demo_C_Unicode_strings() --------- ***\n\n");
146 static const UChar text
[]={ 0x41, 0x42, 0x43, 0 }; /* "ABC" */
147 static const UChar appendText
[]={ 0x61, 0x62, 0x63, 0 }; /* "abc" */
148 static const UChar cmpText
[]={ 0x61, 0x53, 0x73, 0x43, 0 }; /* "aSsC" */
151 int32_t length
=u_strlen(text
); /* length=3 */
153 /* simple ANSI C-style functions */
154 buffer
[0]=0; /* empty, NUL-terminated string */
155 u_strncat(buffer
, text
, 1); /* append just n=1 character ('A') */
156 u_strcat(buffer
, appendText
); /* buffer=="Aabc" */
157 length
=u_strlen(buffer
); /* length=4 */
158 printUString("should be \"Aabc\": ", buffer
, -1);
160 /* bitwise comparing buffer with text */
161 compare
=u_strcmp(buffer
, text
);
163 printf("String comparison error, expected \"Aabc\" > \"ABC\"\n");
166 /* Build "A<sharp s>C" in the buffer... */
167 u_strcpy(buffer
, text
);
168 buffer
[1]=0xdf; /* sharp s, case-compares equal to "ss" */
169 printUString("should be \"A<sharp s>C\": ", buffer
, -1);
171 /* Compare two strings case-insensitively using full case folding */
172 compare
=u_strcasecmp(buffer
, cmpText
, U_FOLD_CASE_DEFAULT
);
174 printf("String case insensitive comparison error, expected \"AbC\" to be equal to \"ABC\"\n");
178 // sample code for case mappings with C APIs -------------------------------- ***
180 static void demoCaseMapInC() {
183 * "aB<capital sigma>"
184 * "iI<small dotless i><capital dotted I> "
185 * "<sharp s> <small lig. ffi>"
186 * "<small final sigma><small sigma><capital sigma>"
188 static const UChar input
[]={
190 0x69, 0x49, 0x131, 0x130, 0x20,
192 0x3c2, 0x3c3, 0x3a3, 0
196 UErrorCode errorCode
;
198 int32_t i
, j
, length
;
201 printf("\n* demoCaseMapInC() ----------------- ***\n\n");
204 * First, use simple case mapping functions which provide
205 * 1:1 code point mappings without context/locale ID.
207 * Note that some mappings will not be "right" because some "real"
208 * case mappings require context, depend on the locale ID,
209 * and/or result in a change in the number of code points.
211 printUString("input string: ", input
, -1);
215 for(i
=j
=0; j
<UPRV_LENGTHOF(buffer
) && !isError
; /* U16_NEXT post-increments */) {
216 U16_NEXT(input
, i
, INT32_MAX
, c
); /* without length because NUL-terminated */
218 break; /* stop at terminating NUL, no need to terminate buffer */
221 U16_APPEND(buffer
, j
, UPRV_LENGTHOF(buffer
), c
, isError
);
223 printUString("simple-uppercased: ", buffer
, j
);
226 for(i
=j
=0; j
<UPRV_LENGTHOF(buffer
) && !isError
; /* U16_NEXT post-increments */) {
227 U16_NEXT(input
, i
, INT32_MAX
, c
); /* without length because NUL-terminated */
229 break; /* stop at terminating NUL, no need to terminate buffer */
232 U16_APPEND(buffer
, j
, UPRV_LENGTHOF(buffer
), c
, isError
);
234 printUString("simple-lowercased: ", buffer
, j
);
237 for(i
=j
=0; j
<UPRV_LENGTHOF(buffer
) && !isError
; /* U16_NEXT post-increments */) {
238 U16_NEXT(input
, i
, INT32_MAX
, c
); /* without length because NUL-terminated */
240 break; /* stop at terminating NUL, no need to terminate buffer */
243 U16_APPEND(buffer
, j
, UPRV_LENGTHOF(buffer
), c
, isError
);
245 printUString("simple-titlecased: ", buffer
, j
);
246 /* case-fold/default */
248 for(i
=j
=0; j
<UPRV_LENGTHOF(buffer
) && !isError
; /* U16_NEXT post-increments */) {
249 U16_NEXT(input
, i
, INT32_MAX
, c
); /* without length because NUL-terminated */
251 break; /* stop at terminating NUL, no need to terminate buffer */
253 c
=u_foldCase(c
, U_FOLD_CASE_DEFAULT
);
254 U16_APPEND(buffer
, j
, UPRV_LENGTHOF(buffer
), c
, isError
);
256 printUString("simple-case-folded/default: ", buffer
, j
);
257 /* case-fold/Turkic */
259 for(i
=j
=0; j
<UPRV_LENGTHOF(buffer
) && !isError
; /* U16_NEXT post-increments */) {
260 U16_NEXT(input
, i
, INT32_MAX
, c
); /* without length because NUL-terminated */
262 break; /* stop at terminating NUL, no need to terminate buffer */
264 c
=u_foldCase(c
, U_FOLD_CASE_EXCLUDE_SPECIAL_I
);
265 U16_APPEND(buffer
, j
, UPRV_LENGTHOF(buffer
), c
, isError
);
267 printUString("simple-case-folded/Turkic: ", buffer
, j
);
270 * Second, use full case mapping functions which provide
271 * 1:n code point mappings (n can be 0!) and are sensitive to context and locale ID.
273 * Note that lower/upper/titlecasing take a locale ID while case-folding
274 * has bit flag options instead, by design of the Unicode SpecialCasing.txt UCD file.
276 * Also, string titlecasing requires a BreakIterator to find starts of words.
277 * The sample code here passes in a NULL pointer; u_strToTitle() will open and close a default
278 * titlecasing BreakIterator automatically.
279 * For production code where many strings are titlecased it would be more efficient
280 * to open a BreakIterator externally and pass it in.
282 printUString("\ninput string: ", input
, -1);
284 /* lowercase/English */
285 errorCode
=U_ZERO_ERROR
;
286 length
=u_strToLower(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, "en", &errorCode
);
287 if(U_SUCCESS(errorCode
)) {
288 printUString("full-lowercased/en: ", buffer
, length
);
290 printf("error in u_strToLower(en)=%ld error=%s\n", length
, u_errorName(errorCode
));
292 /* lowercase/Turkish */
293 errorCode
=U_ZERO_ERROR
;
294 length
=u_strToLower(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, "tr", &errorCode
);
295 if(U_SUCCESS(errorCode
)) {
296 printUString("full-lowercased/tr: ", buffer
, length
);
298 printf("error in u_strToLower(tr)=%ld error=%s\n", length
, u_errorName(errorCode
));
300 /* uppercase/English */
301 errorCode
=U_ZERO_ERROR
;
302 length
=u_strToUpper(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, "en", &errorCode
);
303 if(U_SUCCESS(errorCode
)) {
304 printUString("full-uppercased/en: ", buffer
, length
);
306 printf("error in u_strToUpper(en)=%ld error=%s\n", length
, u_errorName(errorCode
));
308 /* uppercase/Turkish */
309 errorCode
=U_ZERO_ERROR
;
310 length
=u_strToUpper(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, "tr", &errorCode
);
311 if(U_SUCCESS(errorCode
)) {
312 printUString("full-uppercased/tr: ", buffer
, length
);
314 printf("error in u_strToUpper(tr)=%ld error=%s\n", length
, u_errorName(errorCode
));
316 /* titlecase/English */
317 errorCode
=U_ZERO_ERROR
;
318 length
=u_strToTitle(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, NULL
, "en", &errorCode
);
319 if(U_SUCCESS(errorCode
)) {
320 printUString("full-titlecased/en: ", buffer
, length
);
322 printf("error in u_strToTitle(en)=%ld error=%s\n", length
, u_errorName(errorCode
));
324 /* titlecase/Turkish */
325 errorCode
=U_ZERO_ERROR
;
326 length
=u_strToTitle(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, NULL
, "tr", &errorCode
);
327 if(U_SUCCESS(errorCode
)) {
328 printUString("full-titlecased/tr: ", buffer
, length
);
330 printf("error in u_strToTitle(tr)=%ld error=%s\n", length
, u_errorName(errorCode
));
332 /* case-fold/default */
333 errorCode
=U_ZERO_ERROR
;
334 length
=u_strFoldCase(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, U_FOLD_CASE_DEFAULT
, &errorCode
);
335 if(U_SUCCESS(errorCode
)) {
336 printUString("full-case-folded/default: ", buffer
, length
);
338 printf("error in u_strFoldCase(default)=%ld error=%s\n", length
, u_errorName(errorCode
));
340 /* case-fold/Turkic */
341 errorCode
=U_ZERO_ERROR
;
342 length
=u_strFoldCase(buffer
, UPRV_LENGTHOF(buffer
), input
, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I
, &errorCode
);
343 if(U_SUCCESS(errorCode
)) {
344 printUString("full-case-folded/Turkic: ", buffer
, length
);
346 printf("error in u_strFoldCase(Turkic)=%ld error=%s\n", length
, u_errorName(errorCode
));
350 // sample code for case mappings with C++ APIs ------------------------------ ***
352 static void demoCaseMapInCPlusPlus() {
355 * "aB<capital sigma>"
356 * "iI<small dotless i><capital dotted I> "
357 * "<sharp s> <small lig. ffi>"
358 * "<small final sigma><small sigma><capital sigma>"
360 static const UChar input
[]={
362 0x69, 0x49, 0x131, 0x130, 0x20,
364 0x3c2, 0x3c3, 0x3a3, 0
367 printf("\n* demoCaseMapInCPlusPlus() --------- ***\n\n");
369 UnicodeString
s(input
), t
;
370 const Locale
&en
=Locale::getEnglish();
374 * Full case mappings as in demoCaseMapInC(), using UnicodeString functions.
375 * These functions modify the string object itself.
376 * Since we want to keep the input string around, we copy it each time
377 * and case-map the copy.
379 printUnicodeString("input string: ", s
);
381 /* lowercase/English */
382 printUnicodeString("full-lowercased/en: ", (t
=s
).toLower(en
));
383 /* lowercase/Turkish */
384 printUnicodeString("full-lowercased/tr: ", (t
=s
).toLower(tr
));
385 /* uppercase/English */
386 printUnicodeString("full-uppercased/en: ", (t
=s
).toUpper(en
));
387 /* uppercase/Turkish */
388 printUnicodeString("full-uppercased/tr: ", (t
=s
).toUpper(tr
));
389 /* titlecase/English */
390 printUnicodeString("full-titlecased/en: ", (t
=s
).toTitle(NULL
, en
));
391 /* titlecase/Turkish */
392 printUnicodeString("full-titlecased/tr: ", (t
=s
).toTitle(NULL
, tr
));
393 /* case-folde/default */
394 printUnicodeString("full-case-folded/default: ", (t
=s
).foldCase(U_FOLD_CASE_DEFAULT
));
395 /* case-folde/Turkic */
396 printUnicodeString("full-case-folded/Turkic: ", (t
=s
).foldCase(U_FOLD_CASE_EXCLUDE_SPECIAL_I
));
399 // sample code for UnicodeString storage models ----------------------------- ***
401 static const UChar readonly
[]={
404 static UChar writeable
[]={
405 0x62, 0x32, 0xdbc0, 0xdc01 // includes a surrogate pair for a supplementary code point
407 static char out
[100];
410 demoUnicodeStringStorage() {
411 // These sample code lines illustrate how to use UnicodeString, and the
412 // comments tell what happens internally. There are no APIs to observe
413 // most of this programmatically, except for stepping into the code
415 // This is by design to hide such details from the user.
418 printf("\n* demoUnicodeStringStorage() ------- ***\n\n");
420 // * UnicodeString with internally stored contents
421 // instantiate a UnicodeString from a single code point
422 // the few (2) UChars will be stored in the object itself
423 UnicodeString
one((UChar32
)0x24001);
424 // this copies the few UChars into the "two" object
425 UnicodeString two
=one
;
426 printf("length of short string copy: %d\n", two
.length());
427 // set "one" to contain the 3 UChars from readonly
428 // this setTo() variant copies the characters
429 one
.setTo(readonly
, UPRV_LENGTHOF(readonly
));
431 // * UnicodeString with allocated contents
432 // build a longer string that will not fit into the object's buffer
433 one
+=UnicodeString(writeable
, UPRV_LENGTHOF(writeable
));
436 printf("length of longer string: %d\n", one
.length());
437 // copying will use the same allocated buffer and increment the reference
440 printf("length of longer string copy: %d\n", two
.length());
442 // * UnicodeString using readonly-alias to a const UChar array
443 // construct a string that aliases a readonly buffer
444 UnicodeString
three(FALSE
, readonly
, UPRV_LENGTHOF(readonly
));
445 printUnicodeString("readonly-alias string: ", three
);
446 // copy-on-write: any modification to the string results in
447 // a copy to either the internal buffer or to a newly allocated one
448 three
.setCharAt(1, 0x39);
449 printUnicodeString("readonly-aliasing string after modification: ", three
);
450 // the aliased array is not modified
451 for(i
=0; i
<three
.length(); ++i
) {
452 printf("readonly buffer[%d] after modifying its string: 0x%lx\n",
455 // setTo() readonly alias
456 one
.setTo(FALSE
, writeable
, UPRV_LENGTHOF(writeable
));
457 // copying the readonly-alias object with fastCopyFrom() (new in ICU 2.4)
458 // will readonly-alias the same buffer
459 two
.fastCopyFrom(one
);
460 printUnicodeString("fastCopyFrom(readonly alias of \"writeable\" array): ", two
);
461 printf("verify that a fastCopyFrom(readonly alias) uses the same buffer pointer: %d (should be 1)\n",
462 one
.getBuffer()==two
.getBuffer());
463 // a normal assignment will clone the contents (new in ICU 2.4)
465 printf("verify that a regular copy of a readonly alias uses a different buffer pointer: %d (should be 0)\n",
466 one
.getBuffer()==two
.getBuffer());
468 // * UnicodeString using writeable-alias to a non-const UChar array
469 UnicodeString
four(writeable
, UPRV_LENGTHOF(writeable
), UPRV_LENGTHOF(writeable
));
470 printUnicodeString("writeable-alias string: ", four
);
471 // a modification writes through to the buffer
472 four
.setCharAt(1, 0x39);
473 for(i
=0; i
<four
.length(); ++i
) {
474 printf("writeable-alias backing buffer[%d]=0x%lx "
475 "after modification\n", i
, writeable
[i
]);
477 // a copy will not alias any more;
478 // instead, it will get a copy of the contents into allocated memory
480 two
.setCharAt(1, 0x21);
481 for(i
=0; i
<two
.length(); ++i
) {
482 printf("writeable-alias backing buffer[%d]=0x%lx after "
483 "modification of string copy\n", i
, writeable
[i
]);
485 // setTo() writeable alias, capacity==length
486 one
.setTo(writeable
, UPRV_LENGTHOF(writeable
), UPRV_LENGTHOF(writeable
));
487 // grow the string - it will not fit into the backing buffer any more
488 // and will get copied before modification
489 one
.append((UChar
)0x40);
490 // shrink it back so it would fit
491 one
.truncate(one
.length()-1);
492 // we still operate on the copy
493 one
.setCharAt(1, 0x25);
494 printf("string after growing too much and then shrinking[1]=0x%lx\n"
495 " backing store for this[1]=0x%lx\n",
496 one
.charAt(1), writeable
[1]);
497 // if we need it in the original buffer, then extract() to it
498 // extract() does not do anything if the string aliases that same buffer
499 // i=min(one.length(), length of array)
500 if(one
.length()<UPRV_LENGTHOF(writeable
)) {
503 i
=UPRV_LENGTHOF(writeable
);
505 one
.extract(0, i
, writeable
);
506 for(i
=0; i
<UPRV_LENGTHOF(writeable
); ++i
) {
507 printf("writeable-alias backing buffer[%d]=0x%lx after re-extract\n",
512 // sample code for UnicodeString instantiations ----------------------------- ***
515 demoUnicodeStringInit() {
516 // *** Make sure to read about invariant characters in utypes.h! ***
517 // Initialization of Unicode strings from C literals works _only_ for
518 // invariant characters!
520 printf("\n* demoUnicodeStringInit() ---------- ***\n\n");
522 // the string literal is 32 chars long - this must be counted for the macro
523 UnicodeString invariantOnly
=UNICODE_STRING("such characters are safe 123 %-.", 32);
526 * In C, we need two macros: one to declare the UChar[] array, and
527 * one to populate it; the second one is a noop on platforms where
528 * wchar_t is compatible with UChar and ASCII-based.
529 * The length of the string literal must be counted for both macros.
531 /* declare the invString array for the string */
532 U_STRING_DECL(invString
, "such characters are safe 123 %-.", 32);
533 /* populate it with the characters */
534 U_STRING_INIT(invString
, "such characters are safe 123 %-.", 32);
536 // compare the C and C++ strings
537 printf("C and C++ Unicode strings are equal: %d\n", invariantOnly
==UnicodeString(TRUE
, invString
, 32));
540 * convert between char * and UChar * strings that
541 * contain only invariant characters
543 static const char *cs1
="such characters are safe 123 %-.";
544 static UChar us1
[40];
546 u_charsToUChars(cs1
, us1
, 33); /* include the terminating NUL */
547 u_UCharsToChars(us1
, cs2
, 33);
548 printf("char * -> UChar * -> char * with only "
549 "invariant characters: \"%s\"\n",
552 // initialize a UnicodeString from a string literal that contains
553 // escape sequences written with invariant characters
554 // do not forget to duplicate the backslashes for ICU to see them
555 // then, count each double backslash only once!
556 UnicodeString german
=UNICODE_STRING(
557 "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", 64).
559 printUnicodeString("german UnicodeString from unescaping:\n ", german
);
562 * C: convert and unescape a char * string with only invariant
563 * characters to fill a UChar * string
568 "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n",
569 buffer
, UPRV_LENGTHOF(buffer
));
570 printf("german C Unicode string from char * unescaping: (length %d)\n ", length
);
571 printUnicodeString("", UnicodeString(buffer
));
575 main(int argc
, const char *argv
[]) {
576 UErrorCode errorCode
=U_ZERO_ERROR
;
578 // Note: Using a global variable for any object is not exactly thread-safe...
580 // You can change this call to e.g. ucnv_open("UTF-8", &errorCode) if you pipe
581 // the output to a file and look at it with a Unicode-capable editor.
582 // This will currently affect only the printUString() function, see the code above.
583 // printUnicodeString() could use this, too, by changing to an extract() overload
584 // that takes a UConverter argument.
585 cnv
=ucnv_open(NULL
, &errorCode
);
586 if(U_FAILURE(errorCode
)) {
587 fprintf(stderr
, "error %s opening the default converter\n", u_errorName(errorCode
));
591 ucnv_setFromUCallBack(cnv
, UCNV_FROM_U_CALLBACK_ESCAPE
, UCNV_ESCAPE_C
, NULL
, NULL
, &errorCode
);
592 if(U_FAILURE(errorCode
)) {
593 fprintf(stderr
, "error %s setting the escape callback in the default converter\n", u_errorName(errorCode
));
599 demo_C_Unicode_strings();
601 demoCaseMapInCPlusPlus();
602 demoUnicodeStringStorage();
603 demoUnicodeStringInit();