@@ -35,7 +35,8 @@ struct AgentStore : GenericStore {
35
35
std::string patchMinorVersionReplacement;
36
36
};
37
37
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) {
39
40
auto loc = device_property.rfind (" $" );
40
41
while (loc != std::string::npos) {
41
42
const auto replacement = device_property.substr (loc + 1 , 1 );
@@ -56,7 +57,8 @@ DeviceStore fill_device_store(const YAML::Node& device_parser) {
56
57
const auto key = it->first .as <std::string>();
57
58
const auto value = it->second .as <std::string>();
58
59
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 );
60
62
} else if (key == " regex_flag" && value == " i" ) {
61
63
regex_flag = true ;
62
64
} else if (key == " device_replacement" ) {
@@ -73,7 +75,9 @@ DeviceStore fill_device_store(const YAML::Node& device_parser) {
73
75
}
74
76
}
75
77
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 );
77
81
}
78
82
return device;
79
83
}
@@ -89,7 +93,8 @@ AgentStore fill_agent_store(const YAML::Node& node,
89
93
const auto key = it->first .as <std::string>();
90
94
const auto value = it->second .as <std::string>();
91
95
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 );
93
98
} else if (key == repl) {
94
99
agent_store.replacement = value;
95
100
mark_placeholders (agent_store.replacementMap , agent_store.replacement );
@@ -112,15 +117,21 @@ struct UAStore {
112
117
113
118
const auto & user_agent_parsers = regexes[" user_agent_parsers" ];
114
119
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" );
117
125
browserStore.push_back (browser);
118
126
}
119
127
120
128
const auto & os_parsers = regexes[" os_parsers" ];
121
129
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" );
124
135
osStore.push_back (os);
125
136
}
126
137
@@ -139,15 +150,19 @@ struct UAStore {
139
150
// HELPERS //
140
151
// ///////////
141
152
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) {
144
158
ua_property.replace (iter->first , 2 , result[iter->second ].str ());
145
159
}
146
160
boost::algorithm::trim (ua_property);
147
161
return ;
148
162
}
149
163
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) {
151
166
uap_cpp::Device device;
152
167
153
168
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
185
200
}
186
201
187
202
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) {
189
207
if (m.size () > 1 ) {
190
208
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 ];
192
213
} else {
193
214
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 ];
195
219
}
196
220
boost::algorithm::trim (agent.family );
197
221
@@ -206,7 +230,8 @@ void fill_agent(AGENT& agent, const AGENT_STORE& store, const boost::smatch& m,
206
230
// }
207
231
208
232
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 ());
210
235
} else if (m.size () > 2 ) {
211
236
agent.major = m[2 ].str ();
212
237
}
@@ -225,7 +250,8 @@ void fill_agent(AGENT& agent, const AGENT_STORE& store, const boost::smatch& m,
225
250
}
226
251
}
227
252
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) {
229
255
uap_cpp::Agent browser;
230
256
231
257
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) {
257
283
258
284
namespace uap_cpp {
259
285
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} {
261
288
ua_store_ = new UAStore (regexes_file_path);
262
289
}
263
290
@@ -280,32 +307,38 @@ UserAgent UserAgentParser::parse(const std::string& ua) const {
280
307
281
308
Device UserAgentParser::parse_device (const std::string& ua) const {
282
309
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_));
284
311
} catch (...) {
285
312
return Device ();
286
313
}
287
314
}
288
315
289
316
Agent UserAgentParser::parse_os (const std::string& ua) const {
290
317
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_));
292
319
} catch (...) {
293
320
return Agent ();
294
321
}
295
322
}
296
323
297
324
Agent UserAgentParser::parse_browser (const std::string& ua) const {
298
325
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_));
300
327
} catch (...) {
301
328
return Agent ();
302
329
}
303
330
}
304
331
305
332
DeviceType UserAgentParser::device_type (const std::string& ua) const {
306
333
// 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 );
309
342
boost::smatch m;
310
343
try {
311
344
if (boost::regex_search (ua, m, rx_tabl)) {
0 commit comments