2 *******************************************************************************
4 * Copyright (C) 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 2000-2014, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
14 * file name: ustring.c
16 * tab size: 8 (not used)
19 * created on: 2000aug15
20 * created by: Markus W. Scherer
22 * This file contains sample code that illustrates the use of Unicode strings
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"
34 // helper functions -------------------------------------------------------- ***
36 // default converter for the platform encoding
37 static UConverter *cnv=NULL;
40 printUString(const char *announce, const UChar *s, int32_t length) {
44 UErrorCode errorCode=U_ZERO_ERROR;
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.
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));
57 printf("%s%s {", announce, out);
59 /* output the code points (not code units) */
61 /* s is not NUL-terminated */
62 for(i=0; i<length; /* U16_NEXT post-increments */) {
63 U16_NEXT(s, i, length, c);
67 /* s is NUL-terminated */
68 for(i=0; /* condition in loop body */; /* U16_NEXT post-increments */) {
69 U16_NEXT(s, i, length, c);
80 printUnicodeString(const char *announce, const UnicodeString &s) {
84 // output the string, converted to the platform encoding
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);
95 // output the code units (not code points)
97 for(i=0; i<length; ++i) {
98 printf(" %04x", s.charAt(i));
103 // sample code for utf.h macros -------------------------------------------- ***
106 demo_utf_h_macros() {
107 static UChar input[]={ 0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062 };
112 printf("\n* demo_utf_h_macros() -------------- ***\n\n");
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
122 printf("Codepoint at offset %d: U+", i);
123 U16_NEXT(input, i, UPRV_LENGTHOF(input), c);
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);
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
143 printf("Codepoint at offset %d: U+%04x\n", i, c);
147 // sample code for Unicode strings in C ------------------------------------ ***
149 static void demo_C_Unicode_strings() {
150 printf("\n* demo_C_Unicode_strings() --------- ***\n\n");
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" */
157 int32_t length=u_strlen(text); /* length=3 */
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);
166 /* bitwise comparing buffer with text */
167 compare=u_strcmp(buffer, text);
169 printf("String comparison error, expected \"Aabc\" > \"ABC\"\n");
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);
177 /* Compare two strings case-insensitively using full case folding */
178 compare=u_strcasecmp(buffer, cmpText, U_FOLD_CASE_DEFAULT);
180 printf("String case insensitive comparison error, expected \"AbC\" to be equal to \"ABC\"\n");
184 // sample code for case mappings with C APIs -------------------------------- ***
186 static void demoCaseMapInC() {
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>"
194 static const UChar input[]={
196 0x69, 0x49, 0x131, 0x130, 0x20,
198 0x3c2, 0x3c3, 0x3a3, 0
202 UErrorCode errorCode;
204 int32_t i, j, length;
207 printf("\n* demoCaseMapInC() ----------------- ***\n\n");
210 * First, use simple case mapping functions which provide
211 * 1:1 code point mappings without context/locale ID.
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.
217 printUString("input string: ", input, -1);
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 */
224 break; /* stop at terminating NUL, no need to terminate buffer */
227 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
229 printUString("simple-uppercased: ", buffer, j);
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 */
235 break; /* stop at terminating NUL, no need to terminate buffer */
238 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
240 printUString("simple-lowercased: ", buffer, j);
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 */
246 break; /* stop at terminating NUL, no need to terminate buffer */
249 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
251 printUString("simple-titlecased: ", buffer, j);
252 /* case-fold/default */
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 */
257 break; /* stop at terminating NUL, no need to terminate buffer */
259 c=u_foldCase(c, U_FOLD_CASE_DEFAULT);
260 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
262 printUString("simple-case-folded/default: ", buffer, j);
263 /* case-fold/Turkic */
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 */
268 break; /* stop at terminating NUL, no need to terminate buffer */
270 c=u_foldCase(c, U_FOLD_CASE_EXCLUDE_SPECIAL_I);
271 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError);
273 printUString("simple-case-folded/Turkic: ", buffer, j);
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.
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.
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.
288 printUString("\ninput string: ", input, -1);
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);
296 printf("error in u_strToLower(en)=%ld error=%s\n", length, u_errorName(errorCode));
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);
304 printf("error in u_strToLower(tr)=%ld error=%s\n", length, u_errorName(errorCode));
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);
312 printf("error in u_strToUpper(en)=%ld error=%s\n", length, u_errorName(errorCode));
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);
320 printf("error in u_strToUpper(tr)=%ld error=%s\n", length, u_errorName(errorCode));
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);
328 printf("error in u_strToTitle(en)=%ld error=%s\n", length, u_errorName(errorCode));
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);
336 printf("error in u_strToTitle(tr)=%ld error=%s\n", length, u_errorName(errorCode));
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);
344 printf("error in u_strFoldCase(default)=%ld error=%s\n", length, u_errorName(errorCode));
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);
352 printf("error in u_strFoldCase(Turkic)=%ld error=%s\n", length, u_errorName(errorCode));
356 // sample code for case mappings with C++ APIs ------------------------------ ***
358 static void demoCaseMapInCPlusPlus() {
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>"
366 static const UChar input[]={
368 0x69, 0x49, 0x131, 0x130, 0x20,
370 0x3c2, 0x3c3, 0x3a3, 0
373 printf("\n* demoCaseMapInCPlusPlus() --------- ***\n\n");
375 UnicodeString s(input), t;
376 const Locale &en=Locale::getEnglish();
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.
385 printUnicodeString("input string: ", s);
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));
405 // sample code for UnicodeString storage models ----------------------------- ***
407 static const UChar readonly[]={
410 static UChar writeable[]={
411 0x62, 0x32, 0xdbc0, 0xdc01 // includes a surrogate pair for a supplementary code point
413 static char out[100];
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
421 // This is by design to hide such details from the user.
424 printf("\n* demoUnicodeStringStorage() ------- ***\n\n");
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));
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));
442 printf("length of longer string: %d\n", one.length());
443 // copying will use the same allocated buffer and increment the reference
446 printf("length of longer string copy: %d\n", two.length());
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",
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)
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());
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]);
483 // a copy will not alias any more;
484 // instead, it will get a copy of the contents into allocated memory
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]);
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)) {
509 i=UPRV_LENGTHOF(writeable);
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",
518 // sample code for UnicodeString instantiations ----------------------------- ***
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!
526 printf("\n* demoUnicodeStringInit() ---------- ***\n\n");
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);
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.
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);
542 // compare the C and C++ strings
543 printf("C and C++ Unicode strings are equal: %d\n", invariantOnly==UnicodeString(TRUE, invString, 32));
546 * convert between char * and UChar * strings that
547 * contain only invariant characters
549 static const char *cs1="such characters are safe 123 %-.";
550 static UChar us1[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",
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).
565 printUnicodeString("german UnicodeString from unescaping:\n ", german);
568 * C: convert and unescape a char * string with only invariant
569 * characters to fill a UChar * string
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));
581 main(int argc, const char *argv[]) {
582 UErrorCode errorCode=U_ZERO_ERROR;
584 // Note: Using a global variable for any object is not exactly thread-safe...
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));
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));
605 demo_C_Unicode_strings();
607 demoCaseMapInCPlusPlus();
608 demoUnicodeStringStorage();
609 demoUnicodeStringInit();