StringUtil: Use std::string_view more

This commit is contained in:
JosJuice 2019-07-08 13:35:53 +02:00
parent 29ba53f6c3
commit a2a1e04fc9
13 changed files with 92 additions and 83 deletions

View File

@ -26,7 +26,7 @@ static void LogCallback(const char* format, ...)
const char* filename = va_arg(args, const char*) + s_path_cutoff_point;
int lineno = va_arg(args, int);
std::string adapted_format = StripSpaces(format + strlen("%s:%d:"));
std::string adapted_format(StripSpaces(format + strlen("%s:%d:")));
LogManager::GetInstance()->LogWithFullPath(LogTypes::LNOTICE, LogTypes::AUDIO, filename, lineno,
adapted_format.c_str(), args);

View File

@ -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);
}
}
}
}

View File

@ -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; }

View File

@ -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

View File

@ -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;
}

View File

@ -8,6 +8,7 @@
#include <cstring>
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@ -298,7 +299,7 @@ bool PPCSymbolDB::LoadMap(const std::string& filename, bool bad)
// Detect two columns with three columns fallback
if (column_count == 0)
{
const std::string stripped_line = StripSpaces(line);
const std::string_view stripped_line = StripSpaces(line);
if (std::count(stripped_line.begin(), stripped_line.end(), ' ') == 1)
column_count = 2;
else

View File

@ -7,6 +7,8 @@
#include <cstddef>
#include <fstream>
#include <functional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
@ -39,7 +41,7 @@ static Map LoadMap(const std::string& file_path)
const size_t equals_index = line.find('=');
if (equals_index != std::string::npos)
{
const std::string game_id = StripSpaces(line.substr(0, equals_index));
const std::string_view game_id = StripSpaces(line.substr(0, equals_index));
if (game_id.length() >= 4)
map.emplace(game_id, StripSpaces(line.substr(equals_index + 1)));
}

View File

@ -4,6 +4,8 @@
#include "DolphinQt/Settings/USBDeviceAddToWhitelistDialog.h"
#include <string_view>
#include <QButtonGroup>
#include <QDialog>
#include <QDialogButtonBox>
@ -27,7 +29,7 @@
#include "UICommon/USBUtils.h"
static bool IsValidUSBIDString(const std::string& string)
static bool IsValidUSBIDString(std::string_view string)
{
if (string.empty() || string.length() > 4)
return false;
@ -124,8 +126,8 @@ void USBDeviceAddToWhitelistDialog::RefreshDeviceList()
void USBDeviceAddToWhitelistDialog::AddUSBDeviceToWhitelist()
{
const std::string vid_string = StripSpaces(device_vid_textbox->text().toStdString());
const std::string pid_string = StripSpaces(device_pid_textbox->text().toStdString());
const std::string_view vid_string = StripSpaces(device_vid_textbox->text().toStdString());
const std::string_view pid_string = StripSpaces(device_pid_textbox->text().toStdString());
if (!IsValidUSBIDString(vid_string))
{
// i18n: Here, VID means Vendor ID (for a USB device).
@ -151,8 +153,8 @@ void USBDeviceAddToWhitelistDialog::AddUSBDeviceToWhitelist()
return;
}
const u16 vid = static_cast<u16>(std::stoul(vid_string, nullptr, 16));
const u16 pid = static_cast<u16>(std::stoul(pid_string, nullptr, 16));
const u16 vid = static_cast<u16>(std::stoul(std::string(vid_string), nullptr, 16));
const u16 pid = static_cast<u16>(std::stoul(std::string(pid_string), nullptr, 16));
if (SConfig::GetInstance().IsUSBDeviceWhitelisted({vid, pid}))
{

View File

@ -141,7 +141,7 @@ static std::string GetDeviceRefName(IOHIDDeviceRef inIOHIDDeviceRef)
{
const NSString* name = reinterpret_cast<const NSString*>(
IOHIDDeviceGetProperty(inIOHIDDeviceRef, CFSTR(kIOHIDProductKey)));
return (name != nullptr) ? StripSpaces([name UTF8String]) : "Unknown device";
return (name != nullptr) ? std::string(StripSpaces([name UTF8String])) : "Unknown device";
}
static void DeviceRemovalCallback(void* inContext, IOReturn inResult, void* inSender,

View File

@ -164,7 +164,7 @@ std::string Joystick::Button::GetName() const
{
std::ostringstream s;
s << IOHIDElementGetUsage(m_element);
return std::string("Button ") + StripSpaces(s.str());
return std::string("Button ").append(StripSpaces(s.str()));
}
Joystick::Axis::Axis(IOHIDElementRef element, IOHIDDeviceRef device, direction dir)

View File

@ -313,7 +313,7 @@ std::string evdevDevice::Button::GetName() const
{
const char* name = libevdev_event_code_get_name(EV_KEY, m_code);
if (name)
return StripSpaces(name);
return std::string(StripSpaces(name));
}
// But controllers use codes above 0x100, and the standard label often doesn't match.
// We are better off with Button 0 and so on.

View File

@ -32,7 +32,7 @@ std::vector<std::string> GetProfilesFromSetting(const std::string& setting, cons
std::vector<std::string> result;
for (const std::string& setting_choice : setting_choices)
{
const std::string path = root + StripSpaces(setting_choice);
const std::string path = root + std::string(StripSpaces(setting_choice));
if (File::IsDirectory(path))
{
const auto files_under_directory = Common::DoFileSearch({path}, {".ini"}, true);

View File

@ -6,6 +6,7 @@
#include <sstream>
#include <string>
#include <string_view>
#include "Common/Assert.h"
#include "Common/CommonPaths.h"
@ -117,16 +118,15 @@ void PostProcessingConfiguration::LoadOptions(const std::string& code)
GLSLStringOption* current_strings = nullptr;
while (!in.eof())
{
std::string line;
if (std::getline(in, line))
std::string line_str;
if (std::getline(in, line_str))
{
std::string_view line = line_str;
#ifndef _WIN32
// Check for CRLF eol and convert it to LF
if (!line.empty() && line.at(line.size() - 1) == '\r')
{
line.erase(line.size() - 1);
}
line.remove_suffix(1);
#endif
if (!line.empty())
@ -138,8 +138,8 @@ void PostProcessingConfiguration::LoadOptions(const std::string& code)
if (endpos != std::string::npos)
{
// New section!
std::string sub = line.substr(1, endpos - 1);
option_strings.push_back({sub});
std::string_view sub = line.substr(1, endpos - 1);
option_strings.push_back({std::string(sub)});
current_strings = &option_strings.back();
}
}