#ifndef CHARSTRING_H
#define CHARSTRING_H
#include "unicode/utypes.h"
#include "unicode/uobject.h"
#include "unicode/unistr.h"
#include "cmemory.h"
U_NAMESPACE_BEGIN
class U_COMMON_API CharString : public UMemory {
public:
#if !UCONFIG_NO_CONVERSION
inline CharString(const UnicodeString& str, const char *codepage);
#endif
inline CharString(const UnicodeString& str);
inline ~CharString();
inline operator const char*() const { return ptr; }
private:
char buf[128];
char* ptr;
CharString(const CharString &other); CharString &operator=(const CharString &other); };
#if !UCONFIG_NO_CONVERSION
inline CharString::CharString(const UnicodeString& str, const char *codepage) {
int32_t len;
ptr = buf;
len = str.extract(0, 0x7FFFFFFF, buf ,sizeof(buf)-1, codepage);
if (len >= (int32_t)(sizeof(buf)-1)) {
ptr = (char *)uprv_malloc(len+1);
str.extract(0, 0x7FFFFFFF, ptr, len+1, codepage);
}
}
#endif
inline CharString::CharString(const UnicodeString& str) {
int32_t len;
ptr = buf;
len = str.extract(0, 0x7FFFFFFF, buf, (int32_t)(sizeof(buf)-1), US_INV);
if (len >= (int32_t)(sizeof(buf)-1)) {
ptr = (char *)uprv_malloc(len+1);
str.extract(0, 0x7FFFFFFF, ptr, len+1, US_INV);
}
}
inline CharString::~CharString() {
if (ptr != buf) {
uprv_free(ptr);
}
}
U_NAMESPACE_END
#endif