// © 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) U_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) U_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);7677UBool isEmpty() const { return len==0; }78int32_t length() const { return len; }79char operator[](int32_t index) const { return buffer[index]; }80StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); }8182const char *data() const { return buffer.getAlias(); }83char *data() { return buffer.getAlias(); }84/**85* Allocates length()+1 chars and copies the NUL-terminated data().86* The caller must uprv_free() the result.87*/88char *cloneData(UErrorCode &errorCode) const;89/**90* Copies the contents of the string into dest.91* Checks if there is enough space in dest, extracts the entire string if possible,92* and NUL-terminates dest if possible.93*94* If the string fits into dest but cannot be NUL-terminated (length()==capacity),95* then the error code is set to U_STRING_NOT_TERMINATED_WARNING.96* If the string itself does not fit into dest (length()>capacity),97* then the error code is set to U_BUFFER_OVERFLOW_ERROR.98*99* @param dest Destination string buffer.100* @param capacity Size of the dest buffer (number of chars).101* @param errorCode ICU error code.102* @return length()103*/104int32_t extract(char *dest, int32_t capacity, UErrorCode &errorCode) const;105106bool operator==(StringPiece other) const {107return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);108}109bool operator!=(StringPiece other) const {110return !operator==(other);111}112113/** @return last index of c, or -1 if c is not in this string */114int32_t lastIndexOf(char c) const;115116bool contains(StringPiece s) const;117118CharString &clear() { len=0; buffer[0]=0; return *this; }119CharString &truncate(int32_t newLength);120121CharString &append(char c, UErrorCode &errorCode);122CharString &append(StringPiece s, UErrorCode &errorCode) {123return append(s.data(), s.length(), errorCode);124}125CharString &append(const CharString &s, UErrorCode &errorCode) {126return append(s.data(), s.length(), errorCode);127}128CharString &append(const char *s, int32_t sLength, UErrorCode &status);129130CharString &appendNumber(int32_t number, UErrorCode &status);131132/**133* Returns a writable buffer for appending and writes the buffer's capacity to134* resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS().135* There will additionally be space for a terminating NUL right at resultCapacity.136* (This function is similar to ByteSink.GetAppendBuffer().)137*138* The returned buffer is only valid until the next write operation139* on this string.140*141* After writing at most resultCapacity bytes, call append() with the142* pointer returned from this function and the number of bytes written.143*144* @param minCapacity required minimum capacity of the returned buffer;145* must be non-negative146* @param desiredCapacityHint desired capacity of the returned buffer;147* must be non-negative148* @param resultCapacity will be set to the capacity of the returned buffer149* @param errorCode in/out error code150* @return a buffer with resultCapacity>=min_capacity151*/152char *getAppendBuffer(int32_t minCapacity,153int32_t desiredCapacityHint,154int32_t &resultCapacity,155UErrorCode &errorCode);156157CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode);158CharString &appendInvariantChars(const UChar* uchars, int32_t ucharsLen, UErrorCode& errorCode);159160/**161* Appends a filename/path part, e.g., a directory name.162* First appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if necessary.163* Does nothing if s is empty.164*/165CharString &appendPathPart(StringPiece s, UErrorCode &errorCode);166167/**168* Appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if this string is not empty169* and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR.170*/171CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode);172173private:174MaybeStackArray<char, 40> buffer;175int32_t len;176177UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode);178179CharString(const CharString &other) = delete; // forbid copying of this class180CharString &operator=(const CharString &other) = delete; // forbid copying of this class181182/**183* Returns U_FILE_ALT_SEP_CHAR if found in string, and U_FILE_SEP_CHAR is not found.184* Otherwise returns U_FILE_SEP_CHAR.185*/186char getDirSepChar() const;187};188189U_NAMESPACE_END190191#endif192//eof193194195