Move all `startsWith()` funcs to `Util` namespace

The function checks if a string is in the beginning of another. This
is intended for use until the move to C++20 when
`std::basic_string<>::starts_with()` becomes available. Suggested in
https://github.com/Exiv2/exiv2/pull/1777 .

Separate identical definitions have been moved into a single copy.
main
postscript-dev 4 years ago
parent 0827a76b6a
commit 510a38e27d
No known key found for this signature in database
GPG Key ID: F3EC02A099292862

@ -38,6 +38,7 @@ add_library( exiv2lib_int OBJECT
tifffwd_int.hpp
timegm.h
unused.h
utils.cpp utils.hpp
)
set(PUBLIC_HEADERS

@ -33,6 +33,7 @@
#include "error.hpp"
#include "futils.hpp"
#include "version.hpp"
#include "utils.hpp"
// + standard includes
#include <algorithm>
@ -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<uint32_t>(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)
{

@ -32,6 +32,7 @@
#include "tiffvisitor_int.hpp"
#include "tiffimage.hpp"
#include "tiffimage_int.hpp"
#include "utils.hpp"
// + standard includes
#include <string>
@ -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<CharT,Traits,Allocator>::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;

@ -27,6 +27,7 @@
#include "value.hpp"
#include "exif.hpp"
#include "i18n.h" // NLS support.
#include "utils.hpp"
// + standard includes
#include <string>
@ -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)
{

@ -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<CharT,Traits,Allocator>::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);

@ -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

@ -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<CharT,Traits,Allocator>::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<CharT,Traits,Allocator>::starts_with().
*/
bool startsWith(const std::string& s, const std::string& start);
} // namespace Util
#endif // #ifndef UTILS_HPP_

Loading…
Cancel
Save