ICU-557 add some sample code for using unicode strings in icu
X-SVN-Rev: 2312
This commit is contained in:
parent
9466acce28
commit
51e1051922
218
icu4c/source/samples/ustring/ustring.cpp
Normal file
218
icu4c/source/samples/ustring/ustring.cpp
Normal file
@ -0,0 +1,218 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
*
|
||||
* Copyright (C) 2000, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
*
|
||||
*******************************************************************************
|
||||
* file name: ustring.c
|
||||
* encoding: US-ASCII
|
||||
* tab size: 8 (not used)
|
||||
* indentation:4
|
||||
*
|
||||
* created on: 2000aug15
|
||||
* created by: Markus W. Scherer
|
||||
*
|
||||
* This file contains sample code that illustrates the use of Unicode strings
|
||||
* with ICU.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "unicode/utypes.h"
|
||||
#include "unicode/ustring.h"
|
||||
#include "unicode/unistr.h"
|
||||
|
||||
// helper function --------------------------------------------------------- ***
|
||||
|
||||
static void
|
||||
printUnicodeString(const UnicodeString &s) {
|
||||
static char out[200];
|
||||
int32_t i, length;
|
||||
|
||||
// output the string, converted to the platform encoding
|
||||
out[s.extract(0, 99, out)]=0;
|
||||
printf("%s {", out);
|
||||
|
||||
// output the code units
|
||||
length=s.length();
|
||||
for(i=0; i<length; ++i) {
|
||||
printf(" %04x", s.charAt(i));
|
||||
}
|
||||
printf(" }\n");
|
||||
}
|
||||
|
||||
// sample code for storage models ------------------------------------------ ***
|
||||
|
||||
#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
|
||||
|
||||
static const UChar readonly[]={
|
||||
0x61, 0x31, 0x20ac
|
||||
};
|
||||
static UChar writeable[]={
|
||||
0x62, 0x32, 0xdbc0, 0xdc01
|
||||
};
|
||||
static char out[100];
|
||||
|
||||
static void
|
||||
demoStorage() {
|
||||
// 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.
|
||||
|
||||
// * UnicodeString with internally stored contents
|
||||
// instantiate a UnicodeString from a single code point
|
||||
// the few (2) UChars will be stored in the object itself
|
||||
UnicodeString one((UChar32)0x24001);
|
||||
// this copies the few UChars into the "two" object
|
||||
UnicodeString two=one;
|
||||
printf("length of short string copy: %d\n", two.length());
|
||||
// set "one" to contain the 3 UChars from readonly
|
||||
one.setTo(readonly, LENGTHOF(readonly));
|
||||
|
||||
// * UnicodeString with allocated contents
|
||||
// build a longer string that will not fit into the object's buffer
|
||||
one+=UnicodeString(writeable, LENGTHOF(writeable));
|
||||
one+=one;
|
||||
one+=one;
|
||||
printf("length of longer string: %d\n", one.length());
|
||||
// copying will use the same allocated buffer and increment the reference
|
||||
// counter
|
||||
two=one;
|
||||
printf("length of longer string copy: %d\n", two.length());
|
||||
|
||||
// * UnicodeString using readonly-alias to a const UChar array
|
||||
// construct a string that aliases a readonly buffer
|
||||
UnicodeString three(FALSE, readonly, LENGTHOF(readonly));
|
||||
UTextOffset i;
|
||||
for(i=0; i<three.length(); ++i) {
|
||||
printf("readonly-alias string[%d]=0x%lx\n", i, three.charAt(i));
|
||||
}
|
||||
// copy-on-write: any modification to the string results in
|
||||
// a copy to either the internal buffer or to a newly allocated one
|
||||
three.setCharAt(1, 0x39);
|
||||
out[three.extract(0, 99, out)]=0;
|
||||
printf("readonly-aliasing string after modification: %s\n", out);
|
||||
// the aliased array is not modified
|
||||
for(i=0; i<three.length(); ++i) {
|
||||
printf("readonly buffer[%d] after modifying its string: 0x%lx\n",
|
||||
i, readonly[i]);
|
||||
}
|
||||
// setTo() readonly alias
|
||||
one.setTo(FALSE, writeable, LENGTHOF(writeable));
|
||||
// copying the readonly-alias object will readonly-alias the same buffer
|
||||
two=one;
|
||||
out[two.extract(0, 99, out)]=0;
|
||||
printf("copy of readonly-alias string of \"writeable\" array: %s\n", out);
|
||||
|
||||
// * UnicodeString using writeable-alias to a non-const UChar array
|
||||
UnicodeString four(writeable, LENGTHOF(writeable), LENGTHOF(writeable));
|
||||
for(i=0; i<four.length(); ++i) {
|
||||
printf("writeable-alias string[%d]=0x%lx\n", i, four.charAt(i));
|
||||
}
|
||||
// a modification writes through to the buffer
|
||||
four.setCharAt(1, 0x39);
|
||||
for(i=0; i<four.length(); ++i) {
|
||||
printf("writeable-alias backing buffer[%d]=0x%lx "
|
||||
"after modification\n", i, writeable[i]);
|
||||
}
|
||||
// a copy will not alias any more;
|
||||
// instead, it will get a copy of the contents into allocated memory
|
||||
two=four;
|
||||
two.setCharAt(1, 0x21);
|
||||
for(i=0; i<two.length(); ++i) {
|
||||
printf("writeable-alias backing buffer[%d]=0x%lx after "
|
||||
"modification of string copy\n", i, writeable[i]);
|
||||
}
|
||||
// setTo() writeable alias
|
||||
one.setTo(writeable, LENGTHOF(writeable), LENGTHOF(writeable));
|
||||
// grow the string - it will not fit into the backing buffer any more
|
||||
// and will get copied before modification
|
||||
one.append((UChar)0x40);
|
||||
// shrink it back so it would fit
|
||||
one.truncate(one.length()-1);
|
||||
// we still operate on the copy
|
||||
one.setCharAt(1, 0x25);
|
||||
printf("string after growing too much and then shrinking[1]=0x%lx\n"
|
||||
" backing store for this[1]=0x%lx\n",
|
||||
one.charAt(1), writeable[1]);
|
||||
// if we need it in the original buffer, then extract() to it
|
||||
// extract() does not do anything if the string aliases that same buffer
|
||||
// i=min(one.length(), length of array)
|
||||
if(one.length()<LENGTHOF(writeable)) {
|
||||
i=one.length();
|
||||
} else {
|
||||
i=LENGTHOF(writeable);
|
||||
}
|
||||
one.extract(0, i, writeable);
|
||||
for(i=0; i<LENGTHOF(writeable); ++i) {
|
||||
printf("writeable-alias backing buffer[%d]=0x%lx after re-extract\n",
|
||||
i, writeable[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// sample code for instantiating Unicode strings --------------------------- ***
|
||||
|
||||
static void
|
||||
initString() {
|
||||
// the string literal is 32 chars long - this must be counted for the macro
|
||||
UnicodeString invariantOnly=UNICODE_STRING("such characters are safe 123 %-.", 32);
|
||||
|
||||
/*
|
||||
* In C, we need two macros: one to declare the UChar[] array, and
|
||||
* one to populate it; the second one is a noop on platforms where
|
||||
* wchar_t is compatible with UChar and ASCII-based.
|
||||
* The length of the string literal must be counted for both macros.
|
||||
*/
|
||||
/* declare the invString array for the string */
|
||||
U_STRING_DECL(invString, "such characters are safe 123 %-.", 32);
|
||||
/* populate it with the characters */
|
||||
U_STRING_INIT(invString, "such characters are safe 123 %-.", 32);
|
||||
|
||||
// compare the C and C++ strings
|
||||
printf("C and C++ Unicode strings are equal: %d\n", invariantOnly==UnicodeString(TRUE, invString, 32));
|
||||
|
||||
/*
|
||||
* convert between char * and UChar * strings that
|
||||
* contain only invariant characters
|
||||
*/
|
||||
static const char *cs1="such characters are safe 123 %-.";
|
||||
static UChar us1[40];
|
||||
static char cs2[40];
|
||||
u_charsToUChars(cs1, us1, 33); /* include the terminating NUL */
|
||||
u_UCharsToChars(us1, cs2, 33);
|
||||
printf("char * -> UChar * -> char * with only "
|
||||
"invariant characters: \"%s\"\n",
|
||||
cs2);
|
||||
|
||||
// initialize a UnicodeString from a string literal that contains
|
||||
// escape sequences written with invariant characters
|
||||
// do not forget to duplicate the backslashes for ICU to see them
|
||||
// then, count each double backslash only once!
|
||||
UnicodeString german=UNICODE_STRING(
|
||||
"Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", 64).
|
||||
unescape();
|
||||
printf("german UnicodeString from unescaping:\n ");
|
||||
printUnicodeString(german);
|
||||
|
||||
/*
|
||||
* C: convert and unescape a char * string with only invariant
|
||||
* characters to fill a UChar * string
|
||||
*/
|
||||
UChar buffer[200];
|
||||
int32_t length;
|
||||
length=u_unescape(
|
||||
"Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n",
|
||||
buffer, LENGTHOF(buffer));
|
||||
printf("german C Unicode string from char * unescaping: (length %d)\n ", length);
|
||||
printUnicodeString(UnicodeString(buffer));
|
||||
}
|
||||
|
||||
extern int
|
||||
main(int argc, const char *argv[]) {
|
||||
demoStorage();
|
||||
initString();
|
||||
|
||||
return 0;
|
||||
}
|
102
icu4c/source/samples/ustring/ustring.dsp
Normal file
102
icu4c/source/samples/ustring/ustring.dsp
Normal file
@ -0,0 +1,102 @@
|
||||
# Microsoft Developer Studio Project File - Name="ustring" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Console Application" 0x0103
|
||||
|
||||
CFG=ustring - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "ustring.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "ustring.mak" CFG="ustring - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "ustring - Win32 Release" (based on "Win32 (x86) Console Application")
|
||||
!MESSAGE "ustring - Win32 Debug" (based on "Win32 (x86) Console Application")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "ustring - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||
# ADD CPP /nologo /W3 /GX /O2 /I "..\..\..\include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
|
||||
# ADD LINK32 icuuc.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /libpath:"..\..\..\lib\Release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "ustring - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "..\..\..\include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
|
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 icuuc.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"..\..\..\lib\Debug"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "ustring - Win32 Release"
|
||||
# Name "ustring - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\ustring.cpp
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
29
icu4c/source/samples/ustring/ustring.dsw
Normal file
29
icu4c/source/samples/ustring/ustring.dsw
Normal file
@ -0,0 +1,29 @@
|
||||
Microsoft Developer Studio Workspace File, Format Version 6.00
|
||||
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
|
||||
|
||||
###############################################################################
|
||||
|
||||
Project: "ustring"=.\ustring.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<4>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
||||
Global:
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<3>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
Loading…
Reference in New Issue
Block a user