diff options
| author | Léo Lam <leo@leolam.fr> | 2019-12-30 10:48:11 +0100 |
|---|---|---|
| committer | Léo Lam <leo@leolam.fr> | 2020-01-16 00:22:26 +0100 |
| commit | 89b0ab2d22db510e799e6f9619e8c8e0678f13ef (patch) | |
| tree | bfab21acea4cfb34c2ad7b9543bce92eb5a4ac79 /Source/Core/Common | |
| parent | 1cc7ef356b9a33814efb290368150a1cceb0a3ed (diff) | |
StringUtil: Add IsPrintableCharacter and use it
Add a function that safely returns whether a character is printable
i.e. whether 0x20 <= c <= 0x7e is true.
This is done in several places in our codebase and it's easy to run
into undefined behaviour if the C version defined in <cctype>
is used instead of this one, since its behaviour is undefined
if the character is not representable as an unsigned char.
This fixes MemoryViewWidget.
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/StringUtil.cpp | 2 | ||||
| -rw-r--r-- | Source/Core/Common/StringUtil.h | 8 |
2 files changed, 9 insertions, 1 deletions
diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index 2369208f45..ac7e32efcf 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -71,7 +71,7 @@ std::string HexDump(const u8* data, size_t size) if (row_start + i < size) { char c = static_cast<char>(data[row_start + i]); - out += std::isprint(c, std::locale::classic()) ? c : '.'; + out += IsPrintableCharacter(c) ? c : '.'; } } out += "\n"; diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index c47fd1f620..93199917b9 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -222,3 +222,11 @@ std::string ThousandSeparate(I value, int spaces = 0) return stream.str(); #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()); +} |
