Skip to content

Commit

Permalink
Move Config ValueToString to StringUtil
Browse files Browse the repository at this point in the history
An identical implementation is used by IniFile, so move those functions
to StringUtil. A future commit will modify IniFile to use them.
  • Loading branch information
leoetlino committed Jun 3, 2018
1 parent 66ee47c commit fc0193c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 57 deletions.
38 changes: 0 additions & 38 deletions Source/Core/Common/Config/Layer.cpp
Expand Up @@ -11,44 +11,6 @@


namespace Config namespace Config
{ {
namespace detail
{
std::string ValueToString(u16 value)
{
return StringFromFormat("0x%04x", value);
}

std::string ValueToString(u32 value)
{
return StringFromFormat("0x%08x", value);
}

std::string ValueToString(float value)
{
return StringFromFormat("%#.9g", value);
}

std::string ValueToString(double value)
{
return StringFromFormat("%#.17g", value);
}

std::string ValueToString(int value)
{
return std::to_string(value);
}

std::string ValueToString(bool value)
{
return StringFromBool(value);
}

std::string ValueToString(const std::string& value)
{
return value;
}
}

ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer) ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer)
{ {
} }
Expand Down
15 changes: 1 addition & 14 deletions Source/Core/Common/Config/Layer.h
Expand Up @@ -19,19 +19,6 @@ namespace Config
{ {
namespace detail namespace detail
{ {
std::string ValueToString(u16 value);
std::string ValueToString(u32 value);
std::string ValueToString(float value);
std::string ValueToString(double value);
std::string ValueToString(int value);
std::string ValueToString(bool value);
std::string ValueToString(const std::string& value);
template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
std::string ValueToString(T value)
{
return ValueToString(static_cast<std::underlying_type_t<T>>(value));
}

template <typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr> template <typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
std::optional<T> TryParse(const std::string& str_value) std::optional<T> TryParse(const std::string& str_value)
{ {
Expand Down Expand Up @@ -139,7 +126,7 @@ class Layer
template <typename T> template <typename T>
void Set(const ConfigLocation& location, const T& value) void Set(const ConfigLocation& location, const T& value)
{ {
const std::string new_value = detail::ValueToString(value); const std::string new_value = ValueToString(value);
std::optional<std::string>& current_value = m_map[location]; std::optional<std::string>& current_value = m_map[location];
if (current_value == new_value) if (current_value == new_value)
return; return;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Common/IniFile.cpp
Expand Up @@ -103,7 +103,7 @@ void IniFile::Section::Set(const std::string& key, s64 newValue)


void IniFile::Section::Set(const std::string& key, bool newValue) void IniFile::Section::Set(const std::string& key, bool newValue)
{ {
Set(key, StringFromBool(newValue)); Set(key, ValueToString(newValue));
} }


bool IniFile::Section::Get(const std::string& key, std::string* value, bool IniFile::Section::Get(const std::string& key, std::string* value,
Expand Down
43 changes: 42 additions & 1 deletion Source/Core/Common/StringUtil.cpp
Expand Up @@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.


#include <algorithm> #include <algorithm>
#include <cinttypes>
#include <cstdarg> #include <cstdarg>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
Expand Down Expand Up @@ -291,11 +292,51 @@ bool TryParse(const std::string& str, bool* const output)
return true; return true;
} }


std::string StringFromBool(bool value) std::string ValueToString(u16 value)
{
return StringFromFormat("0x%04x", value);
}

std::string ValueToString(u32 value)
{
return StringFromFormat("0x%08x", value);
}

std::string ValueToString(u64 value)
{
return StringFromFormat("0x%016" PRIx64, value);
}

std::string ValueToString(float value)
{
return StringFromFormat("%#.9g", value);
}

std::string ValueToString(double value)
{
return StringFromFormat("%#.17g", value);
}

std::string ValueToString(int value)
{
return std::to_string(value);
}

std::string ValueToString(s64 value)
{
return StringFromFormat("%" PRId64, value);
}

std::string ValueToString(bool value)
{ {
return value ? "True" : "False"; return value ? "True" : "False";
} }


std::string ValueToString(const std::string& value)
{
return value;
}

bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
std::string* _pExtension) std::string* _pExtension)
{ {
Expand Down
18 changes: 16 additions & 2 deletions Source/Core/Common/StringUtil.h
Expand Up @@ -9,6 +9,7 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <type_traits>
#include <vector> #include <vector>


#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
Expand Down Expand Up @@ -41,8 +42,6 @@ std::string ArrayToString(const u8* data, u32 size, int line_len = 20, bool spac
std::string StripSpaces(const std::string& s); std::string StripSpaces(const std::string& s);
std::string StripQuotes(const std::string& s); std::string StripQuotes(const std::string& s);


std::string StringFromBool(bool value);

bool TryParse(const std::string& str, bool* output); bool TryParse(const std::string& str, bool* output);
bool TryParse(const std::string& str, u16* output); bool TryParse(const std::string& str, u16* output);
bool TryParse(const std::string& str, u32* output); bool TryParse(const std::string& str, u32* output);
Expand Down Expand Up @@ -83,6 +82,21 @@ bool TryParseVector(const std::string& str, std::vector<N>* output, const char d
return true; return true;
} }


std::string ValueToString(u16 value);
std::string ValueToString(u32 value);
std::string ValueToString(u64 value);
std::string ValueToString(float value);
std::string ValueToString(double value);
std::string ValueToString(int value);
std::string ValueToString(s64 value);
std::string ValueToString(bool value);
std::string ValueToString(const std::string& value);
template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
std::string ValueToString(T value)
{
return ValueToString(static_cast<std::underlying_type_t<T>>(value));
}

// Generates an hexdump-like representation of a binary data blob. // Generates an hexdump-like representation of a binary data blob.
std::string HexDump(const u8* data, size_t size); std::string HexDump(const u8* data, size_t size);


Expand Down
2 changes: 1 addition & 1 deletion Source/Core/UICommon/CommandLineParse.cpp
Expand Up @@ -30,7 +30,7 @@ class CommandLineConfigLayerLoader final : public Config::ConfigLayerLoader


if (audio_backend.size()) if (audio_backend.size())
m_values.emplace_back( m_values.emplace_back(
std::make_tuple(Config::MAIN_DSP_HLE.location, StringFromBool(audio_backend == "HLE"))); std::make_tuple(Config::MAIN_DSP_HLE.location, ValueToString(audio_backend == "HLE")));


// Arguments are in the format of <System>.<Section>.<Key>=Value // Arguments are in the format of <System>.<Section>.<Key>=Value
for (const auto& arg : args) for (const auto& arg : args)
Expand Down

0 comments on commit fc0193c

Please sign in to comment.