]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/charstr.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / charstr.h
1 /*
2 **********************************************************************
3 * Copyright (c) 2001, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/19/2001 aliu Creation.
8 **********************************************************************
9 */
10
11 #include "unicode/utypes.h"
12 #include "unicode/uobject.h"
13 #include "unicode/unistr.h"
14 #include "cmemory.h"
15
16 //--------------------------------------------------------------------
17 // class CharString
18 //
19 // This is a tiny wrapper class that is used internally to make a
20 // UnicodeString look like a const char*. It can be allocated on the
21 // stack. It only creates a heap buffer if it needs to.
22 //--------------------------------------------------------------------
23
24 U_NAMESPACE_BEGIN
25
26 class U_COMMON_API CharString : public UMemory {
27 public:
28 inline CharString(const UnicodeString& str);
29 inline ~CharString();
30 inline operator const char*() const { return ptr; }
31
32 private:
33 char buf[128];
34 char* ptr;
35
36 CharString(const CharString &other); // forbid copying of this class
37 CharString &operator=(const CharString &other); // forbid copying of this class
38 };
39
40 inline CharString::CharString(const UnicodeString& str) {
41 // Invariant converter should create str.length() chars
42 if (str.length() >= (int32_t)sizeof(buf)) {
43 ptr = (char *)uprv_malloc(str.length() + 8);
44 } else {
45 ptr = buf;
46 }
47 str.extract(0, 0x7FFFFFFF, ptr, "");
48 }
49
50 inline CharString::~CharString() {
51 if (ptr != buf) {
52 uprv_free(ptr);
53 }
54 }
55
56 U_NAMESPACE_END
57
58 //eof