Browse Source

Help Coverity figure out how auto string sizes work, and fix 64-bit

portability at the same time
Michael Hansen 11 years ago
parent
commit
05567a2881
  1. 55
      Sources/Plasma/CoreLib/plString.cpp
  2. 28
      Sources/Plasma/CoreLib/plString.h

55
Sources/Plasma/CoreLib/plString.cpp

@ -56,45 +56,33 @@ const plString plString::Null;
#endif
#if WCHAR_BYTES == 2
#define u16slen(str, max) wcsnlen((const wchar_t *)(str), (max))
#define u16slen(str) wcslen(reinterpret_cast<const wchar_t *>(str))
#else
static inline size_t u16slen(const uint16_t *ustr, size_t max)
static inline size_t u16slen(const uint16_t *ustr)
{
size_t length = 0;
for ( ; *ustr++ && max--; ++length)
for ( ; *ustr++; ++length)
;
return length;
}
#endif
/* Provide strnlen and wcsnlen for MinGW which doesn't have them */
#ifdef __MINGW32__
size_t strnlen(const char *s, size_t maxlen)
{
size_t len;
for (len = 0; len < maxlen && *s; len++, s++) { }
return len;
}
size_t wcsnlen(const wchar_t *s, size_t maxlen)
{
size_t len;
for (len = 0; len < maxlen && *s; len++, s++) { }
return len;
}
#endif
#define BADCHAR_REPLACEMENT (0xFFFDul)
// This is 1GB worth of UTF-8 string data
#define FREAKING_BIG (0x40000000)
void plString::IConvertFromUtf8(const char *utf8, size_t size)
{
hsAssert(size == STRLEN_AUTO || size < FREAKING_BIG, "Your string is WAAAAAY too big");
if (!utf8) {
fUtf8Buffer = plStringBuffer<char>();
return;
}
if ((int32_t)size < 0)
size = strnlen(utf8, -(int32_t)size);
if (size == STRLEN_AUTO)
size = strlen(utf8);
operator=(plStringBuffer<char>(utf8, size));
}
@ -133,12 +121,14 @@ plString &plString::operator=(const plStringBuffer<char> &init)
void plString::IConvertFromUtf16(const uint16_t *utf16, size_t size)
{
hsAssert(size == STRLEN_AUTO || size < FREAKING_BIG, "Your string is WAAAAAY too big");
fUtf8Buffer = plStringBuffer<char>();
if (!utf16)
return;
if ((int32_t)size < 0)
size = u16slen(utf16, -(int32_t)size);
if (size == STRLEN_AUTO)
size = u16slen(utf16);
// Calculate the UTF-8 size
size_t convlen = 0;
@ -212,12 +202,14 @@ void plString::IConvertFromWchar(const wchar_t *wstr, size_t size)
void plString::IConvertFromUtf32(const UniChar *ustr, size_t size)
{
hsAssert(size == STRLEN_AUTO || size < FREAKING_BIG, "Your string is WAAAAAY too big");
fUtf8Buffer = plStringBuffer<char>();
if (!ustr)
return;
if ((int32_t)size < 0)
size = ustrlen(ustr, -(int32_t)size);
if (size == STRLEN_AUTO)
size = ustrlen(ustr);
// Calculate the UTF-8 size
size_t convlen = 0;
@ -270,11 +262,13 @@ void plString::IConvertFromUtf32(const UniChar *ustr, size_t size)
void plString::IConvertFromIso8859_1(const char *astr, size_t size)
{
hsAssert(size == STRLEN_AUTO || size < FREAKING_BIG, "Your string is WAAAAAY too big");
fUtf8Buffer = plStringBuffer<char>();
if (!astr)
return;
if ((int32_t)size < 0)
if (size == STRLEN_AUTO)
size = strnlen(astr, -(int32_t)size);
// Calculate the UTF-8 size
@ -700,6 +694,9 @@ plString plString::Substr(ssize_t start, size_t size) const
{
size_t maxSize = GetSize();
if (size == STRLEN_AUTO)
size = maxSize;
if (start < 0) {
// Handle negative indexes from the right of the string
start += maxSize;
@ -919,10 +916,10 @@ plStringStream &plStringStream::operator<<(double num)
return operator<<(buffer);
}
size_t ustrlen(const UniChar *ustr, size_t max)
size_t ustrlen(const UniChar *ustr)
{
size_t length = 0;
for ( ; *ustr++ && max--; ++length)
for ( ; *ustr++; ++length)
;
return length;
}

28
Sources/Plasma/CoreLib/plString.h

@ -57,6 +57,7 @@ typedef unsigned int UniChar;
#define SSO_CHARS (16)
#define STRING_STACK_SIZE (256)
#define WHITESPACE_CHARS " \t\n\r"
#define STRLEN_AUTO (static_cast<size_t>(-1))
// Use "correct" stricmp based on the selected compiler / library
#if _MSC_VER
@ -225,13 +226,6 @@ typedef plStringBuffer<UniChar> plUnicodeBuffer;
class plString
{
public:
enum {
/** Automatically determine the size of input (Requires input to be
* correctly null-terminated).
*/
kSizeAuto = (size_t)(0x80000000)
};
/** Represents a "null" plString object. */
static const plString Null;
@ -261,7 +255,7 @@ public:
* \note This constructor expects the input to be UTF-8 encoded. For
* conversion from ISO-8859-1 8-bit data, use FromIso8859_1().
*/
plString(const char *cstr, size_t size = kSizeAuto) { IConvertFromUtf8(cstr, size); }
plString(const char *cstr, size_t size = STRLEN_AUTO) { IConvertFromUtf8(cstr, size); }
/** Copy constructor. */
plString(const plString &copy) : fUtf8Buffer(copy.fUtf8Buffer) { }
@ -276,7 +270,7 @@ public:
plString(const plUnicodeBuffer &init) { IConvertFromUtf32(init.GetData(), init.GetSize()); }
/** Assignment operator. Same as plString(const char *). */
plString &operator=(const char *cstr) { IConvertFromUtf8(cstr, kSizeAuto); return *this; }
plString &operator=(const char *cstr) { IConvertFromUtf8(cstr, STRLEN_AUTO); return *this; }
/** Assignment operator. Same as plString(const plString &). */
plString &operator=(const plString &copy) { fUtf8Buffer = copy.fUtf8Buffer; return *this; }
@ -299,7 +293,7 @@ public:
plString &operator+=(const plString &str) { return operator=(*this + str); }
/** Create a new plString object from the UTF-8 formatted data in \a utf8. */
static inline plString FromUtf8(const char *utf8, size_t size = kSizeAuto)
static inline plString FromUtf8(const char *utf8, size_t size = STRLEN_AUTO)
{
plString str;
str.IConvertFromUtf8(utf8, size);
@ -307,7 +301,7 @@ public:
}
/** Create a new plString object from the UTF-16 formatted data in \a utf16. */
static inline plString FromUtf16(const uint16_t *utf16, size_t size = kSizeAuto)
static inline plString FromUtf16(const uint16_t *utf16, size_t size = STRLEN_AUTO)
{
plString str;
str.IConvertFromUtf16(utf16, size);
@ -315,7 +309,7 @@ public:
}
/** Create a new plString object from the \p wchar_t data in \a wstr. */
static inline plString FromWchar(const wchar_t *wstr, size_t size = kSizeAuto)
static inline plString FromWchar(const wchar_t *wstr, size_t size = STRLEN_AUTO)
{
plString str;
str.IConvertFromWchar(wstr, size);
@ -323,7 +317,7 @@ public:
}
/** Create a new plString object from the UTF-32 formatted data in \a utf32. */
static inline plString FromUtf32(const UniChar *utf32, size_t size = kSizeAuto)
static inline plString FromUtf32(const UniChar *utf32, size_t size = STRLEN_AUTO)
{
plString str;
str.IConvertFromUtf32(utf32, size);
@ -331,7 +325,7 @@ public:
}
/** Create a new plString object from the ISO-8859-1 formatted data in \a astr. */
static inline plString FromIso8859_1(const char *astr, size_t size = kSizeAuto)
static inline plString FromIso8859_1(const char *astr, size_t size = STRLEN_AUTO)
{
plString str;
str.IConvertFromIso8859_1(astr, size);
@ -552,7 +546,7 @@ public:
* number of characters left in the string after \a start, Substr will
* return the remainder of the string.
*/
plString Substr(ssize_t start, size_t size = kSizeAuto) const;
plString Substr(ssize_t start, size_t size = STRLEN_AUTO) const;
/** Return a substring containing at most \a size characters from the left
* of the string. Equivalent to Substr(0, size).
@ -586,7 +580,7 @@ public:
* string in the returned vector.
* \sa Tokenize()
*/
std::vector<plString> Split(const char *split, size_t maxSplits = kSizeAuto) const;
std::vector<plString> Split(const char *split, size_t maxSplits = (size_t)-1) const;
/** Split this string into tokens, delimited by \a delims.
* Note that, unlike Split(), Tokenize will return only non-blank strings
@ -740,6 +734,6 @@ private:
};
/** \p strlen implementation for UniChar based C-style string buffers. */
size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto);
size_t ustrlen(const UniChar *ustr);
#endif //plString_Defined

Loading…
Cancel
Save