Skip to content
Permalink
Browse files

Merge pull request #8233 from JosJuice/stringutil-string-view

StringUtil: Use std::string_view more
  • Loading branch information...
stenzek committed Aug 9, 2019
2 parents 0ab7e2e + 117a60c commit 48ca2c6f2e70111cf72052d5d258328c2fdb9afd
@@ -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);
@@ -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,27 +252,26 @@ 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;
else
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 @@ class IniFile
// 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,8 +334,8 @@ std::string ValueToString(bool value)
return value ? "True" : "False";
}

bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
std::string* _pExtension)
bool SplitPath(std::string_view full_path, std::string* path, std::string* filename,
std::string* extension)
{
if (full_path.empty())
return false;
@@ -355,29 +355,29 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
if (fname_end < dir_end || std::string::npos == fname_end)
fname_end = full_path.size();

if (_pPath)
*_pPath = full_path.substr(0, dir_end);
if (path)
*path = full_path.substr(0, dir_end);

if (_pFilename)
*_pFilename = full_path.substr(dir_end, fname_end - dir_end);
if (filename)
*filename = full_path.substr(dir_end, fname_end - dir_end);

if (_pExtension)
*_pExtension = full_path.substr(fname_end);
if (extension)
*extension = full_path.substr(fname_end);

return true;
}

void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
const std::string& _Filename)
void BuildCompleteFilename(std::string& complete_filename, std::string_view path,
std::string_view filename)
{
_CompleteFilename = _Path;
complete_filename = path;

// check for seperator
if (DIR_SEP_CHR != *_CompleteFilename.rbegin())
_CompleteFilename += DIR_SEP_CHR;
if (DIR_SEP_CHR != *complete_filename.rbegin())
complete_filename += DIR_SEP_CHR;

// add the filename
_CompleteFilename += _Filename;
complete_filename += filename;
}

std::vector<std::string> SplitString(const std::string& str, const char delim)
@@ -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,35 +486,36 @@ 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();
}
}

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

0 comments on commit 48ca2c6

Please sign in to comment.
You can’t perform that action at this time.