From a25bbe133f81841b8dc95a5f88593747a8f0c853 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 4 Mar 2022 01:05:46 -0800 Subject: [PATCH] constexpr string conversions Signed-off-by: Rosen Penev --- include/exiv2/basicio.hpp | 4 ++-- src/basicio.cpp | 5 +---- src/epsimage.cpp | 21 +++++++++++---------- src/pngimage.cpp | 17 ++++++++--------- src/value.cpp | 2 +- 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/include/exiv2/basicio.hpp b/include/exiv2/basicio.hpp index 116be90e..198c05cb 100644 --- a/include/exiv2/basicio.hpp +++ b/include/exiv2/basicio.hpp @@ -690,12 +690,12 @@ namespace Exiv2 { @brief The extension of the temporary file which is created when getting input data to read metadata. This file will be deleted in destructor. */ - static const std::string TEMP_FILE_EXT; + static constexpr std::string_view TEMP_FILE_EXT = ".exiv2_temp"; /*! @brief The extension of the generated file which is created when getting input data to add or modify the metadata. */ - static const std::string GEN_FILE_EXT; + static constexpr std::string_view GEN_FILE_EXT = ".exiv2"; //! @name Creators //@{ diff --git a/src/basicio.cpp b/src/basicio.cpp index ee202a2b..921f570e 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -53,7 +53,7 @@ namespace fs = std::filesystem; // class member definitions namespace { /// @brief replace each substring of the subject that matches the given search string with the given replacement. - void ReplaceStringInPlace(std::string& subject, const std::string& search, const std::string& replace) + void ReplaceStringInPlace(std::string& subject, std::string_view search, std::string_view replace) { auto pos = subject.find(search); while (pos != std::string::npos) { @@ -946,9 +946,6 @@ namespace Exiv2 { } #else - const std::string XPathIo::TEMP_FILE_EXT = ".exiv2_temp"; - const std::string XPathIo::GEN_FILE_EXT = ".exiv2"; - XPathIo::XPathIo(const std::string& orgPath) : FileIo(XPathIo::writeDataToFile(orgPath)), isTemp_(true) { tempFilePath_ = path(); diff --git a/src/epsimage.cpp b/src/epsimage.cpp index 9dade980..2e7bcde5 100644 --- a/src/epsimage.cpp +++ b/src/epsimage.cpp @@ -33,7 +33,7 @@ namespace { using Exiv2::byte; // signature of DOS EPS - const std::string dosEpsSignature = "\xC5\xD0\xD3\xC6"; + constexpr auto dosEpsSignature = std::string_view("\xC5\xD0\xD3\xC6"); // first line of EPS constexpr auto epsFirstLine = std::array{ @@ -43,11 +43,12 @@ namespace { }; // blank EPS file - const std::string epsBlank = "%!PS-Adobe-3.0 EPSF-3.0\n" - "%%BoundingBox: 0 0 0 0\n"; + constexpr auto epsBlank = std::string_view( + "%!PS-Adobe-3.0 EPSF-3.0\n" + "%%BoundingBox: 0 0 0 0\n"); // list of all valid XMP headers - const std::string xmpHeaders[] = { + constexpr std::string_view xmpHeaders[] = { // We do not enforce the trailing "?>" here, because the XMP specification // permits additional attributes after begin="..." and id="...". @@ -67,23 +68,23 @@ namespace { // list of all valid XMP trailers struct XmpTrailer { - std::string trailer; + std::string_view trailer; bool readOnly; }; - const XmpTrailer xmpTrailers[] = { + constexpr XmpTrailer xmpTrailers[] = { // We do not enforce the trailing "?>" here, because the XMP specification // permits additional attributes after end="...". {""; + constexpr auto xmpTrailerEnd = std::string_view("?>"); //! Write data into temp file, taking care of errors void writeTemp(BasicIo& tempIo, const byte* data, size_t size) @@ -186,7 +187,7 @@ namespace { for (size_t trailerPos = xmpPos + header.size(); trailerPos < size; trailerPos++) { if (data[xmpPos] != '\x00' && data[xmpPos] != '<') continue; for (auto&& xmpTrailer : xmpTrailers) { - const std::string& trailer = xmpTrailer.trailer; + auto trailer = xmpTrailer.trailer; const bool readOnly = xmpTrailer.readOnly; if (trailerPos + trailer.size() > size) continue; diff --git a/src/pngimage.cpp b/src/pngimage.cpp index 74df305a..952a3d5f 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -206,15 +206,14 @@ namespace Exiv2 { chType[4]=0; if ( option == kpsBasic || option == kpsXMP || option == kpsIccProfile || option == kpsRecursive ) { - - const std::string xmpKey = "XML:com.adobe.xmp"; - const std::string exifKey = "Raw profile type exif"; - const std::string app1Key = "Raw profile type APP1"; - const std::string iptcKey = "Raw profile type iptc"; - const std::string iccKey = "icc"; - const std::string softKey = "Software"; - const std::string commKey = "Comment"; - const std::string descKey = "Description"; + constexpr auto xmpKey = "XML:com.adobe.xmp"; + constexpr auto exifKey = "Raw profile type exif"; + constexpr auto app1Key = "Raw profile type APP1"; + constexpr auto iptcKey = "Raw profile type iptc"; + constexpr auto iccKey = "icc"; + constexpr auto softKey = "Software"; + constexpr auto commKey = "Comment"; + constexpr auto descKey = "Description"; bool bPrint = option == kpsBasic || option == kpsRecursive ; if ( bPrint ) { diff --git a/src/value.cpp b/src/value.cpp index 6b8b20ee..b7edb3b3 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -831,7 +831,7 @@ namespace Exiv2 { return value_.size(); } - static const std::string x_default = "x-default"; + static constexpr auto x_default = "x-default"; std::ostream& LangAltValue::write(std::ostream& os) const {