michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2001-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * Date Name Description michael@0: * 11/19/2001 aliu Creation. michael@0: * 05/19/2010 markus Rewritten from scratch michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #ifndef CHARSTRING_H michael@0: #define CHARSTRING_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/uobject.h" michael@0: #include "cmemory.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // Windows needs us to DLL-export the MaybeStackArray template specialization, michael@0: // but MacOS X cannot handle it. Same as in digitlst.h. michael@0: #if !U_PLATFORM_IS_DARWIN_BASED michael@0: template class U_COMMON_API MaybeStackArray; michael@0: #endif michael@0: michael@0: /** michael@0: * ICU-internal char * string class. michael@0: * This class does not assume or enforce any particular character encoding. michael@0: * Raw bytes can be stored. The string object owns its characters. michael@0: * A terminating NUL is stored, but the class does not prevent embedded NUL characters. michael@0: * michael@0: * This class wants to be convenient but is also deliberately minimalist. michael@0: * Please do not add methods if they only add minor convenience. michael@0: * For example: michael@0: * cs.data()[5]='a'; // no need for setCharAt(5, 'a') michael@0: */ michael@0: class U_COMMON_API CharString : public UMemory { michael@0: public: michael@0: CharString() : len(0) { buffer[0]=0; } michael@0: CharString(const StringPiece &s, UErrorCode &errorCode) : len(0) { michael@0: buffer[0]=0; michael@0: append(s, errorCode); michael@0: } michael@0: CharString(const CharString &s, UErrorCode &errorCode) : len(0) { michael@0: buffer[0]=0; michael@0: append(s, errorCode); michael@0: } michael@0: CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) { michael@0: buffer[0]=0; michael@0: append(s, sLength, errorCode); michael@0: } michael@0: ~CharString() {} michael@0: michael@0: /** michael@0: * Replaces this string's contents with the other string's contents. michael@0: * CharString does not support the standard copy constructor nor michael@0: * the assignment operator, to make copies explicit and to michael@0: * use a UErrorCode where memory allocations might be needed. michael@0: */ michael@0: CharString ©From(const CharString &other, UErrorCode &errorCode); michael@0: michael@0: UBool isEmpty() const { return len==0; } michael@0: int32_t length() const { return len; } michael@0: char operator[](int32_t index) const { return buffer[index]; } michael@0: StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); } michael@0: michael@0: const char *data() const { return buffer.getAlias(); } michael@0: char *data() { return buffer.getAlias(); } michael@0: michael@0: CharString &clear() { len=0; buffer[0]=0; return *this; } michael@0: CharString &truncate(int32_t newLength); michael@0: michael@0: CharString &append(char c, UErrorCode &errorCode); michael@0: CharString &append(const StringPiece &s, UErrorCode &errorCode) { michael@0: return append(s.data(), s.length(), errorCode); michael@0: } michael@0: CharString &append(const CharString &s, UErrorCode &errorCode) { michael@0: return append(s.data(), s.length(), errorCode); michael@0: } michael@0: CharString &append(const char *s, int32_t sLength, UErrorCode &status); michael@0: /** michael@0: * Returns a writable buffer for appending and writes the buffer's capacity to michael@0: * resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS(). michael@0: * There will additionally be space for a terminating NUL right at resultCapacity. michael@0: * (This function is similar to ByteSink.GetAppendBuffer().) michael@0: * michael@0: * The returned buffer is only valid until the next write operation michael@0: * on this string. michael@0: * michael@0: * After writing at most resultCapacity bytes, call append() with the michael@0: * pointer returned from this function and the number of bytes written. michael@0: * michael@0: * @param minCapacity required minimum capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param desiredCapacityHint desired capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param resultCapacity will be set to the capacity of the returned buffer michael@0: * @param errorCode in/out error code michael@0: * @return a buffer with resultCapacity>=min_capacity michael@0: */ michael@0: char *getAppendBuffer(int32_t minCapacity, michael@0: int32_t desiredCapacityHint, michael@0: int32_t &resultCapacity, michael@0: UErrorCode &errorCode); michael@0: michael@0: CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Appends a filename/path part, e.g., a directory name. michael@0: * First appends a U_FILE_SEP_CHAR if necessary. michael@0: * Does nothing if s is empty. michael@0: */ michael@0: CharString &appendPathPart(const StringPiece &s, UErrorCode &errorCode); michael@0: michael@0: private: michael@0: MaybeStackArray buffer; michael@0: int32_t len; michael@0: michael@0: UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode); michael@0: michael@0: CharString(const CharString &other); // forbid copying of this class michael@0: CharString &operator=(const CharString &other); // forbid copying of this class michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: //eof