Mantid
Loading...
Searching...
No Matches
RegexStrings.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidKernel/DllConfig.h"
11
12#include <algorithm>
13#include <vector>
14
16
17namespace {
18Logger logger("Regex");
19}
20
29template <typename T> int StrComp(const std::string &Text, const boost::regex &Re, T &Aout, const int compNum) {
30 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
31 boost::sregex_iterator empty;
32 // Failed search
33 if (m1 == empty || static_cast<int>((*m1).size()) < compNum)
34 return 0;
35 int count = compNum;
36 for (; count != 0; count--)
37 m1++;
38 return convert((*m1)[0].str(), Aout);
39}
40
49template <typename T> int StrComp(const char *Text, const boost::regex &Re, T &Aout, const int compNum) {
50 return StrComp(std::string(Text), Re, Aout, compNum);
51}
52
59int StrLook(const std::string &Text, const boost::regex &Re) {
60 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
61 boost::sregex_iterator empty;
62 // Failed search
63 if (m1 == empty)
64 return 0;
65 return 1;
66}
67
75std::vector<std::string> StrParts(std::string Sdx, const boost::regex &Re) {
76 std::vector<std::string> Aout;
77 boost::regex_split(std::back_inserter(Aout), Sdx,
78 Re); // Destroys string in process
79 return Aout;
80}
81
94template <typename T> int StrFullCut(std::string &Text, const boost::regex &Re, T &Aout, const int compNum) {
95 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
96 boost::sregex_iterator empty;
97 if (m1 == empty)
98 return 0;
99
100 if (compNum + 1 >= static_cast<int>(m1->size()))
101 return 0;
102 // Mantid::Kernel::Strings::Convert to required output form
103 if (!Mantid::Kernel::Strings::convert((*m1)[compNum + 1].str(), Aout))
104 return 0;
105 // Found object
106 unsigned int zero = 0; // Needed for boost 1.40 (can't just put 0 in next line)
107 Text.erase(m1->position(zero), (*m1)[0].str().length());
108 return 1;
109}
110
122template <typename T> int StrFullCut(std::string &Text, const boost::regex &Re, std::vector<T> &Aout) {
123 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
124 boost::sregex_iterator empty;
125 if (m1 == empty)
126 return 0;
127
128 logger.information() << "SFC :: \n";
129 Aout.clear();
130 unsigned int zero = 0; // Needed for boost 1.40
131 const size_t M0 = m1->position(zero);
132 size_t ML = M0;
133 for (; m1 != empty; m1++) {
134 for (unsigned int index = 1; index < m1->size(); index++) {
135 T tmp;
136 if (!Mantid::Kernel::Strings::convert((*m1)[index].str(), tmp))
137 return 0;
138 Aout.emplace_back(tmp);
139 }
140 ML = m1->position(zero) + (*m1)[0].str().length();
141 }
142 logger.information() << "SFC :: " << M0 << " " << ML << '\n';
143 // Found object
144 Text.erase(M0, ML);
145 return 1;
146}
147
161template <> int StrFullCut(std::string &Text, const boost::regex &Re, std::vector<std::string> &Aout) {
162 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
163 boost::sregex_iterator empty;
164 if (m1 == empty)
165 return 0;
166
167 unsigned int zero = 0; // Needed for boost 1.40
168 const auto M0 = static_cast<int>(m1->position(zero));
169 int ML = M0;
170 for (; m1 != empty; m1++) {
171 ML = static_cast<int>(m1->position(zero) + (*m1)[0].str().length());
172 for (unsigned int index = 1; index < m1->size(); index++)
173 Aout.emplace_back((*m1)[index].str());
174 }
175 logger.information() << "SFC :: " << M0 << " " << ML << '\n';
176 // Found object
177 Text.erase(M0, ML);
178 return 1;
179}
180
192template <typename T> int StrFullSplit(const std::string &text, const boost::regex &Re, std::vector<T> &Aout) {
193 boost::sregex_iterator m1(text.begin(), text.end(), Re);
194 boost::sregex_iterator empty;
195 for (; m1 != empty; m1++)
196 for (unsigned int index = 1; index < m1->size(); index++) {
197 T tmp;
198 if (!Mantid::Kernel::Strings::convert((*m1)[index].str(), tmp))
199 return static_cast<int>(Aout.size());
200 Aout.emplace_back(tmp);
201 }
202 return static_cast<int>(Aout.size());
203}
204
216template <typename T> int StrSingleSplit(const std::string &text, const boost::regex &Re, std::vector<T> &Aout) {
217 boost::sregex_iterator m1(text.begin(), text.end(), Re);
218 boost::sregex_iterator empty;
219 if (m1 != empty)
220 for (unsigned int index = 1; index < m1->size(); index++) {
221 T tmp;
222 if (!Mantid::Kernel::Strings::convert((*m1)[index].str(), tmp))
223 return static_cast<int>(Aout.size());
224 Aout.emplace_back(tmp);
225 }
226
227 return static_cast<int>(Aout.size());
228}
229
242template <> int StrSingleSplit(const std::string &text, const boost::regex &Re, std::vector<std::string> &Aout) {
243 boost::sregex_iterator m1(text.begin(), text.end(), Re);
244 boost::sregex_iterator empty;
245 if (m1 != empty) {
246 for (unsigned int index = 1; index < m1->size(); index++)
247 Aout.emplace_back((*m1)[index].str());
248 return 1;
249 }
250 return 0;
251}
252
260MANTID_KERNEL_DLL int findPattern(std::istream &fh, const boost::regex &Re, std::string &Out) {
261 char ss[512]; // max of 512
262 boost::cmatch ans;
263
264 int cnt = 1;
265 fh.getline(ss, 512, '\n');
266 while (!fh.fail() && !boost::regex_search(ss, ans, Re, boost::match_default)) {
267 fh.getline(ss, 512, '\n');
268 cnt++;
269 }
270 if (fh.fail())
271 return 0;
272 Out = ss;
273 return cnt;
274}
275
283template <typename T> int findComp(std::istream &fh, const boost::regex &Re, T &Out) {
284 char ss[512]; // max of 512
285 boost::cmatch ans;
286
287 int cnt(1);
288 fh.getline(ss, 512, '\n');
289 while (!fh.fail() && !boost::regex_search(ss, ans, Re, boost::match_default)) {
290 cnt++;
291 fh.getline(ss, 512, '\n');
292 }
293 if (ans[0].matched) {
294 std::string xout(ans[1].first, ans[1].second);
296 return cnt;
297 }
298 return 0;
299}
300
308template <> MANTID_KERNEL_DLL int findComp(std::istream &fh, const boost::regex &Re, std::string &Out) {
309 char ss[512]; // max of 512
310 boost::cmatch ans;
311
312 int cnt(1);
313 fh.getline(ss, 512, '\n');
314 while (!fh.fail() && !boost::regex_search(ss, ans, Re, boost::match_default)) {
315 cnt++;
316 fh.getline(ss, 512, '\n');
317 }
318 if (ans[0].matched) {
319 Out = std::string(ans[1].first, ans[1].second);
320 return cnt;
321 }
322 return 0;
323}
324
326
327template MANTID_KERNEL_DLL int StrFullCut(std::string &, const boost::regex &, std::string &, const int);
328template MANTID_KERNEL_DLL int StrFullCut(std::string &, const boost::regex &, int &, const int);
329template MANTID_KERNEL_DLL int StrFullCut(std::string &, const boost::regex &, double &, const int);
330
331template MANTID_KERNEL_DLL int StrFullSplit(const std::string &, const boost::regex &, std::vector<int> &);
332template MANTID_KERNEL_DLL int StrFullSplit(const std::string &, const boost::regex &, std::vector<double> &);
333template MANTID_KERNEL_DLL int StrFullSplit(const std::string &, const boost::regex &, std::vector<std::string> &);
334
335template MANTID_KERNEL_DLL int StrSingleSplit(const std::string &, const boost::regex &, std::vector<int> &);
336template MANTID_KERNEL_DLL int StrSingleSplit(const std::string &, const boost::regex &, std::vector<double> &);
337
338template MANTID_KERNEL_DLL int StrComp(const char *, const boost::regex &, double &, const int);
339template MANTID_KERNEL_DLL int StrComp(const char *, const boost::regex &, int &, const int);
340template MANTID_KERNEL_DLL int StrComp(const std::string &, const boost::regex &, double &, const int);
341template MANTID_KERNEL_DLL int StrComp(const std::string &, const boost::regex &, int &, const int);
342
343template MANTID_KERNEL_DLL int findComp(std::istream &, const boost::regex &, int &);
344
346
347} // namespace Mantid::Kernel::Strings
gsl_vector * tmp
std::map< DeltaEMode::Type, std::string > index
int count
counter
Definition Matrix.cpp:37
Holds support functions for strings.
MANTID_KERNEL_DLL int findComp(std::istream &, const boost::regex &, T &)
Find a compmonent in a Regex in a file.
MANTID_KERNEL_DLL int StrComp(const char *, const boost::regex &, T &, const int=0)
Find if a pattern matches a string.
MANTID_KERNEL_DLL int findPattern(std::istream &, const boost::regex &, std::string &)
Finds a pattern in a file.
int StrSingleSplit(const std::string &, const boost::regex &, std::vector< T > &)
Split a line searched parts.
int StrFullCut(std::string &, const boost::regex &, T &, const int=-1)
Cut out the searched section and returns component.
int StrFullSplit(const std::string &, const boost::regex &, std::vector< T > &)
Split a line searched parts.
MANTID_KERNEL_DLL int StrLook(const std::string &, const boost::regex &)
Find is a pattern matches.
MANTID_KERNEL_DLL std::vector< std::string > StrParts(std::string, const boost::regex &)
Split a line into component parts.
int convert(const std::string &A, T &out)
Convert a string into a number.
Definition Strings.cpp:697