]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ********************************************************************** | |
2ca993e8 | 5 | * Copyright (c) 2001-2015, International Business Machines |
b75a7d8f A |
6 | * Corporation and others. All Rights Reserved. |
7 | ********************************************************************** | |
8 | * Date Name Description | |
9 | * 11/19/2001 aliu Creation. | |
729e4ab9 | 10 | * 05/19/2010 markus Rewritten from scratch |
b75a7d8f A |
11 | ********************************************************************** |
12 | */ | |
13 | ||
374ca955 A |
14 | #ifndef CHARSTRING_H |
15 | #define CHARSTRING_H | |
16 | ||
b75a7d8f | 17 | #include "unicode/utypes.h" |
b75a7d8f | 18 | #include "unicode/unistr.h" |
729e4ab9 | 19 | #include "unicode/uobject.h" |
b75a7d8f A |
20 | #include "cmemory.h" |
21 | ||
b75a7d8f A |
22 | U_NAMESPACE_BEGIN |
23 | ||
729e4ab9 A |
24 | // Windows needs us to DLL-export the MaybeStackArray template specialization, |
25 | // but MacOS X cannot handle it. Same as in digitlst.h. | |
4388f060 | 26 | #if !U_PLATFORM_IS_DARWIN_BASED |
729e4ab9 | 27 | template class U_COMMON_API MaybeStackArray<char, 40>; |
374ca955 A |
28 | #endif |
29 | ||
729e4ab9 A |
30 | /** |
31 | * ICU-internal char * string class. | |
32 | * This class does not assume or enforce any particular character encoding. | |
33 | * Raw bytes can be stored. The string object owns its characters. | |
34 | * A terminating NUL is stored, but the class does not prevent embedded NUL characters. | |
35 | * | |
36 | * This class wants to be convenient but is also deliberately minimalist. | |
37 | * Please do not add methods if they only add minor convenience. | |
38 | * For example: | |
39 | * cs.data()[5]='a'; // no need for setCharAt(5, 'a') | |
40 | */ | |
41 | class U_COMMON_API CharString : public UMemory { | |
42 | public: | |
43 | CharString() : len(0) { buffer[0]=0; } | |
f3c0d7a5 | 44 | CharString(StringPiece s, UErrorCode &errorCode) : len(0) { |
729e4ab9 A |
45 | buffer[0]=0; |
46 | append(s, errorCode); | |
47 | } | |
48 | CharString(const CharString &s, UErrorCode &errorCode) : len(0) { | |
49 | buffer[0]=0; | |
50 | append(s, errorCode); | |
51 | } | |
52 | CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) { | |
53 | buffer[0]=0; | |
54 | append(s, sLength, errorCode); | |
55 | } | |
56 | ~CharString() {} | |
57 | ||
58 | /** | |
59 | * Replaces this string's contents with the other string's contents. | |
60 | * CharString does not support the standard copy constructor nor | |
61 | * the assignment operator, to make copies explicit and to | |
62 | * use a UErrorCode where memory allocations might be needed. | |
63 | */ | |
64 | CharString ©From(const CharString &other, UErrorCode &errorCode); | |
65 | ||
51004dcb | 66 | UBool isEmpty() const { return len==0; } |
729e4ab9 | 67 | int32_t length() const { return len; } |
51004dcb | 68 | char operator[](int32_t index) const { return buffer[index]; } |
729e4ab9 A |
69 | StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); } |
70 | ||
71 | const char *data() const { return buffer.getAlias(); } | |
72 | char *data() { return buffer.getAlias(); } | |
73 | ||
2ca993e8 A |
74 | /** @return last index of c, or -1 if c is not in this string */ |
75 | int32_t lastIndexOf(char c) const; | |
76 | ||
729e4ab9 A |
77 | CharString &clear() { len=0; buffer[0]=0; return *this; } |
78 | CharString &truncate(int32_t newLength); | |
79 | ||
80 | CharString &append(char c, UErrorCode &errorCode); | |
f3c0d7a5 | 81 | CharString &append(StringPiece s, UErrorCode &errorCode) { |
729e4ab9 A |
82 | return append(s.data(), s.length(), errorCode); |
83 | } | |
84 | CharString &append(const CharString &s, UErrorCode &errorCode) { | |
85 | return append(s.data(), s.length(), errorCode); | |
86 | } | |
87 | CharString &append(const char *s, int32_t sLength, UErrorCode &status); | |
88 | /** | |
89 | * Returns a writable buffer for appending and writes the buffer's capacity to | |
90 | * resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS(). | |
91 | * There will additionally be space for a terminating NUL right at resultCapacity. | |
92 | * (This function is similar to ByteSink.GetAppendBuffer().) | |
93 | * | |
94 | * The returned buffer is only valid until the next write operation | |
95 | * on this string. | |
96 | * | |
97 | * After writing at most resultCapacity bytes, call append() with the | |
98 | * pointer returned from this function and the number of bytes written. | |
99 | * | |
100 | * @param minCapacity required minimum capacity of the returned buffer; | |
101 | * must be non-negative | |
102 | * @param desiredCapacityHint desired capacity of the returned buffer; | |
103 | * must be non-negative | |
104 | * @param resultCapacity will be set to the capacity of the returned buffer | |
105 | * @param errorCode in/out error code | |
106 | * @return a buffer with resultCapacity>=min_capacity | |
107 | */ | |
108 | char *getAppendBuffer(int32_t minCapacity, | |
109 | int32_t desiredCapacityHint, | |
110 | int32_t &resultCapacity, | |
111 | UErrorCode &errorCode); | |
112 | ||
113 | CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode); | |
b75a7d8f | 114 | |
4388f060 A |
115 | /** |
116 | * Appends a filename/path part, e.g., a directory name. | |
117 | * First appends a U_FILE_SEP_CHAR if necessary. | |
118 | * Does nothing if s is empty. | |
119 | */ | |
f3c0d7a5 | 120 | CharString &appendPathPart(StringPiece s, UErrorCode &errorCode); |
4388f060 | 121 | |
2ca993e8 A |
122 | /** |
123 | * Appends a U_FILE_SEP_CHAR if this string is not empty | |
124 | * and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR. | |
125 | */ | |
126 | CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode); | |
127 | ||
b75a7d8f | 128 | private: |
729e4ab9 A |
129 | MaybeStackArray<char, 40> buffer; |
130 | int32_t len; | |
131 | ||
132 | UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode); | |
b75a7d8f A |
133 | |
134 | CharString(const CharString &other); // forbid copying of this class | |
135 | CharString &operator=(const CharString &other); // forbid copying of this class | |
136 | }; | |
137 | ||
b75a7d8f A |
138 | U_NAMESPACE_END |
139 | ||
374ca955 | 140 | #endif |
b75a7d8f | 141 | //eof |