From 4b70f9d213e8cb71bc225a73494625e31dfaa721 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 4 Dec 2014 23:05:44 +0100 Subject: util/DivideString: add option "strip" --- src/output/plugins/AoOutputPlugin.cxx | 2 +- src/util/DivideString.cxx | 13 ++++++++++++- src/util/DivideString.hxx | 6 +++++- test/DivideStringTest.hxx | 10 ++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/output/plugins/AoOutputPlugin.cxx b/src/output/plugins/AoOutputPlugin.cxx index 689e7de7c..4b98f4a77 100644 --- a/src/output/plugins/AoOutputPlugin.cxx +++ b/src/output/plugins/AoOutputPlugin.cxx @@ -128,7 +128,7 @@ AoOutput::Configure(const config_param ¶m, Error &error) value = param.GetBlockValue("options", nullptr); if (value != nullptr) { for (const auto &i : SplitString(value, ';')) { - const DivideString ss(i.c_str(), '='); + const DivideString ss(i.c_str(), '=', true); if (!ss.IsDefined()) { error.Format(ao_output_domain, diff --git a/src/util/DivideString.cxx b/src/util/DivideString.cxx index d30dfaa16..f781d141f 100644 --- a/src/util/DivideString.cxx +++ b/src/util/DivideString.cxx @@ -18,10 +18,11 @@ */ #include "DivideString.hxx" +#include "StringUtil.hxx" #include -DivideString::DivideString(const char *s, char separator) +DivideString::DivideString(const char *s, char separator, bool strip) :first(nullptr) { const char *x = strchr(s, separator); @@ -31,6 +32,16 @@ DivideString::DivideString(const char *s, char separator) size_t length = x - s; second = x + 1; + if (strip) + second = StripLeft(second); + + if (strip) { + const char *end = s + length; + s = StripLeft(s); + end = StripRight(s, end); + length = end - s; + } + first = new char[length + 1]; memcpy(first, s, length); first[length] = 0; diff --git a/src/util/DivideString.hxx b/src/util/DivideString.hxx index d8d911691..126aa45d1 100644 --- a/src/util/DivideString.hxx +++ b/src/util/DivideString.hxx @@ -33,7 +33,11 @@ class DivideString { const char *second; public: - DivideString(const char *s, char separator); + /** + * @param strip strip the first part and left-strip the second + * part? + */ + DivideString(const char *s, char separator, bool strip=false); ~DivideString() { delete[] first; diff --git a/test/DivideStringTest.hxx b/test/DivideStringTest.hxx index 83c54dc56..f6c01bdde 100644 --- a/test/DivideStringTest.hxx +++ b/test/DivideStringTest.hxx @@ -15,6 +15,7 @@ class DivideStringTest : public CppUnit::TestFixture { CPPUNIT_TEST(TestBasic); CPPUNIT_TEST(TestEmpty); CPPUNIT_TEST(TestFail); + CPPUNIT_TEST(TestStrip); CPPUNIT_TEST_SUITE_END(); public: @@ -41,4 +42,13 @@ public: const DivideString ds(input, '.'); CPPUNIT_ASSERT(!ds.IsDefined()); } + + void TestStrip() { + constexpr char input[] = " foo\t.\nbar\r"; + const DivideString ds(input, '.', true); + CPPUNIT_ASSERT(ds.IsDefined()); + CPPUNIT_ASSERT(!ds.IsEmpty()); + CPPUNIT_ASSERT_EQUAL(0, strcmp(ds.GetFirst(), "foo")); + CPPUNIT_ASSERT_EQUAL(input + 7, ds.GetSecond()); + } }; -- cgit v1.2.3