Skip to content

Commit

Permalink
replace substr with resize/pop_back
Browse files Browse the repository at this point in the history
Shorter and more efficient.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Jan 27, 2023
1 parent e1410b3 commit 5891660
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ int Rename::run(const std::string& path) {
return 1;
}
std::string v = md->toString();
if (v.length() == 0 || v[0] == ' ') {
if (v.empty() || v.front() == ' ') {
std::cerr << _("Image file creation timestamp not set in the file") << " " << path << "\n";
return 1;
}
Expand Down Expand Up @@ -1602,7 +1602,7 @@ int Timestamp::touch(const std::string& path) const {
//! @endcond

int str2Tm(const std::string& timeStr, struct tm* tm) {
if (timeStr.length() == 0 || timeStr[0] == ' ')
if (timeStr.empty() || timeStr.front() == ' ')
return 1;
if (timeStr.length() < 19)
return 2;
Expand Down
4 changes: 2 additions & 2 deletions src/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ void Converter::cnvExifDate(const char* from, const char* to) {
}

if (subsec.size() > 10)
subsec = subsec.substr(0, 10);
subsec.resize(10);
snprintf(buf, sizeof(buf), "%4d-%02d-%02dT%02d:%02d:%02d%s", year, month, day, hour, min, sec, subsec.c_str());
buf[sizeof(buf) - 1] = 0;

Expand Down Expand Up @@ -1141,7 +1141,7 @@ void Converter::cnvXmpGPSCoord(const char* from, const char* to) {
double deg = 0.0;
double min = 0.0;
double sec = 0.0;
char ref = value[value.length() - 1];
char ref = value.back();
char sep1 = '\0';
char sep2 = '\0';

Expand Down
4 changes: 2 additions & 2 deletions src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st
while (c && first_newline && c < first_newline && h < buffer + body) {
std::string key(h);
std::string value(c + 1);
key = key.substr(0, c - h);
value = value.substr(0, first_newline - c - 1);
key.resize(c - h);
value.resize(first_newline - c - 1);
response[key] = value;
h = first_newline + 1;
c = strchr(h, C);
Expand Down
2 changes: 1 addition & 1 deletion src/pngimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, si
}
while (dataString.size() < iMax)
dataString += ' ';
dataString = dataString.substr(0, iMax);
dataString.resize(iMax);

if (bPrint) {
io_->seek(dataOffset, BasicIo::cur); // jump to checksum
Expand Down
12 changes: 6 additions & 6 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ int CommentValue::read(const std::string& comment) {
// Strip quotes (so you can also specify the charset without quotes)
if (!name.empty() && name.front() == '"')
name = name.substr(1);
if (!name.empty() && name[name.length() - 1] == '"')
name = name.substr(0, name.length() - 1);
if (!name.empty() && name.back() == '"')
name.pop_back();
charsetId = CharsetInfo::charsetIdByName(name);
if (charsetId == invalidCharsetId) {
#ifndef SUPPRESS_WARNINGS
Expand Down Expand Up @@ -391,7 +391,7 @@ std::string CommentValue::comment(const char* encoding) const {
bool bAscii = charsetId() == undefined || charsetId() == ascii;
// # 1266 Remove trailing nulls
if (bAscii && c.find('\0') != std::string::npos) {
c = c.substr(0, c.find('\0'));
c.resize(c.find('\0'));
}
return c;
}
Expand Down Expand Up @@ -503,8 +503,8 @@ int XmpTextValue::read(const std::string& buf) {
// Strip quotes (so you can also specify the type without quotes)
if (!type.empty() && type.front() == '"')
type = type.substr(1);
if (!type.empty() && type[type.length() - 1] == '"')
type = type.substr(0, type.length() - 1);
if (!type.empty() && type.back() == '"')
type.pop_back();
b.clear();
if (pos != std::string::npos)
b = buf.substr(pos + 1);
Expand Down Expand Up @@ -670,7 +670,7 @@ int LangAltValue::read(const std::string& buf) {
if (lang.empty() || lang.find('"') != lang.length() - 1)
throw Error(ErrorCode::kerInvalidLangAltValue, buf);

lang = lang.substr(0, lang.length() - 1);
lang.pop_back();
}

if (lang.empty())
Expand Down
9 changes: 3 additions & 6 deletions src/xmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,8 @@ static XMP_Status nsDumper(void* refCon, XMP_StringPtr buffer, XMP_StringLen buf
bool bNS = out.find(':') != std::string::npos && !bURI;

// pop trailing ':' on a namespace
if (bNS && !out.empty()) {
std::size_t length = out.length();
if (out[length - 1] == ':')
out = out.substr(0, length - 1);
}
if (bNS && !out.empty() && out.back() == ':')
out.pop_back();

if (bURI || bNS) {
auto p = static_cast<std::map<std::string, std::string>*>(refCon);
Expand Down Expand Up @@ -706,7 +703,7 @@ int XmpParser::decode(XmpData& xmpData, const std::string& xmpPacket) {
bool ret = SXMPMeta::GetNamespacePrefix(schemaNs.c_str(), &prefix);
if (!ret)
throw Error(ErrorCode::kerSchemaNamespaceNotRegistered, schemaNs);
prefix = prefix.substr(0, prefix.size() - 1);
prefix.pop_back();
XmpProperties::registerNs(schemaNs, prefix);
}
continue;
Expand Down

0 comments on commit 5891660

Please sign in to comment.