1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-29 18:32:36 +00:00

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;
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<int>(line.find(']'));
if(utf8[0] == '[') {
int pos = static_cast<int>(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<int>(line.find(';'));
int comment_pos = static_cast<int>(utf8.find(';'));
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) {
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<std::string>() ) );
}
map[key].push_back(value);
auto it = map.find(key);
if (it == map.end())
it = map.emplace_hint(it, std::move(key), std::vector<std::string>());
it->second.push_back(std::string(value));
}
return map;

View File

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