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

Side by Side 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: rebase Created March 7, 2018, 12:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <vector>
19 #include "Serializer.h"
20 #include "../subscription/Subscription.h"
21
22 namespace
23 {
24 // escape [, if nothing is escaped then `second` is false.
25 std::pair<OwnedString, bool> EscapeOpeningBracket(const String& value)
26 {
27 auto valueLength = value.length();
28 // there are only few filters with [, so it's faster to not copy the value
29 // but quickly scan firstly whether it makes sense to create a new string.
30 int bracketsCounter = 0;
31 for (String::size_type i = 0; i < valueLength; ++i)
32 {
33 if (value[i] == u'[')
34 ++bracketsCounter;
35 }
36 if (bracketsCounter == 0)
37 return std::make_pair(OwnedString(), false);
38
39 OwnedString escapedString(valueLength + bracketsCounter);
40 String::size_type writeOffset = 0;
41 for (String::size_type i = 0; i < valueLength; ++i)
42 {
43 if (value[i] == u'[')
44 escapedString[i + writeOffset++] = u'\\';
45 escapedString[i + writeOffset] = value[i];
46 }
47 return std::make_pair(std::move(escapedString), true);
48 }
49 }
50
51 Serializer::Serializer()
52 {
53 mStream.append(u"# Adblock Plus preferences\nversion=5\n"_str);
54 }
55
56 void Serializer::Serialize(const Subscription& subscription)
57 {
58 mStream.append(u"[Subscription]\n"_str);
59 mStream.append(subscription.SerializeProperties());
60
61 const auto& filters = subscription.GetFilters();
62 if (!filters.empty())
63 {
64 mStream.append(u"[Subscription filters]\n"_str);
65 for (const auto& filter : filters)
66 {
67 auto escapedResult = EscapeOpeningBracket(filter->GetText());
68 mStream.append(escapedResult.second ? escapedResult.first : filter->GetTex t());
69 mStream.append(u'\n');
70 }
71 }
72 }
OLDNEW

Powered by Google App Engine
This is Rietveld