replace substr with resize/pop_back

Shorter and more efficient.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
This commit is contained in:
Rosen Penev 2023-01-26 18:47:04 -08:00
parent dd445e6c8d
commit 6976aab5bd
7 changed files with 21 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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