24 #include "precompiled.h"
26 #ifndef CStr_CPP_FIRST
27 #define CStr_CPP_FIRST
37 #define UNIDOUBLER_HEADER "CStr.cpp"
48 CStr8 CStrW::ToUTF8()
const
59 CStrW CStr8::FromUTF8()
const
74 #define tstringstream wstringstream
75 #define _istspace iswspace
76 #define _totlower towlower
77 #define _totupper towupper
79 #define tstringstream stringstream
80 #define _istspace isspace
81 #define _totlower tolower
82 #define _totupper toupper
85 CStr CStr::Repeat(
const CStr& String,
size_t Reps)
88 ret.reserve(String.length() * Reps);
89 while (Reps--) ret += String;
95 CStr CStr::FromInt(
int n)
97 std::tstringstream ss;
102 CStr CStr::FromUInt(
unsigned int n)
104 std::tstringstream ss;
109 CStr CStr::FromInt64(
i64 n)
111 std::tstringstream ss;
116 CStr CStr::FromDouble(
double n)
118 std::tstringstream ss;
125 int CStr::ToInt()
const
128 std::tstringstream str(*
this);
133 unsigned int CStr::ToUInt()
const
135 unsigned int ret = 0;
136 std::tstringstream str(*
this);
141 long CStr::ToLong()
const
144 std::tstringstream str(*
this);
149 unsigned long CStr::ToULong()
const
151 unsigned long ret = 0;
152 std::tstringstream str(*
this);
157 float CStr::ToFloat()
const
160 std::tstringstream str(*
this);
165 double CStr::ToDouble()
const
168 std::tstringstream str(*
this);
175 long CStr::Find(
const CStr& Str)
const
177 size_t Pos = find(Str, 0);
186 long CStr::Find(
const tchar chr)
const
188 size_t Pos = find(chr, 0);
197 long CStr::Find(
const int start,
const tchar chr)
const
199 size_t Pos = find(chr, start);
207 long CStr::FindInsensitive(
const int start,
const tchar chr)
const {
return LowerCase().Find(start, _totlower(chr)); }
208 long CStr::FindInsensitive(
const tchar chr)
const {
return LowerCase().Find(_totlower(chr)); }
209 long CStr::FindInsensitive(
const CStr& Str)
const {
return LowerCase().Find(Str.LowerCase()); }
212 long CStr::ReverseFind(
const CStr& Str)
const
214 size_t Pos = rfind(Str, length() );
224 CStr CStr::LowerCase()
const
226 std::tstring NewString = *
this;
227 for (
size_t i = 0; i < length(); i++)
228 NewString[i] = (
tchar)_totlower((*
this)[i]);
233 CStr CStr::UpperCase()
const
235 std::tstring NewString = *
this;
236 for (
size_t i = 0; i < length(); i++)
237 NewString[i] = (
tchar)_totupper((*
this)[i]);
244 CStr CStr::Left(
size_t len)
const
247 return substr(0, len);
251 CStr CStr::Right(
size_t len)
const
254 return substr(length()-len, len);
259 CStr CStr::AfterLast(
const CStr& Str,
size_t startPos)
const
261 size_t pos = rfind(Str, startPos);
265 return substr(pos + Str.length());
270 CStr CStr::BeforeLast(
const CStr& Str,
size_t startPos)
const
272 size_t pos = rfind(Str, startPos);
276 return substr(0, pos);
281 CStr CStr::AfterFirst(
const CStr& Str,
size_t startPos)
const
283 size_t pos = find(Str, startPos);
287 return substr(pos + Str.length());
292 CStr CStr::BeforeFirst(
const CStr& Str,
size_t startPos)
const
294 size_t pos = find(Str, startPos);
298 return substr(0, pos);
302 void CStr::Remove(
const CStr& Str)
305 while (FoundAt != npos)
307 FoundAt = find(Str, 0);
310 erase(FoundAt, Str.length());
315 void CStr::Replace(
const CStr& ToReplace,
const CStr& ReplaceWith)
321 Pos = find(ToReplace, Pos);
324 erase(Pos, ToReplace.length());
325 insert(Pos, ReplaceWith);
326 Pos += ReplaceWith.length();
331 CStr CStr::UnescapeBackslashes()
const
335 bool escaping =
false;
336 for (
size_t i = 0; i < length(); i++)
338 tchar ch = (*this)[i];
343 case 'n': NewString +=
'\n';
break;
344 default: NewString += ch;
break;
359 std::string CStr::EscapeToPrintableASCII()
const
361 std::string NewString;
362 for (
size_t i = 0; i < length(); i++)
364 tchar ch = (*this)[i];
366 if (ch ==
'"') NewString +=
"\\\"";
367 else if (ch ==
'\\') NewString +=
"\\\\";
368 else if (ch ==
'\b') NewString +=
"\\b";
369 else if (ch ==
'\f') NewString +=
"\\f";
370 else if (ch ==
'\n') NewString +=
"\\n";
371 else if (ch ==
'\r') NewString +=
"\\r";
372 else if (ch ==
'\t') NewString +=
"\\t";
373 else if (ch >= 32 && ch <= 126)
377 std::stringstream ss;
378 ss <<
"\\u" << std::hex << std::setfill(
'0') << std::setw(4) << (int)(
unsigned char)ch;
379 NewString += ss.str();
388 size_t Left = 0, Right = 0;
394 for (Left = 0; Left < length(); Left++)
395 if (_istspace((*
this)[Left]) ==
false)
403 if (_istspace((*
this)[Right]) ==
false)
409 for (Left = 0; Left < length(); Left++)
410 if (_istspace((*
this)[Left]) ==
false)
415 if (_istspace((*
this)[Right]) ==
false)
424 return substr(Left, Right-Left+1);
429 size_t Left = 0, Right = 0;
431 if (Length <= length())
439 Left = Length - length();
443 Right = Length - length();
447 Left = (Length - length() + 1)/2;
448 Right = (Length - length() - 1)/2;
455 return std::tstring(Left, _T(
' ')) + *
this + std::tstring(Right, _T(
' '));
458 size_t CStr::GetHashCode()
const
460 return (
size_t)
fnv_hash(data(), length()*
sizeof(value_type));
470 u8* CStrW::Serialize(
u8* buffer)
const
472 size_t len = length();
474 for (i = 0; i < len; i++)
477 *(
u16 *)(buffer + i*2) = bigEndian;
479 *(
u16 *)(buffer + i*2) = 0;
480 return buffer + len*2 + 2;
483 const u8* CStrW::Deserialize(
const u8* buffer,
const u8* bufferend)
485 const u16 *strend = (
const u16 *)buffer;
486 while ((
const u8 *)strend < bufferend && *strend) strend++;
487 if ((
const u8 *)strend >= bufferend)
return NULL;
489 resize(strend - (
const u16 *)buffer);
490 const u16 *ptr = (
const u16 *)buffer;
492 std::wstring::iterator str = begin();
496 *(str++) = (
tchar)native;
499 return (
const u8 *)(strend+1);
502 size_t CStr::GetSerializedLength()
const
504 return size_t(length()*2 + 2);
513 u8* CStr8::Serialize(
u8* buffer)
const
515 size_t len = length();
518 for (i = 0; i < len; i++)
519 buffer[i] = (*
this)[i];
523 const u8* CStr8::Deserialize(
const u8* buffer,
const u8* bufferend)
527 if (buffer + len > bufferend)
529 *
this = std::string(buffer, buffer + len);
533 size_t CStr::GetSerializedLength()
const
549 #endif // CStr_CPP_FIRST
Trim all white space from the beginning of the string.
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
#define Serialize_int_4(_pos, _val)
Trim all white space from the end of the string.
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
i64 Status
Error handling system.
std::wstring wstring_from_utf8(const std::string &src, Status *err)
convert UTF-8 to a wide string (UTF-16 or UCS-4, depending on the platform's wchar_t).
u32 fnv_hash(const void *buf, size_t len)
rationale: this algorithm was chosen because it delivers 'good' results for string data and is relati...
#define Deserialize_int_4(_pos, _val)
#define debug_warn(expr)
display the error dialog with the given text.
static float Length(const SVec3 v)
PS_TRIM_MODE
File : CStr.h Project : engine Description : Contains CStr class which is a versatile class for makin...