Skip to content

Commit

Permalink
fix toAscii function
Browse files Browse the repository at this point in the history
c == 0 was a dead branch because of the way printable ascii was
calculated. Move it up instead.

While at it, replace std::transform with std::replace. Easier to read.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Jul 14, 2023
1 parent a56bed5 commit 7977215
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ std::string BmffImage::toAscii(uint32_t n) {
std::string result(p, p + 4);
if (!isBigEndianPlatform())
std::reverse(result.begin(), result.end());
std::transform(result.begin(), result.end(), result.begin(), [](char c) {
if (32 <= c && c < 127)
return c; // only allow 7-bit printable ascii
if (c == 0)
return '_'; // show 0 as _
return '.'; // others .
});
// show 0 as _
std::replace(result.begin(), result.end(), '\0', '_');
// show non 7-bit printable ascii as .
std::replace_if(
result.begin(), result.end(), [](char c) { return c < 32 || c > 126; }, '.');
return result;
}

Expand Down

0 comments on commit 7977215

Please sign in to comment.