2014-04-01 00:33:55 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "StrFmt.h"
|
2014-08-29 19:51:11 +00:00
|
|
|
#include <wx/string.h>
|
2014-04-01 00:33:55 +00:00
|
|
|
|
2014-04-08 17:29:17 +00:00
|
|
|
extern const std::string fmt::placeholder = "???";
|
2014-04-01 17:44:38 +00:00
|
|
|
|
2014-06-07 14:15:49 +00:00
|
|
|
std::string replace_first(const std::string& src, const std::string& from, const std::string& to)
|
|
|
|
{
|
|
|
|
auto pos = src.find(from);
|
|
|
|
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
{
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (pos ? src.substr(0, pos) + to : to) + std::string(src.c_str() + pos + from.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string replace_all(std::string src, const std::string& from, const std::string& to)
|
|
|
|
{
|
|
|
|
for (auto pos = src.find(from); pos != std::string::npos; src.find(from, pos + 1))
|
|
|
|
{
|
|
|
|
src = (pos ? src.substr(0, pos) + to : to) + std::string(src.c_str() + pos + from.length());
|
|
|
|
pos += to.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2014-06-08 21:02:20 +00:00
|
|
|
//TODO: move this wx Stuff somewhere else
|
|
|
|
//convert a wxString to a std::string encoded in utf8
|
|
|
|
//CAUTION, only use this to interface with wxWidgets classes
|
|
|
|
std::string fmt::ToUTF8(const wxString& right)
|
|
|
|
{
|
|
|
|
auto ret = std::string(((const char *)right.utf8_str()));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//convert a std::string encoded in utf8 to a wxString
|
|
|
|
//CAUTION, only use this to interface with wxWidgets classes
|
|
|
|
wxString fmt::FromUTF8(const std::string& right)
|
|
|
|
{
|
|
|
|
auto ret = wxString::FromUTF8(right.c_str());
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-01 00:33:55 +00:00
|
|
|
|
|
|
|
//TODO: remove this after every snippet that uses it is gone
|
|
|
|
//WARNING: not fully compatible with CmpNoCase from wxString
|
|
|
|
int fmt::CmpNoCase(const std::string& a, const std::string& b)
|
|
|
|
{
|
|
|
|
if (a.length() != b.length())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return std::equal(a.begin(),
|
|
|
|
a.end(),
|
|
|
|
b.begin(),
|
|
|
|
[](const char& a, const char& b){return tolower(a) == tolower(b); })
|
|
|
|
? 0 : -1;
|
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: remove this after every snippet that uses it is gone
|
|
|
|
//WARNING: not fully compatible with CmpNoCase from wxString
|
|
|
|
void fmt::Replace(std::string &str, const std::string &searchterm, const std::string& replaceterm)
|
|
|
|
{
|
|
|
|
size_t cursor = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
cursor = str.find(searchterm, cursor);
|
|
|
|
if (cursor != std::string::npos)
|
|
|
|
{
|
|
|
|
str.replace(cursor, searchterm.size(), replaceterm);
|
|
|
|
cursor += replaceterm.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> fmt::rSplit(const std::string& source, const std::string& delim)
|
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
size_t cursor = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t prevcurs = cursor;
|
|
|
|
cursor = source.find(delim, cursor);
|
|
|
|
if (cursor != std::string::npos)
|
|
|
|
{
|
|
|
|
ret.push_back(source.substr(prevcurs,cursor-prevcurs));
|
|
|
|
cursor += delim.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.push_back(source.substr(prevcurs));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
return ret;
|
2014-11-29 13:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> fmt::split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
size_t cursor_begin = 0;
|
|
|
|
|
|
|
|
for (size_t cursor_end = 0; cursor_end < source.length(); ++cursor_end)
|
|
|
|
{
|
|
|
|
for (auto &separator : separators)
|
|
|
|
{
|
|
|
|
if (strncmp(source.c_str() + cursor_end, separator.c_str(), separator.length()) == 0)
|
|
|
|
{
|
|
|
|
std::string candidate = source.substr(cursor_begin, cursor_end - cursor_begin);
|
|
|
|
if (!is_skip_empty || !candidate.empty())
|
|
|
|
result.push_back(candidate);
|
|
|
|
|
|
|
|
cursor_begin = cursor_end + separator.length();
|
|
|
|
cursor_end = cursor_begin - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursor_begin != source.length())
|
|
|
|
{
|
|
|
|
result.push_back(source.substr(cursor_begin));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(result);
|
2014-04-01 00:33:55 +00:00
|
|
|
}
|