// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3**********************************************************************4* Copyright (c) 2001-2015, International Business Machines5* Corporation and others. All Rights Reserved.6**********************************************************************7* Date Name Description8* 11/19/2001 aliu Creation.9* 05/19/2010 markus Rewritten from scratch10**********************************************************************11*/1213#ifndef CHARSTRING_H14#define CHARSTRING_H1516#include "unicode/utypes.h"17#include "unicode/unistr.h"18#include "unicode/uobject.h"19#include "cmemory.h"2021U_NAMESPACE_BEGIN2223// Windows needs us to DLL-export the MaybeStackArray template specialization,24// but MacOS X cannot handle it. Same as in digitlst.h.25#if !U_PLATFORM_IS_DARWIN_BASED26template class U_COMMON_API MaybeStackArray<char, 40>;27#endif2829/**30* ICU-internal char * string class.31* This class does not assume or enforce any particular character encoding.32* Raw bytes can be stored. The string object owns its characters.33* A terminating NUL is stored, but the class does not prevent embedded NUL characters.34*35* This class wants to be convenient but is also deliberately minimalist.36* Please do not add methods if they only add minor convenience.37* For example:38* cs.data()[5]='a'; // no need for setCharAt(5, 'a')39*/40class U_COMMON_API CharString : public UMemory {41public:42CharString() : len(0) { buffer[0]=0; }43CharString(StringPiece s, UErrorCode &errorCode) : len(0) {44buffer[0]=0;45append(s, errorCode);46}47CharString(const CharString &s, UErrorCode &errorCode) : len(0) {48buffer[0]=0;49append(s, errorCode);50}51CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) {52buffer[0]=0;53append(s, sLength, errorCode);54}55~CharString() {}5657/**58* Move constructor; might leave src in an undefined state.59* This string will have the same contents and state that the source string had.60*/61CharString(CharString &&src) noexcept;62/**63* Move assignment operator; might leave src in an undefined state.64* This string will have the same contents and state that the source string had.65* The behavior is undefined if *this and src are the same object.66*/67CharString &operator=(CharString &&src) noexcept;6869/**70* Replaces this string's contents with the other string's contents.71* CharString does not support the standard copy constructor nor72* the assignment operator, to make copies explicit and to73* use a UErrorCode where memory allocations might be needed.74*/75CharString ©From(const CharString &other, UErrorCode &errorCode);76CharString ©From(StringPiece s, UErrorCode &errorCode);7778UBool isEmpty() const { return len==0; }79int32_t length() const { return len; }80char operator[](int32_t index) const { return buffer[index]; }81StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); }8283const char *data() const { return buffer.getAlias(); }84char *data() { return buffer.getAlias(); }85/**86* Allocates length()+1 chars and copies the NUL-terminated data().87* The caller must uprv_free() the result.88*/89char *cloneData(UErrorCode &errorCode) const;90/**91* Copies the contents of the string into dest.92* Checks if there is enough space in dest, extracts the entire string if possible,93* and NUL-terminates dest if possible.94*95* If the string fits into dest but cannot be NUL-terminated (length()==capacity),96* then the error code is set to U_STRING_NOT_TERMINATED_WARNING.97* If the string itself does not fit into dest (length()>capacity),98* then the error code is set to U_BUFFER_OVERFLOW_ERROR.99*100* @param dest Destination string buffer.101* @param capacity Size of the dest buffer (number of chars).102* @param errorCode ICU error code.103* @return length()104*/105int32_t extract(char *dest, int32_t capacity, UErrorCode &errorCode) const;106107bool operator==(const CharString& other) const {108return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);109}110bool operator!=(const CharString& other) const {111return !operator==(other);112}113114bool operator==(StringPiece other) const {115return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);116}117bool operator!=(StringPiece other) const {118return !operator==(other);119}120121/** @return last index of c, or -1 if c is not in this string */122int32_t lastIndexOf(char c) const;123124bool contains(StringPiece s) const;125126CharString &clear() { len=0; buffer[0]=0; return *this; }127CharString &truncate(int32_t newLength);128129CharString &append(char c, UErrorCode &errorCode);130CharString &append(StringPiece s, UErrorCode &errorCode) {131return append(s.data(), s.length(), errorCode);132}133CharString &append(const CharString &s, UErrorCode &errorCode) {134return append(s.data(), s.length(), errorCode);135}136CharString &append(const char *s, int32_t sLength, UErrorCode &status);137138CharString &appendNumber(int64_t number, UErrorCode &status);139140/**141* Returns a writable buffer for appending and writes the buffer's capacity to142* resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS().143* There will additionally be space for a terminating NUL right at resultCapacity.144* (This function is similar to ByteSink.GetAppendBuffer().)145*146* The returned buffer is only valid until the next write operation147* on this string.148*149* After writing at most resultCapacity bytes, call append() with the150* pointer returned from this function and the number of bytes written.151*152* @param minCapacity required minimum capacity of the returned buffer;153* must be non-negative154* @param desiredCapacityHint desired capacity of the returned buffer;155* must be non-negative156* @param resultCapacity will be set to the capacity of the returned buffer157* @param errorCode in/out error code158* @return a buffer with resultCapacity>=min_capacity159*/160char *getAppendBuffer(int32_t minCapacity,161int32_t desiredCapacityHint,162int32_t &resultCapacity,163UErrorCode &errorCode);164165CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode);166CharString &appendInvariantChars(const char16_t* uchars, int32_t ucharsLen, UErrorCode& errorCode);167168/**169* Appends a filename/path part, e.g., a directory name.170* First appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if necessary.171* Does nothing if s is empty.172*/173CharString &appendPathPart(StringPiece s, UErrorCode &errorCode);174175/**176* Appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if this string is not empty177* and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR.178*/179CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode);180181private:182MaybeStackArray<char, 40> buffer;183int32_t len;184185UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode);186187CharString(const CharString &other) = delete; // forbid copying of this class188CharString &operator=(const CharString &other) = delete; // forbid copying of this class189190/**191* Returns U_FILE_ALT_SEP_CHAR if found in string, and U_FILE_SEP_CHAR is not found.192* Otherwise returns U_FILE_SEP_CHAR.193*/194char getDirSepChar() const;195};196197U_NAMESPACE_END198199#endif200//eof201202203