// © 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/**24* ICU-internal char * string class.25* This class does not assume or enforce any particular character encoding.26* Raw bytes can be stored. The string object owns its characters.27* A terminating NUL is stored, but the class does not prevent embedded NUL characters.28*29* This class wants to be convenient but is also deliberately minimalist.30* Please do not add methods if they only add minor convenience.31* For example:32* cs.data()[5]='a'; // no need for setCharAt(5, 'a')33*/34class U_COMMON_API_CLASS CharString : public UMemory {35public:36U_COMMON_API CharString() : len(0) { buffer[0]=0; }37U_COMMON_API CharString(StringPiece s, UErrorCode &errorCode) : len(0) {38buffer[0]=0;39append(s, errorCode);40}41U_COMMON_API CharString(const CharString &s, UErrorCode &errorCode) : len(0) {42buffer[0]=0;43append(s, errorCode);44}45U_COMMON_API CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) {46buffer[0]=0;47append(s, sLength, errorCode);48}49U_COMMON_API ~CharString() {}5051/**52* Move constructor; might leave src in an undefined state.53* This string will have the same contents and state that the source string had.54*/55U_COMMON_API CharString(CharString &&src) noexcept;56/**57* Move assignment operator; might leave src in an undefined state.58* This string will have the same contents and state that the source string had.59* The behavior is undefined if *this and src are the same object.60*/61U_COMMON_API CharString &operator=(CharString &&src) noexcept;6263/**64* Replaces this string's contents with the other string's contents.65* CharString does not support the standard copy constructor nor66* the assignment operator, to make copies explicit and to67* use a UErrorCode where memory allocations might be needed.68*/69U_COMMON_API CharString ©From(const CharString &other, UErrorCode &errorCode);70U_COMMON_API CharString ©From(StringPiece s, UErrorCode &errorCode);7172U_COMMON_API UBool isEmpty() const { return len==0; }73U_COMMON_API int32_t length() const { return len; }74U_COMMON_API char operator[](int32_t index) const { return buffer[index]; }75U_COMMON_API StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); }7677U_COMMON_API const char *data() const { return buffer.getAlias(); }78U_COMMON_API char *data() { return buffer.getAlias(); }79/**80* Allocates length()+1 chars and copies the NUL-terminated data().81* The caller must uprv_free() the result.82*/83U_COMMON_API char *cloneData(UErrorCode &errorCode) const;84/**85* Copies the contents of the string into dest.86* Checks if there is enough space in dest, extracts the entire string if possible,87* and NUL-terminates dest if possible.88*89* If the string fits into dest but cannot be NUL-terminated (length()==capacity),90* then the error code is set to U_STRING_NOT_TERMINATED_WARNING.91* If the string itself does not fit into dest (length()>capacity),92* then the error code is set to U_BUFFER_OVERFLOW_ERROR.93*94* @param dest Destination string buffer.95* @param capacity Size of the dest buffer (number of chars).96* @param errorCode ICU error code.97* @return length()98*/99U_COMMON_API int32_t extract(char *dest, int32_t capacity, UErrorCode &errorCode) const;100101U_COMMON_API bool operator==(const CharString& other) const {102return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);103}104U_COMMON_API bool operator!=(const CharString& other) const {105return !operator==(other);106}107108U_COMMON_API bool operator==(StringPiece other) const {109return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);110}111U_COMMON_API bool operator!=(StringPiece other) const {112return !operator==(other);113}114115/** @return last index of c, or -1 if c is not in this string */116U_COMMON_API int32_t lastIndexOf(char c) const;117118U_COMMON_API bool contains(StringPiece s) const;119120U_COMMON_API CharString &clear() { len=0; buffer[0]=0; return *this; }121U_COMMON_API CharString &truncate(int32_t newLength);122123U_COMMON_API CharString &append(char c, UErrorCode &errorCode);124U_COMMON_API CharString &append(StringPiece s, UErrorCode &errorCode) {125return append(s.data(), s.length(), errorCode);126}127U_COMMON_API CharString &append(const CharString &s, UErrorCode &errorCode) {128return append(s.data(), s.length(), errorCode);129}130U_COMMON_API CharString &append(const char *s, int32_t sLength, UErrorCode &status);131132U_COMMON_API CharString &appendNumber(int64_t number, UErrorCode &status);133134/**135* Returns a writable buffer for appending and writes the buffer's capacity to136* resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS().137* There will additionally be space for a terminating NUL right at resultCapacity.138* (This function is similar to ByteSink.GetAppendBuffer().)139*140* The returned buffer is only valid until the next write operation141* on this string.142*143* After writing at most resultCapacity bytes, call append() with the144* pointer returned from this function and the number of bytes written.145*146* @param minCapacity required minimum capacity of the returned buffer;147* must be non-negative148* @param desiredCapacityHint desired capacity of the returned buffer;149* must be non-negative150* @param resultCapacity will be set to the capacity of the returned buffer151* @param errorCode in/out error code152* @return a buffer with resultCapacity>=min_capacity153*/154U_COMMON_API char *getAppendBuffer(int32_t minCapacity,155int32_t desiredCapacityHint,156int32_t &resultCapacity,157UErrorCode &errorCode);158159U_COMMON_API CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode);160U_COMMON_API CharString &appendInvariantChars(const char16_t* uchars,161int32_t ucharsLen,162UErrorCode& errorCode);163164/**165* Appends a filename/path part, e.g., a directory name.166* First appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if necessary.167* Does nothing if s is empty.168*/169U_COMMON_API CharString &appendPathPart(StringPiece s, UErrorCode &errorCode);170171/**172* Appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if this string is not empty173* and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR.174*/175U_COMMON_API CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode);176177private:178MaybeStackArray<char, 40> buffer;179int32_t len;180181UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode);182183CharString(const CharString &other) = delete; // forbid copying of this class184CharString &operator=(const CharString &other) = delete; // forbid copying of this class185186/**187* Returns U_FILE_ALT_SEP_CHAR if found in string, and U_FILE_SEP_CHAR is not found.188* Otherwise returns U_FILE_SEP_CHAR.189*/190char getDirSepChar() const;191};192193U_NAMESPACE_END194195#endif196//eof197198199