tahoma2d/toonz/sources/common/tcore/tstring.cpp

190 lines
3.3 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tconvert.h"
//#include "texception.h"
#include "tfilepath.h"
#ifndef TNZCORE_LIGHT
#include <QString>
#endif
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
#pragma warning(disable : 4996)
#include "windows.h"
#endif
#include <sstream>
2016-03-19 06:57:51 +13:00
class TStringConvertException : public TException
{
std::string m_string;
2016-03-19 06:57:51 +13:00
public:
TStringConvertException(const std::string str) : m_string(str) {}
2016-03-19 06:57:51 +13:00
};
std::wstring to_wstring(std::string s)
2016-03-19 06:57:51 +13:00
{
#ifdef TNZCORE_LIGHT
std::wstring ws;
ws.assign(s.begin(), s.end());
return ws;
#else
QString testString = QString::fromStdString(s);
QString qString = QString::fromUtf8(s.c_str());
// To detect if 's' is UTF-8 encoded or not
if (qString != testString && std::string(qString.toUtf8()) == s)
2016-03-19 06:57:51 +13:00
return qString.toStdWString();
else
return testString.toStdWString();
#endif
}
std::string to_string(std::wstring ws)
2016-03-19 06:57:51 +13:00
{
#ifdef TNZCORE_LIGHT
std::string s;
s.assign(ws.begin(), ws.end());
return s;
#else
QString const qString = QString::fromStdWString(ws);
2016-03-19 06:57:51 +13:00
// Test if 'ws' is not unicode (UTF-8)
2016-03-19 06:57:51 +13:00
if (qString.toLatin1() == qString)
return qString.toStdString();
2016-03-19 06:57:51 +13:00
return std::string(qString.toUtf8());
2016-03-19 06:57:51 +13:00
#endif
}
std::string to_string(const TFilePath &fp)
2016-03-19 06:57:51 +13:00
{
return ::to_string(fp.getWideString());
2016-03-19 06:57:51 +13:00
}
/*!
The default precision is six decimal places. If the
precision is less than of the decimal places in the fractonal
part, the remainder is not cut off but rounded.
*/
std::string to_string(double value, int prec)
2016-03-19 06:57:51 +13:00
{
if (prec < 0) {
return std::to_string(value);
}
2016-03-19 06:57:51 +13:00
std::ostringstream out;
out.setf(std::ios_base::fixed, std::ios_base::floatfield);
out.precision(prec);
out << value;
return out.str();
2016-03-19 06:57:51 +13:00
}
std::string to_string(void* p)
2016-03-19 06:57:51 +13:00
{
std::ostringstream out;
out << p;
return out.str();
2016-03-19 06:57:51 +13:00
}
bool isInt(std::string s)
2016-03-19 06:57:51 +13:00
{
int i = 0, len = (int)s.size();
if (len == 0)
return false;
if (s[0] == '-') {
if (len == 1)
return false;
else
i++;
}
while (i < len) {
if (s[i] < '0' || s[i] > '9')
return false;
i++;
}
return true;
}
bool isDouble(std::string s)
2016-03-19 06:57:51 +13:00
{
int i = 0, len = (int)s.size();
if (len == 0)
return false;
if (i < len && s[i] == '-')
i++;
while (i < len && s[i] != '.') {
if (s[i] < '0' || s[i] > '9')
return false;
i++;
}
if (i >= len)
return true;
i++;
while (i < len) {
if (s[i] < '0' || s[i] > '9')
return false;
i++;
}
return true;
}
bool isInt(std::wstring s) { return isInt(::to_string(s)); }
bool isDouble(std::wstring s) { return isDouble(::to_string(s)); }
2016-03-19 06:57:51 +13:00
std::string toUpper(std::string a)
2016-03-19 06:57:51 +13:00
{
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
return _strupr(const_cast<char *>(a.c_str()));
#else
std::string ret = a;
2016-03-19 06:57:51 +13:00
for (int i = 0; i < (int)ret.length(); i++)
ret[i] = toupper(ret[i]);
return ret;
#endif
}
std::string toLower(std::string a)
2016-03-19 06:57:51 +13:00
{
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
return _strlwr(const_cast<char *>(a.c_str()));
#else
std::string ret = a;
2016-03-19 06:57:51 +13:00
for (int i = 0; i < (int)ret.length(); i++)
ret[i] = tolower(ret[i]);
return ret;
#endif
}
std::wstring toUpper(std::wstring a)
2016-03-19 06:57:51 +13:00
{
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
return _wcsupr(const_cast<wchar_t *>(a.c_str()));
#else
std::wstring ret;
2016-03-19 06:57:51 +13:00
for (int i = 0; i < (int)a.length(); i++) {
wchar_t c = towupper(a[i]);
ret += c;
}
return ret;
#endif
}
std::wstring toLower(std::wstring a)
2016-03-19 06:57:51 +13:00
{
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
return _wcslwr(const_cast<wchar_t *>(a.c_str()));
#else
std::wstring ret;
2016-03-19 06:57:51 +13:00
for (int i = 0; i < (int)a.length(); i++) {
wchar_t c = towlower(a[i]);
ret += c;
}
return ret;
#endif
}