mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-30 20:58:54 +00:00
StringUtil: Use std::string_view more
This commit is contained in:
parent
29ba53f6c3
commit
a2a1e04fc9
13 changed files with 92 additions and 83 deletions
|
@ -9,13 +9,14 @@
|
|||
#include <fstream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "Common/FileUtil.h"
|
||||
#include "Common/StringUtil.h"
|
||||
|
||||
void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut)
|
||||
void IniFile::ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut)
|
||||
{
|
||||
if (line.empty() || line.front() == '#')
|
||||
return;
|
||||
|
@ -96,7 +97,7 @@ bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remo
|
|||
{
|
||||
for (const std::string& line : m_lines)
|
||||
{
|
||||
std::string stripped_line = StripSpaces(line);
|
||||
std::string_view stripped_line = StripSpaces(line);
|
||||
|
||||
if (remove_comments)
|
||||
{
|
||||
|
@ -112,7 +113,7 @@ bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remo
|
|||
}
|
||||
}
|
||||
|
||||
lines->push_back(std::move(stripped_line));
|
||||
lines->emplace_back(stripped_line);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -251,9 +252,8 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
|
|||
bool first_line = true;
|
||||
while (!in.eof())
|
||||
{
|
||||
std::string line;
|
||||
|
||||
if (!std::getline(in, line))
|
||||
std::string line_str;
|
||||
if (!std::getline(in, line_str))
|
||||
{
|
||||
if (in.eof())
|
||||
return true;
|
||||
|
@ -261,17 +261,17 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
|
|||
return false;
|
||||
}
|
||||
|
||||
std::string_view line = line_str;
|
||||
|
||||
// Skips the UTF-8 BOM at the start of files. Notepad likes to add this.
|
||||
if (first_line && line.substr(0, 3) == "\xEF\xBB\xBF")
|
||||
line = line.substr(3);
|
||||
line.remove_prefix(3);
|
||||
first_line = false;
|
||||
|
||||
#ifndef _WIN32
|
||||
// Check for CRLF eol and convert it to LF
|
||||
if (!line.empty() && line.back() == '\r')
|
||||
{
|
||||
line.pop_back();
|
||||
}
|
||||
line.remove_suffix(1);
|
||||
#endif
|
||||
|
||||
if (!line.empty())
|
||||
|
@ -283,7 +283,7 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
|
|||
if (endpos != std::string::npos)
|
||||
{
|
||||
// New section!
|
||||
std::string sub = line.substr(1, endpos - 1);
|
||||
std::string_view sub = line.substr(1, endpos - 1);
|
||||
current_section = GetOrCreateSection(sub);
|
||||
}
|
||||
}
|
||||
|
@ -299,9 +299,13 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
|
|||
// INI is a hack anyway.
|
||||
if ((key.empty() && value.empty()) ||
|
||||
(!line.empty() && (line[0] == '$' || line[0] == '+' || line[0] == '*')))
|
||||
current_section->m_lines.push_back(line);
|
||||
{
|
||||
current_section->m_lines.emplace_back(line);
|
||||
}
|
||||
else
|
||||
{
|
||||
current_section->Set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -158,7 +158,7 @@ public:
|
|||
// This function is related to parsing data from lines of INI files
|
||||
// It's used outside of IniFile, which is why it is exposed publicly
|
||||
// In particular it is used in PostProcessing for its configuration
|
||||
static void ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut);
|
||||
static void ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut);
|
||||
|
||||
const std::list<Section>& GetSections() const { return sections; }
|
||||
|
||||
|
|
|
@ -214,7 +214,7 @@ std::string ArrayToString(const u8* data, u32 size, int line_len, bool spaces)
|
|||
}
|
||||
|
||||
// Turns " hello " into "hello". Also handles tabs.
|
||||
std::string StripSpaces(const std::string& str)
|
||||
std::string_view StripSpaces(std::string_view str)
|
||||
{
|
||||
const size_t s = str.find_first_not_of(" \t\r\n");
|
||||
|
||||
|
@ -227,7 +227,7 @@ std::string StripSpaces(const std::string& str)
|
|||
// "\"hello\"" is turned to "hello"
|
||||
// This one assumes that the string has already been space stripped in both
|
||||
// ends, as done by StripSpaces above, for example.
|
||||
std::string StripQuotes(const std::string& s)
|
||||
std::string_view StripQuotes(std::string_view s)
|
||||
{
|
||||
if (!s.empty() && '\"' == s[0] && '\"' == *s.rbegin())
|
||||
return s.substr(1, s.size() - 2);
|
||||
|
@ -334,7 +334,7 @@ std::string ValueToString(bool value)
|
|||
return value ? "True" : "False";
|
||||
}
|
||||
|
||||
bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
|
||||
bool SplitPath(std::string_view full_path, std::string* _pPath, std::string* _pFilename,
|
||||
std::string* _pExtension)
|
||||
{
|
||||
if (full_path.empty())
|
||||
|
@ -367,8 +367,8 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
|
|||
return true;
|
||||
}
|
||||
|
||||
void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
|
||||
const std::string& _Filename)
|
||||
void BuildCompleteFilename(std::string& _CompleteFilename, std::string_view _Path,
|
||||
std::string_view _Filename)
|
||||
{
|
||||
_CompleteFilename = _Path;
|
||||
|
||||
|
@ -407,19 +407,18 @@ std::string JoinStrings(const std::vector<std::string>& strings, const std::stri
|
|||
return joined.substr(0, joined.length() - delimiter.length());
|
||||
}
|
||||
|
||||
std::string TabsToSpaces(int tab_size, const std::string& in)
|
||||
std::string TabsToSpaces(int tab_size, std::string str)
|
||||
{
|
||||
const std::string spaces(tab_size, ' ');
|
||||
std::string out(in);
|
||||
|
||||
size_t i = 0;
|
||||
while (out.npos != (i = out.find('\t')))
|
||||
out.replace(i, 1, spaces);
|
||||
while (str.npos != (i = str.find('\t')))
|
||||
str.replace(i, 1, spaces);
|
||||
|
||||
return out;
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest)
|
||||
std::string ReplaceAll(std::string result, std::string_view src, std::string_view dest)
|
||||
{
|
||||
size_t pos = 0;
|
||||
|
||||
|
@ -435,12 +434,12 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
|
|||
return result;
|
||||
}
|
||||
|
||||
bool StringBeginsWith(const std::string& str, const std::string& begin)
|
||||
bool StringBeginsWith(std::string_view str, std::string_view begin)
|
||||
{
|
||||
return str.size() >= begin.size() && std::equal(begin.begin(), begin.end(), str.begin());
|
||||
}
|
||||
|
||||
bool StringEndsWith(const std::string& str, const std::string& end)
|
||||
bool StringEndsWith(std::string_view str, std::string_view end)
|
||||
{
|
||||
return str.size() >= end.size() && std::equal(end.rbegin(), end.rend(), str.rbegin());
|
||||
}
|
||||
|
@ -453,7 +452,7 @@ void StringPopBackIf(std::string* s, char c)
|
|||
|
||||
#ifdef _WIN32
|
||||
|
||||
std::wstring CPToUTF16(u32 code_page, const std::string& input)
|
||||
std::wstring CPToUTF16(u32 code_page, std::string_view input)
|
||||
{
|
||||
auto const size =
|
||||
MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
|
||||
|
@ -471,7 +470,7 @@ std::wstring CPToUTF16(u32 code_page, const std::string& input)
|
|||
return output;
|
||||
}
|
||||
|
||||
std::string UTF16ToCP(u32 code_page, const std::wstring& input)
|
||||
std::string UTF16ToCP(u32 code_page, std::wstring_view input)
|
||||
{
|
||||
std::string output;
|
||||
|
||||
|
@ -487,7 +486,8 @@ std::string UTF16ToCP(u32 code_page, const std::wstring& input)
|
|||
&output[0], static_cast<int>(output.size()), nullptr, false))
|
||||
{
|
||||
const DWORD error_code = GetLastError();
|
||||
ERROR_LOG(COMMON, "WideCharToMultiByte Error in String '%s': %lu", input.c_str(), error_code);
|
||||
ERROR_LOG(COMMON, "WideCharToMultiByte Error in String '%s': %lu",
|
||||
std::wstring(input).c_str(), error_code);
|
||||
output.clear();
|
||||
}
|
||||
}
|
||||
|
@ -495,27 +495,27 @@ std::string UTF16ToCP(u32 code_page, const std::wstring& input)
|
|||
return output;
|
||||
}
|
||||
|
||||
std::wstring UTF8ToUTF16(const std::string& input)
|
||||
std::wstring UTF8ToUTF16(std::string_view input)
|
||||
{
|
||||
return CPToUTF16(CP_UTF8, input);
|
||||
}
|
||||
|
||||
std::string UTF16ToUTF8(const std::wstring& input)
|
||||
std::string UTF16ToUTF8(std::wstring_view input)
|
||||
{
|
||||
return UTF16ToCP(CP_UTF8, input);
|
||||
}
|
||||
|
||||
std::string SHIFTJISToUTF8(const std::string& input)
|
||||
std::string SHIFTJISToUTF8(std::string_view input)
|
||||
{
|
||||
return UTF16ToUTF8(CPToUTF16(CODEPAGE_SHIFT_JIS, input));
|
||||
}
|
||||
|
||||
std::string UTF8ToSHIFTJIS(const std::string& input)
|
||||
std::string UTF8ToSHIFTJIS(std::string_view input)
|
||||
{
|
||||
return UTF16ToCP(CODEPAGE_SHIFT_JIS, UTF8ToUTF16(input));
|
||||
}
|
||||
|
||||
std::string CP1252ToUTF8(const std::string& input)
|
||||
std::string CP1252ToUTF8(std::string_view input)
|
||||
{
|
||||
return UTF16ToUTF8(CPToUTF16(CODEPAGE_WINDOWS_1252, input));
|
||||
}
|
||||
|
@ -531,7 +531,7 @@ std::string UTF16BEToUTF8(const char16_t* str, size_t max_size)
|
|||
#else
|
||||
|
||||
template <typename T>
|
||||
std::string CodeTo(const char* tocode, const char* fromcode, const std::basic_string<T>& input)
|
||||
std::string CodeTo(const char* tocode, const char* fromcode, std::basic_string_view<T> input)
|
||||
{
|
||||
std::string result;
|
||||
|
||||
|
@ -548,9 +548,9 @@ std::string CodeTo(const char* tocode, const char* fromcode, const std::basic_st
|
|||
std::string out_buffer;
|
||||
out_buffer.resize(out_buffer_size);
|
||||
|
||||
auto src_buffer = &input[0];
|
||||
auto src_buffer = input.data();
|
||||
size_t src_bytes = in_bytes;
|
||||
auto dst_buffer = &out_buffer[0];
|
||||
auto dst_buffer = out_buffer.data();
|
||||
size_t dst_bytes = out_buffer.size();
|
||||
|
||||
while (src_bytes != 0)
|
||||
|
@ -587,39 +587,39 @@ std::string CodeTo(const char* tocode, const char* fromcode, const std::basic_st
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input)
|
||||
std::string CodeToUTF8(const char* fromcode, std::basic_string_view<T> input)
|
||||
{
|
||||
return CodeTo("UTF-8", fromcode, input);
|
||||
}
|
||||
|
||||
std::string CP1252ToUTF8(const std::string& input)
|
||||
std::string CP1252ToUTF8(std::string_view input)
|
||||
{
|
||||
// return CodeToUTF8("CP1252//TRANSLIT", input);
|
||||
// return CodeToUTF8("CP1252//IGNORE", input);
|
||||
return CodeToUTF8("CP1252", input);
|
||||
}
|
||||
|
||||
std::string SHIFTJISToUTF8(const std::string& input)
|
||||
std::string SHIFTJISToUTF8(std::string_view input)
|
||||
{
|
||||
// return CodeToUTF8("CP932", input);
|
||||
return CodeToUTF8("SJIS", input);
|
||||
}
|
||||
|
||||
std::string UTF8ToSHIFTJIS(const std::string& input)
|
||||
std::string UTF8ToSHIFTJIS(std::string_view input)
|
||||
{
|
||||
return CodeTo("SJIS", "UTF-8", input);
|
||||
}
|
||||
|
||||
std::string UTF16ToUTF8(const std::wstring& input)
|
||||
std::string UTF16ToUTF8(std::wstring_view input)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
|
||||
return converter.to_bytes(input);
|
||||
return converter.to_bytes(input.data(), input.data() + input.size());
|
||||
}
|
||||
|
||||
std::string UTF16BEToUTF8(const char16_t* str, size_t max_size)
|
||||
{
|
||||
const char16_t* str_end = std::find(str, str + max_size, '\0');
|
||||
return CodeToUTF8("UTF-16BE", std::u16string(str, static_cast<size_t>(str_end - str)));
|
||||
return CodeToUTF8("UTF-16BE", std::u16string_view(str, static_cast<size_t>(str_end - str)));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -629,7 +629,7 @@ std::string UTF16BEToUTF8(const char16_t* str, size_t max_size)
|
|||
std::filesystem::path StringToPath(std::string_view path)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
return std::filesystem::path(UTF8ToUTF16(std::string(path)));
|
||||
return std::filesystem::path(UTF8ToUTF16(path));
|
||||
#else
|
||||
return std::filesystem::path(path);
|
||||
#endif
|
||||
|
|
|
@ -44,8 +44,8 @@ inline void CharArrayFromFormat(char (&out)[Count], const char* format, ...)
|
|||
// Good
|
||||
std::string ArrayToString(const u8* data, u32 size, int line_len = 20, bool spaces = true);
|
||||
|
||||
std::string StripSpaces(const std::string& s);
|
||||
std::string StripQuotes(const std::string& s);
|
||||
std::string_view StripSpaces(std::string_view s);
|
||||
std::string_view StripQuotes(std::string_view s);
|
||||
|
||||
bool TryParse(const std::string& str, bool* output);
|
||||
bool TryParse(const std::string& str, u16* output);
|
||||
|
@ -107,50 +107,50 @@ std::string HexDump(const u8* data, size_t size);
|
|||
// TODO: kill this
|
||||
bool AsciiToHex(const std::string& _szValue, u32& result);
|
||||
|
||||
std::string TabsToSpaces(int tab_size, const std::string& in);
|
||||
std::string TabsToSpaces(int tab_size, std::string str);
|
||||
|
||||
std::vector<std::string> SplitString(const std::string& str, char delim);
|
||||
std::string JoinStrings(const std::vector<std::string>& strings, const std::string& delimiter);
|
||||
|
||||
// "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
|
||||
bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
|
||||
bool SplitPath(std::string_view full_path, std::string* _pPath, std::string* _pFilename,
|
||||
std::string* _pExtension);
|
||||
|
||||
void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
|
||||
const std::string& _Filename);
|
||||
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest);
|
||||
void BuildCompleteFilename(std::string& _CompleteFilename, std::string_view _Path,
|
||||
std::string_view _Filename);
|
||||
std::string ReplaceAll(std::string result, std::string_view src, std::string_view dest);
|
||||
|
||||
bool StringBeginsWith(const std::string& str, const std::string& begin);
|
||||
bool StringEndsWith(const std::string& str, const std::string& end);
|
||||
bool StringBeginsWith(std::string_view str, std::string_view begin);
|
||||
bool StringEndsWith(std::string_view str, std::string_view end);
|
||||
void StringPopBackIf(std::string* s, char c);
|
||||
|
||||
std::string CP1252ToUTF8(const std::string& str);
|
||||
std::string SHIFTJISToUTF8(const std::string& str);
|
||||
std::string UTF8ToSHIFTJIS(const std::string& str);
|
||||
std::string UTF16ToUTF8(const std::wstring& str);
|
||||
std::string CP1252ToUTF8(std::string_view str);
|
||||
std::string SHIFTJISToUTF8(std::string_view str);
|
||||
std::string UTF8ToSHIFTJIS(std::string_view str);
|
||||
std::string UTF16ToUTF8(std::wstring_view str);
|
||||
std::string UTF16BEToUTF8(const char16_t* str, size_t max_size); // Stops at \0
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
std::wstring UTF8ToUTF16(const std::string& str);
|
||||
std::wstring UTF8ToUTF16(std::string_view str);
|
||||
|
||||
#ifdef _UNICODE
|
||||
inline std::string TStrToUTF8(const std::wstring& str)
|
||||
inline std::string TStrToUTF8(std::wstring_view str)
|
||||
{
|
||||
return UTF16ToUTF8(str);
|
||||
}
|
||||
|
||||
inline std::wstring UTF8ToTStr(const std::string& str)
|
||||
inline std::wstring UTF8ToTStr(std::string_view str)
|
||||
{
|
||||
return UTF8ToUTF16(str);
|
||||
}
|
||||
#else
|
||||
inline std::string TStrToUTF8(const std::string& str)
|
||||
inline std::string TStrToUTF8(std::string_view str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
inline std::string UTF8ToTStr(const std::string& str)
|
||||
inline std::string UTF8ToTStr(std::string_view str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue