Skip to content

Commit 6bb5ad6

Browse files
committed
Apply clang-format to source
1 parent 3ae7222 commit 6bb5ad6

File tree

4 files changed

+136
-35
lines changed

4 files changed

+136
-35
lines changed

.clang-format

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
BasedOnStyle: Chromium
2+
---
3+
Language: Cpp
4+
AccessModifierOffset: -1
5+
ConstructorInitializerIndentWidth: 4
6+
AlignEscapedNewlinesLeft: true
7+
AlignAfterOpenBracket: true
8+
AlignTrailingComments: true
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortIfStatementsOnASingleLine: false
13+
AllowShortLoopsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: Inline
15+
AlwaysBreakAfterDefinitionReturnType: false
16+
AlwaysBreakTemplateDeclarations: true
17+
AlwaysBreakBeforeMultilineStrings: true
18+
BreakBeforeBinaryOperators: false
19+
BreakBeforeTernaryOperators: true
20+
BreakConstructorInitializersBeforeComma: false
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
ColumnLimit: 80
24+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
25+
ConstructorInitializerIndentWidth: 4
26+
DerivePointerAlignment: false
27+
ExperimentalAutoDetectBinPacking: false
28+
IndentCaseLabels: true
29+
IndentWrappedFunctionNames: false
30+
IndentFunctionDeclarationAfterType: false
31+
MaxEmptyLinesToKeep: 1
32+
KeepEmptyLinesAtTheStartOfBlocks: false
33+
NamespaceIndentation: None
34+
PenaltyBreakBeforeFirstCallParameter: 1
35+
PenaltyBreakComment: 300
36+
PenaltyBreakString: 1000
37+
PenaltyBreakFirstLessLess: 120
38+
PenaltyExcessCharacter: 1000000
39+
PenaltyReturnTypeOnItsOwnLine: 200
40+
PointerAlignment: Left
41+
SpacesBeforeTrailingComments: 2
42+
Cpp11BracedListStyle: true
43+
Standard: Cpp11
44+
IndentWidth: 2
45+
TabWidth: 8
46+
UseTab: Never
47+
BreakBeforeBraces: Attach
48+
SpacesInParentheses: false
49+
SpacesInSquareBrackets: false
50+
SpacesInAngles: false
51+
SpaceInEmptyParentheses: false
52+
SpacesInCStyleCastParentheses: false
53+
SpaceAfterCStyleCast: false
54+
SpacesInContainerLiterals: true
55+
SpaceBeforeAssignmentOperators: true
56+
ContinuationIndentWidth: 4
57+
CommentPragmas: '^ IWYU pragma:'
58+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
59+
SpaceBeforeParens: ControlStatements
60+
DisableFormat: false
61+
...
62+

UaParser.cpp

+55-22
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ struct AgentStore : GenericStore {
3535
std::string patchMinorVersionReplacement;
3636
};
3737

38-
void mark_placeholders(i2tuple& replacement_map, const std::string& device_property) {
38+
void mark_placeholders(i2tuple& replacement_map,
39+
const std::string& device_property) {
3940
auto loc = device_property.rfind("$");
4041
while (loc != std::string::npos) {
4142
const auto replacement = device_property.substr(loc + 1, 1);
@@ -56,7 +57,8 @@ DeviceStore fill_device_store(const YAML::Node& device_parser) {
5657
const auto key = it->first.as<std::string>();
5758
const auto value = it->second.as<std::string>();
5859
if (key == "regex") {
59-
device.regExpr.assign(value, boost::regex::optimize | boost::regex::normal);
60+
device.regExpr.assign(value,
61+
boost::regex::optimize | boost::regex::normal);
6062
} else if (key == "regex_flag" && value == "i") {
6163
regex_flag = true;
6264
} else if (key == "device_replacement") {
@@ -73,7 +75,9 @@ DeviceStore fill_device_store(const YAML::Node& device_parser) {
7375
}
7476
}
7577
if (regex_flag == true) {
76-
device.regExpr.assign(device.regExpr.str(), boost::regex::optimize | boost::regex::icase | boost::regex::normal);
78+
device.regExpr.assign(
79+
device.regExpr.str(),
80+
boost::regex::optimize | boost::regex::icase | boost::regex::normal);
7781
}
7882
return device;
7983
}
@@ -89,7 +93,8 @@ AgentStore fill_agent_store(const YAML::Node& node,
8993
const auto key = it->first.as<std::string>();
9094
const auto value = it->second.as<std::string>();
9195
if (key == "regex") {
92-
agent_store.regExpr.assign(value, boost::regex::optimize | boost::regex::normal);
96+
agent_store.regExpr.assign(value,
97+
boost::regex::optimize | boost::regex::normal);
9398
} else if (key == repl) {
9499
agent_store.replacement = value;
95100
mark_placeholders(agent_store.replacementMap, agent_store.replacement);
@@ -112,15 +117,21 @@ struct UAStore {
112117

113118
const auto& user_agent_parsers = regexes["user_agent_parsers"];
114119
for (const auto& user_agent : user_agent_parsers) {
115-
const auto browser =
116-
fill_agent_store(user_agent, "family_replacement", "v1_replacement", "v2_replacement", "v3_replacement");
120+
const auto browser = fill_agent_store(user_agent,
121+
"family_replacement",
122+
"v1_replacement",
123+
"v2_replacement",
124+
"v3_replacement");
117125
browserStore.push_back(browser);
118126
}
119127

120128
const auto& os_parsers = regexes["os_parsers"];
121129
for (const auto& o : os_parsers) {
122-
const auto os =
123-
fill_agent_store(o, "os_replacement", "os_v1_replacement", "os_v2_replacement", "os_v3_replacement");
130+
const auto os = fill_agent_store(o,
131+
"os_replacement",
132+
"os_v1_replacement",
133+
"os_v2_replacement",
134+
"os_v3_replacement");
124135
osStore.push_back(os);
125136
}
126137

@@ -139,15 +150,19 @@ struct UAStore {
139150
// HELPERS //
140151
/////////////
141152

142-
void replace_all_placeholders(std::string& ua_property, const boost::smatch& result, const i2tuple& replacement_map) {
143-
for (auto iter = replacement_map.rbegin(); iter != replacement_map.rend(); ++iter) {
153+
void replace_all_placeholders(std::string& ua_property,
154+
const boost::smatch& result,
155+
const i2tuple& replacement_map) {
156+
for (auto iter = replacement_map.rbegin(); iter != replacement_map.rend();
157+
++iter) {
144158
ua_property.replace(iter->first, 2, result[iter->second].str());
145159
}
146160
boost::algorithm::trim(ua_property);
147161
return;
148162
}
149163

150-
uap_cpp::Device parse_device_impl(const std::string& ua, const UAStore* ua_store) {
164+
uap_cpp::Device parse_device_impl(const std::string& ua,
165+
const UAStore* ua_store) {
151166
uap_cpp::Device device;
152167

153168
for (const auto& d : ua_store->deviceStore) {
@@ -185,13 +200,22 @@ uap_cpp::Device parse_device_impl(const std::string& ua, const UAStore* ua_store
185200
}
186201

187202
template <class AGENT, class AGENT_STORE>
188-
void fill_agent(AGENT& agent, const AGENT_STORE& store, const boost::smatch& m, const bool os) {
203+
void fill_agent(AGENT& agent,
204+
const AGENT_STORE& store,
205+
const boost::smatch& m,
206+
const bool os) {
189207
if (m.size() > 1) {
190208
agent.family =
191-
!store.replacement.empty() ? boost::regex_replace(store.replacement, boost::regex("\\$1"), m[1].str()) : m[1];
209+
!store.replacement.empty()
210+
? boost::regex_replace(
211+
store.replacement, boost::regex("\\$1"), m[1].str())
212+
: m[1];
192213
} else {
193214
agent.family =
194-
!store.replacement.empty() ? boost::regex_replace(store.replacement, boost::regex("\\$1"), m[0].str()) : m[0];
215+
!store.replacement.empty()
216+
? boost::regex_replace(
217+
store.replacement, boost::regex("\\$1"), m[0].str())
218+
: m[0];
195219
}
196220
boost::algorithm::trim(agent.family);
197221

@@ -206,7 +230,8 @@ void fill_agent(AGENT& agent, const AGENT_STORE& store, const boost::smatch& m,
206230
// }
207231

208232
if (!store.majorVersionReplacement.empty()) {
209-
agent.major = boost::regex_replace(store.majorVersionReplacement, boost::regex("\\$1"), m[1].str());
233+
agent.major = boost::regex_replace(
234+
store.majorVersionReplacement, boost::regex("\\$1"), m[1].str());
210235
} else if (m.size() > 2) {
211236
agent.major = m[2].str();
212237
}
@@ -225,7 +250,8 @@ void fill_agent(AGENT& agent, const AGENT_STORE& store, const boost::smatch& m,
225250
}
226251
}
227252

228-
uap_cpp::Agent parse_browser_impl(const std::string& ua, const UAStore* ua_store) {
253+
uap_cpp::Agent parse_browser_impl(const std::string& ua,
254+
const UAStore* ua_store) {
229255
uap_cpp::Agent browser;
230256

231257
for (const auto& b : ua_store->browserStore) {
@@ -257,7 +283,8 @@ uap_cpp::Agent parse_os_impl(const std::string& ua, const UAStore* ua_store) {
257283

258284
namespace uap_cpp {
259285

260-
UserAgentParser::UserAgentParser(const std::string& regexes_file_path) : regexes_file_path_{regexes_file_path} {
286+
UserAgentParser::UserAgentParser(const std::string& regexes_file_path)
287+
: regexes_file_path_{regexes_file_path} {
261288
ua_store_ = new UAStore(regexes_file_path);
262289
}
263290

@@ -280,32 +307,38 @@ UserAgent UserAgentParser::parse(const std::string& ua) const {
280307

281308
Device UserAgentParser::parse_device(const std::string& ua) const {
282309
try {
283-
return parse_device_impl(ua, static_cast<const UAStore*> (ua_store_));
310+
return parse_device_impl(ua, static_cast<const UAStore*>(ua_store_));
284311
} catch (...) {
285312
return Device();
286313
}
287314
}
288315

289316
Agent UserAgentParser::parse_os(const std::string& ua) const {
290317
try {
291-
return parse_os_impl(ua, static_cast<const UAStore*> (ua_store_));
318+
return parse_os_impl(ua, static_cast<const UAStore*>(ua_store_));
292319
} catch (...) {
293320
return Agent();
294321
}
295322
}
296323

297324
Agent UserAgentParser::parse_browser(const std::string& ua) const {
298325
try {
299-
return parse_browser_impl(ua, static_cast<const UAStore*> (ua_store_));
326+
return parse_browser_impl(ua, static_cast<const UAStore*>(ua_store_));
300327
} catch (...) {
301328
return Agent();
302329
}
303330
}
304331

305332
DeviceType UserAgentParser::device_type(const std::string& ua) const {
306333
// https://gist.github.com/dalethedeveloper/1503252/931cc8b613aaa930ef92a4027916e6687d07feac
307-
static boost::regex rx_mob("Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune", boost::regex::optimize | boost::regex::normal);
308-
static boost::regex rx_tabl("(tablet|ipad|playbook|silk)|(android(?!.*mobile))", boost::regex::icase | boost::regex::optimize | boost::regex::normal);
334+
static boost::regex rx_mob(
335+
"Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-"
336+
"Accelerated|(hpw|web)OS|Fennec|Minimo|Opera "
337+
"M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune",
338+
boost::regex::optimize | boost::regex::normal);
339+
static boost::regex rx_tabl(
340+
"(tablet|ipad|playbook|silk)|(android(?!.*mobile))",
341+
boost::regex::icase | boost::regex::optimize | boost::regex::normal);
309342
boost::smatch m;
310343
try {
311344
if (boost::regex_search(ua, m, rx_tabl)) {

UaParser.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ struct Agent : Generic {
2323
std::string toString() const { return family + " " + toVersionString(); }
2424

2525
std::string toVersionString() const {
26-
return (major.empty() ? "0" : major) + "." + (minor.empty() ? "0" : minor) + "." + (patch.empty() ? "0" : patch);
26+
return (major.empty() ? "0" : major) + "." + (minor.empty() ? "0" : minor) +
27+
"." + (patch.empty() ? "0" : patch);
2728
}
2829
};
2930

@@ -33,30 +34,30 @@ struct UserAgent {
3334
Agent os;
3435
Agent browser;
3536

36-
std::string toFullString() const { return browser.toString() + "/" + os.toString(); }
37+
std::string toFullString() const {
38+
return browser.toString() + "/" + os.toString();
39+
}
3740

3841
bool isSpider() const { return device.family == "Spider"; }
3942
};
4043

41-
enum DeviceType {
42-
kUnknown = 0, kDesktop, kMobile, kTablet
43-
};
44+
enum DeviceType { kUnknown = 0, kDesktop, kMobile, kTablet };
4445

4546
class UserAgentParser {
46-
public:
47+
public:
4748
explicit UserAgentParser(const std::string& regexes_file_path);
4849

4950
UserAgent parse(const std::string&) const;
5051

5152
Device parse_device(const std::string&) const;
5253
Agent parse_os(const std::string&) const;
5354
Agent parse_browser(const std::string&) const;
54-
55+
5556
DeviceType device_type(const std::string&) const;
5657

5758
~UserAgentParser();
5859

59-
private:
60+
private:
6061
const std::string regexes_file_path_;
6162
const void* ua_store_;
6263
};

UaParserTest.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const uap_cpp::UserAgentParser g_ua_parser(UA_CORE_DIR + "/regexes.yaml");
1010

1111
TEST(UserAgentParser, basic) {
1212
const auto uagent = g_ua_parser.parse(
13-
"Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 "
13+
"Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) "
14+
"AppleWebKit/534.46 "
1415
"(KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3");
1516
ASSERT_EQ("Mobile Safari", uagent.browser.family);
1617
ASSERT_EQ("5", uagent.browser.major);
@@ -84,19 +85,23 @@ TEST(OsVersion, test_ua) {
8485
}
8586

8687
TEST(BrowserVersion, firefox_user_agent_strings) {
87-
test_browser_or_os(UA_CORE_DIR + "/test_resources/firefox_user_agent_strings.yaml", true);
88+
test_browser_or_os(
89+
UA_CORE_DIR + "/test_resources/firefox_user_agent_strings.yaml", true);
8890
}
8991

9092
TEST(BrowserVersion, opera_mini_user_agent_strings) {
91-
test_browser_or_os(UA_CORE_DIR + "/test_resources/opera_mini_user_agent_strings.yaml", true);
93+
test_browser_or_os(
94+
UA_CORE_DIR + "/test_resources/opera_mini_user_agent_strings.yaml", true);
9295
}
9396

9497
TEST(BrowserVersion, pgts_browser_list) {
95-
test_browser_or_os(UA_CORE_DIR + "/test_resources/pgts_browser_list.yaml", true);
98+
test_browser_or_os(UA_CORE_DIR + "/test_resources/pgts_browser_list.yaml",
99+
true);
96100
}
97101

98102
TEST(OsVersion, additional_os_tests) {
99-
test_browser_or_os(UA_CORE_DIR + "/test_resources/additional_os_tests.yaml", false);
103+
test_browser_or_os(UA_CORE_DIR + "/test_resources/additional_os_tests.yaml",
104+
false);
100105
}
101106

102107
TEST(DeviceFamily, test_device) {

0 commit comments

Comments
 (0)