diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c6c3f9e1..3a300ff9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,7 @@ add_library( exiv2lib_int OBJECT tifffwd_int.hpp timegm.h unused.h + utils.cpp utils.hpp ) set(PUBLIC_HEADERS diff --git a/src/epsimage.cpp b/src/epsimage.cpp index e5de6bb3..68d2b36b 100644 --- a/src/epsimage.cpp +++ b/src/epsimage.cpp @@ -33,6 +33,7 @@ #include "error.hpp" #include "futils.hpp" #include "version.hpp" +#include "utils.hpp" // + standard includes #include @@ -48,6 +49,7 @@ namespace { using namespace Exiv2; + using namespace Util; using Exiv2::byte; // signature of DOS EPS @@ -134,12 +136,6 @@ namespace { return static_cast(pos); } - //! Check whether a string has a certain beginning - bool startsWith(const std::string& s, const std::string& start) - { - return s.size() >= start.size() && memcmp(s.data(), start.data(), start.size()) == 0; - } - //! Check whether a string contains only white space characters bool onlyWhitespaces(const std::string& s) { diff --git a/src/makernote_int.cpp b/src/makernote_int.cpp index 1598503a..f101b4ec 100644 --- a/src/makernote_int.cpp +++ b/src/makernote_int.cpp @@ -32,6 +32,7 @@ #include "tiffvisitor_int.hpp" #include "tiffimage.hpp" #include "tiffimage_int.hpp" +#include "utils.hpp" // + standard includes #include @@ -1080,14 +1081,6 @@ namespace Exiv2 { #define NA ((uint32_t)-1) - // TODO: When moving to C++20, this can be replace with - // std::basic_string::starts_with() . - // Suggested in https://github.com/Exiv2/exiv2/pull/1777 . - bool startsWith(const std::string& s, const std::string& start) - { - return s.size() >= start.size() && std::memcmp(s.data(), start.data(), start.size()) == 0; - } - //! Nikon binary array version lookup table constexpr NikonArrayIdx nikonArrayIdx[] = { // NikonSi @@ -1203,7 +1196,7 @@ namespace Exiv2 { // Not valid for models beginning std::string model = getExifModel(pRoot); for (auto& m : { "SLT-", "HV", "ILCA-" }) { - if (startsWith(model, m)) + if (Util::startsWith(model, m)) return -1; } return 0; diff --git a/src/sonymn_int.cpp b/src/sonymn_int.cpp index 4719892c..8a2e7d59 100644 --- a/src/sonymn_int.cpp +++ b/src/sonymn_int.cpp @@ -27,6 +27,7 @@ #include "value.hpp" #include "exif.hpp" #include "i18n.h" // NLS support. +#include "utils.hpp" // + standard includes #include @@ -864,7 +865,7 @@ namespace Exiv2 { // Ranges of models that do not support this tag std::string model = pos->toString(); for (auto& m : { "DSC-", "Stellar" }) { - if (startsWith(model, m)) { + if (Util::startsWith(model, m)) { os << N_("n/a"); return os; } @@ -955,11 +956,6 @@ namespace Exiv2 { return tagInfo2010e_; } - bool SonyMakerNote::startsWith(const std::string& s, const std::string& start) - { - return s.size() >= start.size() && std::memcmp(s.data(), start.data(), start.size()) == 0; - } - // https://github.com/Exiv2/exiv2/pull/906#issuecomment-504338797 static DataBuf sonyTagCipher(uint16_t /* tag */, const byte* bytes, uint32_t size, TiffComponent* const /*object*/, bool bDecipher) { diff --git a/src/sonymn_int.hpp b/src/sonymn_int.hpp index 58768fe7..877e2b84 100644 --- a/src/sonymn_int.hpp +++ b/src/sonymn_int.hpp @@ -76,10 +76,6 @@ namespace Exiv2 { static const TagInfo tagInfoSonyMisc1_[]; static const TagInfo tagInfo2010e_[]; - // TODO: When moving to C++20, this can be replace with - // std::basic_string::starts_with() . - // Suggested in https://github.com/Exiv2/exiv2/pull/1777 . - static bool startsWith(const std::string& s, const std::string& start); }; // class SonyMakerNote DataBuf sonyTagDecipher(uint16_t, const byte*, uint32_t, TiffComponent* const); diff --git a/src/utils.cpp b/src/utils.cpp index 3dae2ee8..0e29750b 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -119,4 +119,10 @@ namespace Util { } } + bool startsWith(const std::string& s, const std::string& start) + { + return s.size() >= start.size() && std::memcmp(s.data(), start.data(), start.size()) == 0; + } + + } // namespace Util diff --git a/src/utils.hpp b/src/utils.hpp index 3a23099a..22beeb57 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -77,6 +77,15 @@ namespace Util { */ void replace(std::string& text, const std::string& searchText, const std::string& replaceText); + // TODO: When using C++20, replace with std::basic_string::starts_with() + // (suggested in https://github.com/Exiv2/exiv2/pull/1777). + /*! + @brief Tests if \em start occurs at the beginning of \em s. + Returns true if found, else false. + When Exiv2 uses C++20, this will be replaced with + std::basic_string::starts_with(). + */ + bool startsWith(const std::string& s, const std::string& start); } // namespace Util #endif // #ifndef UTILS_HPP_