Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: compiled/storage/Serializer.cpp

Issue 29548581: Issue 4128, 5138 - Add Parser and Serializer implemented in C++ Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: Created Sept. 18, 2017, 5:23 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: compiled/storage/Serializer.cpp
diff --git a/compiled/storage/Serializer.cpp b/compiled/storage/Serializer.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..18b50d574ead9b7ce82aab554d7be76dce650988
--- /dev/null
+++ b/compiled/storage/Serializer.cpp
@@ -0,0 +1,72 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <vector>
+#include "Serializer.h"
+#include "../subscription/Subscription.h"
+
+namespace
+{
+ // escape [, if nothing is escaped then `second` is false.
+ std::pair<OwnedString, bool> EscapeOpeningBracket(const String& value)
+ {
+ auto valueLength = value.length();
+ // there are only few filters with [, so it's faster to not copy the value
+ // but quickly scan firstly whether it makes sense to create a new string.
+ int bracketsCounter = 0;
+ for (String::size_type i = 0; i < valueLength; ++i)
+ {
+ if (value[i] == u'[')
+ ++bracketsCounter;
+ }
+ if (bracketsCounter == 0)
+ return std::make_pair(OwnedString(), false);
+
+ OwnedString escapedString(valueLength + bracketsCounter);
+ String::size_type writeOffset = 0;
+ for (String::size_type i = 0; i < valueLength; ++i)
+ {
+ if (value[i] == u'[')
+ escapedString[i + writeOffset++] = u'\\';
+ escapedString[i + writeOffset] = value[i];
+ }
+ return std::make_pair(std::move(escapedString), true);
+ }
+}
+
+Serializer::Serializer()
+{
+ mStream.append(u"# Adblock Plus preferences\nversion=5\n"_str);
Wladimir Palant 2017/12/21 10:30:37 I hope that this is part of your "not complete yet
+}
+
+void Serializer::Serialize(const Subscription& subscription)
+{
+ mStream.append(u"[Subscription]\n"_str);
+ mStream.append(subscription.SerializeProperties());
+
+ const auto& filters = subscription.GetFilters();
+ if (!filters.empty())
+ {
+ mStream.append(u"[Subscription filters]\n"_str);
+ for (const auto& filter : filters)
+ {
+ auto escapedResult = EscapeOpeningBracket(filter->GetText());
sergei 2017/09/19 09:17:35 BTW, what about escaping of '[' only when it's the
Wladimir Palant 2017/12/21 10:30:37 That should be fine. For now we still have to unes
+ mStream.append(escapedResult.second ? escapedResult.first : filter->GetText());
+ mStream.append(u'\n');
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld