#endif
#include <boost/algorithm/string.hpp>
+#include <boost/algorithm/string/trim_all.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
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
* 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;
}
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);
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;