More uses of string_view

This commit is contained in:
Henrik Rydgård 2023-11-13 23:36:34 +01:00
parent d0ee5fc308
commit 19eeaef2ea
11 changed files with 53 additions and 45 deletions

View File

@ -8,7 +8,7 @@ bool AndroidContentURI::Parse(std::string_view path) {
std::string_view components = path.substr(strlen(prefix));
std::vector<std::string> parts;
std::vector<std::string_view> parts;
SplitString(components, '/', parts);
if (parts.size() == 3) {
// Single file URI.

View File

@ -592,7 +592,7 @@ bool CreateFullPath(const Path &path) {
return false;
}
std::vector<std::string> parts;
std::vector<std::string_view> parts;
SplitString(diff, '/', parts);
// Probably not necessary sanity check, ported from the old code.
@ -602,7 +602,7 @@ bool CreateFullPath(const Path &path) {
}
Path curPath = root;
for (auto &part : parts) {
for (auto part : parts) {
curPath /= part;
if (!File::Exists(curPath)) {
File::CreateDir(curPath);

View File

@ -78,7 +78,7 @@ bool LoadRemoteFileList(const Path &url, const std::string &userAgent, bool *can
return false;
}
for (std::string item : items) {
for (auto &item : items) {
// Apply some workarounds.
if (item.empty())
continue;

View File

@ -334,10 +334,10 @@ void GLQueueRunner::RunInitSteps(const FastVec<GLRInitStep> &steps, bool skipGLC
step.create_shader.shader->desc.c_str(),
infoLog.c_str(),
LineNumberString(code).c_str());
std::vector<std::string> lines;
std::vector<std::string_view> lines;
SplitString(errorString, '\n', lines);
for (auto &line : lines) {
ERROR_LOG(G3D, "%s", line.c_str());
for (auto line : lines) {
ERROR_LOG(G3D, "%.*s", line.size(), line.data());
}
if (errorCallback_) {
std::string desc = StringFromFormat("Shader compilation failed: %s", step.create_shader.stage == GL_VERTEX_SHADER ? "vertex" : "fragment");

View File

@ -86,11 +86,12 @@ int GetAnalogYDirection(InputDeviceID deviceId) {
}
// NOTE: Changing the format of FromConfigString/ToConfigString breaks controls.ini backwards compatibility.
InputMapping InputMapping::FromConfigString(const std::string &str) {
std::vector<std::string> parts;
InputMapping InputMapping::FromConfigString(const std::string_view str) {
std::vector<std::string_view> parts;
SplitString(str, '-', parts);
InputDeviceID deviceId = (InputDeviceID)(atoi(parts[0].c_str()));
InputKeyCode keyCode = (InputKeyCode)atoi(parts[1].c_str());
// We only convert to std::string here to add null terminators for atoi.
InputDeviceID deviceId = (InputDeviceID)(atoi(std::string(parts[0]).c_str()));
InputKeyCode keyCode = (InputKeyCode)atoi(std::string(parts[1]).c_str());
InputMapping mapping;
mapping.deviceId = deviceId;

View File

@ -79,7 +79,7 @@ public:
_dbg_assert_(direction != 0);
}
static InputMapping FromConfigString(const std::string &str);
static InputMapping FromConfigString(std::string_view str);
std::string ToConfigString() const;
InputDeviceID deviceId;

View File

@ -26,12 +26,12 @@ bool RequestHeader::GetParamValue(const char *param_name, std::string *value) co
if (!params)
return false;
std::string p(params);
std::vector<std::string> v;
std::vector<std::string_view> v;
SplitString(p, '&', v);
for (size_t i = 0; i < v.size(); i++) {
std::vector<std::string> parts;
std::vector<std::string_view> parts;
SplitString(v[i], '=', parts);
DEBUG_LOG(IO, "Param: %s Value: %s", parts[0].c_str(), parts[1].c_str());
DEBUG_LOG(IO, "Param: %.*s Value: %.*s", parts[0].size(), parts[0].data(), parts[1].size(), parts[1].data());
if (parts[0] == param_name) {
*value = parts[1];
return true;

View File

@ -115,13 +115,13 @@ const char HEX2DEC[256] =
/* F */ N1,N1,N1,N1, N1,N1,N1,N1, N1,N1,N1,N1, N1,N1,N1,N1
};
std::string UriDecode(const std::string & sSrc)
std::string UriDecode(std::string_view sSrc)
{
// Note from RFC1630: "Sequences which start with a percent sign
// but are not followed by two hexadecimal characters (0-9, A-F) are reserved
// for future extension"
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const unsigned char * pSrc = (const unsigned char *)sSrc.data();
const size_t SRC_LEN = sSrc.length();
const unsigned char * const SRC_END = pSrc + SRC_LEN;
const unsigned char * const SRC_LAST_DEC = SRC_END - 2; // last decodable '%'
@ -129,14 +129,10 @@ std::string UriDecode(const std::string & sSrc)
char * const pStart = new char[SRC_LEN]; // Output will be shorter.
char * pEnd = pStart;
while (pSrc < SRC_LAST_DEC)
{
if (*pSrc == '%')
{
while (pSrc < SRC_LAST_DEC) {
if (*pSrc == '%') {
char dec1, dec2;
if (N1 != (dec1 = HEX2DEC[*(pSrc + 1)])
&& N1 != (dec2 = HEX2DEC[*(pSrc + 2)]))
{
if (N1 != (dec1 = HEX2DEC[*(pSrc + 1)]) && N1 != (dec2 = HEX2DEC[*(pSrc + 2)])) {
*pEnd++ = (dec1 << 4) + dec2;
pSrc += 3;
continue;
@ -156,8 +152,7 @@ std::string UriDecode(const std::string & sSrc)
}
// Only alphanum and underscore is safe.
const char SAFE[256] =
{
static const char SAFE[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
/* 1 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
@ -180,21 +175,18 @@ const char SAFE[256] =
/* F */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
};
std::string UriEncode(const std::string & sSrc)
{
std::string UriEncode(std::string_view sSrc) {
const char DEC2HEX[16 + 1] = "0123456789ABCDEF";
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const unsigned char * pSrc = (const unsigned char *)sSrc.data();
const size_t SRC_LEN = sSrc.length();
unsigned char * const pStart = new unsigned char[SRC_LEN * 3];
unsigned char * pEnd = pStart;
const unsigned char * const SRC_END = pSrc + SRC_LEN;
for (; pSrc < SRC_END; ++pSrc)
{
if (SAFE[*pSrc])
for (; pSrc < SRC_END; ++pSrc) {
if (SAFE[*pSrc]) {
*pEnd++ = *pSrc;
else
{
} else {
// escape this char
*pEnd++ = '%';
*pEnd++ = DEC2HEX[*pSrc >> 4];

View File

@ -203,5 +203,5 @@ private:
};
std::string UriDecode(const std::string & sSrc);
std::string UriEncode(const std::string & sSrc);
std::string UriDecode(std::string_view sSrc);
std::string UriEncode(std::string_view sSrc);

View File

@ -284,6 +284,23 @@ std::string_view StripQuotes(std::string_view s) {
return s;
}
void SplitString(std::string_view str, const char delim, std::vector<std::string_view> &output) {
size_t next = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
if (str[pos] == delim) {
output.emplace_back(str.substr(next, pos - next));
// Skip the delimiter itself.
next = pos + 1;
}
}
if (next == 0) {
output.push_back(str);
} else if (next < str.length()) {
output.emplace_back(str.substr(next));
}
}
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output) {
size_t next = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
@ -295,7 +312,7 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
}
if (next == 0) {
output.push_back(std::string(str));
output.emplace_back(str);
} else if (next < str.length()) {
output.emplace_back(str.substr(next));
}
@ -320,8 +337,7 @@ static std::string ApplyHtmlEscapes(std::string str) {
}
// Meant for HTML listings and similar, so supports some HTML escapes.
void GetQuotedStrings(const std::string& str, std::vector<std::string>& output)
{
void GetQuotedStrings(const std::string& str, std::vector<std::string> &output) {
size_t next = 0;
bool even = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
@ -340,15 +356,13 @@ void GetQuotedStrings(const std::string& str, std::vector<std::string>& output)
}
}
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest)
{
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
size_t pos = 0;
if (src == dest)
return result;
while (1)
{
while (true) {
pos = result.find(src, pos);
if (pos == result.npos)
break;

View File

@ -43,7 +43,7 @@ inline bool startsWith(std::string_view str, std::string_view key) {
return !memcmp(str.data(), key.data(), key.size());
}
inline bool endsWith(const std::string &str, const std::string &what) {
inline bool endsWith(std::string_view str, std::string_view what) {
if (str.size() < what.size())
return false;
return str.substr(str.size() - what.size()) == what;
@ -82,7 +82,8 @@ std::string_view StripSpaces(std::string_view s);
std::string_view StripQuotes(std::string_view s);
// TODO: Make this a lot more efficient by outputting string_views.
void SplitString(std::string_view str, const char delim, std::vector<std::string>& output);
void SplitString(std::string_view str, const char delim, std::vector<std::string_view> &output);
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output);
void GetQuotedStrings(const std::string& str, std::vector<std::string>& output);