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