From c1402612146ba06606ebf64963a02bc1efe11e74 Mon Sep 17 00:00:00 2001 From: Micovec <30734146+Micovec@users.noreply.github.com> Date: Fri, 29 Mar 2024 01:24:51 +0100 Subject: [PATCH] lib: Allow comment escaping with multiple `#` if it's the first char (#38) * parse line - ignore leading spaces, tabs * removed unnecessary substr functions * parse line - discard empty lines sooner --- src/config.cpp | 17 +++++++++++++---- tests/config/config.conf | 5 +++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index c6492ee..ecefe52 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -513,8 +513,14 @@ CParseResult CConfig::parseVariable(const std::string& lhs, const std::string& r CParseResult CConfig::parseLine(std::string line, bool dynamic) { CParseResult result; - auto commentPos = line.find('#'); - size_t lastHashPos = 0; + line = removeBeginEndSpacesTabs(line); + + auto commentPos = line.find('#'); + + if (commentPos == 0) + return result; + + size_t lastHashPos = 0; while (commentPos != std::string::npos) { bool escaped = false; @@ -536,9 +542,12 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) { line = removeBeginEndSpacesTabs(line); + if (line.empty()) + return result; + auto equalsPos = line.find('='); - if (equalsPos == std::string::npos && !line.ends_with("{") && line != "}" && !line.empty()) { + if (equalsPos == std::string::npos && !line.ends_with("{") && line != "}") { // invalid line result.setError("Invalid config line"); return result; @@ -607,7 +616,7 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) { if (ret.error) return ret; - } else if (!line.empty()) { + } else { // has to be a set if (line.contains("}")) { // easiest. } or invalid. diff --git a/tests/config/config.conf b/tests/config/config.conf index f3b018f..243421c 100644 --- a/tests/config/config.conf +++ b/tests/config/config.conf @@ -1,5 +1,10 @@ # Test comment +## This is also a comment + ## This is a comment with space as a first character + ## This is a comment with tab as a first character + ## This is a comment with leading spaces and tabs + ##### Comment with more hash tags testInt = 123 testFloat = 123.456