mirror of
https://github.com/baldurk/renderdoc.git
synced 2026-05-29 13:20:54 +00:00
Remove wstring variants of string utils functions
* When needed on windows we convert to/from UTF-8. For most places this is easy enough, the callstack processing is now moved to store most data as UTF-8 in the first place.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
#include <wctype.h>
|
||||
#include <algorithm>
|
||||
#include "common/globalconfig.h"
|
||||
#include "os/os_specific.h"
|
||||
|
||||
uint32_t strhash(const char *str, uint32_t seed)
|
||||
{
|
||||
@@ -59,31 +60,17 @@ char tocupper(char c)
|
||||
return (char)toupper(c);
|
||||
}
|
||||
|
||||
string strlower(const string &str)
|
||||
std::string strlower(const std::string &str)
|
||||
{
|
||||
string newstr(str);
|
||||
transform(newstr.begin(), newstr.end(), newstr.begin(), toclower);
|
||||
std::string newstr(str);
|
||||
std::transform(newstr.begin(), newstr.end(), newstr.begin(), toclower);
|
||||
return newstr;
|
||||
}
|
||||
|
||||
wstring strlower(const wstring &str)
|
||||
std::string strupper(const std::string &str)
|
||||
{
|
||||
wstring newstr(str);
|
||||
transform(newstr.begin(), newstr.end(), newstr.begin(), towlower);
|
||||
return newstr;
|
||||
}
|
||||
|
||||
string strupper(const string &str)
|
||||
{
|
||||
string newstr(str);
|
||||
transform(newstr.begin(), newstr.end(), newstr.begin(), tocupper);
|
||||
return newstr;
|
||||
}
|
||||
|
||||
wstring strupper(const wstring &str)
|
||||
{
|
||||
wstring newstr(str);
|
||||
transform(newstr.begin(), newstr.end(), newstr.begin(), towupper);
|
||||
std::string newstr(str);
|
||||
std::transform(newstr.begin(), newstr.end(), newstr.begin(), tocupper);
|
||||
return newstr;
|
||||
}
|
||||
|
||||
@@ -109,6 +96,88 @@ bool endswith(const std::string &value, const std::string &ending)
|
||||
return (0 == value.compare(value.length() - ending.length(), ending.length(), ending));
|
||||
}
|
||||
|
||||
std::string get_basename(const std::string &path)
|
||||
{
|
||||
std::string base = path;
|
||||
|
||||
if(base.length() == 0)
|
||||
return base;
|
||||
|
||||
if(base[base.length() - 1] == '/' || base[base.length() - 1] == '\\')
|
||||
base.erase(base.size() - 1);
|
||||
|
||||
char pathSep[] = {'\\', '/', 0};
|
||||
|
||||
size_t offset = base.find_last_of(pathSep);
|
||||
|
||||
if(offset == std::string::npos)
|
||||
return base;
|
||||
|
||||
return base.substr(offset + 1);
|
||||
}
|
||||
|
||||
std::wstring get_basename(const std::wstring &path)
|
||||
{
|
||||
return StringFormat::UTF82Wide(get_basename(StringFormat::Wide2UTF8(path)));
|
||||
}
|
||||
|
||||
std::string get_dirname(const std::string &path)
|
||||
{
|
||||
std::string base = path;
|
||||
|
||||
if(base.length() == 0)
|
||||
return base;
|
||||
|
||||
if(base[base.length() - 1] == '/' || base[base.length() - 1] == '\\')
|
||||
base.erase(base.size() - 1);
|
||||
|
||||
char pathSep[3] = {'\\', '/', 0};
|
||||
|
||||
size_t offset = base.find_last_of(pathSep);
|
||||
|
||||
if(offset == std::string::npos)
|
||||
{
|
||||
base.resize(1);
|
||||
base[0] = '.';
|
||||
return base;
|
||||
}
|
||||
|
||||
return base.substr(0, offset);
|
||||
}
|
||||
|
||||
std::wstring get_dirname(const std::wstring &path)
|
||||
{
|
||||
return StringFormat::UTF82Wide(get_dirname(StringFormat::Wide2UTF8(path)));
|
||||
}
|
||||
|
||||
void split(const std::string &in, std::vector<std::string> &out, const char sep)
|
||||
{
|
||||
std::string work = in;
|
||||
size_t offset = work.find(sep);
|
||||
|
||||
while(offset != std::string::npos)
|
||||
{
|
||||
out.push_back(work.substr(0, offset));
|
||||
work = work.substr(offset + 1);
|
||||
|
||||
offset = work.find(sep);
|
||||
}
|
||||
|
||||
if(work.size() && work[0] != 0)
|
||||
out.push_back(work);
|
||||
}
|
||||
|
||||
void merge(const std::vector<std::string> &in, std::string &out, const char sep)
|
||||
{
|
||||
out = std::string();
|
||||
for(size_t i = 0; i < in.size(); i++)
|
||||
{
|
||||
out += in[i];
|
||||
if(i + 1 < in.size())
|
||||
out += sep;
|
||||
}
|
||||
}
|
||||
|
||||
std::string removeFromEnd(const std::string &value, const std::string &ending)
|
||||
{
|
||||
string::size_type pos;
|
||||
@@ -173,6 +242,34 @@ TEST_CASE("String manipulation", "[string]")
|
||||
CHECK(strlower("FOOBAR") == "foobar");
|
||||
};
|
||||
|
||||
SECTION("basename")
|
||||
{
|
||||
CHECK(get_basename("foo") == "foo");
|
||||
CHECK(get_basename("/foo") == "foo");
|
||||
CHECK(get_basename("/dir/foo") == "foo");
|
||||
CHECK(get_basename("/long/path/dir/foo") == "foo");
|
||||
CHECK(get_basename("relative/long/path/dir/foo") == "foo");
|
||||
CHECK(get_basename("../foo") == "foo");
|
||||
CHECK(get_basename("relative/../foo") == "foo");
|
||||
CHECK(get_basename("C:/windows/foo") == "foo");
|
||||
CHECK(get_basename("C:\\windows\\foo") == "foo");
|
||||
CHECK(get_basename("C:\\windows\\path/mixed/slashes\\foo") == "foo");
|
||||
};
|
||||
|
||||
SECTION("dirname")
|
||||
{
|
||||
CHECK(get_dirname("foo") == ".");
|
||||
CHECK(get_dirname("/foo") == "");
|
||||
CHECK(get_dirname("/dir/foo") == "/dir");
|
||||
CHECK(get_dirname("/long/path/dir/foo") == "/long/path/dir");
|
||||
CHECK(get_dirname("relative/long/path/dir/foo") == "relative/long/path/dir");
|
||||
CHECK(get_dirname("../foo") == "..");
|
||||
CHECK(get_dirname("relative/../foo") == "relative/..");
|
||||
CHECK(get_dirname("C:/windows/foo") == "C:/windows");
|
||||
CHECK(get_dirname("C:\\windows\\foo") == "C:\\windows");
|
||||
CHECK(get_dirname("C:\\windows\\path/mixed/slashes\\foo") == "C:\\windows\\path/mixed/slashes");
|
||||
};
|
||||
|
||||
SECTION("strupper")
|
||||
{
|
||||
CHECK(strupper("foobar") == "FOOBAR");
|
||||
|
||||
@@ -28,14 +28,9 @@
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using std::string;
|
||||
using std::wstring;
|
||||
using std::vector;
|
||||
|
||||
std::string strlower(const std::string &str);
|
||||
std::wstring strlower(const std::wstring &str);
|
||||
std::string strupper(const std::string &str);
|
||||
std::wstring strupper(const std::wstring &str);
|
||||
|
||||
std::string trim(const std::string &str);
|
||||
std::string removeFromEnd(const std::string &value, const std::string &ending);
|
||||
@@ -44,80 +39,8 @@ uint32_t strhash(const char *str, uint32_t existingHash = 5381);
|
||||
|
||||
bool endswith(const std::string &value, const std::string &ending);
|
||||
|
||||
template <class strType>
|
||||
strType basename(const strType &path)
|
||||
{
|
||||
strType base = path;
|
||||
std::string get_basename(const std::string &path);
|
||||
std::string get_dirname(const std::string &path);
|
||||
|
||||
if(base.length() == 0)
|
||||
return base;
|
||||
|
||||
if(base[base.length() - 1] == '/' || base[base.length() - 1] == '\\')
|
||||
base.erase(base.size() - 1);
|
||||
|
||||
typename strType::value_type pathSep[3] = {'\\', '/', 0};
|
||||
|
||||
size_t offset = base.find_last_of(pathSep);
|
||||
|
||||
if(offset == strType::npos)
|
||||
return base;
|
||||
|
||||
return base.substr(offset + 1);
|
||||
}
|
||||
|
||||
template <class strType>
|
||||
strType dirname(const strType &path)
|
||||
{
|
||||
strType base = path;
|
||||
|
||||
if(base.length() == 0)
|
||||
return base;
|
||||
|
||||
if(base[base.length() - 1] == '/' || base[base.length() - 1] == '\\')
|
||||
base.erase(base.size() - 1);
|
||||
|
||||
typename strType::value_type pathSep[3] = {'\\', '/', 0};
|
||||
|
||||
size_t offset = base.find_last_of(pathSep);
|
||||
|
||||
if(offset == strType::npos)
|
||||
{
|
||||
base.resize(1);
|
||||
base[0] = typename strType::value_type('.');
|
||||
return base;
|
||||
}
|
||||
|
||||
return base.substr(0, offset);
|
||||
}
|
||||
|
||||
template <class CharType>
|
||||
void split(const std::basic_string<CharType> &in, vector<std::basic_string<CharType> > &out,
|
||||
const CharType sep)
|
||||
{
|
||||
std::basic_string<CharType> work = in;
|
||||
typename std::basic_string<CharType>::size_type offset = work.find(sep);
|
||||
|
||||
while(offset != std::basic_string<CharType>::npos)
|
||||
{
|
||||
out.push_back(work.substr(0, offset));
|
||||
work = work.substr(offset + 1);
|
||||
|
||||
offset = work.find(sep);
|
||||
}
|
||||
|
||||
if(work.size() && work[0] != 0)
|
||||
out.push_back(work);
|
||||
}
|
||||
|
||||
template <class CharType>
|
||||
void merge(const vector<std::basic_string<CharType> > &in, std::basic_string<CharType> &out,
|
||||
const CharType sep)
|
||||
{
|
||||
out = std::basic_string<CharType>();
|
||||
for(size_t i = 0; i < in.size(); i++)
|
||||
{
|
||||
out += in[i];
|
||||
if(i + 1 < in.size())
|
||||
out += sep;
|
||||
}
|
||||
}
|
||||
void split(const std::string &in, std::vector<std::string> &out, const char sep);
|
||||
void merge(const std::vector<std::string> &in, std::string &out, const char sep);
|
||||
|
||||
Reference in New Issue
Block a user