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

overpass_api/data/regular_expression.h

27 of 29 instrumented lines run by the suite (93.1%) 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:
330 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)
44681133 q {
45681133 q if (regex == ".*")
467825 q strategy = match_anything;
47603114 q else if (regex == ".")
485516 q strategy = match_nonempty;
49548108 q else
50548108 q strategy = call_library;
52// cache_available = false;
53// prev_line = "";
54// prev_result = false;
56681133 q if (strategy == call_library)
57548108 q {
58548108 q setlocale(LC_ALL, "C.UTF-8");
59548108 q int case_flag = case_sensitive ? 0 : REG_ICASE;
60548108 q int error_no = regcomp(&preg, regex.c_str(), REG_EXTENDED|REG_NOSUB|case_flag);
61548108 q if (error_no != 0)
620 throw Regular_Expression_Error(error_no);
63548108 q }
64681133 q }
66 ~Regular_Expression()
67681133 q {
68681133 q if (strategy == call_library)
69548108 q regfree(&preg);
70681133 q }
72 inline bool matches(const std::string& line) const
731943899105 q {
741943899105 q if (strategy == match_anything)
7527263019 q return true;
76167126989 q else if (strategy == match_nonempty)
7761131113 q return !line.empty();
78// if (is_cache_available && line == prev_line)
79// return prev_result;
81105995884 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;
87105995884 q return (result);
881943899105 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