2001-11-19 21:24:29 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (c) 2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/19/2001 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
2001-11-19 21:24:29 +00:00
|
|
|
#include "unicode/unistr.h"
|
2002-07-16 01:55:55 +00:00
|
|
|
#include "cmemory.h"
|
2001-11-19 21:24:29 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// class CharString
|
|
|
|
//
|
|
|
|
// This is a tiny wrapper class that is used internally to make a
|
|
|
|
// UnicodeString look like a const char*. It can be allocated on the
|
|
|
|
// stack. It only creates a heap buffer if it needs to.
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2002-10-04 16:35:10 +00:00
|
|
|
class U_COMMON_API CharString : public UMemory {
|
|
|
|
public:
|
2001-11-19 21:24:29 +00:00
|
|
|
inline CharString(const UnicodeString& str);
|
|
|
|
inline ~CharString();
|
2002-10-30 18:07:23 +00:00
|
|
|
inline operator const char*() const { return ptr; }
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-10-04 16:35:10 +00:00
|
|
|
private:
|
2001-11-19 21:24:29 +00:00
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
inline CharString::CharString(const UnicodeString& str) {
|
2002-10-30 18:07:23 +00:00
|
|
|
// Invariant converter should create str.length() chars
|
2001-11-19 21:24:29 +00:00
|
|
|
if (str.length() >= (int32_t)sizeof(buf)) {
|
2002-07-16 01:55:55 +00:00
|
|
|
ptr = (char *)uprv_malloc(str.length() + 8);
|
2001-11-19 21:24:29 +00:00
|
|
|
} else {
|
|
|
|
ptr = buf;
|
|
|
|
}
|
|
|
|
str.extract(0, 0x7FFFFFFF, ptr, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CharString::~CharString() {
|
|
|
|
if (ptr != buf) {
|
2002-07-16 01:55:55 +00:00
|
|
|
uprv_free(ptr);
|
2001-11-19 21:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
//eof
|