2 **********************************************************************
3 * Copyright (c) 2001-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/19/2001 aliu Creation.
8 * 05/19/2010 markus Rewritten from scratch
9 **********************************************************************
15 #include "unicode/utypes.h"
16 #include "unicode/unistr.h"
17 #include "unicode/uobject.h"
22 // Windows needs us to DLL-export the MaybeStackArray template specialization,
23 // but MacOS X cannot handle it. Same as in digitlst.h.
24 #if !defined(U_DARWIN)
25 template class U_COMMON_API MaybeStackArray
<char, 40>;
29 * ICU-internal char * string class.
30 * This class does not assume or enforce any particular character encoding.
31 * Raw bytes can be stored. The string object owns its characters.
32 * A terminating NUL is stored, but the class does not prevent embedded NUL characters.
34 * This class wants to be convenient but is also deliberately minimalist.
35 * Please do not add methods if they only add minor convenience.
37 * cs.data()[5]='a'; // no need for setCharAt(5, 'a')
39 class U_COMMON_API CharString
: public UMemory
{
41 CharString() : len(0) { buffer
[0]=0; }
42 CharString(const StringPiece
&s
, UErrorCode
&errorCode
) : len(0) {
46 CharString(const CharString
&s
, UErrorCode
&errorCode
) : len(0) {
50 CharString(const char *s
, int32_t sLength
, UErrorCode
&errorCode
) : len(0) {
52 append(s
, sLength
, errorCode
);
57 * Replaces this string's contents with the other string's contents.
58 * CharString does not support the standard copy constructor nor
59 * the assignment operator, to make copies explicit and to
60 * use a UErrorCode where memory allocations might be needed.
62 CharString
©From(const CharString
&other
, UErrorCode
&errorCode
);
64 UBool
isEmpty() { return len
==0; }
65 int32_t length() const { return len
; }
66 char operator[] (int32_t index
) const { return buffer
[index
]; }
67 StringPiece
toStringPiece() const { return StringPiece(buffer
.getAlias(), len
); }
69 const char *data() const { return buffer
.getAlias(); }
70 char *data() { return buffer
.getAlias(); }
72 CharString
&clear() { len
=0; buffer
[0]=0; return *this; }
73 CharString
&truncate(int32_t newLength
);
75 CharString
&append(char c
, UErrorCode
&errorCode
);
76 CharString
&append(const StringPiece
&s
, UErrorCode
&errorCode
) {
77 return append(s
.data(), s
.length(), errorCode
);
79 CharString
&append(const CharString
&s
, UErrorCode
&errorCode
) {
80 return append(s
.data(), s
.length(), errorCode
);
82 CharString
&append(const char *s
, int32_t sLength
, UErrorCode
&status
);
84 * Returns a writable buffer for appending and writes the buffer's capacity to
85 * resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS().
86 * There will additionally be space for a terminating NUL right at resultCapacity.
87 * (This function is similar to ByteSink.GetAppendBuffer().)
89 * The returned buffer is only valid until the next write operation
92 * After writing at most resultCapacity bytes, call append() with the
93 * pointer returned from this function and the number of bytes written.
95 * @param minCapacity required minimum capacity of the returned buffer;
96 * must be non-negative
97 * @param desiredCapacityHint desired capacity of the returned buffer;
98 * must be non-negative
99 * @param resultCapacity will be set to the capacity of the returned buffer
100 * @param errorCode in/out error code
101 * @return a buffer with resultCapacity>=min_capacity
103 char *getAppendBuffer(int32_t minCapacity
,
104 int32_t desiredCapacityHint
,
105 int32_t &resultCapacity
,
106 UErrorCode
&errorCode
);
108 CharString
&appendInvariantChars(const UnicodeString
&s
, UErrorCode
&errorCode
);
111 MaybeStackArray
<char, 40> buffer
;
114 UBool
ensureCapacity(int32_t capacity
, int32_t desiredCapacityHint
, UErrorCode
&errorCode
);
116 CharString(const CharString
&other
); // forbid copying of this class
117 CharString
&operator=(const CharString
&other
); // forbid copying of this class