Skip to content

Commit

Permalink
Merge pull request #18421 from hrydgard/string-view-opts
Browse files Browse the repository at this point in the history
String view optimizations (code cleanup)
  • Loading branch information
hrydgard committed Nov 14, 2023
2 parents 1dea3f8 + 1da6da4 commit 50d232d
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 119 deletions.
17 changes: 9 additions & 8 deletions Common/File/AndroidContentURI.cpp
@@ -1,14 +1,14 @@
#include "Common/File/AndroidContentURI.h"

bool AndroidContentURI::Parse(const std::string &path) {
bool AndroidContentURI::Parse(std::string_view path) {
const char *prefix = "content://";
if (!startsWith(path, prefix)) {
return false;
}

std::string components = path.substr(strlen(prefix));
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 Expand Up @@ -60,24 +60,25 @@ AndroidContentURI AndroidContentURI::WithRootFilePath(const std::string &filePat
return uri;
}

AndroidContentURI AndroidContentURI::WithComponent(const std::string &filePath) {
AndroidContentURI AndroidContentURI::WithComponent(std::string_view filePath) {
AndroidContentURI uri = *this;
if (uri.file.empty()) {
// Not sure what to do.
return uri;
}
if (uri.file.back() == ':') {
// Special case handling for Document URIs: Treat the ':' as a directory separator too (but preserved in the filename).
uri.file = uri.file + filePath;
uri.file.append(filePath);
} else {
uri.file = uri.file + "/" + filePath;
uri.file.push_back('/');
uri.file.append(filePath);
}
return uri;
}

AndroidContentURI AndroidContentURI::WithExtraExtension(const std::string &extension) {
AndroidContentURI AndroidContentURI::WithExtraExtension(std::string_view extension) {
AndroidContentURI uri = *this;
uri.file = uri.file + extension;
uri.file.append(extension);
return uri;
}

Expand Down
8 changes: 4 additions & 4 deletions Common/File/AndroidContentURI.h
Expand Up @@ -23,15 +23,15 @@ class AndroidContentURI {
std::string file;
public:
AndroidContentURI() {}
explicit AndroidContentURI(const std::string &path) {
explicit AndroidContentURI(std::string_view path) {
Parse(path);
}

bool Parse(const std::string &path);
bool Parse(std::string_view path);

AndroidContentURI WithRootFilePath(const std::string &filePath);
AndroidContentURI WithComponent(const std::string &filePath);
AndroidContentURI WithExtraExtension(const std::string &extension);
AndroidContentURI WithComponent(std::string_view filePath);
AndroidContentURI WithExtraExtension(std::string_view extension); // The ext string contains the dot.
AndroidContentURI WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const;
AndroidContentURI WithReplacedExtension(const std::string &newExtension) const;

Expand Down
6 changes: 3 additions & 3 deletions Common/File/AndroidStorage.cpp
Expand Up @@ -61,16 +61,16 @@ void Android_RegisterStorageCallbacks(JNIEnv * env, jobject obj) {
_dbg_assert_(computeRecursiveDirectorySize);
}

bool Android_IsContentUri(const std::string &filename) {
bool Android_IsContentUri(std::string_view filename) {
return startsWith(filename, "content://");
}

int Android_OpenContentUriFd(const std::string &filename, Android_OpenContentUriMode mode) {
int Android_OpenContentUriFd(std::string_view filename, Android_OpenContentUriMode mode) {
if (!g_nativeActivity) {
return -1;
}

std::string fname = filename;
std::string fname(filename);
// PPSSPP adds an ending slash to directories before looking them up.
// TODO: Fix that in the caller (or don't call this for directories).
if (fname.back() == '/')
Expand Down
9 changes: 5 additions & 4 deletions Common/File/AndroidStorage.h
Expand Up @@ -2,6 +2,7 @@

#include <vector>
#include <string>
#include <string_view>

#include "Common/File/DirListing.h"

Expand Down Expand Up @@ -39,8 +40,8 @@ extern std::string g_externalDir;

void Android_StorageSetNativeActivity(jobject nativeActivity);

bool Android_IsContentUri(const std::string &uri);
int Android_OpenContentUriFd(const std::string &uri, const Android_OpenContentUriMode mode);
bool Android_IsContentUri(std::string_view uri);
int Android_OpenContentUriFd(std::string_view uri, const Android_OpenContentUriMode mode);
StorageError Android_CreateDirectory(const std::string &parentTreeUri, const std::string &dirName);
StorageError Android_CreateFile(const std::string &parentTreeUri, const std::string &fileName);
StorageError Android_MoveFile(const std::string &fileUri, const std::string &srcParentUri, const std::string &destParentUri);
Expand All @@ -63,8 +64,8 @@ void Android_RegisterStorageCallbacks(JNIEnv * env, jobject obj);

// Stub out the Android Storage wrappers, so that we can avoid ifdefs everywhere.

inline bool Android_IsContentUri(const std::string &uri) { return false; }
inline int Android_OpenContentUriFd(const std::string &uri, const Android_OpenContentUriMode mode) { return -1; }
inline bool Android_IsContentUri(std::string_view uri) { return false; }
inline int Android_OpenContentUriFd(std::string_view uri, const Android_OpenContentUriMode mode) { return -1; }
inline StorageError Android_CreateDirectory(const std::string &parentTreeUri, const std::string &dirName) { return StorageError::UNKNOWN; }
inline StorageError Android_CreateFile(const std::string &parentTreeUri, const std::string &fileName) { return StorageError::UNKNOWN; }
inline StorageError Android_MoveFile(const std::string &fileUri, const std::string &srcParentUri, const std::string &destParentUri) { return StorageError::UNKNOWN; }
Expand Down
4 changes: 2 additions & 2 deletions Common/File/FileUtil.cpp
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
16 changes: 8 additions & 8 deletions Common/File/Path.cpp
Expand Up @@ -22,7 +22,7 @@
#include <sys/stat.h>
#endif

Path::Path(const std::string &str) {
Path::Path(std::string_view str) {
Init(str);
}

Expand All @@ -33,7 +33,7 @@ Path::Path(const std::wstring &str) {
}
#endif

void Path::Init(const std::string &str) {
void Path::Init(std::string_view str) {
if (str.empty()) {
type_ = PathType::UNDEFINED;
path_.clear();
Expand Down Expand Up @@ -81,7 +81,7 @@ void Path::Init(const std::string &str) {

// We always use forward slashes internally, we convert to backslash only when
// converted to a wstring.
Path Path::operator /(const std::string &subdir) const {
Path Path::operator /(std::string_view subdir) const {
if (type_ == PathType::CONTENT_URI) {
AndroidContentURI uri(path_);
return Path(uri.WithComponent(subdir).ToString());
Expand All @@ -104,18 +104,18 @@ Path Path::operator /(const std::string &subdir) const {
return Path(fullPath);
}

void Path::operator /=(const std::string &subdir) {
void Path::operator /=(std::string_view subdir) {
*this = *this / subdir;
}

Path Path::WithExtraExtension(const std::string &ext) const {
Path Path::WithExtraExtension(std::string_view ext) const {
if (type_ == PathType::CONTENT_URI) {
AndroidContentURI uri(path_);
return Path(uri.WithExtraExtension(ext).ToString());
}

_dbg_assert_(!ext.empty() && ext[0] == '.');
return Path(path_ + ext);
return Path(path_ + std::string(ext));
}

Path Path::WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const {
Expand Down Expand Up @@ -161,7 +161,7 @@ std::string Path::GetFilename() const {
return path_;
}

std::string GetExtFromString(const std::string &str) {
std::string GetExtFromString(std::string_view str) {
size_t pos = str.rfind(".");
if (pos == std::string::npos) {
return "";
Expand All @@ -171,7 +171,7 @@ std::string GetExtFromString(const std::string &str) {
// Don't want to detect "df/file" from "/as.df/file"
return "";
}
std::string ext = str.substr(pos);
std::string ext(str.substr(pos));
for (size_t i = 0; i < ext.size(); i++) {
ext[i] = tolower(ext[i]);
}
Expand Down
13 changes: 7 additions & 6 deletions Common/File/Path.h
Expand Up @@ -3,6 +3,7 @@
#include "ppsspp_config.h"

#include <string>
#include <string_view>

#if defined(__APPLE__)

Expand Down Expand Up @@ -36,11 +37,11 @@ enum class PathType {

class Path {
private:
void Init(const std::string &str);
void Init(std::string_view str);

public:
Path() : type_(PathType::UNDEFINED) {}
explicit Path(const std::string &str);
explicit Path(std::string_view str);

#if PPSSPP_PLATFORM(WINDOWS)
explicit Path(const std::wstring &str);
Expand Down Expand Up @@ -71,13 +72,13 @@ class Path {
bool IsAbsolute() const;

// Returns a path extended with a subdirectory.
Path operator /(const std::string &subdir) const;
Path operator /(std::string_view subdir) const;

// Navigates down into a subdir.
void operator /=(const std::string &subdir);
void operator /=(std::string_view subdir);

// File extension manipulation.
Path WithExtraExtension(const std::string &ext) const;
Path WithExtraExtension(std::string_view ext) const;
Path WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const;
Path WithReplacedExtension(const std::string &newExtension) const;

Expand Down Expand Up @@ -139,7 +140,7 @@ class Path {
};

// Utility function for parsing out file extensions.
std::string GetExtFromString(const std::string &str);
std::string GetExtFromString(std::string_view str);

// Utility function for fixing the case of paths. Only present on Unix-like systems.

Expand Down
2 changes: 1 addition & 1 deletion Common/File/PathBrowser.cpp
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
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
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
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
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
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
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);

0 comments on commit 50d232d

Please sign in to comment.