From 6976aab5bd36bcbc43ff723212b221feeac90254 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 26 Jan 2023 18:47:04 -0800 Subject: [PATCH] replace substr with resize/pop_back Shorter and more efficient. Signed-off-by: Rosen Penev --- app/actions.cpp | 4 ++-- src/convert.cpp | 4 ++-- src/http.cpp | 4 ++-- src/pngimage.cpp | 2 +- src/properties.cpp | 8 ++++---- src/value.cpp | 14 +++++++------- src/xmp.cpp | 9 +++------ 7 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/actions.cpp b/app/actions.cpp index d7343f98..bd7744c7 100644 --- a/app/actions.cpp +++ b/app/actions.cpp @@ -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; } @@ -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; diff --git a/src/convert.cpp b/src/convert.cpp index 577fd080..51f9b12b 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -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; @@ -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'; diff --git a/src/http.cpp b/src/http.cpp index 50ccd053..2c140ec0 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -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); diff --git a/src/pngimage.cpp b/src/pngimage.cpp index fc96a5b0..eed1a071 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -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 diff --git a/src/properties.cpp b/src/properties.cpp index 005e2aa8..264f01b3 100644 --- a/src/properties.cpp +++ b/src/properties.cpp @@ -4942,8 +4942,8 @@ const XmpNsInfo* XmpProperties::lookupNsRegistryUnsafe(const XmpNsInfo::Prefix& void XmpProperties::registerNs(const std::string& ns, const std::string& prefix) { auto scopedWriteLock = std::scoped_lock(mutex_); std::string ns2 = ns; - if (ns2.substr(ns2.size() - 1, 1) != "/" && ns2.substr(ns2.size() - 1, 1) != "#") - ns2 += "/"; + if (ns2.back() != '/' && ns2.back() != '#') + ns2 += '/'; // Check if there is already a registered namespace with this prefix const XmpNsInfo* xnp = lookupNsRegistryUnsafe(XmpNsInfo::Prefix(prefix)); if (xnp) { @@ -4995,8 +4995,8 @@ void XmpProperties::unregisterNs() { std::string XmpProperties::prefix(const std::string& ns) { auto scoped_read_lock = std::scoped_lock(mutex_); std::string ns2 = ns; - if (ns2.substr(ns2.size() - 1, 1) != "/" && ns2.substr(ns2.size() - 1, 1) != "#") - ns2 += "/"; + if (ns2.back() != '/' && ns2.back() != '#') + ns2 += '/'; auto i = nsRegistry_.find(ns2); std::string p; diff --git a/src/value.cpp b/src/value.cpp index 3a9edabb..c18a613c 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -255,7 +255,7 @@ AsciiValue::AsciiValue(const std::string& buf) : StringValueBase(asciiString, bu int AsciiValue::read(const std::string& buf) { value_ = buf; // ensure count>0 and nul terminated # https://github.com/Exiv2/exiv2/issues/1484 - if (value_.empty() || value_.at(value_.size() - 1) != '\0') { + if (value_.empty() || value_.back() != '\0') { value_ += '\0'; } return 0; @@ -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 @@ -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; } @@ -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); @@ -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()) diff --git a/src/xmp.cpp b/src/xmp.cpp index f136f651..3c528dc9 100644 --- a/src/xmp.cpp +++ b/src/xmp.cpp @@ -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*>(refCon); @@ -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;