From 5692311f7a9f42242b238dbb13c859611b09b1d8 Mon Sep 17 00:00:00 2001 From: Ziy1-Tan Date: Thu, 16 Nov 2023 23:36:13 +0800 Subject: [PATCH] fix read config Signed-off-by: Ziy1-Tan --- src/common/configuration.cpp | 11 +++---- src/common/string_util.h | 22 ++++++++++++++ test/common/string_util_test.cpp | 51 +++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/src/common/configuration.cpp b/src/common/configuration.cpp index 0956423a3c..6c4ba141c0 100644 --- a/src/common/configuration.cpp +++ b/src/common/configuration.cpp @@ -22,12 +22,12 @@ */ #include "src/common/configuration.h" +#include "src/common/string_util.h" +#include #include #include -#include #include -#include namespace curve { namespace common { @@ -38,16 +38,17 @@ bool Configuration::LoadConfig() { if (cFile.is_open()) { std::string line; while (getline(cFile, line)) { - // FIXME: may not remove middle spaces - line.erase(std::remove_if(line.begin(), line.end(), isspace), - line.end()); + Trim(line); if (line.empty() || line[0] == '#') continue; int delimiterPos = line.find("="); std::string key = line.substr(0, delimiterPos); + Trim(key); + int commentPos = line.find("#"); std::string value = line.substr(delimiterPos + 1, commentPos - delimiterPos - 1); + Trim(value); SetValue(key, value); } } else { diff --git a/src/common/string_util.h b/src/common/string_util.h index 71a43919b1..7f568207f9 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -35,6 +35,7 @@ #include #include #include +#include #include namespace curve { @@ -165,6 +166,27 @@ inline std::string ToHexString(void* p) { return oss.str(); } +// trim from start (in place) +inline void LTrim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](const char &ch) { + return !std::isspace(ch); + })); +} + +// trim from end (in place) +inline void RTrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), + [](const char &ch) { return !std::isspace(ch); }) + .base(), + s.end()); +} + +// trim from both ends (in place) +inline void Trim(std::string &s) { + LTrim(s); + RTrim(s); +} + } // namespace common } // namespace curve diff --git a/test/common/string_util_test.cpp b/test/common/string_util_test.cpp index 12316db991..d72d6493f1 100644 --- a/test/common/string_util_test.cpp +++ b/test/common/string_util_test.cpp @@ -28,7 +28,7 @@ namespace curve { namespace common { -TEST(Common, SpliteString) { +TEST(Common, SplitString) { const struct {std::string path; std::string sep; const int size; std::vector items;} testCases[] = { @@ -59,5 +59,54 @@ TEST(Common, StringToUll) { str = "ffff"; ASSERT_FALSE(StringToUll(str, &out)); } + +TEST(StringUtilTest, LTrim) { + std::array, 4> cases = { + std::array{"hello", "hello"}, + std::array{"", ""}, + std::array{" ", ""}, + std::array{" hello", "hello"}, + }; + + for (auto &c : cases) { + curve::common::LTrim(c[0]); + EXPECT_EQ(c[0], c[1]); + } +} + +TEST(StringUtilTest, RTrim) { + std::array, 4> cases = { + std::array{"hello", "hello"}, + std::array{"", ""}, + std::array{" ", ""}, + std::array{"hello ", "hello"}, + }; + + for (auto &c : cases) { + curve::common::RTrim(c[0]); + EXPECT_EQ(c[0], c[1]); + } +} + +TEST(StringUtilTest, Trim) { + std::array, 10> cases = { + std::array{"hello", "hello"}, + std::array{" hello", "hello"}, + std::array{"hello ", "hello"}, + std::array{" hello ", "hello"}, + std::array{"S3 Browser", "S3 Browser"}, + std::array{"S3 Browser ", "S3 Browser"}, + std::array{" S3 Browser", "S3 Browser"}, + std::array{" S3 Browser ", "S3 Browser"}, + std::array{" ", ""}, + std::array{"", ""}, + }; + + for (auto &c : cases) { + curve::common::Trim(c[0]); + EXPECT_EQ(c[0], c[1]); + } +} + } // namespace common } // namespace curve