bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
BESRegex.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2005 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#ifndef _Regex_h
27#define _Regex_h 1
28
29// What's going on here? The regex.h header file is not C++ compliant. The preprocessor
30// '#if' below looks at the compiler and some macros to determine if the compiler has
31// a usable version of the C++11 regex library. If so, then the C++11 regex library is
32// used. If not, we fall back to the POSIX regex library.
33//
34// The code in the FORCE_OLD_REGEX macro is used to test the old code on OSX machines (and
35// others) for testing. Setting FORCE_OLD_REGEX to zero lets the compiler choose which code
36// to use. Setting it to one forces the use of the old POSIX-based code.
37//
38// See the README about regex tests in the unit-tests directory. Neither regex implementation
39// is faster in every case, but the new code handles complex expressions faster. jhrg 12/30/22
40//
41// NB: I added this test for OSX because on my development machine, the C++11 regex library
42// does not quite work as it does currently on linux. jhrg 1/2/13
43#if __APPLE__
44#define FORCE_OLD_REGEX 1
45#endif
46
47#if FORCE_OLD_REGEX
48
49#include <string>
50#include <memory>
51#include <regex.h>
52#define HAVE_WORKING_REGEX 0
53
54#else // FORCE_OLD_REGEX
55
56#if __cplusplus >= 201103L && \
57 (!defined(__GLIBCXX__) || (__cplusplus >= 201402L) || \
58 (defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
59 defined(_GLIBCXX_REGEX_STATE_LIMIT) || \
60 (defined(_GLIBCXX_RELEASE) && \
61 _GLIBCXX_RELEASE > 4)))
62#include <regex>
63#define HAVE_WORKING_REGEX 1
64#else
65#include <string>
66#include <memory>
67#include <regex.h>
68#define HAVE_WORKING_REGEX 0
69#endif // __cplusplus >= 201103L
70
71#endif // FORCE_OLD_REGEX
72
89{
90private:
91#if HAVE_WORKING_REGEX
92 std::regex d_exp;
93 std::string d_pattern;
94
95 void init(const char *s) { d_exp = std::regex(s); }
96 void init(const std::string &s) { d_exp = std::regex(s); } // , std::regex::basic
97#else
98 // d_preg was a regex_t* but I needed to include both regex.h and config.h
99 // to make the gnulib code work. Because this header is installed (and is
100 // used by other libraries) it cannot include config.h, so I moved the
101 // regex.h and config.h (among other) includes to the implementation. It
102 // would be cleaner to use a special class, but for one field that seems
103 // like overkill.
104 std::unique_ptr<regex_t> d_preg;
105 std::string d_pattern;
106
107 void init(const char *t);
108 void init(const std::string &s) { init(s.c_str()); d_pattern = s; }
109#endif
110
111public:
113 explicit BESRegex(const char *s) { init(s); }
115 explicit BESRegex(const std::string &s) { init(s); }
117 BESRegex(const char *s, int) { init(s); }
118
119#if HAVE_WORKING_REGEX
120 ~BESRegex() = default;
121#else
122 ~BESRegex();
123#endif
124
125 std::string pattern() const { return d_pattern; }
126
128 int match(const char *s, int len, int pos = 0) const;
130 int match(const std::string &s) const;
131
133 int search(const char *s, int len, int &matchlen, int pos = 0) const ;
135 int search(const std::string &s, int &matchlen) const;
136};
137
138#endif
Regular expression matching.
Definition BESRegex.h:89
BESRegex(const char *s)
initialize a BESRegex with a C string
Definition BESRegex.h:113
int match(const char *s, int len, int pos=0) const
Does the pattern match.
Definition BESRegex.cc:70
BESRegex(const std::string &s)
initialize a BESRegex with a C++ string
Definition BESRegex.h:115
int search(const char *s, int len, int &matchlen, int pos=0) const
Where does the pattern match.
Definition BESRegex.cc:127
BESRegex(const char *s, int)
Definition BESRegex.h:117