2014-10-14 13:11:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2018-04-19 23:20:10 +00:00
|
|
|
* Copyright (C) 2001-2018, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-08-22 14:40:02 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
2014-10-14 13:11:37 +00:00
|
|
|
*
|
|
|
|
* This program 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see http://gnu.org/licenses
|
|
|
|
*/
|
2012-07-11 18:50:42 +00:00
|
|
|
|
|
|
|
|
2012-10-17 19:55:45 +00:00
|
|
|
#include "common/stringutils.h"
|
2012-07-11 18:50:42 +00:00
|
|
|
|
2013-12-02 23:11:26 +00:00
|
|
|
#include <cstdarg>
|
2013-12-25 22:41:50 +00:00
|
|
|
#include <cstdio>
|
2020-07-05 11:08:39 +00:00
|
|
|
#include <stdexcept>
|
2013-12-02 23:11:26 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
2015-08-25 10:18:15 +00:00
|
|
|
unsigned int StrUtils::HexStringToInt(const std::string& str)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex << str;
|
|
|
|
unsigned int x;
|
|
|
|
ss >> x;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2015-08-06 08:12:20 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string VFormat(const char *fmt, va_list ap)
|
2013-12-02 23:11:26 +00:00
|
|
|
{
|
2015-08-06 12:24:31 +00:00
|
|
|
std::size_t size = 1024;
|
2013-12-02 23:11:26 +00:00
|
|
|
char stackbuf[1024];
|
|
|
|
std::vector<char> dynamicbuf;
|
|
|
|
char *buf = &stackbuf[0];
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int needed = vsnprintf (buf, size, fmt, ap);
|
|
|
|
|
|
|
|
if (needed <= static_cast<int>(size) && needed >= 0)
|
|
|
|
{
|
2015-08-06 12:24:31 +00:00
|
|
|
return std::string(buf, static_cast<std::size_t>(needed));
|
2013-12-02 23:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size = (needed > 0) ? (needed+1) : (size*2);
|
|
|
|
dynamicbuf.resize(size);
|
|
|
|
buf = &dynamicbuf[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 08:12:20 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-12-02 23:11:26 +00:00
|
|
|
std::string StrUtils::Format(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
std::string buf = VFormat(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return buf;
|
|
|
|
}
|
2012-07-11 18:50:42 +00:00
|
|
|
|
|
|
|
std::string StrUtils::Replace(const std::string &str, const std::string &oldStr, const std::string &newStr)
|
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
std::string result = str;
|
|
|
|
std::size_t pos = 0;
|
|
|
|
while ((pos = str.find(oldStr, pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
result.replace(pos, oldStr.length(), newStr);
|
|
|
|
pos += newStr.length();
|
|
|
|
}
|
|
|
|
return result;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string StrUtils::UnicodeCharToUtf8(unsigned int ch)
|
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
std::string result;
|
|
|
|
if (ch < 0x0080)
|
|
|
|
{
|
|
|
|
result += static_cast<char>(ch);
|
|
|
|
}
|
|
|
|
else if (ch < 0x0800)
|
|
|
|
{
|
|
|
|
char ch1 = 0xC0 | ((ch & 0x07C0) >> 6);
|
|
|
|
char ch2 = 0x80 | (ch & 0x3F);
|
|
|
|
result += ch1;
|
|
|
|
result += ch2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char ch1 = 0xE0 | ((ch & 0xF000) >> 12);
|
|
|
|
char ch2 = 0x80 | ((ch & 0x07C0) >> 6);
|
|
|
|
char ch3 = 0x80 | (ch & 0x3F);
|
|
|
|
result += ch1;
|
|
|
|
result += ch2;
|
|
|
|
result += ch3;
|
|
|
|
}
|
|
|
|
return result;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string StrUtils::UnicodeStringToUtf8(const std::wstring &str)
|
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
std::string result;
|
|
|
|
for (unsigned int i = 0; i < str.size(); ++i)
|
|
|
|
result += StrUtils::UnicodeCharToUtf8(static_cast<unsigned int>(str[i]));
|
2012-07-11 18:50:42 +00:00
|
|
|
|
2015-08-06 12:26:41 +00:00
|
|
|
return result;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int StrUtils::Utf8CharToUnicode(const std::string &ch)
|
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
if (ch.empty())
|
|
|
|
return 0;
|
2012-07-11 18:50:42 +00:00
|
|
|
|
2015-08-06 12:26:41 +00:00
|
|
|
unsigned int result = 0;
|
|
|
|
if ((ch[0] & 0x80) == 0)
|
|
|
|
{
|
|
|
|
if (ch.size() == 1)
|
|
|
|
result = static_cast<unsigned int>(ch[0]);
|
|
|
|
}
|
|
|
|
else if ((ch[0] & 0xC0) == 0xC0)
|
2012-07-11 18:50:42 +00:00
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
if (ch.size() == 2)
|
|
|
|
{
|
|
|
|
unsigned int ch1 = (ch[0] & 0x1F) << 6;
|
|
|
|
unsigned int ch2 = (ch[1] & 0x3F);
|
|
|
|
result = ch1 | ch2;
|
|
|
|
}
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
2015-08-06 12:26:41 +00:00
|
|
|
else
|
2012-07-11 18:50:42 +00:00
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
if (ch.size() == 3)
|
|
|
|
{
|
|
|
|
unsigned int ch1 = (ch[0] & 0xF0) << 12;
|
|
|
|
unsigned int ch2 = (ch[1] & 0xC0) << 6;
|
|
|
|
unsigned int ch3 = (ch[2] & 0xC0);
|
|
|
|
result = ch1 | ch2 | ch3;
|
|
|
|
}
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 12:26:41 +00:00
|
|
|
return result;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring StrUtils::Utf8StringToUnicode(const std::string &str)
|
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
std::wstring result;
|
|
|
|
unsigned int pos = 0;
|
|
|
|
while (pos < str.size())
|
|
|
|
{
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(str, pos);
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::string ch = str.substr(pos, len);
|
|
|
|
result += static_cast<wchar_t>(StrUtils::Utf8CharToUnicode(ch));
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
return result;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos)
|
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
if (pos >= str.size())
|
|
|
|
return 0;
|
2012-07-11 18:50:42 +00:00
|
|
|
|
2019-10-07 15:09:15 +00:00
|
|
|
const char c = str[pos];
|
|
|
|
if(c >= 0xF0)
|
|
|
|
return 4;
|
|
|
|
if(c >= 0xE0)
|
2015-08-06 12:26:41 +00:00
|
|
|
return 3;
|
2019-10-07 15:09:15 +00:00
|
|
|
if(c >= 0xC0)
|
|
|
|
return 2;
|
2012-07-11 18:50:42 +00:00
|
|
|
|
2019-10-07 15:09:15 +00:00
|
|
|
// Invalid char - unexpected continuation byte
|
|
|
|
if(c >= 0x80)
|
2020-07-05 11:08:39 +00:00
|
|
|
throw new std::invalid_argument("Unexpected UTF-8 continuation byte");
|
2019-10-07 15:09:15 +00:00
|
|
|
|
|
|
|
return 1;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 12:24:31 +00:00
|
|
|
std::size_t StrUtils::Utf8StringLength(const std::string &str)
|
2012-07-11 18:50:42 +00:00
|
|
|
{
|
2015-08-06 12:26:41 +00:00
|
|
|
std::size_t result = 0;
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (i < str.size())
|
|
|
|
{
|
|
|
|
i += Utf8CharSizeAt(str, i);
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
return result;
|
2012-07-11 18:50:42 +00:00
|
|
|
}
|
2013-05-26 15:47:54 +00:00
|
|
|
|