Skip to content

Commit

Permalink
More uses of string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 13, 2023
1 parent d0ee5fc commit 19eeaef
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Common/File/AndroidContentURI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Common/File/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Common/File/PathBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Common/GPU/OpenGL/GLQueueRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 5 additions & 4 deletions Common/Input/InputState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Common/Input/InputState.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class InputMapping {
_dbg_assert_(direction != 0);
}

static InputMapping FromConfigString(const std::string &str);
static InputMapping FromConfigString(std::string_view str);
std::string ToConfigString() const;

InputDeviceID deviceId;
Expand Down
6 changes: 3 additions & 3 deletions Common/Net/HTTPHeaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 11 additions & 19 deletions Common/Net/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,24 @@ 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 '%'

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;
Expand All @@ -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,
Expand All @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions Common/Net/URL.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,5 @@ class Url {
};


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);
28 changes: 21 additions & 7 deletions Common/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions Common/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 19eeaef

Please sign in to comment.