1
1
forked from 0ad/0ad

remove *tot - non-portable and inefficient. replaced with stringstream

This was SVN commit r529.
This commit is contained in:
janwas 2004-06-18 13:22:26 +00:00
parent df62c14469
commit b09211c4a3
4 changed files with 91 additions and 131 deletions

View File

@ -351,52 +351,3 @@ void base32(const int len, const u8* in, u8* out)
}
}
#ifndef _WIN32
char *_itoa(int value, char *out, int radix)
{
return _ltoa(value, out, radix);
}
static const char digits[]="0123456789abcdef";
char *_ultoa(unsigned long int value, char *out, int radix)
{
char buf[21];
char *p=buf+21;
do
{
*(--p)=digits[value % radix];
value /= radix;
}
while (value);
memcpy(out, p, (buf+21)-p);
out[(buf+21)-p]=0;
return out;
}
char *_ltoa(long val, char *out, int radix)
{
char buf[21];
char *p=buf+21;
bool sign=val < 0;
if (sign) val=-val;
do
{
*(--p)=digits[val % radix];
val /= radix;
}
while (val);
if (sign) *(--p) = '-';
memcpy(out, p, (buf+21)-p);
out[(buf+21)-p]=0;
return out;
}
#endif

View File

@ -14,12 +14,14 @@
#include "CStr.h"
using namespace std;
#include <sstream>
CStr::CStr()
{
// Default Constructor
}
CStr::CStr(const CStr &Str)
CStr::CStr(const CStr& Str)
{
// Copy Constructor
m_String = Str.m_String;
@ -44,43 +46,47 @@ CStr::CStr(TCHAR Char)
CStr::CStr(int Number)
{
// Creates CStr from a int
m_String = _itot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
}
CStr::CStr(unsigned int Number)
{
// Creates CStr from a uint
m_String = _ultot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
}
CStr::CStr(long Number)
{
// Creates CStr from a long
m_String = _ltot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
}
CStr::CStr(unsigned long Number)
{
// Creates CStr from a ulong
m_String = _ultot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
}
CStr::CStr(float Number)
{
// Creates CStr from a float
_tsnprintf(m_ConversionBuffer, CONVERSION_BUFFER_SIZE, FLOAT_CONVERSION, Number);
m_String = m_ConversionBuffer;
std::tstringstream ss;
ss << Number;
ss >> m_String;
}
CStr::CStr(double Number)
{
// Creates CStr from a double
_tsnprintf(m_ConversionBuffer, CONVERSION_BUFFER_SIZE, FLOAT_CONVERSION, Number);
m_String = m_ConversionBuffer;
std::tstringstream ss;
ss << Number;
ss >> m_String;
}
CStr::~CStr()
@ -128,7 +134,7 @@ CStr CStr::GetSubstring(size_t start, size_t len) const
//Search the string for another string
long CStr::Find(const CStr &Str) const
long CStr::Find(const CStr& Str) const
{
long Pos = (long)m_String.find(Str.m_String, 0);
@ -160,7 +166,7 @@ long CStr::Find(const int &start, const TCHAR &tchar) const
return -1;
}
long CStr::ReverseFind(const CStr &Str) const
long CStr::ReverseFind(const CStr& Str) const
{
long Pos = (long)m_String.rfind(Str.m_String, m_String.length() );
@ -223,7 +229,7 @@ CStr CStr::Right(long len) const
}
//Remove all occurences of some character or substring
void CStr::Remove(const CStr &Str)
void CStr::Remove(const CStr& Str)
{
size_t FoundAt = 0;
while (FoundAt != tstring::npos)
@ -236,7 +242,7 @@ void CStr::Remove(const CStr &Str)
}
//Replace all occurences of some substring by another
void CStr::Replace(const CStr &ToReplace, const CStr &ReplaceWith)
void CStr::Replace(const CStr& ToReplace, const CStr& ReplaceWith)
{
size_t Pos = 0;
@ -293,100 +299,114 @@ CStr CStr::Trim(PS_TRIM_MODE Mode)
}
// Overload operations
CStr &CStr::operator=(const CStr &Str)
CStr& CStr::operator=(const CStr& Str)
{
m_String = Str.m_String;
return *this;
}
CStr &CStr::operator=(const TCHAR* String)
CStr& CStr::operator=(const TCHAR* String)
{
m_String = String;
return *this;
}
CStr &CStr::operator=(TCHAR Char)
CStr& CStr::operator=(TCHAR Char)
{
m_String = Char;
return *this;
}
CStr &CStr::operator=(int Number)
CStr& CStr::operator=(int Number)
{
m_String = _itot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
return *this;
}
CStr &CStr::operator=(long Number)
CStr& CStr::operator=(long Number)
{
m_String = _ltot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
return *this;
}
CStr &CStr::operator=(unsigned int Number)
CStr& CStr::operator=(unsigned int Number)
{
m_String = _ultot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
return *this;
}
CStr &CStr::operator=(unsigned long Number)
CStr& CStr::operator=(unsigned long Number)
{
m_String = _ultot(Number, m_ConversionBuffer, 10);
std::tstringstream ss;
ss << Number;
ss >> m_String;
return *this;
}
CStr &CStr::operator=(float Number)
CStr& CStr::operator=(float Number)
{
_tsnprintf(m_ConversionBuffer, CONVERSION_BUFFER_SIZE, FLOAT_CONVERSION, Number);
m_String = m_ConversionBuffer;
std::tstringstream ss;
ss << Number;
ss >> m_String;
return *this;
}
CStr &CStr::operator=(double Number)
CStr& CStr::operator=(double Number)
{
_tsnprintf(m_ConversionBuffer, CONVERSION_BUFFER_SIZE, FLOAT_CONVERSION, Number);
m_String = m_ConversionBuffer;
std::tstringstream ss;
ss << Number;
ss >> m_String;
return *this;
}
bool CStr::operator==(const CStr &Str) const
bool CStr::operator==(const CStr& Str) const
{
return (m_String == Str.m_String);
}
bool CStr::operator!=(const CStr &Str) const
bool CStr::operator!=(const CStr& Str) const
{
return (m_String != Str.m_String);
}
bool CStr::operator<(const CStr &Str) const
bool CStr::operator<(const CStr& Str) const
{
return (m_String < Str.m_String);
}
bool CStr::operator<=(const CStr &Str) const
bool CStr::operator<=(const CStr& Str) const
{
return (m_String <= Str.m_String);
}
bool CStr::operator>(const CStr &Str) const
bool CStr::operator>(const CStr& Str) const
{
return (m_String > Str.m_String);
}
bool CStr::operator>=(const CStr &Str) const
bool CStr::operator>=(const CStr& Str) const
{
return (m_String >= Str.m_String);
}
CStr &CStr::operator+=(const CStr &Str)
CStr& CStr::operator+=(const CStr& Str)
{
m_String += Str.m_String;
return *this;
}
CStr CStr::operator+(const CStr &Str)
CStr CStr::operator+(const CStr& Str)
{
CStr NewStr(*this);
NewStr.m_String += Str.m_String;
@ -427,7 +447,7 @@ TCHAR &CStr::operator[](unsigned long n)
return m_String[n];
}
ostream &operator<<(ostream &os, CStr &Str)
ostream &operator<<(ostream &os, CStr& Str)
{
os << (const TCHAR*)Str;
return os;

View File

@ -35,10 +35,6 @@ More Info:
#ifndef CSTR_H_FIRST
#define CSTR_H_FIRST
// DEFINES/ENUMS
#define CONVERSION_BUFFER_SIZE 32
#define FLOAT_CONVERSION _T("%.6f")
enum PS_TRIM_MODE {PS_TRIM_LEFT, PS_TRIM_RIGHT, PS_TRIM_BOTH};
#ifndef IN_UNIDOUBLER
@ -64,34 +60,30 @@ enum PS_TRIM_MODE {PS_TRIM_LEFT, PS_TRIM_RIGHT, PS_TRIM_BOTH};
#ifdef _UNICODE
#define tstring wstring
#define tstringstream wstringstream
#define _tcout wcout
#define _tstod wcstod
#define TCHAR wchar_t
#define _ttoi _wtoi
#define _ttol _wtol
#define _itot _itow
#define _ultot _itow
#define _T(t) L ## t
#define _totlower towlower
#define _istspace iswspace
#define _tsnprintf swprintf
#define _ltot _ltow
#else
#define tstringstream stringstream
#define tstring string
#define _tcout cout
#define _tstod strtod
#define _ttoi atoi
#define _ttol atol
#define _itot _itoa
#define TCHAR char
#define _T(t) t
#define _istspace isspace
#define _tsnprintf snprintf
#define _totlower tolower
#define _ultot _ultoa
#define _ltot _ltoa
#endif
@ -102,7 +94,7 @@ public:
// CONSTRUCTORS
CStr(); // Default constructor
CStr(const CStr &Str); // Copy Constructor
CStr(const CStr& Str); // Copy Constructor
CStr(std::tstring String); // Creates CStr from C++ string
CStr(const TCHAR* String); // Creates CStr from C-Style TCHAR string
@ -128,7 +120,7 @@ public:
CStr GetSubstring(size_t start, size_t len) const;
//Search the string for another string
long Find(const CStr &Str) const;
long Find(const CStr& Str) const;
//Search the string for another string
long Find(const TCHAR &tchar) const;
@ -137,7 +129,7 @@ public:
long Find(const int &start, const TCHAR &tchar) const;
//You can also do a "ReverseFind"- i.e. search starting from the end
long ReverseFind(const CStr &Str) const;
long ReverseFind(const CStr& Str) const;
// Lowercase and uppercase
CStr LowerCase() const;
@ -154,33 +146,33 @@ public:
CStr Right(long len) const;
//Remove all occurences of some character or substring
void Remove(const CStr &Str);
void Remove(const CStr& Str);
//Replace all occurences of some substring by another
void Replace(const CStr &StrToReplace, const CStr &ReplaceWith);
void Replace(const CStr& StrToReplace, const CStr& ReplaceWith);
// Returns a trimed string, removes whitespace from the left/right/both
CStr Trim(PS_TRIM_MODE Mode);
// Overload operations
CStr &operator=(const CStr &Str);
CStr &operator=(const TCHAR* String);
CStr &operator=(TCHAR Char);
CStr &operator=(int Number);
CStr &operator=(long Number);
CStr &operator=(unsigned int Number);
CStr &operator=(unsigned long Number);
CStr &operator=(float Number);
CStr &operator=(double Number);
CStr& operator=(const CStr& Str);
CStr& operator=(const TCHAR* String);
CStr& operator=(TCHAR Char);
CStr& operator=(int Number);
CStr& operator=(long Number);
CStr& operator=(unsigned int Number);
CStr& operator=(unsigned long Number);
CStr& operator=(float Number);
CStr& operator=(double Number);
bool operator==(const CStr &Str) const;
bool operator!=(const CStr &Str) const;
bool operator<(const CStr &Str) const;
bool operator<=(const CStr &Str) const;
bool operator>(const CStr &Str) const;
bool operator>=(const CStr &Str) const;
CStr &operator+=(const CStr &Str);
CStr operator+(const CStr &Str);
bool operator==(const CStr& Str) const;
bool operator!=(const CStr& Str) const;
bool operator<(const CStr& Str) const;
bool operator<=(const CStr& Str) const;
bool operator>(const CStr& Str) const;
bool operator>=(const CStr& Str) const;
CStr& operator+=(const CStr& Str);
CStr operator+(const CStr& Str);
operator const TCHAR*();
operator const TCHAR*() const; // Gee, I've added this, Maybe the one above should be removed?
TCHAR &operator[](int n);
@ -200,7 +192,6 @@ public:
protected:
std::tstring m_String;
TCHAR m_ConversionBuffer[CONVERSION_BUFFER_SIZE];
};
class CStr_hash_compare
@ -219,6 +210,6 @@ public:
};
// overloaded operator for ostreams
std::ostream &operator<<(std::ostream &os, CStr &Str);
std::ostream &operator<<(std::ostream &os, CStr& Str);
#endif

View File

@ -14,18 +14,16 @@
// Undef all the Conversion Macros
#undef tstring
#undef tstringstream
#undef _tcout
#undef _tstod
#undef _ttoi
#undef _ttol
#undef _itot
#undef TCHAR
#undef _T
#undef _istspace
#undef _tsnprintf
#undef _totlower
#undef _ultot
#undef _ltot
// Now include the 8-bit version under the name CStr8
#undef _UNICODE