From 21df3ca572a0fad37440b29c56c83d50c45c92f4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 16 May 2023 14:17:54 -0400 Subject: [PATCH] StringUtil: Move IsPrintableCharacter() into Common namespace --- Source/Core/Common/StringUtil.cpp | 2 +- Source/Core/Common/StringUtil.h | 16 ++++++++-------- Source/Core/Core/Core.cpp | 2 +- Source/Core/Core/IOS/ES/Formats.cpp | 4 ++-- Source/Core/Core/IOS/FS/HostBackend/FS.cpp | 5 ++++- Source/Core/Core/IOS/USB/USBV4.cpp | 2 +- Source/Core/DiscIO/VolumeWad.cpp | 2 +- .../Core/DolphinQt/Config/FilesystemWidget.cpp | 2 +- .../Core/DolphinQt/Debugger/MemoryViewWidget.cpp | 4 ++-- 9 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index af81e2b84b..c041164f22 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -77,7 +77,7 @@ std::string HexDump(const u8* data, size_t size) if (row_start + i < size) { char c = static_cast(data[row_start + i]); - out += IsPrintableCharacter(c) ? c : '.'; + out += Common::IsPrintableCharacter(c) ? c : '.'; } } out += "\n"; diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index d2177c2208..9e321ca59a 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -230,14 +230,6 @@ std::string ThousandSeparate(I value, int spaces = 0) #endif } -/// Returns whether a character is printable, i.e. whether 0x20 <= c <= 0x7e is true. -/// Use this instead of calling std::isprint directly to ensure -/// the C locale is being used and to avoid possibly undefined behaviour. -inline bool IsPrintableCharacter(char c) -{ - return std::isprint(c, std::locale::classic()); -} - #ifdef _WIN32 std::vector CommandLineToUtf8Argv(const wchar_t* command_line); #endif @@ -246,6 +238,14 @@ std::string GetEscapedHtml(std::string html); namespace Common { +/// Returns whether a character is printable, i.e. whether 0x20 <= c <= 0x7e is true. +/// Use this instead of calling std::isprint directly to ensure +/// the C locale is being used and to avoid possibly undefined behaviour. +inline bool IsPrintableCharacter(char c) +{ + return std::isprint(c, std::locale::classic()); +} + /// Returns whether a character is a letter, i.e. whether 'a' <= c <= 'z' || 'A' <= c <= 'Z' /// is true. Use this instead of calling std::isalpha directly to ensure /// the C locale is being used and to avoid possibly undefined behaviour. diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index 435b26985d..1e940d1fed 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -193,7 +193,7 @@ void DisplayMessage(std::string message, int time_in_ms) return; // Actually displaying non-ASCII could cause things to go pear-shaped - if (!std::all_of(message.begin(), message.end(), IsPrintableCharacter)) + if (!std::all_of(message.begin(), message.end(), Common::IsPrintableCharacter)) return; OSD::AddMessage(std::move(message), time_in_ms); diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index 60749b70c9..fe5b7d38f2 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -302,7 +302,7 @@ std::string TMDReader::GetGameID() const std::memcpy(game_id, m_bytes.data() + offsetof(TMDHeader, title_id) + 4, 4); std::memcpy(game_id + 4, m_bytes.data() + offsetof(TMDHeader, group_id), 2); - if (std::all_of(std::begin(game_id), std::end(game_id), IsPrintableCharacter)) + if (std::all_of(std::begin(game_id), std::end(game_id), Common::IsPrintableCharacter)) return std::string(game_id, sizeof(game_id)); return fmt::format("{:016x}", GetTitleId()); @@ -313,7 +313,7 @@ std::string TMDReader::GetGameTDBID() const const u8* begin = m_bytes.data() + offsetof(TMDHeader, title_id) + 4; const u8* end = begin + 4; - if (std::all_of(begin, end, IsPrintableCharacter)) + if (std::all_of(begin, end, Common::IsPrintableCharacter)) return std::string(begin, end); return fmt::format("{:016x}", GetTitleId()); diff --git a/Source/Core/Core/IOS/FS/HostBackend/FS.cpp b/Source/Core/Core/IOS/FS/HostBackend/FS.cpp index 991f1353d2..f09cadb7c8 100644 --- a/Source/Core/Core/IOS/FS/HostBackend/FS.cpp +++ b/Source/Core/Core/IOS/FS/HostBackend/FS.cpp @@ -474,8 +474,11 @@ ResultCode HostFileSystem::Format(Uid uid) ResultCode HostFileSystem::CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path, FileAttribute attr, Modes modes, bool is_file) { - if (!IsValidNonRootPath(path) || !std::all_of(path.begin(), path.end(), IsPrintableCharacter)) + if (!IsValidNonRootPath(path) || + !std::all_of(path.begin(), path.end(), Common::IsPrintableCharacter)) + { return ResultCode::Invalid; + } if (!is_file && std::count(path.begin(), path.end(), '/') > int(MaxPathDepth)) return ResultCode::TooManyPathComponents; diff --git a/Source/Core/Core/IOS/USB/USBV4.cpp b/Source/Core/Core/IOS/USB/USBV4.cpp index 26514f412c..6c8f58ec2b 100644 --- a/Source/Core/Core/IOS/USB/USBV4.cpp +++ b/Source/Core/Core/IOS/USB/USBV4.cpp @@ -87,7 +87,7 @@ void V4GetUSStringMessage::OnTransferComplete(s32 return_value) const auto& memory = system.GetMemory(); std::string message = memory.GetString(data_address); - std::replace_if(message.begin(), message.end(), std::not_fn(IsPrintableCharacter), '?'); + std::replace_if(message.begin(), message.end(), std::not_fn(Common::IsPrintableCharacter), '?'); memory.CopyToEmu(data_address, message.c_str(), message.size()); TransferCommand::OnTransferComplete(return_value); } diff --git a/Source/Core/DiscIO/VolumeWad.cpp b/Source/Core/DiscIO/VolumeWad.cpp index 81e2912671..4058497546 100644 --- a/Source/Core/DiscIO/VolumeWad.cpp +++ b/Source/Core/DiscIO/VolumeWad.cpp @@ -256,7 +256,7 @@ std::string VolumeWAD::GetMakerID(const Partition& partition) const return "00"; // Some weird channels use 0x0000 in place of the MakerID, so we need a check here - if (!IsPrintableCharacter(temp[0]) || !IsPrintableCharacter(temp[1])) + if (!Common::IsPrintableCharacter(temp[0]) || !Common::IsPrintableCharacter(temp[1])) return "00"; return DecodeString(temp); diff --git a/Source/Core/DolphinQt/Config/FilesystemWidget.cpp b/Source/Core/DolphinQt/Config/FilesystemWidget.cpp index 215161c585..7dbf0014ea 100644 --- a/Source/Core/DolphinQt/Config/FilesystemWidget.cpp +++ b/Source/Core/DolphinQt/Config/FilesystemWidget.cpp @@ -168,7 +168,7 @@ void FilesystemWidget::PopulateDirectory(int partition_id, QStandardItem* root, for (u32 i = 0; i < 4; i++) { char c = static_cast(title_id.value() >> 8 * (3 - i)); - if (IsPrintableCharacter(c)) + if (Common::IsPrintableCharacter(c)) text += QLatin1Char(c); else text += QLatin1Char('.'); diff --git a/Source/Core/DolphinQt/Debugger/MemoryViewWidget.cpp b/Source/Core/DolphinQt/Debugger/MemoryViewWidget.cpp index 9d727fee10..519dea6ebe 100644 --- a/Source/Core/DolphinQt/Debugger/MemoryViewWidget.cpp +++ b/Source/Core/DolphinQt/Debugger/MemoryViewWidget.cpp @@ -495,8 +495,8 @@ QString MemoryViewWidget::ValueToString(const Core::CPUThreadGuard& guard, u32 a case Type::ASCII: { const char value = accessors->ReadU8(guard, address); - return IsPrintableCharacter(value) ? QString{QChar::fromLatin1(value)} : - QString{QChar::fromLatin1('.')}; + return Common::IsPrintableCharacter(value) ? QString{QChar::fromLatin1(value)} : + QString{QChar::fromLatin1('.')}; } case Type::Hex16: {