StringUtil: Move IsPrintableCharacter() into Common namespace

This commit is contained in:
Lioncash 2023-05-16 14:17:54 -04:00
parent a9f1edeb61
commit 21df3ca572
9 changed files with 21 additions and 18 deletions

View File

@ -77,7 +77,7 @@ std::string HexDump(const u8* data, size_t size)
if (row_start + i < size) if (row_start + i < size)
{ {
char c = static_cast<char>(data[row_start + i]); char c = static_cast<char>(data[row_start + i]);
out += IsPrintableCharacter(c) ? c : '.'; out += Common::IsPrintableCharacter(c) ? c : '.';
} }
} }
out += "\n"; out += "\n";

View File

@ -230,14 +230,6 @@ std::string ThousandSeparate(I value, int spaces = 0)
#endif #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 #ifdef _WIN32
std::vector<std::string> CommandLineToUtf8Argv(const wchar_t* command_line); std::vector<std::string> CommandLineToUtf8Argv(const wchar_t* command_line);
#endif #endif
@ -246,6 +238,14 @@ std::string GetEscapedHtml(std::string html);
namespace Common 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' /// 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 /// is true. Use this instead of calling std::isalpha directly to ensure
/// the C locale is being used and to avoid possibly undefined behaviour. /// the C locale is being used and to avoid possibly undefined behaviour.

View File

@ -193,7 +193,7 @@ void DisplayMessage(std::string message, int time_in_ms)
return; return;
// Actually displaying non-ASCII could cause things to go pear-shaped // 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; return;
OSD::AddMessage(std::move(message), time_in_ms); OSD::AddMessage(std::move(message), time_in_ms);

View File

@ -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, m_bytes.data() + offsetof(TMDHeader, title_id) + 4, 4);
std::memcpy(game_id + 4, m_bytes.data() + offsetof(TMDHeader, group_id), 2); 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 std::string(game_id, sizeof(game_id));
return fmt::format("{:016x}", GetTitleId()); 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* begin = m_bytes.data() + offsetof(TMDHeader, title_id) + 4;
const u8* end = begin + 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 std::string(begin, end);
return fmt::format("{:016x}", GetTitleId()); return fmt::format("{:016x}", GetTitleId());

View File

@ -474,8 +474,11 @@ ResultCode HostFileSystem::Format(Uid uid)
ResultCode HostFileSystem::CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path, ResultCode HostFileSystem::CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path,
FileAttribute attr, Modes modes, bool is_file) 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; return ResultCode::Invalid;
}
if (!is_file && std::count(path.begin(), path.end(), '/') > int(MaxPathDepth)) if (!is_file && std::count(path.begin(), path.end(), '/') > int(MaxPathDepth))
return ResultCode::TooManyPathComponents; return ResultCode::TooManyPathComponents;

View File

@ -87,7 +87,7 @@ void V4GetUSStringMessage::OnTransferComplete(s32 return_value) const
auto& memory = system.GetMemory(); auto& memory = system.GetMemory();
std::string message = memory.GetString(data_address); 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()); memory.CopyToEmu(data_address, message.c_str(), message.size());
TransferCommand::OnTransferComplete(return_value); TransferCommand::OnTransferComplete(return_value);
} }

View File

@ -256,7 +256,7 @@ std::string VolumeWAD::GetMakerID(const Partition& partition) const
return "00"; return "00";
// Some weird channels use 0x0000 in place of the MakerID, so we need a check here // 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 "00";
return DecodeString(temp); return DecodeString(temp);

View File

@ -168,7 +168,7 @@ void FilesystemWidget::PopulateDirectory(int partition_id, QStandardItem* root,
for (u32 i = 0; i < 4; i++) for (u32 i = 0; i < 4; i++)
{ {
char c = static_cast<char>(title_id.value() >> 8 * (3 - i)); char c = static_cast<char>(title_id.value() >> 8 * (3 - i));
if (IsPrintableCharacter(c)) if (Common::IsPrintableCharacter(c))
text += QLatin1Char(c); text += QLatin1Char(c);
else else
text += QLatin1Char('.'); text += QLatin1Char('.');

View File

@ -495,8 +495,8 @@ QString MemoryViewWidget::ValueToString(const Core::CPUThreadGuard& guard, u32 a
case Type::ASCII: case Type::ASCII:
{ {
const char value = accessors->ReadU8(guard, address); const char value = accessors->ReadU8(guard, address);
return IsPrintableCharacter(value) ? QString{QChar::fromLatin1(value)} : return Common::IsPrintableCharacter(value) ? QString{QChar::fromLatin1(value)} :
QString{QChar::fromLatin1('.')}; QString{QChar::fromLatin1('.')};
} }
case Type::Hex16: case Type::Hex16:
{ {