Merge branch 'use_utf8_string_view' into 'master'

Avoid extra copy for Utf8Encoder::getUtf8 result

See merge request OpenMW/openmw!1661
This commit is contained in:
psi29a 2022-02-16 20:58:12 +00:00
commit b73eaadf20
2 changed files with 25 additions and 23 deletions

View file

@ -663,49 +663,51 @@ MwIniImporter::multistrmap MwIniImporter::loadIniFile(const boost::filesystem::p
std::string line; std::string line;
while (std::getline(file, line)) { while (std::getline(file, line)) {
line = encoder.getUtf8(line); std::string_view utf8 = encoder.getUtf8(line);
// unify Unix-style and Windows file ending // unify Unix-style and Windows file ending
if (!(line.empty()) && (line[line.length()-1]) == '\r') { if (!(utf8.empty()) && (utf8[utf8.length()-1]) == '\r') {
line = line.substr(0, line.length()-1); utf8 = utf8.substr(0, utf8.length()-1);
} }
if(line.empty()) { if(utf8.empty()) {
continue; continue;
} }
if(line[0] == '[') { if(utf8[0] == '[') {
int pos = static_cast<int>(line.find(']')); int pos = static_cast<int>(utf8.find(']'));
if(pos < 2) { 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; continue;
} }
section = line.substr(1, line.find(']')-1); section = utf8.substr(1, utf8.find(']')-1);
continue; continue;
} }
int comment_pos = static_cast<int>(line.find(';')); int comment_pos = static_cast<int>(utf8.find(';'));
if(comment_pos > 0) { if(comment_pos > 0) {
line = line.substr(0,comment_pos); utf8 = utf8.substr(0,comment_pos);
} }
int pos = static_cast<int>(line.find('=')); int pos = static_cast<int>(utf8.find('='));
if(pos < 1) { if(pos < 1) {
continue; continue;
} }
std::string key(section + ":" + line.substr(0,pos)); std::string key(section + ":" + std::string(utf8.substr(0, pos)));
std::string value(line.substr(pos+1)); const std::string_view value(utf8.substr(pos+1));
if(value.empty()) { if(value.empty()) {
std::cout << "Warning: ignored empty value for key '" << key << "'." << std::endl; std::cout << "Warning: ignored empty value for key '" << key << "'." << std::endl;
continue; continue;
} }
if(map.find(key) == map.end()) { auto it = map.find(key);
map.insert( std::make_pair (key, std::vector<std::string>() ) );
} if (it == map.end())
map[key].push_back(value); it = map.emplace_hint(it, std::move(key), std::vector<std::string>());
it->second.push_back(std::string(value));
} }
return map; return map;

View file

@ -53,16 +53,16 @@ namespace Translation
if (!line.empty()) if (!line.empty())
{ {
line = mEncoder->getUtf8(line); const std::string_view utf8 = mEncoder->getUtf8(line);
size_t tab_pos = line.find('\t'); size_t tab_pos = utf8.find('\t');
if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < line.size() - 1) if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < utf8.size() - 1)
{ {
std::string key = line.substr(0, tab_pos); const std::string_view key = utf8.substr(0, tab_pos);
std::string value = line.substr(tab_pos + 1); const std::string_view value = utf8.substr(tab_pos + 1);
if (!key.empty() && !value.empty()) if (!key.empty() && !value.empty())
container.insert(std::make_pair(key, value)); container.emplace(key, value);
} }
} }
} }