Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize toAscii (backport #2693) #3049

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ BmffImage::BmffImage(BasicIo::UniquePtr io, bool /* create */, size_t max_box_de

std::string BmffImage::toAscii(uint32_t n) {
const auto p = reinterpret_cast<const char*>(&n);
std::string result(4, '.');
std::transform(p, p + 4, 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 .
});
std::string result(p, p + 4);
if (!isBigEndianPlatform())
std::reverse(result.begin(), result.end());
// 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
Loading