From 73885efb8e07d9058ccf53206d7ca413c6520387 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Mon, 3 Feb 2025 10:55:44 -0800 Subject: [PATCH] use some std::min Signed-off-by: Rosen Penev --- app/actions.cpp | 2 +- src/jp2image.cpp | 2 +- src/jpgimage.cpp | 6 +++--- src/pngimage.cpp | 4 ++-- src/psdimage.cpp | 5 ++--- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/actions.cpp b/app/actions.cpp index fa9400baa0..3d4f246f94 100644 --- a/app/actions.cpp +++ b/app/actions.cpp @@ -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(chunk, length - start); std::cout << code.substr(start, count) << '\n'; } } diff --git a/src/jp2image.cpp b/src/jp2image.cpp index 342c1d5bdd..08325d14aa 100644 --- a/src/jp2image.cpp +++ b/src/jp2image.cpp @@ -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(40, rawData.size()))); out.flush(); } lf(out, bLF); diff --git a/src/jpgimage.cpp b/src/jpgimage.cpp index 104b205f2f..67d485041e 100644 --- a/src/jpgimage.cpp +++ b/src/jpgimage.cpp @@ -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(34, size); out << "| "; if (start < end) out << Internal::binaryToString(makeSlice(buf, start, end)); @@ -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(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 */)); @@ -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, chunk_size); // bytes to write size -= bytes; // write JPEG marker (2 bytes) diff --git a/src/pngimage.cpp b/src/pngimage.cpp index 58b47fa144..17eddef542 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -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(iMax, dataOffset); std::string dataString; // if blen == 0 => slice construction fails if (blen > 0) { @@ -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(50, parsedBuf.size()), 0)) << '\n'; #endif if (!parsedBuf.empty()) { diff --git a/src/psdimage.cpp b/src/psdimage.cpp index 59c8a9422a..b991055c9d 100644 --- a/src/psdimage.cpp +++ b/src/psdimage.cpp @@ -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(colorDataLength) - readTotal : lbuf.size(); + auto toRead = std::min(colorDataLength - readTotal, lbuf.size()); if (io_->read(lbuf.data(), toRead) != toRead) throw Error(ErrorCode::kerNotAnImage, "Photoshop"); readTotal += toRead; @@ -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(pResourceSize - readTotal, lbuf.size()); if (io_->read(lbuf.data(), toRead) != toRead) { throw Error(ErrorCode::kerNotAnImage, "Photoshop"); }