Skip to content

Commit

Permalink
std::find to find template
Browse files Browse the repository at this point in the history
std::find in C++20 can use ranges, which is equivalent here. Less error
prone.

Namespace is properly to avoid any conflicts with std::find or others

Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
neheb committed Jan 14, 2023
1 parent 842ef05 commit 81d7de1
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 107 deletions.
23 changes: 15 additions & 8 deletions app/exiv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ constexpr auto emptyYodAdjust_ = std::array{
};

//! List of all command identifiers and corresponding strings
constexpr auto cmdIdAndString = std::array{
std::pair(CmdId::add, "add"),
std::pair(CmdId::set, "set"),
std::pair(CmdId::del, "del"),
std::pair(CmdId::reg, "reg"),
std::pair(CmdId::invalid, "invalidCmd"), // End of list marker
constexpr struct CmdIdAndString {
CmdId cmdId_;
const char* string_;
//! Comparison operator for \em string
bool operator==(const std::string& string) const {
return string == string_;
}
} cmdIdAndString[] = {
{CmdId::add, "add"},
{CmdId::set, "set"},
{CmdId::del, "del"},
{CmdId::reg, "reg"},
{CmdId::invalid, "invalidCmd"}, // End of list marker
};

// Return a command Id for a command string
Expand Down Expand Up @@ -1433,8 +1440,8 @@ bool parseLine(ModifyCmd& modifyCmd, const std::string& line, int num) {
} // parseLine

CmdId commandId(const std::string& cmdString) {
auto it = std::find_if(cmdIdAndString.begin(), cmdIdAndString.end(), [&](auto cs) { return cs.second == cmdString; });
return it != cmdIdAndString.end() ? it->first : CmdId::invalid;
auto it = Exiv2::find(cmdIdAndString, cmdString);
return it ? it->cmdId_ : CmdId::invalid;
}

std::string parseEscapes(const std::string& input) {
Expand Down
13 changes: 5 additions & 8 deletions src/asfvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ void AsfVideo::readMetadata() {
void AsfVideo::decodeBlock() {
DataBuf buf(BUFF_MIN_SIZE + 1);
uint64_t size = 0;
const Internal::TagVocabulary* tv;
uint64_t cur_pos = io_->tell();

byte guidBuf[GUI_SIZE];
Expand All @@ -278,12 +277,11 @@ void AsfVideo::decodeBlock() {

char GUID[GUID_SIZE] = ""; // the getGUID function write the GUID[36],

getGUID(guidBuf, GUID);
tv = find(GUIDReferenceTags, GUID);

io_->read(buf.data(), BUFF_MIN_SIZE);
size = Util::getUint64_t(buf);

getGUID(guidBuf, GUID);
auto tv = Exiv2::find(GUIDReferenceTags, GUID);
if (tv) {
auto tagDecoder = [&](const Internal::TagVocabulary* tv, uint64_t size) {
uint64_t cur_pos = io_->tell();
Expand Down Expand Up @@ -392,7 +390,7 @@ void AsfVideo::contentDescription(uint64_t size) {
io_->read(buf.data(), length[i]);
if (io_->error() || io_->eof())
throw Error(ErrorCode::kerFailedToReadImageData);
const TagDetails* td = find(contentDescriptionTags, i);
auto td = Exiv2::find(contentDescriptionTags, i);
assert(td);
std::string str(reinterpret_cast<const char*>(buf.data()), length[i]);
if (convertStringCharset(str, "UCS-2LE", "UTF-8")) {
Expand All @@ -414,8 +412,7 @@ void AsfVideo::streamProperties() {
char streamType[GUID_SIZE] = "";

getGUID(guidBuf, streamType);
const TagVocabulary* tv;
tv = find(GUIDReferenceTags, streamType);
auto tv = Exiv2::find(GUIDReferenceTags, streamType);
io_->read(guidBuf, GUI_SIZE);

if (compareTag(exvGettext(tv->label_), "Audio_Media"))
Expand Down Expand Up @@ -632,7 +629,7 @@ void AsfVideo::fileProperties() {
const TagDetails* td;

while (count--) {
td = find(filePropertiesTags, (count + 1));
td = Exiv2::find(filePropertiesTags, (count + 1));
io_->read(buf.data(), BUFF_MIN_SIZE);

if (count == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,8 +1553,8 @@ const ConvFctList convFctList[] = {

[[maybe_unused]] bool convertStringCharsetWindows(std::string& str, const char* from, const char* to) {
bool ret = false;
const ConvFctList* p = find(convFctList, std::pair(from, to));
std::string tmpstr = str;
auto p = Exiv2::find(convFctList, std::pair(from, to));
if (p)
ret = p->convFct_(tmpstr);
#ifndef SUPPRESS_WARNINGS
Expand Down
4 changes: 2 additions & 2 deletions src/fujimn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ std::ostream& printFujiDriveSetting(std::ostream& os, const Value& value, const
auto byte3 = (value.toInt64() >> 16) & 0xff;
auto fps = value.toInt64() >> 24;

auto setting = find(fujiDriveSettingByte1, byte1);
auto setting = Exiv2::find(fujiDriveSettingByte1, byte1);
if (setting) {
os << exvGettext(setting->label_);
} else {
Expand Down Expand Up @@ -347,7 +347,7 @@ std::ostream& printFujiFaceElementTypes(std::ostream& os, const Value& value, co
longValue -= '0';
}

auto td = find(fujiFaceElementType, longValue);
auto td = Exiv2::find(fujiFaceElementType, longValue);
if (n != 0) {
os << " ";
}
Expand Down
2 changes: 1 addition & 1 deletion src/minoltamn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ static bool inRange(long value, long min, long max) {
}

static std::ostream& resolvedLens(std::ostream& os, long lensID, long index) {
const TagDetails* td = find(minoltaSonyLensID, lensID);
auto td = Exiv2::find(minoltaSonyLensID, lensID);
std::vector<std::string> tokens = split(td[0].label_, "|");
return os << exvGettext(trim(tokens.at(index - 1)).c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion src/nikonmn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3791,7 +3791,7 @@ std::ostream& Nikon3MakerNote::print0x009e(std::ostream& os, const Value& value,
if (l != 0)
trim = false;
std::string d = s.empty() ? "" : "; ";
const TagDetails* td = find(nikonRetouchHistory, l);
auto td = Exiv2::find(nikonRetouchHistory, l);
if (td) {
s = std::string(exvGettext(td->label_)).append(d).append(s);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/pentaxmn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ std::ostream& resolveLens0x32c(std::ostream& os, const Value& value, const ExifD

if (index > 0) {
const unsigned long lensID = 0x32c;
const TagDetails* td = find(pentaxLensType, lensID);
auto td = Exiv2::find(pentaxLensType, lensID);
os << exvGettext(td[index].label_);
return os;
}
Expand Down Expand Up @@ -1160,7 +1160,7 @@ std::ostream& resolveLens0x3ff(std::ostream& os, const Value& value, const ExifD

if (index > 0) {
const unsigned long lensID = 0x3ff;
const TagDetails* td = find(pentaxLensType, lensID);
auto td = Exiv2::find(pentaxLensType, lensID);
os << exvGettext(td[index].label_);
return os;
}
Expand All @@ -1187,7 +1187,7 @@ std::ostream& resolveLens0x8ff(std::ostream& os, const Value& value, const ExifD

if (index > 0) {
const unsigned long lensID = 0x8ff;
const TagDetails* td = find(pentaxLensType, lensID);
auto td = Exiv2::find(pentaxLensType, lensID);
os << exvGettext(td[index].label_);
return os;
}
Expand Down Expand Up @@ -1221,7 +1221,7 @@ std::ostream& resolveLens0x319(std::ostream& os, const Value& value, const ExifD

if (index > 0) {
const unsigned long lensID = 0x319;
const TagDetails* td = find(pentaxLensType, lensID);
auto td = Exiv2::find(pentaxLensType, lensID);
os << exvGettext(td[index].label_);
return os;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pentaxmn_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ std::ostream& printCombiTag(std::ostream& os, const Value& value, const ExifData
}
l += (value.toUint32(c) << ((count - c - 1) * 8));
}
const TagDetails* td = find(array, l);
auto td = Exiv2::find(array, l);
if (td) {
os << exvGettext(td->label_);
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5003,7 +5003,7 @@ std::string XmpProperties::prefix(const std::string& ns) {
if (i != nsRegistry_.end()) {
p = i->second.prefix_;
} else {
const XmpNsInfo* xn = find(xmpNsInfo, XmpNsInfo::Ns(ns2));
auto xn = Exiv2::find(xmpNsInfo, XmpNsInfo::Ns(ns2));
if (xn)
p = std::string(xn->prefix_);
}
Expand Down Expand Up @@ -5082,7 +5082,7 @@ const XmpNsInfo* XmpProperties::nsInfoUnsafe(const std::string& prefix) {
const XmpNsInfo::Prefix pf(prefix);
const XmpNsInfo* xn = lookupNsRegistryUnsafe(pf);
if (!xn)
xn = find(xmpNsInfo, pf);
xn = Exiv2::find(xmpNsInfo, pf);
if (!xn)
throw Error(ErrorCode::kerNoNamespaceInfoForXmpPrefix, prefix);
return xn;
Expand All @@ -5108,7 +5108,7 @@ void XmpProperties::printProperties(std::ostream& os, const std::string& prefix)
std::ostream& XmpProperties::printProperty(std::ostream& os, const std::string& key, const Value& value) {
PrintFct fct = printValue;
if (value.count() != 0) {
const XmpPrintInfo* info = find(xmpPrintInfo, key);
auto info = Exiv2::find(xmpPrintInfo, key);
if (info)
fct = info->printFct_;
}
Expand Down
Loading

0 comments on commit 81d7de1

Please sign in to comment.