Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/CStr.cpp
/* Copyright (C) 2014 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 16 Lines | |||||
#ifndef CStr_CPP_FIRST | #ifndef CStr_CPP_FIRST | ||||
#define CStr_CPP_FIRST | #define CStr_CPP_FIRST | ||||
#include "lib/fnv_hash.h" | #include "lib/fnv_hash.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "lib/byte_order.h" | #include "lib/byte_order.h" | ||||
#include "network/Serialization.h" | #include "network/Serialization.h" | ||||
#include <cctype> | |||||
#include <iomanip> | #include <iomanip> | ||||
#include <sstream> | #include <sstream> | ||||
#define UNIDOUBLER_HEADER "CStr.cpp" | #define UNIDOUBLER_HEADER "CStr.cpp" | ||||
#include "UniDoubler.h" | #include "UniDoubler.h" | ||||
// Only include these function definitions in the first instance of CStr.cpp: | // Only include these function definitions in the first instance of CStr.cpp: | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | |||||
unsigned long CStr::ToULong() const | unsigned long CStr::ToULong() const | ||||
{ | { | ||||
unsigned long ret = 0; | unsigned long ret = 0; | ||||
std::tstringstream str(*this); | std::tstringstream str(*this); | ||||
str >> ret; | str >> ret; | ||||
return ret; | return ret; | ||||
} | } | ||||
/** | |||||
* 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) | |||||
{ | |||||
for (tchar& c : cleaned_copy) | |||||
if (!std::isdigit(c) && c != '.' && c != '-' && c != '+') | |||||
c = ' '; | |||||
return cleaned_copy; | |||||
} | |||||
float CStr::ToFloat() const | float CStr::ToFloat() const | ||||
{ | { | ||||
float ret = 0; | float ret = 0; | ||||
std::tstringstream str(*this); | std::tstringstream str(ParseableAsNumber(*this)); | ||||
str >> ret; | str >> ret; | ||||
return ret; | return ret; | ||||
} | } | ||||
double CStr::ToDouble() const | double CStr::ToDouble() const | ||||
{ | { | ||||
double ret = 0; | double ret = 0; | ||||
std::tstringstream str(*this); | std::tstringstream str(ParseableAsNumber(*this)); | ||||
str >> ret; | str >> ret; | ||||
return ret; | return ret; | ||||
} | } | ||||
// Search the string for another string | // Search the string for another string | ||||
long CStr::Find(const CStr& Str) const | long CStr::Find(const CStr& Str) const | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 345 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator