1
1
forked from 0ad/0ad

Removes unused macro from CStr, reduces macro dependency.

Tested By: Freagarach, Stan
Differential Revision: https://code.wildfiregames.com/D4245
This was SVN commit r25891.
This commit is contained in:
Vladislav Belov 2021-09-03 21:06:22 +00:00
parent 3b417062bb
commit e62dac7ad4
2 changed files with 133 additions and 136 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -70,22 +70,28 @@ CStrW CStr8::FromUTF8() const
#include <sstream>
#ifdef _UNICODE
#define tstringstream wstringstream
#define _istspace iswspace
#define _totlower towlower
#define _totupper towupper
#else
#define tstringstream stringstream
#define _istspace isspace
#define _totlower tolower
#define _totupper toupper
#endif
CStr CStr::Repeat(const CStr& String, size_t Reps)
namespace
{
template<typename StrBase>
using tstringstream = std::basic_stringstream<typename StrBase::value_type>;
} // anonymous namespace
CStr CStr::Repeat(const CStr& str, size_t reps)
{
CStr ret;
ret.reserve(String.length() * Reps);
while (Reps--) ret += String;
ret.reserve(str.length() * reps);
while (reps--) ret += str;
return ret;
}
@ -93,28 +99,28 @@ CStr CStr::Repeat(const CStr& String, size_t Reps)
CStr CStr::FromInt(int n)
{
std::tstringstream ss;
tstringstream<StrBase> ss;
ss << n;
return ss.str();
}
CStr CStr::FromUInt(unsigned int n)
{
std::tstringstream ss;
tstringstream<StrBase> ss;
ss << n;
return ss.str();
}
CStr CStr::FromInt64(i64 n)
{
std::tstringstream ss;
tstringstream<StrBase> ss;
ss << n;
return ss.str();
}
CStr CStr::FromDouble(double n)
{
std::tstringstream ss;
tstringstream<StrBase> ss;
ss << n;
return ss.str();
}
@ -124,7 +130,7 @@ CStr CStr::FromDouble(double n)
int CStr::ToInt() const
{
int ret = 0;
std::tstringstream str(*this);
tstringstream<StrBase> str(*this);
str >> ret;
return ret;
}
@ -132,7 +138,7 @@ int CStr::ToInt() const
unsigned int CStr::ToUInt() const
{
unsigned int ret = 0;
std::tstringstream str(*this);
tstringstream<StrBase> str(*this);
str >> ret;
return ret;
}
@ -140,7 +146,7 @@ unsigned int CStr::ToUInt() const
long CStr::ToLong() const
{
long ret = 0;
std::tstringstream str(*this);
tstringstream<StrBase> str(*this);
str >> ret;
return ret;
}
@ -148,7 +154,7 @@ long CStr::ToLong() const
unsigned long CStr::ToULong() const
{
unsigned long ret = 0;
std::tstringstream str(*this);
tstringstream<StrBase> str(*this);
str >> ret;
return ret;
}
@ -170,7 +176,7 @@ CStr ParseableAsNumber(CStr cleaned_copy)
float CStr::ToFloat() const
{
float ret = 0;
std::tstringstream str(ParseableAsNumber(*this));
tstringstream<StrBase> str(ParseableAsNumber(*this));
str >> ret;
return ret;
}
@ -178,19 +184,18 @@ float CStr::ToFloat() const
double CStr::ToDouble() const
{
double ret = 0;
std::tstringstream str(ParseableAsNumber(*this));
tstringstream<StrBase> str(ParseableAsNumber(*this));
str >> ret;
return ret;
}
// Search the string for another string
long CStr::Find(const CStr& Str) const
long CStr::Find(const CStr& str) const
{
size_t Pos = find(Str, 0);
size_t pos = find(str, 0);
if (Pos != npos)
return (long)Pos;
if (pos != npos)
return static_cast<long>(pos);
return -1;
}
@ -198,10 +203,10 @@ long CStr::Find(const CStr& Str) const
// Search the string for another string
long CStr::Find(const tchar chr) const
{
size_t Pos = find(chr, 0);
size_t pos = find(chr, 0);
if (Pos != npos)
return (long)Pos;
if (pos != npos)
return static_cast<long>(pos);
return -1;
}
@ -209,47 +214,45 @@ long CStr::Find(const tchar chr) const
// Search the string for another string
long CStr::Find(const int start, const tchar chr) const
{
size_t Pos = find(chr, start);
size_t pos = find(chr, start);
if (Pos != npos)
return (long)Pos;
if (pos != npos)
return static_cast<long>(pos);
return -1;
}
long CStr::FindInsensitive(const int start, const tchar chr) const { return LowerCase().Find(start, _totlower(chr)); }
long CStr::FindInsensitive(const tchar chr) const { return LowerCase().Find(_totlower(chr)); }
long CStr::FindInsensitive(const CStr& Str) const { return LowerCase().Find(Str.LowerCase()); }
long CStr::FindInsensitive(const CStr& str) const { return LowerCase().Find(str.LowerCase()); }
long CStr::ReverseFind(const CStr& Str) const
long CStr::ReverseFind(const CStr& str) const
{
size_t Pos = rfind(Str, length() );
size_t pos = rfind(str, length() );
if (Pos != npos)
return (long)Pos;
if (pos != npos)
return static_cast<long>(pos);
return -1;
}
// Lowercase and uppercase
CStr CStr::LowerCase() const
{
std::tstring NewString = *this;
StrBase newStr = *this;
for (size_t i = 0; i < length(); i++)
NewString[i] = (tchar)_totlower((*this)[i]);
newStr[i] = (tchar)_totlower((*this)[i]);
return NewString;
return newStr;
}
CStr CStr::UpperCase() const
{
std::tstring NewString = *this;
StrBase newStr = *this;
for (size_t i = 0; i < length(); i++)
NewString[i] = (tchar)_totupper((*this)[i]);
newStr[i] = (tchar)_totupper((*this)[i]);
return NewString;
return newStr;
}
@ -269,20 +272,20 @@ CStr CStr::Right(size_t len) const
// Retrieve the substring following the last occurrence of Str
// (or the whole string if it doesn't contain Str)
CStr CStr::AfterLast(const CStr& Str, size_t startPos) const
CStr CStr::AfterLast(const CStr& str, size_t startPos) const
{
size_t pos = rfind(Str, startPos);
size_t pos = rfind(str, startPos);
if (pos == npos)
return *this;
else
return substr(pos + Str.length());
return substr(pos + str.length());
}
// Retrieve the substring preceding the last occurrence of Str
// (or the whole string if it doesn't contain Str)
CStr CStr::BeforeLast(const CStr& Str, size_t startPos) const
CStr CStr::BeforeLast(const CStr& str, size_t startPos) const
{
size_t pos = rfind(Str, startPos);
size_t pos = rfind(str, startPos);
if (pos == npos)
return *this;
else
@ -291,20 +294,20 @@ CStr CStr::BeforeLast(const CStr& Str, size_t startPos) const
// Retrieve the substring following the first occurrence of Str
// (or the whole string if it doesn't contain Str)
CStr CStr::AfterFirst(const CStr& Str, size_t startPos) const
CStr CStr::AfterFirst(const CStr& str, size_t startPos) const
{
size_t pos = find(Str, startPos);
size_t pos = find(str, startPos);
if (pos == npos)
return *this;
else
return substr(pos + Str.length());
return substr(pos + str.length());
}
// Retrieve the substring preceding the first occurrence of Str
// (or the whole string if it doesn't contain Str)
CStr CStr::BeforeFirst(const CStr& Str, size_t startPos) const
CStr CStr::BeforeFirst(const CStr& str, size_t startPos) const
{
size_t pos = find(Str, startPos);
size_t pos = find(str, startPos);
if (pos == npos)
return *this;
else
@ -312,92 +315,91 @@ CStr CStr::BeforeFirst(const CStr& Str, size_t startPos) const
}
// Remove all occurrences of some character or substring
void CStr::Remove(const CStr& Str)
void CStr::Remove(const CStr& str)
{
size_t FoundAt = 0;
while (FoundAt != npos)
size_t foundAt = 0;
while (foundAt != npos)
{
FoundAt = find(Str, 0);
foundAt = find(str, 0);
if (FoundAt != npos)
erase(FoundAt, Str.length());
if (foundAt != npos)
erase(foundAt, str.length());
}
}
// Replace all occurrences 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;
while (Pos != npos)
size_t pos = 0;
while (pos != npos)
{
Pos = find(ToReplace, Pos);
if (Pos != npos)
pos = find(toReplace, pos);
if (pos != npos)
{
erase(Pos, ToReplace.length());
insert(Pos, ReplaceWith);
Pos += ReplaceWith.length();
erase(pos, toReplace.length());
insert(pos, replaceWith);
pos += replaceWith.length();
}
}
}
std::string CStr::EscapeToPrintableASCII() const
{
std::string NewString;
std::string newStr;
for (size_t i = 0; i < length(); i++)
{
tchar ch = (*this)[i];
if (ch == '"') NewString += "\\\"";
else if (ch == '\\') NewString += "\\\\";
else if (ch == '\b') NewString += "\\b";
else if (ch == '\f') NewString += "\\f";
else if (ch == '\n') NewString += "\\n";
else if (ch == '\r') NewString += "\\r";
else if (ch == '\t') NewString += "\\t";
if (ch == '"') newStr += "\\\"";
else if (ch == '\\') newStr += "\\\\";
else if (ch == '\b') newStr += "\\b";
else if (ch == '\f') newStr += "\\f";
else if (ch == '\n') newStr += "\\n";
else if (ch == '\r') newStr += "\\r";
else if (ch == '\t') newStr += "\\t";
else if (ch >= 32 && ch <= 126)
NewString += ch;
newStr += ch;
else
{
std::stringstream ss;
ss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << (int)(unsigned char)ch;
NewString += ss.str();
newStr += ss.str();
}
}
return NewString;
return newStr;
}
// Returns a trimmed string, removes whitespace from the left/right/both
CStr CStr::Trim(PS_TRIM_MODE Mode) const
CStr CStr::Trim(PS_TRIM_MODE mode) const
{
size_t Left = 0, Right = 0;
size_t left = 0, right = 0;
switch (Mode)
switch (mode)
{
case PS_TRIM_LEFT:
{
for (Left = 0; Left < length(); Left++)
if (_istspace((*this)[Left]) == false)
for (left = 0; left < length(); left++)
if (_istspace((*this)[left]) == false)
break; // end found, trim 0 to Left-1 inclusive
} break;
case PS_TRIM_RIGHT:
{
Right = length();
while (Right--)
if (_istspace((*this)[Right]) == false)
right = length();
while (right--)
if (_istspace((*this)[right]) == false)
break; // end found, trim len-1 to Right+1 inclusive
} break;
case PS_TRIM_BOTH:
{
for (Left = 0; Left < length(); Left++)
if (_istspace((*this)[Left]) == false)
for (left = 0; left < length(); left++)
if (_istspace((*this)[left]) == false)
break; // end found, trim 0 to Left-1 inclusive
Right = length();
while (Right--)
if (_istspace((*this)[Right]) == false)
right = length();
while (right--)
if (_istspace((*this)[right]) == false)
break; // end found, trim len-1 to Right+1 inclusive
} break;
@ -406,38 +408,38 @@ CStr CStr::Trim(PS_TRIM_MODE Mode) const
}
return substr(Left, Right-Left+1);
return substr(left, right - left + 1);
}
CStr CStr::Pad(PS_TRIM_MODE Mode, size_t Length) const
CStr CStr::Pad(PS_TRIM_MODE mode, size_t len) const
{
size_t Left = 0, Right = 0;
size_t left = 0, right = 0;
if (Length <= length())
if (len <= length())
return *this;
// From here: Length-length() >= 1
switch (Mode)
switch (mode)
{
case PS_TRIM_LEFT:
Left = Length - length();
left = len - length();
break;
case PS_TRIM_RIGHT:
Right = Length - length();
right = len - length();
break;
case PS_TRIM_BOTH:
Left = (Length - length() + 1)/2;
Right = (Length - length() - 1)/2; // cannot be negative
left = (len - length() + 1) / 2;
right = (len - length() - 1) / 2; // cannot be negative
break;
default:
debug_warn(L"CStr::Trim: invalid Mode");
}
return std::tstring(Left, _T(' ')) + *this + std::tstring(Right, _T(' '));
return StrBase(left, _T(' ')) + *this + StrBase(right, _T(' '));
}
size_t CStr::GetHashCode() const
@ -527,10 +529,6 @@ size_t CStr::GetSerializedLength() const
#endif // _UNICODE
// Clean up, to keep the second pass through unidoubler happy
#undef tstringstream
#undef _tstod
#undef _ttoi
#undef _ttol
#undef _istspace
#undef _totlower
#undef _totupper

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -59,34 +59,33 @@ class CStrW;
/**
* The base class of all strings
**/
class CStr: public std::tstring
class CStr : public std::tstring
{
public:
// CONSTRUCTORS
using StrBase = std::tstring;
CStr() {}
CStr(const tchar* String) : std::tstring(String) {}
CStr(const tchar* String, size_t Length) : std::tstring(String, Length) {}
CStr(const std::tstring& String) : std::tstring(String) {}
template <class InputIterator>
CStr (InputIterator first, InputIterator last) : std::tstring(first, last) {}
CStr(const tchar* str) : StrBase(str) {}
CStr(const tchar* str, size_t len) : StrBase(str, len) {}
CStr(const StrBase& str) : StrBase(str) {}
template<class InputIterator>
CStr (InputIterator first, InputIterator last) : StrBase(first, last) {}
/**
* Repeat: Named constructor, to avoid overload overload.
*
* @param const CStr & String reference to another CStr object to be repeated for initialization
* @param const CStr & str reference to another CStr object to be repeated for initialization
* @param size_t Reps number of times to repeat the initialization
* @return CStr new CStr object
**/
static CStr Repeat(const CStr& String, size_t Reps);
static CStr Repeat(const CStr& str, size_t reps);
/**
* Construction from utf16strings.
*
* @param utf16string String utf16string to be used for initialization.
**/
explicit CStr(const utf16string& String) : std::tstring(String.begin(), String.end()) {}
explicit CStr(const utf16string& str) : StrBase(str.begin(), str.end()) {}
// Conversion to/from UTF-8, encoded in a CStr8.
// Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
@ -151,11 +150,11 @@ public:
* Search the CStr for another string.
* The search is case-sensitive.
*
* @param const CStr & Str reference to the search string
* @param const CStr & str reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long Find(const CStr& Str) const;
long Find(const CStr& str) const;
/**
* Search the CStr for another string.
* The search is case-sensitive.
@ -180,11 +179,11 @@ public:
* Search the CStr for another string.
* The search is case-insensitive.
*
* @param const CStr & Str reference to the search string
* @param const CStr & str reference to the search string
* @return long offset into the CStr of the first occurrence of the search string
* -1 if the search string is not found
**/
long FindInsensitive(const CStr& Str) const;
long FindInsensitive(const CStr& str) const;
/**
* Search the CStr for another string.
* The search is case-insensitive.
@ -209,11 +208,11 @@ public:
* Search the CStr for another string.
* The search is case-sensitive.
*
* @param const CStr & Str reference to the search string
* @param const CStr & str reference to the search string
* @return long offset into the CStr of the last occurrence of the search string
* -1 if the search string is not found
**/
long ReverseFind(const CStr& Str) const;
long ReverseFind(const CStr& str) const;
/**
* Make a copy of the CStr in lower-case.
@ -248,60 +247,60 @@ public:
* Retrieve substring of the CStr after last occurrence of a string.
* Return substring of the CStr after the last occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param const CStr & str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring remaining after match
* the CStr if no match is found
**/
CStr AfterLast(const CStr& Str, size_t startPos = npos) const;
CStr AfterLast(const CStr& str, size_t startPos = npos) const;
/**
* Retrieve substring of the CStr preceding last occurrence of a string.
* Return substring of the CStr preceding the last occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param const CStr & str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring preceding before match
* the CStr if no match is found
**/
CStr BeforeLast(const CStr& Str, size_t startPos = npos) const;
CStr BeforeLast(const CStr& str, size_t startPos = npos) const;
/**
* Retrieve substring of the CStr after first occurrence of a string.
* Return substring of the CStr after the first occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param const CStr & str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring remaining after match
* the CStr if no match is found
**/
CStr AfterFirst(const CStr& Str, size_t startPos = 0) const;
CStr AfterFirst(const CStr& str, size_t startPos = 0) const;
/**
* Retrieve substring of the CStr preceding first occurrence of a string.
* Return substring of the CStr preceding the first occurrence of the search string.
*
* @param const CStr & Str reference to search string
* @param const CStr & str reference to search string
* @param size_t startPos character position to start searching from
* @return CStr substring preceding before match
* the CStr if no match is found
**/
CStr BeforeFirst(const CStr& Str, size_t startPos = 0) const;
CStr BeforeFirst(const CStr& str, size_t startPos = 0) const;
/**
* Remove all occurrences of a string from the CStr.
*
* @param const CStr & Str reference to search string to remove.
* @param const CStr & str reference to search string to remove.
**/
void Remove(const CStr& Str);
void Remove(const CStr& str);
/**
* Replace all occurrences of one string by another string in the CStr.
*
* @param const CStr & StrToReplace reference to search string.
* @param const CStr & ReplaceWith reference to replace string.
* @param const CStr & strToReplace reference to search string.
* @param const CStr & replaceWith reference to replace string.
**/
void Replace(const CStr& StrToReplace, const CStr& ReplaceWith);
void Replace(const CStr& toReplace, const CStr& replaceWith);
/**
* Convert strings to printable ASCII characters with JSON-style escapes.
@ -314,7 +313,7 @@ public:
* @param PS_TRIM_MODE Mode value from trim mode enumeration.
* @return CStr copy of trimmed CStr.
**/
CStr Trim(PS_TRIM_MODE Mode) const;
CStr Trim(PS_TRIM_MODE mode) const;
/**
* Return a space padded copy of the CStr.
@ -323,7 +322,7 @@ public:
* @param size_t Length number of pad spaces to add
* @return CStr copy of padded CStr.
**/
CStr Pad(PS_TRIM_MODE Mode, size_t Length) const;
CStr Pad(PS_TRIM_MODE mode, size_t len) const;
// Conversion to utf16string
utf16string utf16() const { return utf16string(begin(), end()); }