From fb67a77d12dcdf1cf6b21f2fdfb0a0eb462b0e34 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Thu, 12 Dec 2002 18:45:33 +0000 Subject: [PATCH] ICU-557 move string sample code here from the User Guide, and improve it X-SVN-Rev: 10645 --- icu4c/source/samples/ustring/ustring.cpp | 451 +++++++++++++++++++++-- 1 file changed, 421 insertions(+), 30 deletions(-) diff --git a/icu4c/source/samples/ustring/ustring.cpp b/icu4c/source/samples/ustring/ustring.cpp index 5a0eb41bfd..52351e47d0 100644 --- a/icu4c/source/samples/ustring/ustring.cpp +++ b/icu4c/source/samples/ustring/ustring.cpp @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 2000, International Business Machines +* Copyright (C) 2000-2002, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -19,21 +19,76 @@ #include #include "unicode/utypes.h" +#include "unicode/uchar.h" +#include "unicode/locid.h" #include "unicode/ustring.h" +#include "unicode/ucnv.h" #include "unicode/unistr.h" -// helper function --------------------------------------------------------- *** +#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0])) + +// helper functions -------------------------------------------------------- *** + +// default converter for the platform encoding +static UConverter *cnv=NULL; static void -printUnicodeString(const UnicodeString &s) { +printUString(const char *announce, const UChar *s, int32_t length) { + static char out[200]; + UChar32 c; + int32_t i; + UErrorCode errorCode=U_ZERO_ERROR; + + /* + * Convert to the "platform encoding". See notes in printUnicodeString(). + * ucnv_fromUChars(), like most ICU APIs understands length==-1 + * to mean that the string is NUL-terminated. + */ + ucnv_fromUChars(cnv, out, sizeof(out), s, length, &errorCode); + if(U_FAILURE(errorCode) || errorCode==U_STRING_NOT_TERMINATED_WARNING) { + printf("%sproblem converting string from Unicode: %s\n", announce, u_errorName(errorCode)); + return; + } + + printf("%s%s {", announce, out); + + /* output the code points (not code units) */ + if(length>=0) { + /* s is not NUL-terminated */ + for(i=0; i0; /* U16_PREV pre-decrements */) { + U16_PREV(input, 0, i, c); + /* Iterating backwards + Codepoint at offset 5: U+0062 + Codepoint at offset 3: U+10ffff + Codepoint at offset 2: U+dc00 -- unpaired surrogate because lead surr. overwritten + Codepoint at offset 1: U+0062 -- by this BMP code point + Codepoint at offset 0: U+0061 + */ + printf("Codepoint at offset %d: U+%04x\n", i, c); + } +} + +// sample code for Unicode strings in C ------------------------------------ *** + +static void demo_C_Unicode_strings() { + printf("\n* demo_C_Unicode_strings() --------- ***\n\n"); + + static const UChar text[]={ 0x41, 0x42, 0x43, 0 }; /* "ABC" */ + static const UChar appendText[]={ 0x61, 0x62, 0x63, 0 }; /* "abc" */ + static const UChar cmpText[]={ 0x61, 0x53, 0x73, 0x43, 0 }; /* "aSsC" */ + UChar buffer[32]; + int32_t compare; + int32_t length=u_strlen(text); /* length=3 */ + + /* simple ANSI C-style functions */ + buffer[0]=0; /* empty, NUL-terminated string */ + u_strncat(buffer, text, 1); /* append just n=1 character ('A') */ + u_strcat(buffer, appendText); /* buffer=="Aabc" */ + length=u_strlen(buffer); /* length=4 */ + printUString("should be \"Aabc\": ", buffer, -1); + + /* bitwise comparing buffer with text */ + compare=u_strcmp(buffer, text); + if(compare<=0) { + printf("String comparison error, expected \"Aabc\" > \"ABC\"\n"); + } + + /* Build "AC" in the buffer... */ + u_strcpy(buffer, text); + buffer[1]=0xdf; /* sharp s, case-compares equal to "ss" */ + printUString("should be \"AC\": ", buffer, -1); + + /* Compare two strings case-insensitively using full case folding */ + compare=u_strcasecmp(buffer, cmpText, U_FOLD_CASE_DEFAULT); + if(compare!=0) { + printf("String case insensitive comparison error, expected \"AbC\" to be equal to \"ABC\"\n"); + } +} + +// sample code for case mappings with C APIs -------------------------------- *** + +static void demoCaseMapInC() { + /* + * input= + * "aB" + * "iI " + * " " + * "" + */ + static const UChar input[]={ + 0x61, 0x42, 0x3a3, + 0x69, 0x49, 0x131, 0x130, 0x20, + 0xdf, 0x20, 0xfb03, + 0x3c2, 0x3c3, 0x3a3, 0 + }; + UChar buffer[32]; + + UErrorCode errorCode; + UChar32 c; + int32_t i, j, length; + UBool isError; + + printf("\n* demoCaseMapInC() ----------------- ***\n\n"); + + /* + * First, use simple case mapping functions which provide + * 1:1 code point mappings without context/locale ID. + * + * Note that some mappings will not be "right" because some "real" + * case mappings require context, depend on the locale ID, + * and/or result in a change in the number of code points. + */ + printUString("input string: ", input, -1); + + /* uppercase */ + isError=FALSE; + for(i=j=0; j" + * "iI " + * " " + * "" + */ + static const UChar input[]={ + 0x61, 0x42, 0x3a3, + 0x69, 0x49, 0x131, 0x130, 0x20, + 0xdf, 0x20, 0xfb03, + 0x3c2, 0x3c3, 0x3a3, 0 + }; + + printf("\n* demoCaseMapInCPlusPlus() --------- ***\n\n"); + + UnicodeString s(input), t; + const Locale &en=Locale::getEnglish(); + Locale tr("tr"); + + /* + * Full case mappings as in demoCaseMapInC(), using UnicodeString functions. + * These functions modify the string object itself. + * Since we want to keep the input string around, we copy it each time + * and case-map the copy. + */ + printUnicodeString("input string: ", s); + + /* lowercase/English */ + printUnicodeString("full-lowercased/en: ", (t=s).toLower(en)); + /* lowercase/Turkish */ + printUnicodeString("full-lowercased/tr: ", (t=s).toLower(tr)); + /* uppercase/English */ + printUnicodeString("full-uppercased/en: ", (t=s).toUpper(en)); + /* uppercase/Turkish */ + printUnicodeString("full-uppercased/tr: ", (t=s).toUpper(tr)); + /* titlecase/English */ + printUnicodeString("full-titlecased/en: ", (t=s).toTitle(NULL, en)); + /* titlecase/Turkish */ + printUnicodeString("full-titlecased/tr: ", (t=s).toTitle(NULL, tr)); + /* case-folde/default */ + printUnicodeString("full-case-folded/default: ", (t=s).foldCase(U_FOLD_CASE_DEFAULT)); + /* case-folde/Turkic */ + printUnicodeString("full-case-folded/Turkic: ", (t=s).foldCase(U_FOLD_CASE_EXCLUDE_SPECIAL_I)); +} + +// sample code for UnicodeString storage models ----------------------------- *** static const UChar readonly[]={ 0x61, 0x31, 0x20ac }; static UChar writeable[]={ - 0x62, 0x32, 0xdbc0, 0xdc01 + 0x62, 0x32, 0xdbc0, 0xdc01 // includes a surrogate pair for a supplementary code point }; static char out[100]; static void -demoStorage() { +demoUnicodeStringStorage() { // These sample code lines illustrate how to use UnicodeString, and the // comments tell what happens internally. There are no APIs to observe // most of this programmatically, except for stepping into the code // with a debugger. // This is by design to hide such details from the user. + int32_t i; + + printf("\n* demoUnicodeStringStorage() ------- ***\n\n"); // * UnicodeString with internally stored contents // instantiate a UnicodeString from a single code point @@ -69,6 +427,7 @@ demoStorage() { UnicodeString two=one; printf("length of short string copy: %d\n", two.length()); // set "one" to contain the 3 UChars from readonly + // this setTo() variant copies the characters one.setTo(readonly, LENGTHOF(readonly)); // * UnicodeString with allocated contents @@ -85,15 +444,11 @@ demoStorage() { // * UnicodeString using readonly-alias to a const UChar array // construct a string that aliases a readonly buffer UnicodeString three(FALSE, readonly, LENGTHOF(readonly)); - int32_t i; - for(i=0; i