Index: compiled/String.cpp
diff --git a/compiled/String.cpp b/compiled/String.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..38f458a432978d7c516ce07f796c77428d6a31ec
--- /dev/null
+++ b/compiled/String.cpp
@@ -0,0 +1,48 @@
+/*
+* This file is part of Adblock Plus ,
+* Copyright (C) 2006-present eyeo GmbH
+*
+* Adblock Plus is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 3 as
+* published by the Free Software Foundation.
+*
+* Adblock Plus is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with Adblock Plus. If not, see .
+*/
+
+#include "String.h"
+
+ABP_NS_BEGIN
+
+DependentString TrimSpaces(const String& value)
+{
+ String::size_type start = 0;
+ auto end = value.length();
+ for (; start < end; ++start)
+ {
+ if (value[start] > u' ')
+ break;
+ }
+ for (; end > start; --end)
+ {
+ if (value[end - 1] > u' ')
+ break;
+ }
+ return DependentString(value, start, end - start);
+}
+
+std::pair SplitString(const String& value, String::size_type separatorPos)
+{
+ const auto secondBeginPos = separatorPos < String::npos ? separatorPos + 1 : String::npos;
+ return {
+ DependentString{value, 0, separatorPos},
+ DependentString{value, secondBeginPos, value.length() - secondBeginPos}
+ };
+}
+
+ABP_NS_END
Index: compiled/String.h
diff --git a/compiled/String.h b/compiled/String.h
index 14e2fda23b75b71fe2bfbd3b6c1aa9631b68af92..3ea7a6782322147a84e5f0da8752910017285167 100644
--- a/compiled/String.h
+++ b/compiled/String.h
@@ -25,6 +25,7 @@
#include
#include
#endif
+#include
#include "base.h"
#include "debug.h"
@@ -455,4 +456,11 @@ inline std::ostream& operator<<(std::ostream& os, const OwnedString& str)
return os << static_cast(str);
}
#endif
+
+DependentString TrimSpaces(const String& value);
+
+// Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`.
+// Useful for parsing.
+std::pair SplitString(const String& value, String::size_type separatorPos);
+
ABP_NS_END
Index: meson.build
diff --git a/meson.build b/meson.build
index cff4aec1d56f732644433deb861cd4fc380321b6..7f04393de9fe7fe6c7dd79d77b4a5c4c60629fda 100644
--- a/meson.build
+++ b/meson.build
@@ -87,6 +87,7 @@ GENERATION_PARAMS = [
shared_sources = [
+ 'compiled/String.cpp',
'compiled/bindings/runtime_utils.cpp',
'compiled/filter/ActiveFilter.cpp',
'compiled/filter/BlockingFilter.cpp',
Index: test/compiled/String.cpp
diff --git a/test/compiled/String.cpp b/test/compiled/String.cpp
index 0067f26b4536240044354d05db87170cc7b49a0e..c81363f1b0227091eb92c70d30ba23dbaf5d78d0 100644
--- a/test/compiled/String.cpp
+++ b/test/compiled/String.cpp
@@ -48,3 +48,66 @@ TEST(TestString, constructInvalidOwnedString)
EXPECT_FALSE(s4.is_invalid());
}
+TEST(TestStringTrimSpaces, zeroLengthString)
+{
+ EXPECT_EQ(u""_str, TrimSpaces(DependentString()));
+ EXPECT_EQ(u""_str, TrimSpaces(OwnedString()));
+ EXPECT_EQ(u""_str, TrimSpaces(u""_str));
+}
+
+TEST(TestStringTrimSpaces, spacesAreRemoved)
+{
+ for (uint16_t leftSpaces = 0; leftSpaces < 5; ++leftSpaces)
+ {
+ for (uint16_t rightSpaces = 0; rightSpaces < 5; ++rightSpaces)
+ {
+ for (uint16_t nonSpaces = 0; nonSpaces < 5; ++nonSpaces)
+ {
+ OwnedString str;
+ std::string leftSpacesStdString(leftSpaces, ' ');
+ str.append(leftSpacesStdString.c_str(), leftSpacesStdString.length());
+ std::string stdString(nonSpaces, 'a');
+ OwnedString trimmedString;
+ trimmedString.append(stdString.c_str(), stdString.length());
+ str.append(trimmedString);
+ std::string rightSpacesStdString(rightSpaces, ' ');
+ str.append(rightSpacesStdString.c_str(), rightSpacesStdString.length());
+ EXPECT_EQ(trimmedString, TrimSpaces(str));
+ }
+ }
+ }
+}
+
+TEST(TestStringSplitString, test)
+{
+ {
+ auto str = u"123:abc"_str;
+ auto split = SplitString(str, 3);
+ EXPECT_EQ(u"123"_str, split.first);
+ EXPECT_EQ(u"abc"_str, split.second);
+ }
+ {
+ auto str = u"123:abc"_str;
+ auto split = SplitString(str, 0);
+ EXPECT_EQ(u""_str, split.first);
+ EXPECT_EQ(u"23:abc"_str, split.second);
+ }
+ {
+ auto str = u"123:abc"_str;
+ auto split = SplitString(str, 6);
+ EXPECT_EQ(u"123:ab"_str, split.first);
+ EXPECT_EQ(u""_str, split.second);
+ }
+ {
+ auto str = u"123:abc"_str;
+ auto split = SplitString(str, 7);
+ EXPECT_EQ(u"123:abc"_str, split.first);
+ EXPECT_EQ(u""_str, split.second);
+ }
+ {
+ auto str = u"123:abc"_str;
+ auto split = SplitString(str, 10);
+ EXPECT_EQ(u"123:abc"_str, split.first);
+ EXPECT_EQ(u""_str, split.second);
+ }
+}