Skip to content

Commit

Permalink
use some std::min
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Feb 4, 2025
1 parent 96b5189 commit 73885ef
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static int setModeAndPrintStructure(Exiv2::PrintStructureOption option, const st
std::string code = std::string("data:") + ascii.c_str();
size_t length = code.size();
for (size_t start = 0; start < length; start += chunk) {
size_t count = (start + chunk) < length ? chunk : length - start;
auto count = std::min<size_t>(chunk, length - start);
std::cout << code.substr(start, count) << '\n';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ void Jp2Image::printStructure(std::ostream& out, PrintStructureOption option, si
throw Error(ErrorCode::kerInputDataReadFailed);

if (bPrint) {
out << Internal::binaryToString(makeSlice(rawData, 0, rawData.size() > 40 ? 40 : rawData.size()));
out << Internal::binaryToString(makeSlice(rawData, 0, std::min<size_t>(40, rawData.size())));
out.flush();
}
lf(out, bLF);
Expand Down
6 changes: 3 additions & 3 deletions src/jpgimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void JpegBase::printStructure(std::ostream& out, PrintStructureOption option, si
iptcDataSegs.emplace_back(io_->tell() - size, io_->tell());
} else if (bPrint) {
const size_t start = 2;
const size_t end = size > 34 ? 34 : size;
const auto end = std::min<size_t>(34, size);
out << "| ";
if (start < end)
out << Internal::binaryToString(makeSlice(buf, start, end));
Expand Down Expand Up @@ -533,7 +533,7 @@ void JpegBase::printStructure(std::ostream& out, PrintStructureOption option, si
// print COM marker
if (bPrint && marker == com_) {
// size includes 2 for the two bytes for size!
const size_t n = (size - 2) > 32 ? 32 : size - 2;
const auto n = std::min<size_t>(32, size - 2);
// start after the two bytes
out << "| "
<< Internal::binaryToString(makeSlice(buf, 2, n + 2 /* cannot overflow as n is at most size - 2 */));
Expand Down Expand Up @@ -819,7 +819,7 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
throw Error(ErrorCode::kerTooLargeJpegSegment, "IccProfile");
const size_t chunks = 1 + ((size - 1) / chunk_size);
for (size_t chunk = 0; chunk < chunks; chunk++) {
size_t bytes = size > chunk_size ? chunk_size : size; // bytes to write
auto bytes = std::min<size_t>(size, chunk_size); // bytes to write
size -= bytes;

// write JPEG marker (2 bytes)
Expand Down
4 changes: 2 additions & 2 deletions src/pngimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, si

// format output
const int iMax = 30;
const uint32_t blen = dataOffset > iMax ? iMax : dataOffset;
const auto blen = std::min<uint32_t>(iMax, dataOffset);
std::string dataString;
// if blen == 0 => slice construction fails
if (blen > 0) {
Expand Down Expand Up @@ -319,7 +319,7 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, si
DataBuf parsedBuf = PngChunk::readRawProfile(dataBuf, tEXt);
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << Exiv2::Internal::binaryToString(
makeSlice(parsedBuf.c_data(), parsedBuf.size() > 50 ? 50 : parsedBuf.size(), 0))
makeSlice(parsedBuf.c_data(), std::min<size_t>(50, parsedBuf.size()), 0))
<< '\n';
#endif
if (!parsedBuf.empty()) {
Expand Down
5 changes: 2 additions & 3 deletions src/psdimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ void PsdImage::doWriteMetadata(BasicIo& outIo) {
// Copy colorData
size_t readTotal = 0;
while (readTotal < colorDataLength) {
size_t toRead =
(colorDataLength - readTotal) < lbuf.size() ? static_cast<size_t>(colorDataLength) - readTotal : lbuf.size();
auto toRead = std::min<size_t>(colorDataLength - readTotal, lbuf.size());
if (io_->read(lbuf.data(), toRead) != toRead)
throw Error(ErrorCode::kerNotAnImage, "Photoshop");
readTotal += toRead;
Expand Down Expand Up @@ -483,7 +482,7 @@ void PsdImage::doWriteMetadata(BasicIo& outIo) {
readTotal = 0;
while (readTotal < pResourceSize) {
/// \todo almost same code as in lines 403-410. Factor out & reuse!
size_t toRead = (pResourceSize - readTotal) < lbuf.size() ? pResourceSize - readTotal : lbuf.size();
auto toRead = std::min<size_t>(pResourceSize - readTotal, lbuf.size());
if (io_->read(lbuf.data(), toRead) != toRead) {
throw Error(ErrorCode::kerNotAnImage, "Photoshop");
}
Expand Down

0 comments on commit 73885ef

Please sign in to comment.