#ifndef _FX_STRING_H_
#define _FX_STRING_H_
+#include <stdint.h> // For intptr_t.
+#include <algorithm>
+
#include "fx_memory.h"
+#include "fx_system.h"
-class CFX_ByteStringC;
+class CFX_BinaryBuf;
class CFX_ByteString;
-class CFX_WideStringC;
class CFX_WideString;
struct CFX_CharMap;
-class CFX_BinaryBuf;
-typedef int FX_STRSIZE;
-class CFX_ByteStringL;
-class CFX_WideStringL;
// An immutable string with caller-provided storage which must outlive the
// string itself.
-class CFX_ByteStringC : public CFX_Object
+class CFX_ByteStringC
{
public:
typedef FX_CHAR value_type;
CFX_ByteStringC(FX_LPCSTR ptr)
{
m_Ptr = (FX_LPCBYTE)ptr;
- m_Length = ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0;
+ m_Length = ptr ? FXSYS_strlen(ptr) : 0;
}
// |ch| must be an lvalue that outlives the the CFX_ByteStringC. However,
CFX_ByteStringC(FX_LPCSTR ptr, FX_STRSIZE len)
{
m_Ptr = (FX_LPCBYTE)ptr;
- if (len == -1) {
- m_Length = (FX_STRSIZE)FXSYS_strlen(ptr);
- } else {
- m_Length = len;
- }
+ m_Length = (len == -1) ? FXSYS_strlen(ptr) : len;
}
CFX_ByteStringC(const CFX_ByteStringC& src)
CFX_ByteStringC& operator = (FX_LPCSTR src)
{
m_Ptr = (FX_LPCBYTE)src;
- m_Length = m_Ptr ? (FX_STRSIZE)FXSYS_strlen(src) : 0;
+ m_Length = m_Ptr ? FXSYS_strlen(src) : 0;
return *this;
}
CFX_ByteStringC& operator = (const CFX_ByteString& src);
- bool operator == (const CFX_ByteStringC& str) const
- {
- return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) == 0;
+ bool operator== (const char* ptr) const {
+ return FXSYS_strlen(ptr) == m_Length &&
+ FXSYS_memcmp32(ptr, m_Ptr, m_Length) == 0;
}
-
- bool operator != (const CFX_ByteStringC& str) const
- {
- return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) != 0;
+ bool operator== (const CFX_ByteStringC& other) const {
+ return other.m_Length == m_Length &&
+ FXSYS_memcmp32(other.m_Ptr, m_Ptr, m_Length) == 0;
+ }
+ bool operator!= (const char* ptr) const { return !(*this == ptr); }
+ bool operator!= (const CFX_ByteStringC& other) const {
+ return !(*this == other);
}
-#define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4))
FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
return m_Length == 0;
}
- operator FX_LPCBYTE() const
- {
- return m_Ptr;
- }
-
FX_BYTE GetAt(FX_STRSIZE index) const
{
return m_Ptr[index];
}
return CFX_ByteStringC(m_Ptr + index, count);
}
-protected:
- FX_LPCBYTE m_Ptr;
+ const FX_BYTE& operator[] (size_t index) const
+ {
+ return m_Ptr[index];
+ }
+
+ bool operator< (const CFX_ByteStringC& that) const
+ {
+ int result = memcmp(m_Ptr, that.m_Ptr, std::min(m_Length, that.m_Length));
+ return result < 0 || (result == 0 && m_Length < that.m_Length);
+ }
+protected:
+ FX_LPCBYTE m_Ptr;
FX_STRSIZE m_Length;
-private:
+private:
void* operator new (size_t) throw()
{
return NULL;
}
};
+inline bool operator== (const char* lhs, const CFX_ByteStringC& rhs) {
+ return rhs == lhs;
+}
+inline bool operator!= (const char* lhs, const CFX_ByteStringC& rhs) {
+ return rhs != lhs;
+}
typedef const CFX_ByteStringC& FX_BSTR;
#define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1)
-struct CFX_StringData {
-
- long m_nRefs;
+#define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4))
+// To ensure ref counts do not overflow, consider the worst possible case:
+// the entire address space contains nothing but pointers to this object.
+// Since the count increments with each new pointer, the largest value is
+// the number of pointers that can fit into the address space. The size of
+// the address space itself is a good upper bound on it; we need not go
+// larger.
+struct CFX_StringData {
+ intptr_t m_nRefs; // Would prefer ssize_t, but no windows support.
FX_STRSIZE m_nDataLength;
-
FX_STRSIZE m_nAllocLength;
-
FX_CHAR m_String[1];
};
-class CFX_ByteString : public CFX_Object
+
+// A mutable string with shared buffers using copy-on-write semantics that
+// avoids the cost of std::string's iterator stability guarantees.
+class CFX_ByteString
{
public:
typedef FX_CHAR value_type;
CFX_ByteString(char ch);
- CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len = -1);
+ CFX_ByteString(FX_LPCSTR ptr)
+ : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) { }
+
+ CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len);
CFX_ByteString(FX_LPCBYTE ptr, FX_STRSIZE len);
CFX_ByteString(FX_BSTR bstrc);
-
CFX_ByteString(FX_BSTR bstrc1, FX_BSTR bstrc2);
~CFX_ByteString();
int Compare(FX_BSTR str) const;
- bool Equal(FX_BSTR str) const;
-
-
- bool EqualNoCase(FX_BSTR str) const;
-
- bool operator == (FX_LPCSTR str) const
- {
- return Equal(str);
- }
+ bool Equal(const char* ptr) const;
+ bool Equal(const CFX_ByteStringC& str) const;
+ bool Equal(const CFX_ByteString& other) const;
- bool operator == (FX_BSTR str) const
- {
- return Equal(str);
- }
+ bool EqualNoCase(FX_BSTR str) const;
- bool operator == (const CFX_ByteString& str) const;
+ bool operator== (const char* ptr) const { return Equal(ptr); }
+ bool operator== (const CFX_ByteStringC& str) const { return Equal(str); }
+ bool operator== (const CFX_ByteString& other) const { return Equal(other); }
- bool operator != (FX_LPCSTR str) const
- {
- return !Equal(str);
+ bool operator!= (const char* ptr) const { return !(*this == ptr); }
+ bool operator!= (const CFX_ByteStringC& str) const {
+ return !(*this == str);
}
-
- bool operator != (FX_BSTR str) const
- {
- return !Equal(str);
+ bool operator!= (const CFX_ByteString& other) const {
+ return !(*this == other);
}
- bool operator != (const CFX_ByteString& str) const
+ bool operator< (const CFX_ByteString& str) const
{
- return !operator==(str);
+ int result = FXSYS_memcmp32(c_str(), str.c_str(), std::min(GetLength(), str.GetLength()));
+ return result < 0 || (result == 0 && GetLength() < str.GetLength());
}
void Empty();
FX_LPSTR GetBuffer(FX_STRSIZE len);
- FX_LPSTR LockBuffer();
-
void ReleaseBuffer(FX_STRSIZE len = -1);
CFX_ByteString Mid(FX_STRSIZE first) const;
return *this;
}
+inline bool operator== (const char* lhs, const CFX_ByteString& rhs) {
+ return rhs == lhs;
+}
+inline bool operator== (const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) {
+ return rhs == lhs;
+}
+inline bool operator!= (const char* lhs, const CFX_ByteString& rhs) {
+ return rhs != lhs;
+}
+inline bool operator!= (const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) {
+ return rhs != lhs;
+}
+
inline CFX_ByteString operator + (FX_BSTR str1, FX_BSTR str2)
{
return CFX_ByteString(str1, str2);
{
return CFX_ByteString(str1, str2);
}
-class CFX_StringBufBase : public CFX_Object
-{
-public:
-
- CFX_StringBufBase(FX_STRSIZE limit)
- {
- m_Size = 0;
- m_Limit = limit;
- }
-
- FX_CHAR* GetPtr() const
- {
- return (FX_CHAR*)(this + 1);
- }
-
- FX_STRSIZE GetSize() const
- {
- return m_Size;
- }
-
- void Empty()
- {
- m_Size = 0;
- }
-
- void Copy(FX_BSTR str);
-
- void Append(FX_BSTR str);
-
- void Append(int i, FX_DWORD flags = 0);
-
- CFX_ByteStringC GetStringC() const
- {
- return CFX_ByteStringC((FX_CHAR*)(this + 1), m_Size);
- }
-
- CFX_ByteString GetString() const
- {
- return CFX_ByteString((FX_CHAR*)(this + 1), m_Size);
- }
-protected:
-
- FX_STRSIZE m_Limit;
-
- FX_STRSIZE m_Size;
-};
-template<FX_STRSIZE limit>
-class CFX_StringBufTemplate : public CFX_StringBufBase
-{
-public:
-
- CFX_StringBufTemplate() : CFX_StringBufBase(limit) {}
-
- FX_CHAR m_Buffer[limit];
-};
-typedef CFX_StringBufTemplate<256> CFX_StringBuf256;
-class CFX_WideStringC : public CFX_Object
+class CFX_WideStringC
{
public:
typedef FX_WCHAR value_type;
CFX_WideStringC(FX_LPCWSTR ptr)
{
m_Ptr = ptr;
- m_Length = ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0;
+ m_Length = ptr ? FXSYS_wcslen(ptr) : 0;
}
CFX_WideStringC(FX_WCHAR& ch)
CFX_WideStringC(FX_LPCWSTR ptr, FX_STRSIZE len)
{
m_Ptr = ptr;
- if (len == -1) {
- m_Length = (FX_STRSIZE)FXSYS_wcslen(ptr);
- } else {
- m_Length = len;
- }
+ m_Length = (len == -1) ? FXSYS_wcslen(ptr) : len;
}
CFX_WideStringC(const CFX_WideStringC& src)
CFX_WideStringC& operator = (FX_LPCWSTR src)
{
m_Ptr = src;
- m_Length = (FX_STRSIZE)FXSYS_wcslen(src);
+ m_Length = FXSYS_wcslen(src);
return *this;
}
CFX_WideStringC& operator = (const CFX_WideString& src);
- bool operator == (const CFX_WideStringC& str) const
- {
- return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) == 0;
+ bool operator== (const wchar_t* ptr) const {
+ return FXSYS_wcslen(ptr) == m_Length &&
+ wmemcmp(ptr, m_Ptr, m_Length) == 0;
}
-
- bool operator != (const CFX_WideStringC& str) const
- {
- return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) != 0;
+ bool operator== (const CFX_WideStringC& str) const {
+ return str.m_Length == m_Length &&
+ wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0;
+ }
+ bool operator!= (const wchar_t* ptr) const { return !(*this == ptr); }
+ bool operator!= (const CFX_WideStringC& str) const {
+ return !(*this == str);
}
FX_LPCWSTR GetPtr() const
}
return CFX_WideStringC(m_Ptr + m_Length - count, count);
}
-protected:
- FX_LPCWSTR m_Ptr;
+ const FX_WCHAR& operator[] (size_t index) const
+ {
+ return m_Ptr[index];
+ }
+
+ bool operator< (const CFX_WideStringC& that) const
+ {
+ int result = wmemcmp(m_Ptr, that.m_Ptr, std::min(m_Length, that.m_Length));
+ return result < 0 || (result == 0 && m_Length < that.m_Length);
+ }
+protected:
+ FX_LPCWSTR m_Ptr;
FX_STRSIZE m_Length;
-private:
+private:
void* operator new (size_t) throw()
{
return NULL;
}
};
+inline bool operator== (const wchar_t* lhs, const CFX_WideStringC& rhs) {
+ return rhs == lhs;
+}
+inline bool operator!= (const wchar_t* lhs, const CFX_WideStringC& rhs) {
+ return rhs != lhs;
+}
typedef const CFX_WideStringC& FX_WSTR;
#define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1)
-struct CFX_StringDataW {
-
- long m_nRefs;
+struct CFX_StringDataW {
+ intptr_t m_nRefs; // Would prefer ssize_t, but no windows support.
FX_STRSIZE m_nDataLength;
-
FX_STRSIZE m_nAllocLength;
-
FX_WCHAR m_String[1];
};
-class CFX_WideString : public CFX_Object
+
+// A mutable string with shared buffers using copy-on-write semantics that
+// avoids the cost of std::string's iterator stability guarantees.
+class CFX_WideString
{
public:
typedef FX_WCHAR value_type;
CFX_WideString(const CFX_WideString& str);
- CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len = -1)
- {
- InitStr(ptr, len);
- }
+ CFX_WideString(FX_LPCWSTR ptr)
+ : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) { }
+
+ CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len);
CFX_WideString(FX_WCHAR ch);
const CFX_WideString& operator += (const CFX_WideStringC& str);
+ bool operator== (const wchar_t* ptr) const { return Equal(ptr); }
+ bool operator== (const CFX_WideStringC& str) const { return Equal(str); }
+ bool operator== (const CFX_WideString& other) const { return Equal(other); }
+
+ bool operator!= (const wchar_t* ptr) const { return !(*this == ptr); }
+ bool operator!= (const CFX_WideStringC& str) const {
+ return !(*this == str);
+ }
+ bool operator!= (const CFX_WideString& other) const {
+ return !(*this == other);
+ }
+
+ bool operator< (const CFX_WideString& str) const {
+ int result = wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength()));
+ return result < 0 || (result == 0 && GetLength() < str.GetLength());
+ }
+
FX_WCHAR GetAt(FX_STRSIZE nIndex) const
{
return m_pData ? m_pData->m_String[nIndex] : 0;
int CompareNoCase(FX_LPCWSTR str) const;
- bool Equal(const CFX_WideStringC& str) const;
+ bool Equal(const wchar_t* ptr) const;
+ bool Equal(const CFX_WideStringC& str) const;
+ bool Equal(const CFX_WideString& other) const;
CFX_WideString Mid(FX_STRSIZE first) const;
FX_LPWSTR GetBuffer(FX_STRSIZE len);
- FX_LPWSTR LockBuffer();
-
void ReleaseBuffer(FX_STRSIZE len = -1);
int GetInteger() const;
CFX_ByteString UTF16LE_Encode() const;
void ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap = NULL);
-protected:
- void InitStr(FX_LPCWSTR ptr, int len);
- CFX_StringDataW* m_pData;
+protected:
void CopyBeforeWrite();
void AllocBeforeWrite(FX_STRSIZE nLen);
void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data);
void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
+
+ CFX_StringDataW* m_pData;
};
inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src)
{
{
return CFX_WideString(str1, str2);
}
-
-bool operator==(const CFX_WideString& s1, const CFX_WideString& s2);
-bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2);
-bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2);
-bool operator== (const CFX_WideString& s1, FX_LPCWSTR s2);
-bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2);
-bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2);
-bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2);
-bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2);
-bool operator!= (const CFX_WideString& s1, FX_LPCWSTR s2);
-bool operator!=(FX_LPCWSTR s1, const CFX_WideString& s2);
+inline bool operator== (const wchar_t* lhs, const CFX_WideString& rhs) {
+ return rhs == lhs;
+}
+inline bool operator== (const CFX_WideStringC& lhs, const CFX_WideString& rhs) {
+ return rhs == lhs;
+}
+inline bool operator!= (const wchar_t* lhs, const CFX_WideString& rhs) {
+ return rhs != lhs;
+}
+inline bool operator!= (const CFX_WideStringC& lhs, const CFX_WideString& rhs) {
+ return rhs != lhs;
+}
FX_FLOAT FX_atof(FX_BSTR str);
void FX_atonum(FX_BSTR str, FX_BOOL& bInteger, void* pData);
FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf);