2021-09-03 23:06:22 +02:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-06-11 04:55:09 +02:00
|
|
|
#ifndef CStr_CPP_FIRST
|
|
|
|
#define CStr_CPP_FIRST
|
|
|
|
|
2007-05-09 23:01:11 +02:00
|
|
|
#include "lib/fnv_hash.h"
|
2010-03-01 15:55:34 +01:00
|
|
|
#include "lib/utf8.h"
|
2011-05-29 21:59:51 +02:00
|
|
|
#include "lib/byte_order.h"
|
2006-08-26 22:25:37 +02:00
|
|
|
#include "network/Serialization.h"
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2019-09-15 14:02:26 +02:00
|
|
|
#include <cctype>
|
2021-09-07 23:01:34 +02:00
|
|
|
#include <cwctype>
|
2011-12-23 14:01:29 +01:00
|
|
|
#include <iomanip>
|
2005-06-01 22:12:45 +02:00
|
|
|
#include <sstream>
|
2021-09-07 23:01:34 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// Use a knowingly false expression, as we can't use
|
|
|
|
// static_assert(false, ...) directly, because it's an ill-formed program
|
|
|
|
// with a value (false) which doesn't depend on any input parameter.
|
|
|
|
// We don't use constexpr bool AlwaysFalse = false, because some compilers
|
|
|
|
// complain about an unused constant.
|
|
|
|
template<typename T>
|
|
|
|
struct AlwaysFalse : std::false_type
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename StrBase>
|
|
|
|
using tstringstream = std::basic_stringstream<typename StrBase::value_type>;
|
|
|
|
|
|
|
|
template<typename Char>
|
|
|
|
bool istspace(const Char chr)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<Char, char>)
|
|
|
|
return static_cast<bool>(std::isspace(chr));
|
|
|
|
else
|
|
|
|
return static_cast<bool>(std::iswspace(chr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Char>
|
|
|
|
Char totlower(const Char chr)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<Char, char>)
|
|
|
|
return std::tolower(chr);
|
|
|
|
else
|
|
|
|
return std::towlower(chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Char>
|
|
|
|
Char totupper(const Char chr)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<Char, char>)
|
|
|
|
return std::toupper(chr);
|
|
|
|
else
|
|
|
|
return std::towupper(chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StrBase>
|
|
|
|
u8* SerializeImpl(const StrBase& str, u8* buffer)
|
|
|
|
{
|
|
|
|
using Char = typename StrBase::value_type;
|
|
|
|
ENSURE(buffer);
|
|
|
|
if constexpr (std::is_same_v<Char, char>)
|
|
|
|
{
|
|
|
|
// CStr8 is always serialized to / from ASCII(or whatever 8 - bit codepage stored
|
|
|
|
// in the CStr).
|
|
|
|
size_t len = str.length();
|
|
|
|
Serialize_int_4(buffer, (u32)len);
|
|
|
|
size_t i = 0;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
buffer[i] = str[i];
|
|
|
|
return buffer + len;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<Char, wchar_t>)
|
|
|
|
{
|
|
|
|
// CStrW is always serialized to / from UTF - 16.
|
|
|
|
size_t len = str.length();
|
|
|
|
size_t i = 0;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
const u16 bigEndian = to_be16(str[i]);
|
|
|
|
*(u16 *)(buffer + i * 2) = bigEndian;
|
|
|
|
}
|
|
|
|
*(u16 *)(buffer + i * 2) = 0;
|
|
|
|
return buffer + len * 2 + 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
static_assert(AlwaysFalse<Char>::value, "Not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StrBase>
|
|
|
|
const u8* DeserializeImpl(const u8* buffer, const u8* bufferend, StrBase& str)
|
|
|
|
{
|
|
|
|
using Char = typename StrBase::value_type;
|
|
|
|
ENSURE(buffer);
|
|
|
|
ENSURE(bufferend);
|
|
|
|
if constexpr (std::is_same_v<Char, char>)
|
|
|
|
{
|
|
|
|
u32 len;
|
|
|
|
Deserialize_int_4(buffer, len);
|
|
|
|
if (buffer + len > bufferend)
|
|
|
|
return NULL;
|
|
|
|
str = StrBase(buffer, buffer + len);
|
|
|
|
return buffer + len;
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<Char, wchar_t>)
|
|
|
|
{
|
|
|
|
const u16 *strend = (const u16 *)buffer;
|
|
|
|
while ((const u8 *)strend < bufferend && *strend)
|
|
|
|
strend++;
|
|
|
|
if ((const u8 *)strend >= bufferend)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
str.resize(strend - (const u16 *)buffer);
|
|
|
|
const u16 *ptr = (const u16 *)buffer;
|
|
|
|
|
|
|
|
typename StrBase::iterator it = str.begin();
|
|
|
|
while (ptr < strend)
|
|
|
|
{
|
|
|
|
const u16 native = to_be16(*(ptr++)); // we want from_be16, but that's the same
|
|
|
|
*(it++) = (Char)native;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (const u8 *)(strend + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
static_assert(AlwaysFalse<Char>::value, "Not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename StrBase>
|
|
|
|
size_t GetSerializedLengthImpl(const StrBase& str)
|
|
|
|
{
|
|
|
|
using Char = typename StrBase::value_type;
|
|
|
|
if constexpr (std::is_same_v<Char, char>)
|
|
|
|
return str.length() + 4;
|
|
|
|
else if constexpr (std::is_same_v<Char, wchar_t>)
|
|
|
|
return str.length() * 2 + 2;
|
|
|
|
else
|
|
|
|
static_assert(AlwaysFalse<Char>::value, "Not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2005-06-01 22:12:45 +02:00
|
|
|
|
2004-06-11 04:55:09 +02:00
|
|
|
#define UNIDOUBLER_HEADER "CStr.cpp"
|
|
|
|
#include "UniDoubler.h"
|
|
|
|
|
2005-06-01 22:12:45 +02:00
|
|
|
|
2004-09-06 13:15:43 +02:00
|
|
|
// Only include these function definitions in the first instance of CStr.cpp:
|
2005-08-16 21:51:18 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* Convert CStr to UTF-8
|
|
|
|
*
|
|
|
|
* @return CStr8 converted string
|
|
|
|
**/
|
2005-05-10 20:15:03 +02:00
|
|
|
CStr8 CStrW::ToUTF8() const
|
|
|
|
{
|
2011-05-03 14:38:42 +02:00
|
|
|
Status err;
|
2009-11-24 16:36:08 +01:00
|
|
|
return utf8_from_wstring(*this, &err);
|
2005-01-12 15:31:47 +01:00
|
|
|
}
|
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* Convert UTF-8 to CStr
|
|
|
|
*
|
|
|
|
* @return CStrW converted string
|
|
|
|
**/
|
2005-05-10 20:15:03 +02:00
|
|
|
CStrW CStr8::FromUTF8() const
|
|
|
|
{
|
2011-05-03 14:38:42 +02:00
|
|
|
Status err;
|
2009-11-24 16:36:08 +01:00
|
|
|
return wstring_from_utf8(*this, &err);
|
2005-05-10 20:15:03 +02:00
|
|
|
}
|
|
|
|
|
2004-06-11 04:55:09 +02:00
|
|
|
#else
|
|
|
|
|
2005-08-16 21:51:18 +02:00
|
|
|
// The following code is compiled twice, as CStrW then as CStr8:
|
|
|
|
|
2004-06-11 04:55:09 +02:00
|
|
|
#include "CStr.h"
|
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::Repeat(const CStr& str, size_t reps)
|
2005-12-17 03:33:57 +01:00
|
|
|
{
|
|
|
|
CStr ret;
|
2021-09-03 23:06:22 +02:00
|
|
|
ret.reserve(str.length() * reps);
|
|
|
|
while (reps--) ret += str;
|
2005-12-17 03:33:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:08:20 +01:00
|
|
|
// Construction from numbers:
|
2004-10-31 21:29:09 +01:00
|
|
|
|
2011-02-17 21:08:20 +01:00
|
|
|
CStr CStr::FromInt(int n)
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> ss;
|
2011-02-17 21:08:20 +01:00
|
|
|
ss << n;
|
|
|
|
return ss.str();
|
|
|
|
}
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2011-02-17 21:08:20 +01:00
|
|
|
CStr CStr::FromUInt(unsigned int n)
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> ss;
|
2011-02-17 21:08:20 +01:00
|
|
|
ss << n;
|
|
|
|
return ss.str();
|
|
|
|
}
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2011-02-17 21:08:20 +01:00
|
|
|
CStr CStr::FromInt64(i64 n)
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> ss;
|
2011-02-17 21:08:20 +01:00
|
|
|
ss << n;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
CStr CStr::FromDouble(double n)
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> ss;
|
2011-02-17 21:08:20 +01:00
|
|
|
ss << n;
|
|
|
|
return ss.str();
|
|
|
|
}
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2004-10-31 21:29:09 +01:00
|
|
|
// Conversion to numbers:
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2004-06-01 18:51:37 +02:00
|
|
|
int CStr::ToInt() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2010-10-01 01:17:41 +02:00
|
|
|
int ret = 0;
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> str(*this);
|
2010-10-01 01:17:41 +02:00
|
|
|
str >> ret;
|
|
|
|
return ret;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2004-06-01 18:51:37 +02:00
|
|
|
unsigned int CStr::ToUInt() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2010-10-01 01:17:41 +02:00
|
|
|
unsigned int ret = 0;
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> str(*this);
|
2010-10-01 01:17:41 +02:00
|
|
|
str >> ret;
|
|
|
|
return ret;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2004-06-01 18:51:37 +02:00
|
|
|
long CStr::ToLong() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2010-10-01 01:17:41 +02:00
|
|
|
long ret = 0;
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> str(*this);
|
2010-10-01 01:17:41 +02:00
|
|
|
str >> ret;
|
|
|
|
return ret;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2004-06-01 18:51:37 +02:00
|
|
|
unsigned long CStr::ToULong() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2010-10-01 01:17:41 +02:00
|
|
|
unsigned long ret = 0;
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> str(*this);
|
2010-10-01 01:17:41 +02:00
|
|
|
str >> ret;
|
|
|
|
return ret;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 14:02:26 +02:00
|
|
|
/**
|
|
|
|
* libc++ and libstd++ differ on how they handle string-to-number parsing for floating-points numbers.
|
|
|
|
* See https://trac.wildfiregames.com/ticket/2780#comment:4 for details.
|
|
|
|
* To prevent this, only consider [0-9.-+], replace the others in-place with a neutral character.
|
|
|
|
*/
|
|
|
|
CStr ParseableAsNumber(CStr cleaned_copy)
|
|
|
|
{
|
2021-09-07 23:01:34 +02:00
|
|
|
for (CStr::Char& c : cleaned_copy)
|
2019-09-15 14:02:26 +02:00
|
|
|
if (!std::isdigit(c) && c != '.' && c != '-' && c != '+')
|
|
|
|
c = ' ';
|
|
|
|
|
|
|
|
return cleaned_copy;
|
|
|
|
}
|
|
|
|
|
2004-06-01 18:51:37 +02:00
|
|
|
float CStr::ToFloat() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2010-10-01 01:17:41 +02:00
|
|
|
float ret = 0;
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> str(ParseableAsNumber(*this));
|
2010-10-01 01:17:41 +02:00
|
|
|
str >> ret;
|
|
|
|
return ret;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2004-10-31 21:29:09 +01:00
|
|
|
double CStr::ToDouble() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2010-10-01 01:17:41 +02:00
|
|
|
double ret = 0;
|
2021-09-03 23:06:22 +02:00
|
|
|
tstringstream<StrBase> str(ParseableAsNumber(*this));
|
2010-10-01 01:17:41 +02:00
|
|
|
str >> ret;
|
|
|
|
return ret;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Search the string for another string
|
2021-09-03 23:06:22 +02:00
|
|
|
long CStr::Find(const CStr& str) const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = find(str, 0);
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
if (pos != npos)
|
|
|
|
return static_cast<long>(pos);
|
2003-10-03 16:10:38 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Search the string for another string
|
2021-09-07 23:01:34 +02:00
|
|
|
long CStr::Find(const Char chr) const
|
2004-05-29 05:53:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = find(chr, 0);
|
2004-05-29 05:53:38 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
if (pos != npos)
|
|
|
|
return static_cast<long>(pos);
|
2004-05-29 05:53:38 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Search the string for another string
|
2021-09-07 23:01:34 +02:00
|
|
|
long CStr::Find(const int start, const Char chr) const
|
2004-05-29 05:53:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = find(chr, start);
|
2004-05-29 05:53:38 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
if (pos != npos)
|
|
|
|
return static_cast<long>(pos);
|
2004-05-29 05:53:38 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:01:34 +02:00
|
|
|
long CStr::FindInsensitive(const int start, const Char chr) const { return LowerCase().Find(start, totlower(chr)); }
|
|
|
|
long CStr::FindInsensitive(const Char chr) const { return LowerCase().Find(totlower(chr)); }
|
2021-09-03 23:06:22 +02:00
|
|
|
long CStr::FindInsensitive(const CStr& str) const { return LowerCase().Find(str.LowerCase()); }
|
2004-12-09 17:54:02 +01:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
long CStr::ReverseFind(const CStr& str) const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = rfind(str, length() );
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
if (pos != npos)
|
|
|
|
return static_cast<long>(pos);
|
2003-10-03 16:10:38 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Lowercase and uppercase
|
2004-05-29 05:53:38 +02:00
|
|
|
CStr CStr::LowerCase() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
StrBase newStr = *this;
|
2004-10-31 21:29:09 +01:00
|
|
|
for (size_t i = 0; i < length(); i++)
|
2021-09-07 23:01:34 +02:00
|
|
|
newStr[i] = (Char)totlower((*this)[i]);
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
return newStr;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2004-05-29 05:53:38 +02:00
|
|
|
CStr CStr::UpperCase() const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
StrBase newStr = *this;
|
2004-10-31 21:29:09 +01:00
|
|
|
for (size_t i = 0; i < length(); i++)
|
2021-09-07 23:01:34 +02:00
|
|
|
newStr[i] = (Char)totupper((*this)[i]);
|
2003-10-03 16:10:38 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
return newStr;
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Retrieve the substring of the first n characters
|
2005-01-12 15:31:47 +01:00
|
|
|
CStr CStr::Left(size_t len) const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(len <= length());
|
2004-10-31 21:29:09 +01:00
|
|
|
return substr(0, len);
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2004-10-31 21:29:09 +01:00
|
|
|
// Retrieve the substring of the last n characters
|
2005-01-12 15:31:47 +01:00
|
|
|
CStr CStr::Right(size_t len) const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(len <= length());
|
2004-10-31 21:29:09 +01:00
|
|
|
return substr(length()-len, len);
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2005-03-18 23:30:23 +01:00
|
|
|
// Retrieve the substring following the last occurrence of Str
|
|
|
|
// (or the whole string if it doesn't contain Str)
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::AfterLast(const CStr& str, size_t startPos) const
|
2005-03-18 23:30:23 +01:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = rfind(str, startPos);
|
2007-02-01 15:46:14 +01:00
|
|
|
if (pos == npos)
|
2005-03-18 23:30:23 +01:00
|
|
|
return *this;
|
|
|
|
else
|
2021-09-03 23:06:22 +02:00
|
|
|
return substr(pos + str.length());
|
2005-03-18 23:30:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the substring preceding the last occurrence of Str
|
|
|
|
// (or the whole string if it doesn't contain Str)
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::BeforeLast(const CStr& str, size_t startPos) const
|
2005-03-18 23:30:23 +01:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = rfind(str, startPos);
|
2007-02-01 15:46:14 +01:00
|
|
|
if (pos == npos)
|
2005-03-18 23:30:23 +01:00
|
|
|
return *this;
|
|
|
|
else
|
|
|
|
return substr(0, pos);
|
|
|
|
}
|
|
|
|
|
2005-03-30 07:43:22 +02:00
|
|
|
// Retrieve the substring following the first occurrence of Str
|
|
|
|
// (or the whole string if it doesn't contain Str)
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::AfterFirst(const CStr& str, size_t startPos) const
|
2005-03-30 07:43:22 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = find(str, startPos);
|
2007-02-01 15:46:14 +01:00
|
|
|
if (pos == npos)
|
2005-03-30 07:43:22 +02:00
|
|
|
return *this;
|
|
|
|
else
|
2021-09-03 23:06:22 +02:00
|
|
|
return substr(pos + str.length());
|
2005-03-30 07:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the substring preceding the first occurrence of Str
|
|
|
|
// (or the whole string if it doesn't contain Str)
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::BeforeFirst(const CStr& str, size_t startPos) const
|
2005-03-30 07:43:22 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = find(str, startPos);
|
2007-02-01 15:46:14 +01:00
|
|
|
if (pos == npos)
|
2005-03-30 07:43:22 +02:00
|
|
|
return *this;
|
|
|
|
else
|
|
|
|
return substr(0, pos);
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Remove all occurrences of some character or substring
|
2021-09-03 23:06:22 +02:00
|
|
|
void CStr::Remove(const CStr& str)
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t foundAt = 0;
|
|
|
|
while (foundAt != npos)
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
foundAt = find(str, 0);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
if (foundAt != npos)
|
|
|
|
erase(foundAt, str.length());
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Replace all occurrences of some substring by another
|
2021-09-03 23:06:22 +02:00
|
|
|
void CStr::Replace(const CStr& toReplace, const CStr& replaceWith)
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos != npos)
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
pos = find(toReplace, pos);
|
|
|
|
if (pos != npos)
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
erase(pos, toReplace.length());
|
|
|
|
insert(pos, replaceWith);
|
|
|
|
pos += replaceWith.length();
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-23 14:01:29 +01:00
|
|
|
std::string CStr::EscapeToPrintableASCII() const
|
2011-10-27 16:16:28 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
std::string newStr;
|
2011-10-27 16:16:28 +02:00
|
|
|
for (size_t i = 0; i < length(); i++)
|
|
|
|
{
|
2021-09-07 23:01:34 +02:00
|
|
|
Char ch = (*this)[i];
|
2011-10-27 16:16:28 +02:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
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";
|
2011-10-27 16:16:28 +02:00
|
|
|
else if (ch >= 32 && ch <= 126)
|
2021-09-03 23:06:22 +02:00
|
|
|
newStr += ch;
|
2011-10-27 16:16:28 +02:00
|
|
|
else
|
|
|
|
{
|
2011-11-04 02:18:34 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << (int)(unsigned char)ch;
|
2021-09-03 23:06:22 +02:00
|
|
|
newStr += ss.str();
|
2011-10-27 16:16:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 23:06:22 +02:00
|
|
|
return newStr;
|
2011-10-27 16:16:28 +02:00
|
|
|
}
|
|
|
|
|
2004-10-31 21:29:09 +01:00
|
|
|
// Returns a trimmed string, removes whitespace from the left/right/both
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::Trim(PS_TRIM_MODE mode) const
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t left = 0, right = 0;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
switch (mode)
|
2003-10-03 16:10:38 +02:00
|
|
|
{
|
|
|
|
case PS_TRIM_LEFT:
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
for (left = 0; left < length(); left++)
|
2021-09-07 23:01:34 +02:00
|
|
|
if (istspace((*this)[left]) == false)
|
2003-10-03 16:10:38 +02:00
|
|
|
break; // end found, trim 0 to Left-1 inclusive
|
|
|
|
} break;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2003-10-03 16:10:38 +02:00
|
|
|
case PS_TRIM_RIGHT:
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
right = length();
|
|
|
|
while (right--)
|
2021-09-07 23:01:34 +02:00
|
|
|
if (istspace((*this)[right]) == false)
|
2003-10-03 16:10:38 +02:00
|
|
|
break; // end found, trim len-1 to Right+1 inclusive
|
|
|
|
} break;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2003-10-03 16:10:38 +02:00
|
|
|
case PS_TRIM_BOTH:
|
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
for (left = 0; left < length(); left++)
|
2021-09-07 23:01:34 +02:00
|
|
|
if (istspace((*this)[left]) == false)
|
2003-10-03 16:10:38 +02:00
|
|
|
break; // end found, trim 0 to Left-1 inclusive
|
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
right = length();
|
|
|
|
while (right--)
|
2021-09-07 23:01:34 +02:00
|
|
|
if (istspace((*this)[right]) == false)
|
2003-10-03 16:10:38 +02:00
|
|
|
break; // end found, trim len-1 to Right+1 inclusive
|
|
|
|
} break;
|
2004-06-21 18:29:47 +02:00
|
|
|
|
|
|
|
default:
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"CStr::Trim: invalid Mode");
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
return substr(left, right - left + 1);
|
2003-10-03 16:10:38 +02:00
|
|
|
}
|
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
CStr CStr::Pad(PS_TRIM_MODE mode, size_t len) const
|
2005-12-17 03:33:57 +01:00
|
|
|
{
|
2021-09-03 23:06:22 +02:00
|
|
|
size_t left = 0, right = 0;
|
2005-12-17 03:33:57 +01:00
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
if (len <= length())
|
2005-12-17 03:33:57 +01:00
|
|
|
return *this;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2005-12-17 03:33:57 +01:00
|
|
|
// From here: Length-length() >= 1
|
|
|
|
|
2021-09-03 23:06:22 +02:00
|
|
|
switch (mode)
|
2005-12-17 03:33:57 +01:00
|
|
|
{
|
|
|
|
case PS_TRIM_LEFT:
|
2021-09-03 23:06:22 +02:00
|
|
|
left = len - length();
|
2005-12-17 03:33:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_TRIM_RIGHT:
|
2021-09-03 23:06:22 +02:00
|
|
|
right = len - length();
|
2005-12-17 03:33:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_TRIM_BOTH:
|
2021-09-03 23:06:22 +02:00
|
|
|
left = (len - length() + 1) / 2;
|
|
|
|
right = (len - length() - 1) / 2; // cannot be negative
|
2005-12-17 03:33:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"CStr::Trim: invalid Mode");
|
2005-12-17 03:33:57 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 23:01:34 +02:00
|
|
|
return StrBase(left, ' ') + *this + StrBase(right, ' ');
|
2005-12-17 03:33:57 +01:00
|
|
|
}
|
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
size_t CStr::GetHashCode() const
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
return (size_t)fnv_hash(data(), length()*sizeof(value_type));
|
2005-03-18 22:57:07 +01:00
|
|
|
// janwas 2005-03-18: now use 32-bit version; 64 is slower and
|
|
|
|
// the result was truncated down to 32 anyway.
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 23:01:34 +02:00
|
|
|
u8* CStr::Serialize(u8* buffer) const
|
2005-01-17 05:52:02 +01:00
|
|
|
{
|
2021-09-07 23:01:34 +02:00
|
|
|
return SerializeImpl(*this, buffer);
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 23:01:34 +02:00
|
|
|
const u8* CStr::Deserialize(const u8* buffer, const u8* bufferend)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2021-09-07 23:01:34 +02:00
|
|
|
return DeserializeImpl(buffer, bufferend, *this);
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
2005-01-17 05:52:02 +01:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t CStr::GetSerializedLength() const
|
2005-01-17 05:52:02 +01:00
|
|
|
{
|
2021-09-07 23:01:34 +02:00
|
|
|
return GetSerializedLengthImpl(*this);
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
|
|
|
|
2004-10-31 21:29:09 +01:00
|
|
|
#endif // CStr_CPP_FIRST
|