2 *******************************************************************************
4 * Copyright (C) 2000-2002, 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 #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
30 // helper functions -------------------------------------------------------- ***
32 // default converter for the platform encoding
33 static UConverter
*cnv
=NULL
;
36 printUString(const char *announce
, const UChar
*s
, int32_t length
) {
40 UErrorCode errorCode
=U_ZERO_ERROR
;
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.
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
));
53 printf("%s%s {", announce
, out
);
55 /* output the code points (not code units) */
57 /* s is not NUL-terminated */
58 for(i
=0; i
<length
; /* U16_NEXT post-increments */) {
59 U16_NEXT(s
, i
, length
, c
);
63 /* s is NUL-terminated */
64 for(i
=0; /* condition in loop body */; /* U16_NEXT post-increments */) {
65 U16_NEXT(s
, i
, length
, c
);
76 printUnicodeString(const char *announce
, const UnicodeString
&s
) {
80 // output the string, converted to the platform encoding
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
);
91 // output the code units (not code points)
93 for(i
=0; i
<length
; ++i
) {
94 printf(" %04x", s
.charAt(i
));
99 // sample code for utf.h macros -------------------------------------------- ***
102 demo_utf_h_macros() {
103 static UChar input
[]={ 0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062 };
108 printf("\n* demo_utf_h_macros() -------------- ***\n\n");
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
118 printf("Codepoint at offset %d: U+", i
);
119 U16_NEXT(input
, i
, LENGTHOF(input
), c
);
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
);
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
139 printf("Codepoint at offset %d: U+%04x\n", i
, c
);
143 // sample code for Unicode strings in C ------------------------------------ ***
145 static void demo_C_Unicode_strings() {
146 printf("\n* demo_C_Unicode_strings() --------- ***\n\n");
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" */
153 int32_t length
=u_strlen(text
); /* length=3 */
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);
162 /* bitwise comparing buffer with text */
163 compare
=u_strcmp(buffer
, text
);
165 printf("String comparison error, expected \"Aabc\" > \"ABC\"\n");
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);
173 /* Compare two strings case-insensitively using full case folding */
174 compare
=u_strcasecmp(buffer
, cmpText
, U_FOLD_CASE_DEFAULT
);
176 printf("String case insensitive comparison error, expected \"AbC\" to be equal to \"ABC\"\n");
180 // sample code for case mappings with C APIs -------------------------------- ***
182 static void demoCaseMapInC() {
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>"
190 static const UChar input
[]={
192 0x69, 0x49, 0x131, 0x130, 0x20,
194 0x3c2, 0x3c3, 0x3a3, 0
198 UErrorCode errorCode
;
200 int32_t i
, j
, length
;
203 printf("\n* demoCaseMapInC() ----------------- ***\n\n");
206 * First, use simple case mapping functions which provide
207 * 1:1 code point mappings without context/locale ID.
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.
213 printUString("input string: ", input
, -1);
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 */
220 break; /* stop at terminating NUL, no need to terminate buffer */
223 U16_APPEND(buffer
, j
, LENGTHOF(buffer
), c
, isError
);
225 printUString("simple-uppercased: ", buffer
, j
);
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 */
231 break; /* stop at terminating NUL, no need to terminate buffer */
234 U16_APPEND(buffer
, j
, LENGTHOF(buffer
), c
, isError
);
236 printUString("simple-lowercased: ", buffer
, j
);
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 */
242 break; /* stop at terminating NUL, no need to terminate buffer */
245 U16_APPEND(buffer
, j
, LENGTHOF(buffer
), c
, isError
);
247 printUString("simple-titlecased: ", buffer
, j
);
248 /* case-fold/default */
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 */
253 break; /* stop at terminating NUL, no need to terminate buffer */
255 c
=u_foldCase(c
, U_FOLD_CASE_DEFAULT
);
256 U16_APPEND(buffer
, j
, LENGTHOF(buffer
), c
, isError
);
258 printUString("simple-case-folded/default: ", buffer
, j
);
259 /* case-fold/Turkic */
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 */
264 break; /* stop at terminating NUL, no need to terminate buffer */
266 c
=u_foldCase(c
, U_FOLD_CASE_EXCLUDE_SPECIAL_I
);
267 U16_APPEND(buffer
, j
, LENGTHOF(buffer
), c
, isError
);
269 printUString("simple-case-folded/Turkic: ", buffer
, j
);
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.
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.
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.
284 printUString("\ninput string: ", input
, -1);
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
);
292 printf("error in u_strToLower(en)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
300 printf("error in u_strToLower(tr)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
308 printf("error in u_strToUpper(en)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
316 printf("error in u_strToUpper(tr)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
324 printf("error in u_strToTitle(en)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
332 printf("error in u_strToTitle(tr)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
340 printf("error in u_strFoldCase(default)=%ld error=%s\n", length
, u_errorName(errorCode
));
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
);
348 printf("error in u_strFoldCase(Turkic)=%ld error=%s\n", length
, u_errorName(errorCode
));
352 // sample code for case mappings with C++ APIs ------------------------------ ***
354 static void demoCaseMapInCPlusPlus() {
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>"
362 static const UChar input
[]={
364 0x69, 0x49, 0x131, 0x130, 0x20,
366 0x3c2, 0x3c3, 0x3a3, 0
369 printf("\n* demoCaseMapInCPlusPlus() --------- ***\n\n");
371 UnicodeString
s(input
), t
;
372 const Locale
&en
=Locale::getEnglish();
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.
381 printUnicodeString("input string: ", s
);
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
));
401 // sample code for UnicodeString storage models ----------------------------- ***
403 static const UChar readonly
[]={
406 static UChar writeable
[]={
407 0x62, 0x32, 0xdbc0, 0xdc01 // includes a surrogate pair for a supplementary code point
409 static char out
[100];
412 demoUnicodeStringStorage() {
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
417 // This is by design to hide such details from the user.
420 printf("\n* demoUnicodeStringStorage() ------- ***\n\n");
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
));
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
));
438 printf("length of longer string: %d\n", one
.length());
439 // copying will use the same allocated buffer and increment the reference
442 printf("length of longer string copy: %d\n", two
.length());
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",
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)
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());
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
]);
479 // a copy will not alias any more;
480 // instead, it will get a copy of the contents into allocated memory
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
]);
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
)) {
505 i
=LENGTHOF(writeable
);
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",
514 // sample code for UnicodeString instantiations ----------------------------- ***
517 demoUnicodeStringInit() {
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!
522 printf("\n* demoUnicodeStringInit() ---------- ***\n\n");
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);
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.
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);
538 // compare the C and C++ strings
539 printf("C and C++ Unicode strings are equal: %d\n", invariantOnly
==UnicodeString(TRUE
, invString
, 32));
542 * convert between char * and UChar * strings that
543 * contain only invariant characters
545 static const char *cs1
="such characters are safe 123 %-.";
546 static UChar us1
[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",
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).
561 printUnicodeString("german UnicodeString from unescaping:\n ", german
);
564 * C: convert and unescape a char * string with only invariant
565 * characters to fill a UChar * string
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
));
577 main(int argc
, const char *argv
[]) {
578 UErrorCode errorCode
=U_ZERO_ERROR
;
580 // Note: Using a global variable for any object is not exactly thread-safe...
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
));
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
));
601 demo_C_Unicode_strings();
603 demoCaseMapInCPlusPlus();
604 demoUnicodeStringStorage();
605 demoUnicodeStringInit();