From 45300ad66723990e76721e2da7a739bf9a1c1348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20D=C3=ADaz=20M=C3=A1s?= Date: Wed, 16 Feb 2022 18:28:54 +0100 Subject: [PATCH] BasicIo::path() returns const ref --- include/exiv2/basicio.hpp | 8 ++++---- src/basicio.cpp | 21 ++++++++++----------- src/tgaimage.cpp | 2 +- test/data/template.exv | Bin 12027 -> 12027 bytes 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/include/exiv2/basicio.hpp b/include/exiv2/basicio.hpp index bd3c7214..8d80c663 100644 --- a/include/exiv2/basicio.hpp +++ b/include/exiv2/basicio.hpp @@ -240,7 +240,7 @@ namespace Exiv2 { comprehensive error messages where only a BasicIo instance is available. */ - virtual std::string path() const =0; + virtual const std::string& path() const noexcept =0; /*! @brief Mark all the bNone blocks to bKnow. This avoids allocating memory @@ -472,7 +472,7 @@ namespace Exiv2 { //! Returns true if the file position has reached the end, otherwise false. bool eof() const override; //! Returns the path of the file - std::string path() const override; + const std::string& path() const noexcept override; /*! @brief Mark all the bNone blocks to bKnow. This avoids allocating memory @@ -654,7 +654,7 @@ namespace Exiv2 { //!Returns true if the IO position has reached the end, otherwise false. bool eof() const override; //! Returns a dummy path, indicating that memory access is used - std::string path() const override; + const std::string& path() const noexcept override; /*! @brief Mark all the bNone blocks to bKnow. This avoids allocating memory @@ -898,7 +898,7 @@ namespace Exiv2 { //!Returns true if the IO position has reached the end, otherwise false. bool eof() const override; //!Returns the URL of the file. - std::string path() const override; + const std::string& path() const noexcept override; /*! @brief Mark all the bNone blocks to bKnow. This avoids allocating memory diff --git a/src/basicio.cpp b/src/basicio.cpp index 4bd2d7e4..062a70db 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -469,10 +469,7 @@ namespace Exiv2 { bool statOk = true; mode_t origStMode = 0; - std::string spf; - char* pf = nullptr; - spf = path(); - pf = const_cast(spf.c_str()); + char* pf = const_cast(path().c_str()); // Get the permissions of the file, or linked-to file, on platforms which have lstat #ifdef EXV_HAVE_LSTAT @@ -720,7 +717,7 @@ namespace Exiv2 { return std::feof(p_->fp_) != 0; } - std::string FileIo::path() const + const std::string& FileIo::path() const noexcept { return p_->path_; } @@ -1044,9 +1041,10 @@ namespace Exiv2 { return p_->eof_; } - std::string MemIo::path() const + const std::string& MemIo::path() const noexcept { - return "MemIo"; + static std::string _path{"MemIo"}; + return _path; } void MemIo::populateFakeData() { @@ -1116,7 +1114,7 @@ namespace Exiv2 { void XPathIo::transfer(BasicIo& src) { if (isTemp_) { // replace temp path to gent path. - std::string currentPath = path(); + auto& currentPath = path(); setPath(ReplaceStringInPlace(currentPath, XPathIo::TEMP_FILE_EXT, XPathIo::GEN_FILE_EXT)); // rename the file tempFilePath_ = path(); @@ -1585,7 +1583,7 @@ namespace Exiv2 { return p_->eof_; } - std::string RemoteIo::path() const + const std::string& RemoteIo::path() const noexcept { return p_->path_; } @@ -1989,8 +1987,9 @@ namespace Exiv2 { return file.write(buf.c_data(), buf.size()); } - std::string ReplaceStringInPlace(std::string subject, const std::string& search, - const std::string& replace) { + /// \todo do it in place! + std::string ReplaceStringInPlace(std::string subject, const std::string& search, const std::string& replace) + { size_t pos = 0; while((pos = subject.find(search, pos)) != std::string::npos) { subject.replace(pos, search.length(), replace); diff --git a/src/tgaimage.cpp b/src/tgaimage.cpp index 4db565ec..f5f9b3ca 100644 --- a/src/tgaimage.cpp +++ b/src/tgaimage.cpp @@ -133,7 +133,7 @@ namespace Exiv2 { bool isTgaType(BasicIo& iIo, bool /*advance*/) { // not all TARGA files have a signature string, so first just try to match the file name extension - std::string path = iIo.path(); + const std::string& path = iIo.path(); if( path.rfind(".tga") != std::string::npos || path.rfind(".TGA") != std::string::npos) { return true; diff --git a/test/data/template.exv b/test/data/template.exv index 8ec5db041f74a1cab9870ebd18bf1521f218909c..9163325146ff137c79f388c2bdebcf4d8a9f12c9 100644 GIT binary patch delta 22 dcmewz`#W|+Jqsr%!wCj+21bU4%^fVoS^#4y2T}k4 delta 22 dcmewz`#W|+Jqzb{h6#)*42%ptn>$#FwE%1)2m$~A