From: Kefu Chai Date: Wed, 19 Jun 2019 10:07:07 +0000 (+0800) Subject: common/ConfUtils: implement trim helpers using boost X-Git-Tag: v15.1.0~2366^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=f02a47dd6a7c17e9eebd2380fd282eb410a55fc9;p=ceph-ci.git common/ConfUtils: implement trim helpers using boost remove unused trim_whitespace() and its tests. as it is not used anymore Signed-off-by: Kefu Chai --- diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index d4d2c262a6d..96c9f352da9 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -32,6 +32,7 @@ namespace fs = std::experimental::filesystem; #endif #include +#include #include #include #include @@ -286,58 +287,6 @@ int ConfFile::read(const std::string& section_name, return -ENOENT; } -void ConfFile:: -trim_whitespace(std::string &str, bool strip_internal) -{ - // strip preceding - const char *in = str.c_str(); - while (true) { - char c = *in; - if ((!c) || (!isspace(c))) - break; - ++in; - } - char output[strlen(in) + 1]; - strcpy(output, in); - - // strip trailing - char *o = output + strlen(output); - while (true) { - if (o == output) - break; - --o; - if (!isspace(*o)) { - ++o; - *o = '\0'; - break; - } - } - - if (!strip_internal) { - str.assign(output); - return; - } - - // strip internal - char output2[strlen(output) + 1]; - char *out2 = output2; - bool prev_was_space = false; - for (char *u = output; *u; ++u) { - char c = *u; - if (isspace(c)) { - if (!prev_was_space) - *out2++ = c; - prev_was_space = true; - } - else { - *out2++ = c; - prev_was_space = false; - } - } - *out2++ = '\0'; - str.assign(output2); -} - /* Normalize a key name. * * Normalized key names have no leading or trailing whitespace, and all @@ -345,15 +294,10 @@ trim_whitespace(std::string &str, bool strip_internal) * normal form is so that in common/config.cc, we can use a macro to stringify * the field names of md_config_t and get a key in normal form. */ -std::string ConfFile:: -normalize_key_name(std::string_view key) +std::string ConfFile::normalize_key_name(std::string_view key) { std::string k{key}; - if (k.find_first_of(" \t\r\n\f\v\xa0") == k.npos) { - return k; - } - ConfFile::trim_whitespace(k, true); - std::replace(k.begin(), k.end(), ' ', '_'); + boost::algorithm::trim_fill_if(k, "_", isspace); return k; } diff --git a/src/common/ConfUtils.h b/src/common/ConfUtils.h index 2672ccf41a4..80dd8df8662 100644 --- a/src/common/ConfUtils.h +++ b/src/common/ConfUtils.h @@ -72,7 +72,6 @@ public: int parse_bufferlist(ceph::bufferlist *bl, std::ostream *warnings); int read(const std::string& section, std::string_view key, std::string &val) const; - static void trim_whitespace(std::string &str, bool strip_internal); static std::string normalize_key_name(std::string_view key); private: bool load_from_buffer(std::string_view buf, std::ostream* warning); diff --git a/src/test/confutils.cc b/src/test/confutils.cc index 029dd5dbb43..c9411b1827d 100644 --- a/src/test/confutils.cc +++ b/src/test/confutils.cc @@ -256,44 +256,6 @@ const char dup_key_config_1[] = "\ log_file = 3\n\ "; -TEST(ConfUtils, Whitespace) { - std::string test0(""); - ConfFile::trim_whitespace(test0, false); - ASSERT_EQ(test0, ""); - - std::string test0a(""); - ConfFile::trim_whitespace(test0a, true); - ASSERT_EQ(test0a, ""); - - std::string test0b(" "); - ConfFile::trim_whitespace(test0b, false); - ASSERT_EQ(test0b, ""); - - std::string test0c(" "); - ConfFile::trim_whitespace(test0c, true); - ASSERT_EQ(test0c, ""); - - std::string test1(" abc "); - ConfFile::trim_whitespace(test1, false); - ASSERT_EQ(test1, "abc"); - - std::string test2(" abc d "); - ConfFile::trim_whitespace(test2, true); - ASSERT_EQ(test2, "abc d"); - - std::string test3(" abc d "); - ConfFile::trim_whitespace(test3, false); - ASSERT_EQ(test3, "abc d"); - - std::string test4("abcd"); - ConfFile::trim_whitespace(test4, false); - ASSERT_EQ(test4, "abcd"); - - std::string test5("abcd"); - ConfFile::trim_whitespace(test5, true); - ASSERT_EQ(test5, "abcd"); -} - TEST(ConfUtils, ParseFiles0) { std::string val;