Overpass API regression suite coverage (Saxony) / overpass_api/data/regular_expression.h

overpass_api/data/regular_expression.h

29 of 29 instrumented lines run by the suite (100.0%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___OVERPASS_API__DATA__REGULAR_EXPRESSION_H
20#define DE__OSM3S___OVERPASS_API__DATA__REGULAR_EXPRESSION_H
22#include "sys/types.h"
23#include "locale.h"
24#include "regex.h"
26#include <iostream>
27#include <string>
30struct Regular_Expression_Error
32 public:
3331 q Regular_Expression_Error(int errno_) : error_no(errno_) {}
34 int error_no;
35};
38class Regular_Expression
40 public:
41 enum Strategy { call_library, match_anything, match_nonempty };
43 Regular_Expression(const std::string& regex, bool case_sensitive)
44746147 q {
45746147 q if (regex == ".*")
467524 q strategy = match_anything;
47671130 q else if (regex == ".")
48319 q strategy = match_nonempty;
49640128 q else
50640128 q strategy = call_library;
52// cache_available = false;
53// prev_line = "";
54// prev_result = false;
56746147 q if (strategy == call_library)
57640128 q {
58640128 q setlocale(LC_ALL, "C.UTF-8");
59640128 q int case_flag = case_sensitive ? 0 : REG_ICASE;
60640128 q int error_no = regcomp(&preg, regex.c_str(), REG_EXTENDED|REG_NOSUB|case_flag);
61640128 q if (error_no != 0)
6231 q throw Regular_Expression_Error(error_no);
63640128 q }
64746147 q }
66 ~Regular_Expression()
67743146 q {
68743146 q if (strategy == call_library)
69637127 q regfree(&preg);
70743146 q }
72 inline bool matches(const std::string& line) const
735235065112 q {
745235065112 q if (strategy == match_anything)
752823216 q return true;
76520683399 q else if (strategy == match_nonempty)
776305306 q return !line.empty();
78// if (is_cache_available && line == prev_line)
79// return prev_result;
81457630398 q bool result = (regexec(&preg, line.c_str(), 0, 0, 0) == 0);
83// is_cache_available = true;
84// prev_result = result;
85// prev_line = line;
87457630398 q return (result);
885235065112 q }
90 private:
91 Regular_Expression(const Regular_Expression&);
92 const Regular_Expression& operator=(const Regular_Expression&);
94 regex_t preg;
95 Strategy strategy;
96// mutable bool cache_available;
97// mutable std::string prev_line;
98// mutable bool prev_result;
99};
101#endif