From fbbf8710673057801b7063d4b7d6d36baf1c4e11 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 14 Feb 2022 22:36:07 +0100 Subject: [PATCH] Avoid extra copy for Utf8Encoder::getUtf8 result --- apps/mwiniimporter/importer.cpp | 36 ++++++++++++++------------ components/translation/translation.cpp | 12 ++++----- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/apps/mwiniimporter/importer.cpp b/apps/mwiniimporter/importer.cpp index 35d9701803..f7523b11cf 100644 --- a/apps/mwiniimporter/importer.cpp +++ b/apps/mwiniimporter/importer.cpp @@ -663,49 +663,51 @@ MwIniImporter::multistrmap MwIniImporter::loadIniFile(const boost::filesystem::p std::string line; while (std::getline(file, line)) { - line = encoder.getUtf8(line); + std::string_view utf8 = encoder.getUtf8(line); // unify Unix-style and Windows file ending - if (!(line.empty()) && (line[line.length()-1]) == '\r') { - line = line.substr(0, line.length()-1); + if (!(utf8.empty()) && (utf8[utf8.length()-1]) == '\r') { + utf8 = utf8.substr(0, utf8.length()-1); } - if(line.empty()) { + if(utf8.empty()) { continue; } - if(line[0] == '[') { - int pos = static_cast(line.find(']')); + if(utf8[0] == '[') { + int pos = static_cast(utf8.find(']')); if(pos < 2) { - std::cout << "Warning: ini file wrongly formatted (" << line << "). Line ignored." << std::endl; + std::cout << "Warning: ini file wrongly formatted (" << utf8 << "). Line ignored." << std::endl; continue; } - section = line.substr(1, line.find(']')-1); + section = utf8.substr(1, utf8.find(']')-1); continue; } - int comment_pos = static_cast(line.find(';')); + int comment_pos = static_cast(utf8.find(';')); if(comment_pos > 0) { - line = line.substr(0,comment_pos); + utf8 = utf8.substr(0,comment_pos); } - int pos = static_cast(line.find('=')); + int pos = static_cast(utf8.find('=')); if(pos < 1) { continue; } - std::string key(section + ":" + line.substr(0,pos)); - std::string value(line.substr(pos+1)); + std::string key(section + ":" + std::string(utf8.substr(0, pos))); + const std::string_view value(utf8.substr(pos+1)); if(value.empty()) { std::cout << "Warning: ignored empty value for key '" << key << "'." << std::endl; continue; } - if(map.find(key) == map.end()) { - map.insert( std::make_pair (key, std::vector() ) ); - } - map[key].push_back(value); + auto it = map.find(key); + + if (it == map.end()) + it = map.emplace_hint(it, std::move(key), std::vector()); + + it->second.push_back(std::string(value)); } return map; diff --git a/components/translation/translation.cpp b/components/translation/translation.cpp index ef0f432075..f2cdd803e7 100644 --- a/components/translation/translation.cpp +++ b/components/translation/translation.cpp @@ -53,16 +53,16 @@ namespace Translation if (!line.empty()) { - line = mEncoder->getUtf8(line); + const std::string_view utf8 = mEncoder->getUtf8(line); - size_t tab_pos = line.find('\t'); - if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < line.size() - 1) + size_t tab_pos = utf8.find('\t'); + if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < utf8.size() - 1) { - std::string key = line.substr(0, tab_pos); - std::string value = line.substr(tab_pos + 1); + const std::string_view key = utf8.substr(0, tab_pos); + const std::string_view value = utf8.substr(tab_pos + 1); if (!key.empty() && !value.empty()) - container.insert(std::make_pair(key, value)); + container.emplace(key, value); } } }