comparison OrthancFramework/Sources/HttpServer/CStringMatcher.cpp @ 4652:0ad5736c8d62

use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 May 2021 11:50:14 +0200
parents
children e8967149d87a
comparison
equal deleted inserted replaced
4651:365f51fae413 4652:0ad5736c8d62
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 **/
21
22
23 #include "../PrecompiledHeaders.h"
24 #include "CStringMatcher.h"
25
26 #include "../OrthancException.h"
27
28 #include <boost/algorithm/searching/boyer_moore.hpp>
29
30 namespace Orthanc
31 {
32 class CStringMatcher::Search : public boost::noncopyable
33 {
34 private:
35 typedef boost::algorithm::boyer_moore<const char*> Algorithm;
36
37 Algorithm algorithm_;
38
39 public:
40 // WARNING - The lifetime of "pattern_" must be larger than
41 // "search_", as the latter internally keeps a pointer to "pattern" (*)
42 explicit Search(const std::string& pattern) :
43 algorithm_(pattern.c_str(), pattern.c_str() + pattern.size())
44 {
45 }
46
47 const char* Apply(const char* start,
48 const char* end) const
49 {
50 #if BOOST_VERSION >= 106200
51 return algorithm_(start, end).first;
52 #else
53 return algorithm_(start, end);
54 #endif
55 }
56 };
57
58
59
60 CStringMatcher::CStringMatcher(const std::string& pattern) :
61 pattern_(pattern),
62 valid_(false)
63 {
64 // WARNING - Don't use "pattern" (local variable, will be
65 // destroyed once exiting the constructor) but "pattern_"
66 // (variable member, will last as long as the algorithm),
67 // otherwise lifetime is bad! (*)
68 search_.reset(new Search(pattern_));
69 }
70
71 const std::string& CStringMatcher::GetPattern() const
72 {
73 return pattern_;
74 }
75
76 bool CStringMatcher::IsValid() const
77 {
78 return valid_;
79 }
80
81
82 bool CStringMatcher::Apply(const char* start,
83 const char* end)
84 {
85 assert(search_.get() != NULL);
86
87 if (start > end)
88 {
89 throw OrthancException(ErrorCode_ParameterOutOfRange);
90 }
91
92 matchBegin_ = search_->Apply(start, end);
93
94 if (matchBegin_ == end)
95 {
96 valid_ = false;
97 }
98 else
99 {
100 matchEnd_ = matchBegin_ + pattern_.size();
101 assert(matchEnd_ <= end);
102 valid_ = true;
103 }
104
105 return valid_;
106 }
107
108
109 bool CStringMatcher::Apply(const std::string& corpus)
110 {
111 if (corpus.empty())
112 {
113 return false;
114 }
115 else
116 {
117 return Apply(corpus.c_str(), corpus.c_str() + corpus.size());
118 }
119 }
120
121
122 const char* CStringMatcher::GetMatchBegin() const
123 {
124 if (valid_)
125 {
126 return matchBegin_;
127 }
128 else
129 {
130 throw OrthancException(ErrorCode_BadSequenceOfCalls);
131 }
132 }
133
134
135 const char* CStringMatcher::GetMatchEnd() const
136 {
137 if (valid_)
138 {
139 return matchEnd_;
140 }
141 else
142 {
143 throw OrthancException(ErrorCode_BadSequenceOfCalls);
144 }
145 }
146 }