Minor perf improvement in ini file parser

This commit is contained in:
Henrik Rydgård 2024-11-30 01:24:54 +01:00
parent bd3c58e540
commit 755b062abd
3 changed files with 11 additions and 15 deletions

View File

@ -162,7 +162,7 @@ static std::string EscapeHash(std::string_view value) {
return result;
}
void ParsedIniLine::ParseFrom(std::string_view line) {
ParsedIniLine::ParsedIniLine(std::string_view line) {
line = StripSpaces(line);
if (line.empty()) {
key.clear();
@ -534,7 +534,7 @@ bool IniFile::Load(std::istream &in) {
while (!(in.eof() || in.fail()))
{
in.getline(templine, MAX_BYTES);
std::string line = templine;
std::string_view line = templine;
// Remove UTF-8 byte order marks.
if (line.substr(0, 3) == "\xEF\xBB\xBF") {
@ -543,8 +543,8 @@ bool IniFile::Load(std::istream &in) {
#ifndef _WIN32
// Check for CRLF eol and convert it to LF
if (!line.empty() && line.at(line.size()-1) == '\r') {
line.erase(line.size()-1);
if (!line.empty() && line.at(line.size() - 1) == '\r') {
line = line.substr(line.size() - 1);
}
#endif
@ -556,7 +556,7 @@ bool IniFile::Load(std::istream &in) {
if (sectionNameEnd != std::string::npos) {
// New section!
std::string sub = line.substr(1, sectionNameEnd - 1);
std::string_view sub = line.substr(1, sectionNameEnd - 1);
sections.push_back(std::make_unique<Section>(sub));
if (sectionNameEnd + 1 < line.size()) {
@ -566,9 +566,7 @@ bool IniFile::Load(std::istream &in) {
if (sections.empty()) {
sections.push_back(std::make_unique<Section>(""));
}
ParsedIniLine parsedLine;
parsedLine.ParseFrom(line);
sections.back()->lines_.push_back(parsedLine);
sections.back()->lines_.emplace_back(line);
}
}
}

View File

@ -18,7 +18,8 @@ class VFSInterface;
class ParsedIniLine {
public:
ParsedIniLine() {}
explicit ParsedIniLine(std::string_view line);
ParsedIniLine(std::string_view key, std::string_view value) {
this->key = key;
this->value = value;
@ -32,8 +33,6 @@ public:
return ParsedIniLine(std::string_view(), std::string_view(), comment);
}
// Comments only come from "ParseFrom".
void ParseFrom(std::string_view line);
void Reconstruct(std::string *output) const;
// Having these as views allows a more efficient internal representation, like one joint string.

View File

@ -989,14 +989,13 @@ bool TestIniFile() {
const std::string testLine2 = "# Just a comment";
std::string temp;
ParsedIniLine line;
line.ParseFrom(testLine);
ParsedIniLine line(testLine);
line.Reconstruct(&temp);
EXPECT_EQ_STR(testLine, temp);
temp.clear();
line.ParseFrom(testLine2);
line.Reconstruct(&temp);
ParsedIniLine line2(testLine2);
line2.Reconstruct(&temp);
EXPECT_EQ_STR(testLine2, temp);
return true;