Skip to content

Commit

Permalink
get rid of startsWith
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
neheb committed Jun 6, 2023
1 parent f6172e6 commit 85c9092
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 56 deletions.
34 changes: 17 additions & 17 deletions src/epsimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,21 +377,21 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
bool significantLine = true;
#endif
// nested documents
if (posPage == posEndEps && (startsWith(line, "%%IncludeDocument:") || startsWith(line, "%%BeginDocument:"))) {
if (posPage == posEndEps && (line.starts_with("%%IncludeDocument:") || line.starts_with("%%BeginDocument:"))) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Nested document at invalid position: " << startPos << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
if (startsWith(line, "%%BeginDocument:")) {
if (line.starts_with("%%BeginDocument:")) {
if (depth == maxDepth) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Document too deeply nested at position: " << startPos << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
depth++;
} else if (startsWith(line, "%%EndDocument")) {
} else if (line.starts_with("%%EndDocument")) {
if (depth == 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unmatched EndDocument at position: " << startPos << "\n";
Expand All @@ -413,17 +413,17 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
if (depth != 0)
continue;
// explicit "Begin" comments
if (startsWith(line, "%%BeginPreview:")) {
if (line.starts_with("%%BeginPreview:")) {
inDefaultsPreviewPrologSetup = true;
} else if (line == "%%BeginDefaults") {
inDefaultsPreviewPrologSetup = true;
} else if (line == "%%BeginProlog") {
inDefaultsPreviewPrologSetup = true;
} else if (line == "%%BeginSetup") {
inDefaultsPreviewPrologSetup = true;
} else if (posPage == posEndEps && startsWith(line, "%%Page:")) {
} else if (posPage == posEndEps && line.starts_with("%%Page:")) {
posPage = startPos;
} else if (posPage != posEndEps && startsWith(line, "%%Page:")) {
} else if (posPage != posEndEps && line.starts_with("%%Page:")) {
if (implicitPage) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Page at position " << startPos << " conflicts with implicit page at position: " << posPage
Expand Down Expand Up @@ -528,27 +528,27 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
#endif
}
// remaining explicit comments
if (posEndComments == posEndEps && posLanguageLevel == posEndEps && startsWith(line, "%%LanguageLevel:")) {
if (posEndComments == posEndEps && posLanguageLevel == posEndEps && line.starts_with("%%LanguageLevel:")) {
posLanguageLevel = startPos;
} else if (posEndComments == posEndEps && posContainsXmp == posEndEps && startsWith(line, "%ADO_ContainsXMP:")) {
} else if (posEndComments == posEndEps && posContainsXmp == posEndEps && line.starts_with("%ADO_ContainsXMP:")) {
posContainsXmp = startPos;
} else if (posEndComments == posEndEps && posPages == posEndEps && startsWith(line, "%%Pages:")) {
} else if (posEndComments == posEndEps && posPages == posEndEps && line.starts_with("%%Pages:")) {
posPages = startPos;
} else if (posEndComments == posEndEps && posExiv2Version == posEndEps && startsWith(line, "%Exiv2Version:")) {
} else if (posEndComments == posEndEps && posExiv2Version == posEndEps && line.starts_with("%Exiv2Version:")) {
posExiv2Version = startPos;
} else if (posEndComments == posEndEps && posExiv2Website == posEndEps && startsWith(line, "%Exiv2Website:")) {
} else if (posEndComments == posEndEps && posExiv2Website == posEndEps && line.starts_with("%Exiv2Website:")) {
posExiv2Website = startPos;
} else if (posEndComments == posEndEps && startsWith(line, "%%Creator: Adobe Illustrator") &&
} else if (posEndComments == posEndEps && line.starts_with("%%Creator: Adobe Illustrator") &&
firstLine == "%!PS-Adobe-3.0 EPSF-3.0") {
illustrator8 = true;
} else if (posEndComments == posEndEps && startsWith(line, "%AI7_Thumbnail:")) {
} else if (posEndComments == posEndEps && line.starts_with("%AI7_Thumbnail:")) {
posAi7Thumbnail = startPos;
} else if (posEndComments == posEndEps && posAi7Thumbnail != posEndEps && posAi7ThumbnailEndData == posEndEps &&
line == "%%EndData") {
posAi7ThumbnailEndData = startPos;
} else if (posEndComments == posEndEps && line == "%%EndComments") {
posEndComments = startPos;
} else if (inDefaultsPreviewPrologSetup && startsWith(line, "%%BeginResource: procset wCorel")) {
} else if (inDefaultsPreviewPrologSetup && line.starts_with("%%BeginResource: procset wCorel")) {
corelDraw = true;
} else if (line == "%%EndPreview") {
inDefaultsPreviewPrologSetup = false;
Expand All @@ -562,7 +562,7 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
posEndPageSetup = startPos;
} else if (posPageTrailer == posEndEps && line == "%%PageTrailer") {
posPageTrailer = startPos;
} else if (posBeginPhotoshop == posEndEps && startsWith(line, "%BeginPhotoshop:")) {
} else if (posBeginPhotoshop == posEndEps && line.starts_with("%BeginPhotoshop:")) {
posBeginPhotoshop = pos;
} else if (posBeginPhotoshop != posEndEps && posEndPhotoshop == posEndEps && line == "%EndPhotoshop") {
posEndPhotoshop = startPos;
Expand Down Expand Up @@ -670,7 +670,7 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
EXV_DEBUG << "readWriteEpsMetadata: Using flexible XMP embedding\n";
#endif
const size_t posBeginXmlPacket = readPrevLine(line, data, xmpPos, posEndEps);
if (startsWith(line, "%begin_xml_packet:")) {
if (line.starts_with("%begin_xml_packet:")) {
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: XMP embedding contains %begin_xml_packet\n";
#endif
Expand Down Expand Up @@ -898,7 +898,7 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
writeTemp(tempIo, "%%EndComments" + lineEnding);
}
}
if (pos == posPage && !startsWith(line, "%%Page:")) {
if (pos == posPage && !line.starts_with("%%Page:")) {
writeTemp(tempIo, "%%Page: 1 1" + lineEnding);
writeTemp(tempIo, "%%EndPageComments" + lineEnding);
}
Expand Down
2 changes: 1 addition & 1 deletion src/futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Protocol fileProtocol(const std::string& path) {
if (result != pFile)
break;

if (Exiv2::Internal::startsWith(path, prot.name))
if (path.starts_with(prot.name))
// URL's require data. Stdin == "-" and no further data
if (prot.isUrl ? path.size() > prot.name.size() : path.size() == prot.name.size())
result = prot.prot;
Expand Down
3 changes: 1 addition & 2 deletions src/jpgimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ using ULONG = uint32_t;
namespace Exiv2 {

using Exiv2::Internal::enforce;
using Exiv2::Internal::startsWith;
namespace {
// JPEG Segment markers (The first byte is always 0xFF, the value of these constants correspond to the 2nd byte)
constexpr byte sos_ = 0xda; //!< JPEG SOS marker
Expand Down Expand Up @@ -374,7 +373,7 @@ void JpegBase::printStructure(std::ostream& out, PrintStructureOption option, si
// 2 | 0xe1 APP1 | 911 | Exif..MM.*.......%.........#....
// 915 | 0xe1 APP1 | 870 | http://ns.adobe.com/xap/1.0/.<x:
// 1787 | 0xe1 APP1 | 65460 | http://ns.adobe.com/xmp/extensio
if (option == kpsXMP && startsWith(signature, "http://ns.adobe.com/x")) {
if (option == kpsXMP && signature.starts_with("http://ns.adobe.com/x")) {
// extract XMP
const char* xmp = buf.c_str();
size_t start = 2;
Expand Down
2 changes: 1 addition & 1 deletion src/makernote_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ int sony2FpSelector(uint16_t /*tag*/, const byte* /*pData*/, size_t /*size*/, Ti
// Not valid for models beginning
std::string model = getExifModel(pRoot);
const std::array strs{"SLT-", "HV", "ILCA-"};
return std::any_of(strs.begin(), strs.end(), [&model](auto& m) { return startsWith(model, m); }) ? -1 : 0;
return std::any_of(strs.begin(), strs.end(), [&model](auto& m) { return model.starts_with(m); }) ? -1 : 0;
}

int sonyMisc2bSelector(uint16_t /*tag*/, const byte* /*pData*/, size_t /*size*/, TiffComponent* pRoot) {
Expand Down
8 changes: 4 additions & 4 deletions src/pentaxmn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ static std::ostream& resolveLens0x8ff(std::ostream& os, const Value& value, cons
const auto lensInfo = findLensInfo(metadata);
if (value.count() == 4) {
std::string model = getKeyString("Exif.Image.Model", metadata);
if (startsWith(model, "PENTAX K-3") && lensInfo->count() == 128 && lensInfo->toUint32(1) == 168 &&
if (model.starts_with("PENTAX K-3") && lensInfo->count() == 128 && lensInfo->toUint32(1) == 168 &&
lensInfo->toUint32(2) == 144)
index = 7;
}
Expand All @@ -1203,15 +1203,15 @@ static std::ostream& resolveLens0x319(std::ostream& os, const Value& value, cons
const auto lensInfo = findLensInfo(metadata);
if (value.count() == 4) {
std::string model = getKeyString("Exif.Image.Model", metadata);
if (startsWith(model, "PENTAX K-3") && lensInfo->count() == 128 && lensInfo->toUint32(1) == 131 &&
if (model.starts_with("PENTAX K-3") && lensInfo->count() == 128 && lensInfo->toUint32(1) == 131 &&
lensInfo->toUint32(2) == 128)
index = 6;
}
if (value.count() == 2) {
std::string model = getKeyString("Exif.Image.Model", metadata);
if (startsWith(model, "PENTAX K100D") && lensInfo->count() == 44)
if (model.starts_with("PENTAX K100D") && lensInfo->count() == 44)
index = 6;
if (startsWith(model, "PENTAX *ist DL") && lensInfo->count() == 36)
if (model.starts_with("PENTAX *ist DL") && lensInfo->count() == 36)
index = 6;
}

Expand Down
36 changes: 18 additions & 18 deletions src/sonymn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ std::ostream& SonyMakerNote::printFocusMode2(std::ostream& os, const Value& valu
const auto v0 = value.toUint32(0);

constexpr std::array models{"DSC-RX10M4", "DSC-RX100M6", "DSC-RX100M7", "DSC-RX100M5A", "DSC-HX99", "DSC-RX0M2"};
if (!startsWith(model, "DSC-") ||
std::any_of(models.begin(), models.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (!model.starts_with("DSC-") ||
std::any_of(models.begin(), models.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG(sonyFocusMode2)(os, v0, metadata);
return os;
}
Expand All @@ -869,19 +869,19 @@ std::ostream& SonyMakerNote::printAFAreaModeSetting(std::ostream& os, const Valu
const auto v0 = value.toUint32(0);

constexpr std::array models1{"SLT-", "HV"};
if (std::any_of(models1.begin(), models1.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::any_of(models1.begin(), models1.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG(sonyAFAreaModeSettingSet1)(os, v0, metadata);
return os;
}

constexpr std::array models2{"NEX-", "ILCE-", "ILME-", "DSC-RX10M4", "DSC-RX100M6",
"DSC-RX100M7", "DSC-RX100M5A", "DSC-HX99", "DSC-RX0M2"};
if (std::any_of(models2.begin(), models2.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::any_of(models2.begin(), models2.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG(sonyAFAreaModeSettingSet2)(os, v0, metadata);
return os;
}

if (startsWith(model, "ILCA-")) {
if (model.starts_with("ILCA-")) {
EXV_PRINT_TAG(sonyAFAreaModeSettingSet3)(os, v0, metadata);
return os;
}
Expand All @@ -906,7 +906,7 @@ std::ostream& SonyMakerNote::printFlexibleSpotPosition(std::ostream& os, const V

constexpr std::array models{"NEX-", "ILCE-", "ILME-", "DSC-RX10M4", "DSC-RX100M6",
"DSC-RX100M7", "DSC-RX100M5A", "DSC-HX99", "DSC-RX0M2"};
if (std::any_of(models.begin(), models.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::any_of(models.begin(), models.end(), [&model](auto& m) { return model.starts_with(m); })) {
os << value.toUint32(0) << ", " << value.toUint32(1);
return os;
}
Expand Down Expand Up @@ -936,29 +936,29 @@ std::ostream& SonyMakerNote::printAFPointSelected(std::ostream& os, const Value&
constexpr std::array models3{"ILCA-68", "ILCA-77M2"};
constexpr std::array models4{"NEX-", "ILCE-", "ILME-"};

if (std::any_of(models1.begin(), models1.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::any_of(models1.begin(), models1.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG(sonyAFPointSelectedSet1)(os, value.toUint32(0), metadata);
return os;
}
if (std::any_of(models2.begin(), models2.end(), [&model](auto& m) { return startsWith(model, m); }) && status &&
if (std::any_of(models2.begin(), models2.end(), [&model](auto& m) { return model.starts_with(m); }) && status &&
aFAreaModeSetting == 4) {
EXV_PRINT_TAG(sonyAFPointSelectedSet1)(os, value.toUint32(0), metadata);
return os;
}
if (std::any_of(models3.begin(), models3.end(), [&model](auto& m) { return startsWith(model, m); }) && status &&
if (std::any_of(models3.begin(), models3.end(), [&model](auto& m) { return model.starts_with(m); }) && status &&
aFAreaModeSetting != 8) {
EXV_PRINT_TAG(sonyAFPointSelectedSet2)(os, value, metadata);
return os;
}
if (startsWith(model, "ILCA-99M2") && status && aFAreaModeSetting != 8) {
if (model.starts_with("ILCA-99M2") && status && aFAreaModeSetting != 8) {
EXV_PRINT_TAG(sonyAFPointSelectedSet3)(os, value, metadata);
return os;
}
if (startsWith(model, "ILCA-") && status && aFAreaModeSetting == 8) {
if (model.starts_with("ILCA-") && status && aFAreaModeSetting == 8) {
EXV_PRINT_TAG(sonyAFPointSelectedSet4)(os, value.toUint32(0), metadata);
return os;
}
if (std::any_of(models4.begin(), models4.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::any_of(models4.begin(), models4.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG(sonyAFPointSelectedSet5)(os, value.toUint32(0), metadata);
return os;
}
Expand All @@ -981,11 +981,11 @@ std::ostream& SonyMakerNote::printAFPointsUsed(std::ostream& os, const Value& va
constexpr std::array models1{"ILCA-", "DSC-"};
constexpr std::array models2{"ILCA-68", "ILCA-77M2"};

if (std::none_of(models1.begin(), models1.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::none_of(models1.begin(), models1.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG_BITLIST_ALL_LE(sonyAFPointsUsedSet1)(os, value, metadata);
return os;
}
if (std::any_of(models2.begin(), models2.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (std::any_of(models2.begin(), models2.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG_BITLIST_ALL_LE(sonyAFPointsUsedSet2)(os, value, metadata);
return os;
}
Expand All @@ -1008,8 +1008,8 @@ std::ostream& SonyMakerNote::printAFTracking(std::ostream& os, const Value& valu
}

constexpr std::array models{"DSC-RX10M4", "DSC-RX100M6", "DSC-RX100M7", "DSC-RX100M5A", "DSC-HX99", "DSC-RX0M2"};
if (!startsWith(model, "DSC-") ||
std::any_of(models.begin(), models.end(), [&model](auto& m) { return startsWith(model, m); })) {
if (!model.starts_with("DSC-") ||
std::any_of(models.begin(), models.end(), [&model](auto& m) { return model.starts_with(m); })) {
EXV_PRINT_TAG(sonyAFTracking)(os, value.toUint32(0), metadata);
return os;
}
Expand Down Expand Up @@ -1366,7 +1366,7 @@ std::ostream& SonyMakerNote::printHighISONoiseReduction2(std::ostream& os, const
return os;
}

if (startsWith(model, "DSC-") || startsWith(model, "Stellar")) {
if (model.starts_with("DSC-") || model.starts_with("Stellar")) {
EXV_PRINT_TAG(sonyHighISONoiseReduction2)(os, value.toUint32(0), metadata);
return os;
}
Expand Down Expand Up @@ -1851,7 +1851,7 @@ std::ostream& SonyMakerNote::printSony2FpFocusPosition2(std::ostream& os, const

// Ranges of models that do not support this tag
for (const auto& m : {"DSC-", "Stellar"}) {
if (startsWith(model, m)) {
if (model.starts_with(m)) {
os << N_("n/a");
return os;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tiffcomposite_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ size_t fillGap(Exiv2::Internal::IoWrapper& ioWrapper, size_t curr, size_t tobe);
// class member definitions
namespace Exiv2::Internal {
bool TiffMappingInfo::operator==(const TiffMappingInfo::Key& key) const {
return (0 == strcmp("*", make_) || startsWith(key.m_, make_)) &&
return (0 == strcmp("*", make_) || key.m_.starts_with(make_)) &&
(Tag::all == extendedTag_ || key.e_ == extendedTag_) && key.g_ == group_;
}

Expand Down
9 changes: 0 additions & 9 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@

namespace Exiv2::Internal {

template <typename T>
constexpr bool startsWith(std::string_view s, T start) {
#ifdef __cpp_lib_starts_ends_with
return s.starts_with(start);
#else
return s.find(start) == 0;
#endif
}

template <typename T>
constexpr bool contains(std::string_view s, T c) {
#ifdef __cpp_lib_string_contains
Expand Down
2 changes: 1 addition & 1 deletion src/xmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ void XmpData::eraseFamily(XmpData::iterator& pos) {
std::string key(pos->key());
std::vector<std::string> keys;
while (pos != xmpMetadata_.end()) {
if (!Exiv2::Internal::startsWith(pos->key(), key))
if (!pos->key().starts_with(key))
break;
keys.push_back(pos->key());
pos++;
Expand Down
6 changes: 4 additions & 2 deletions unitTests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
using namespace Exiv2::Internal;

TEST(stringUtils, startsWithReturnsTrue) {
ASSERT_TRUE(startsWith("Exiv2 rocks", "Exiv2"));
std::string_view s = "Exiv2 rocks";
ASSERT_TRUE(s.starts_with("Exiv2"));
}

TEST(stringUtils, startsWithReturnsFlase) {
ASSERT_FALSE(startsWith("Exiv2 rocks", "exiv2"));
std::string_view s = "Exiv2 rocks";
ASSERT_FALSE(s.starts_with("exiv2"));
}

TEST(stringUtils, upperTransformStringToUpperCase) {
Expand Down

0 comments on commit 85c9092

Please sign in to comment.