]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/charstr.h
2 **********************************************************************
3 * Copyright (c) 2001-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/19/2001 aliu Creation.
8 **********************************************************************
14 #include "unicode/utypes.h"
15 #include "unicode/uobject.h"
16 #include "unicode/unistr.h"
19 //--------------------------------------------------------------------
22 // This is a tiny wrapper class that is used internally to make a
23 // UnicodeString look like a const char*. It can be allocated on the
24 // stack. It only creates a heap buffer if it needs to.
25 //--------------------------------------------------------------------
29 class U_COMMON_API CharString
: public UMemory
{
32 #if !UCONFIG_NO_CONVERSION
34 // @param str The unicode string to be converted to char *
35 // @param codepage The char * code page. "" for invariant conversion.
36 // NULL for default code page.
37 inline CharString(const UnicodeString
& str
, const char *codepage
);
40 inline CharString(const UnicodeString
& str
);
42 inline operator const char*() const { return ptr
; }
48 CharString(const CharString
&other
); // forbid copying of this class
49 CharString
&operator=(const CharString
&other
); // forbid copying of this class
52 #if !UCONFIG_NO_CONVERSION
54 inline CharString::CharString(const UnicodeString
& str
, const char *codepage
) {
57 len
= str
.extract(0, 0x7FFFFFFF, buf
,sizeof(buf
)-1, codepage
);
58 if (len
>= (int32_t)(sizeof(buf
)-1)) {
59 ptr
= (char *)uprv_malloc(len
+1);
60 str
.extract(0, 0x7FFFFFFF, ptr
, len
+1, codepage
);
66 inline CharString::CharString(const UnicodeString
& str
) {
69 len
= str
.extract(0, 0x7FFFFFFF, buf
, (int32_t)(sizeof(buf
)-1), US_INV
);
70 if (len
>= (int32_t)(sizeof(buf
)-1)) {
71 ptr
= (char *)uprv_malloc(len
+1);
72 str
.extract(0, 0x7FFFFFFF, ptr
, len
+1, US_INV
);
76 inline CharString::~CharString() {