1
0
Fork 0

Merge pull request #4444 from FearlessTobi/port-1684

Port yuzu-emu/yuzu#1684: "common/string_util: Minor cleanup"
This commit is contained in:
bunnei 2018-11-23 23:13:39 -05:00 committed by GitHub
commit dc16ba009e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 96 deletions

View File

@ -4,11 +4,10 @@
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cerrno>
#include <codecvt> #include <codecvt>
#include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <locale>
#include <sstream>
#include "common/common_paths.h" #include "common/common_paths.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/string_util.h" #include "common/string_util.h"
@ -33,24 +32,6 @@ std::string ToUpper(std::string str) {
return str; return str;
} }
// For Debugging. Read out an u8 array.
std::string ArrayToString(const u8* data, std::size_t size, int line_len, bool spaces) {
std::ostringstream oss;
oss << std::setfill('0') << std::hex;
for (int line = 0; size; ++data, --size) {
oss << std::setw(2) << (int)*data;
if (line_len == ++line) {
oss << '\n';
line = 0;
} else if (spaces)
oss << ' ';
}
return oss.str();
}
// Turns " hej " into "hej". Also handles tabs. // Turns " hej " into "hej". Also handles tabs.
std::string StripSpaces(const std::string& str) { std::string StripSpaces(const std::string& str) {
const std::size_t s = str.find_first_not_of(" \t\r\n"); const std::size_t s = str.find_first_not_of(" \t\r\n");
@ -71,40 +52,6 @@ std::string StripQuotes(const std::string& s) {
return s; return s;
} }
bool TryParse(const std::string& str, u32* const output) {
char* endptr = nullptr;
// Reset errno to a value other than ERANGE
errno = 0;
unsigned long value = strtoul(str.c_str(), &endptr, 0);
if (!endptr || *endptr)
return false;
if (errno == ERANGE)
return false;
#if ULONG_MAX > UINT_MAX
if (value >= 0x100000000ull && value <= 0xFFFFFFFF00000000ull)
return false;
#endif
*output = static_cast<u32>(value);
return true;
}
bool TryParse(const std::string& str, bool* const output) {
if ("1" == str || "true" == ToLower(str))
*output = true;
else if ("0" == str || "false" == ToLower(str))
*output = false;
else
return false;
return true;
}
std::string StringFromBool(bool value) { std::string StringFromBool(bool value) {
return value ? "True" : "False"; return value ? "True" : "False";
} }

View File

@ -5,8 +5,6 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <iomanip>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
@ -19,42 +17,11 @@ std::string ToLower(std::string str);
/// Make a string uppercase /// Make a string uppercase
std::string ToUpper(std::string str); std::string ToUpper(std::string str);
std::string ArrayToString(const u8* data, std::size_t size, int line_len = 20, bool spaces = true);
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);
// Thousand separator. Turns 12345678 into 12,345,678
template <typename I>
std::string ThousandSeparate(I value, int spaces = 0) {
std::ostringstream oss;
// std::locale("") seems to be broken on many platforms
#if defined _WIN32 || (defined __linux__ && !defined __clang__)
oss.imbue(std::locale(""));
#endif
oss << std::setw(spaces) << value;
return oss.str();
}
std::string StringFromBool(bool value); std::string StringFromBool(bool value);
bool TryParse(const std::string& str, bool* output);
bool TryParse(const std::string& str, u32* output);
template <typename N>
static bool TryParse(const std::string& str, N* const output) {
std::istringstream iss(str);
N tmp = 0;
if (iss >> tmp) {
*output = tmp;
return true;
} else
return false;
}
std::string TabsToSpaces(int tab_size, std::string in); std::string TabsToSpaces(int tab_size, std::string in);
void SplitString(const std::string& str, char delim, std::vector<std::string>& output); void SplitString(const std::string& str, char delim, std::vector<std::string>& output);

View File

@ -165,8 +165,10 @@ void ExtraHID::OnDisconnect() {
void ExtraHID::HandleConfigureHIDPollingRequest(const std::vector<u8>& request) { void ExtraHID::HandleConfigureHIDPollingRequest(const std::vector<u8>& request) {
if (request.size() != 3) { if (request.size() != 3) {
LOG_ERROR(Service_IR, "Wrong request size ({}): {}", request.size(), std::string request_string;
Common::ArrayToString(request.data(), request.size())); for (auto request_part : request)
request_string += fmt::format("{:02x} ", request_part);
LOG_ERROR(Service_IR, "Wrong request size ({}): {}", request.size(), request_string);
return; return;
} }
@ -188,8 +190,11 @@ void ExtraHID::HandleReadCalibrationDataRequest(const std::vector<u8>& request_b
"ReadCalibrationDataRequest has wrong size"); "ReadCalibrationDataRequest has wrong size");
if (request_buf.size() != sizeof(ReadCalibrationDataRequest)) { if (request_buf.size() != sizeof(ReadCalibrationDataRequest)) {
std::string request_buf_string;
for (auto request_buf_part : request_buf)
request_buf_string += fmt::format("{:02x} ", request_buf_part);
LOG_ERROR(Service_IR, "Wrong request size ({}): {}", request_buf.size(), LOG_ERROR(Service_IR, "Wrong request size ({}): {}", request_buf.size(),
Common::ArrayToString(request_buf.data(), request_buf.size())); request_buf_string);
return; return;
} }
@ -223,8 +228,10 @@ void ExtraHID::OnReceive(const std::vector<u8>& data) {
HandleReadCalibrationDataRequest(data); HandleReadCalibrationDataRequest(data);
break; break;
default: default:
LOG_ERROR(Service_IR, "Unknown request: {}", std::string data_string;
Common::ArrayToString(data.data(), data.size())); for (auto data_part : data)
data_string += fmt::format("{:02x} ", data_part);
LOG_ERROR(Service_IR, "Unknown request: {}", data_string);
break; break;
} }
} }

View File

@ -358,9 +358,9 @@ Movie::ValidationResult Movie::ValidateHeader(const CTMHeader& header, u64 progr
return ValidationResult::Invalid; return ValidationResult::Invalid;
} }
std::string revision = std::string revision;
Common::ArrayToString(header.revision.data(), header.revision.size(), 21, false); for (auto header_part : header.revision)
revision = Common::ToLower(revision); revision += fmt::format("{:02x}", header_part);
if (!program_id) if (!program_id)
Core::System::GetInstance().GetAppLoader().ReadProgramId(program_id); Core::System::GetInstance().GetAppLoader().ReadProgramId(program_id);