/src/icu/icu4c/source/common/charstr.h
Line | Count | Source |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ********************************************************************** |
5 | | * Copyright (c) 2001-2015, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ********************************************************************** |
8 | | * Date Name Description |
9 | | * 11/19/2001 aliu Creation. |
10 | | * 05/19/2010 markus Rewritten from scratch |
11 | | ********************************************************************** |
12 | | */ |
13 | | |
14 | | #ifndef CHARSTRING_H |
15 | | #define CHARSTRING_H |
16 | | |
17 | | #include "unicode/utypes.h" |
18 | | #include "unicode/unistr.h" |
19 | | #include "unicode/uobject.h" |
20 | | #include "cmemory.h" |
21 | | |
22 | | U_NAMESPACE_BEGIN |
23 | | |
24 | | /** |
25 | | * ICU-internal char * string class. |
26 | | * This class does not assume or enforce any particular character encoding. |
27 | | * Raw bytes can be stored. The string object owns its characters. |
28 | | * A terminating NUL is stored, but the class does not prevent embedded NUL characters. |
29 | | * |
30 | | * This class wants to be convenient but is also deliberately minimalist. |
31 | | * Please do not add methods if they only add minor convenience. |
32 | | * For example: |
33 | | * cs.data()[5]='a'; // no need for setCharAt(5, 'a') |
34 | | */ |
35 | | class U_COMMON_API_CLASS CharString : public UMemory { |
36 | | public: |
37 | 91.3M | U_COMMON_API CharString() : len(0) { buffer[0]=0; } |
38 | 5.09M | U_COMMON_API CharString(StringPiece s, UErrorCode &errorCode) : len(0) { |
39 | 5.09M | buffer[0]=0; |
40 | 5.09M | append(s, errorCode); |
41 | 5.09M | } |
42 | 4.89M | U_COMMON_API CharString(const CharString &s, UErrorCode &errorCode) : len(0) { |
43 | 4.89M | buffer[0]=0; |
44 | 4.89M | append(s, errorCode); |
45 | 4.89M | } |
46 | 30.7M | U_COMMON_API CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) { |
47 | 30.7M | buffer[0]=0; |
48 | 30.7M | append(s, sLength, errorCode); |
49 | 30.7M | } |
50 | 133M | U_COMMON_API ~CharString() {} |
51 | | |
52 | | /** |
53 | | * Move constructor; might leave src in an undefined state. |
54 | | * This string will have the same contents and state that the source string had. |
55 | | */ |
56 | | U_COMMON_API CharString(CharString &&src) noexcept; |
57 | | /** |
58 | | * Move assignment operator; might leave src in an undefined state. |
59 | | * This string will have the same contents and state that the source string had. |
60 | | * The behavior is undefined if *this and src are the same object. |
61 | | */ |
62 | | U_COMMON_API CharString &operator=(CharString &&src) noexcept; |
63 | | |
64 | | /** |
65 | | * Replaces this string's contents with the other string's contents. |
66 | | * CharString does not support the standard copy constructor nor |
67 | | * the assignment operator, to make copies explicit and to |
68 | | * use a UErrorCode where memory allocations might be needed. |
69 | | */ |
70 | | U_COMMON_API CharString ©From(const CharString &other, UErrorCode &errorCode); |
71 | | U_COMMON_API CharString ©From(StringPiece s, UErrorCode &errorCode); |
72 | | |
73 | 48.8M | U_COMMON_API UBool isEmpty() const { return len==0; } |
74 | 46.1M | U_COMMON_API int32_t length() const { return len; } |
75 | 15.4M | U_COMMON_API char operator[](int32_t index) const { return buffer[index]; } |
76 | 365k | U_COMMON_API StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); } |
77 | | |
78 | 28.1M | U_COMMON_API const char *data() const { return buffer.getAlias(); } |
79 | 95.0M | U_COMMON_API char *data() { return buffer.getAlias(); } |
80 | | /** |
81 | | * Allocates length()+1 chars and copies the NUL-terminated data(). |
82 | | * The caller must uprv_free() the result. |
83 | | */ |
84 | | U_COMMON_API char *cloneData(UErrorCode &errorCode) const; |
85 | | /** |
86 | | * Copies the contents of the string into dest. |
87 | | * Checks if there is enough space in dest, extracts the entire string if possible, |
88 | | * and NUL-terminates dest if possible. |
89 | | * |
90 | | * If the string fits into dest but cannot be NUL-terminated (length()==capacity), |
91 | | * then the error code is set to U_STRING_NOT_TERMINATED_WARNING. |
92 | | * If the string itself does not fit into dest (length()>capacity), |
93 | | * then the error code is set to U_BUFFER_OVERFLOW_ERROR. |
94 | | * |
95 | | * @param dest Destination string buffer. |
96 | | * @param capacity Size of the dest buffer (number of chars). |
97 | | * @param errorCode ICU error code. |
98 | | * @return length() |
99 | | */ |
100 | | U_COMMON_API int32_t extract(char *dest, int32_t capacity, UErrorCode &errorCode) const; |
101 | | |
102 | 1.95M | U_COMMON_API bool operator==(const CharString& other) const { |
103 | 1.95M | return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0); |
104 | 1.95M | } |
105 | 180k | U_COMMON_API bool operator!=(const CharString& other) const { |
106 | 180k | return !operator==(other); |
107 | 180k | } |
108 | | |
109 | 1.36M | U_COMMON_API bool operator==(StringPiece other) const { |
110 | 1.36M | return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0); |
111 | 1.36M | } |
112 | 127k | U_COMMON_API bool operator!=(StringPiece other) const { |
113 | 127k | return !operator==(other); |
114 | 127k | } |
115 | | |
116 | | /** @return last index of c, or -1 if c is not in this string */ |
117 | | U_COMMON_API int32_t lastIndexOf(char c) const; |
118 | | |
119 | | U_COMMON_API bool contains(StringPiece s) const; |
120 | | |
121 | 8.10M | U_COMMON_API CharString &clear() { len=0; buffer[0]=0; return *this; } |
122 | | U_COMMON_API CharString &truncate(int32_t newLength); |
123 | | |
124 | | U_COMMON_API CharString &append(char c, UErrorCode &errorCode); |
125 | 38.0M | U_COMMON_API CharString &append(StringPiece s, UErrorCode &errorCode) { |
126 | 38.0M | return append(s.data(), s.length(), errorCode); |
127 | 38.0M | } |
128 | 20.8M | U_COMMON_API CharString &append(const CharString &s, UErrorCode &errorCode) { |
129 | 20.8M | return append(s.data(), s.length(), errorCode); |
130 | 20.8M | } |
131 | | U_COMMON_API CharString &append(const char *s, int32_t sLength, UErrorCode &status); |
132 | | |
133 | | U_COMMON_API CharString &appendNumber(int64_t number, UErrorCode &status); |
134 | | |
135 | | /** |
136 | | * Returns a writable buffer for appending and writes the buffer's capacity to |
137 | | * resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS(). |
138 | | * There will additionally be space for a terminating NUL right at resultCapacity. |
139 | | * (This function is similar to ByteSink.GetAppendBuffer().) |
140 | | * |
141 | | * The returned buffer is only valid until the next write operation |
142 | | * on this string. |
143 | | * |
144 | | * After writing at most resultCapacity bytes, call append() with the |
145 | | * pointer returned from this function and the number of bytes written. |
146 | | * |
147 | | * @param minCapacity required minimum capacity of the returned buffer; |
148 | | * must be non-negative |
149 | | * @param desiredCapacityHint desired capacity of the returned buffer; |
150 | | * must be non-negative |
151 | | * @param resultCapacity will be set to the capacity of the returned buffer |
152 | | * @param errorCode in/out error code |
153 | | * @return a buffer with resultCapacity>=min_capacity |
154 | | */ |
155 | | U_COMMON_API char *getAppendBuffer(int32_t minCapacity, |
156 | | int32_t desiredCapacityHint, |
157 | | int32_t &resultCapacity, |
158 | | UErrorCode &errorCode); |
159 | | |
160 | | U_COMMON_API CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode); |
161 | | U_COMMON_API CharString &appendInvariantChars(const char16_t* uchars, |
162 | | int32_t ucharsLen, |
163 | | UErrorCode& errorCode); |
164 | | |
165 | | /** |
166 | | * Appends a filename/path part, e.g., a directory name. |
167 | | * First appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if necessary. |
168 | | * Does nothing if s is empty. |
169 | | */ |
170 | | U_COMMON_API CharString &appendPathPart(StringPiece s, UErrorCode &errorCode); |
171 | | |
172 | | /** |
173 | | * Appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if this string is not empty |
174 | | * and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR. |
175 | | */ |
176 | | U_COMMON_API CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode); |
177 | | |
178 | | private: |
179 | | MaybeStackArray<char, 40> buffer; |
180 | | int32_t len; |
181 | | |
182 | | UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode); |
183 | | |
184 | | CharString(const CharString &other) = delete; // forbid copying of this class |
185 | | CharString &operator=(const CharString &other) = delete; // forbid copying of this class |
186 | | |
187 | | /** |
188 | | * Returns U_FILE_ALT_SEP_CHAR if found in string, and U_FILE_SEP_CHAR is not found. |
189 | | * Otherwise returns U_FILE_SEP_CHAR. |
190 | | */ |
191 | | char getDirSepChar() const; |
192 | | }; |
193 | | |
194 | | U_NAMESPACE_END |
195 | | |
196 | | #endif |
197 | | //eof |