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