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 +
10
11#include <algorithm>
12#include <vector>
13
15
16namespace {
17Logger logger("Regex");
18}
19
28template <typename T> int StrComp(const std::string &Text, const boost::regex &Re, T &Aout, const int compNum) {
29 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
30 boost::sregex_iterator empty;
31 // Failed search
32 if (m1 == empty || static_cast<int>((*m1).size()) < compNum)
33 return 0;
34 int count = compNum;
35 for (; count != 0; count--)
36 m1++;
37 return convert((*m1)[0].str(), Aout);
38}
39
48template <typename T> int StrComp(const char *Text, const boost::regex &Re, T &Aout, const int compNum) {
49 return StrComp(std::string(Text), Re, Aout, compNum);
50}
51
58int StrLook(const std::string &Text, const boost::regex &Re) {
59 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
60 boost::sregex_iterator empty;
61 // Failed search
62 if (m1 == empty)
63 return 0;
64 return 1;
65}
66
74std::vector<std::string> StrParts(std::string Sdx, const boost::regex &Re) {
75 std::vector<std::string> Aout;
76 boost::regex_split(std::back_inserter(Aout), Sdx,
77 Re); // Destroys string in process
78 return Aout;
79}
80
93template <typename T> int StrFullCut(std::string &Text, const boost::regex &Re, T &Aout, const int compNum) {
94 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
95 boost::sregex_iterator empty;
96 if (m1 == empty)
97 return 0;
98
99 if (compNum + 1 >= static_cast<int>(m1->size()))
100 return 0;
101 // Mantid::Kernel::Strings::Convert to required output form
102 if (!Mantid::Kernel::Strings::convert((*m1)[compNum + 1].str(), Aout))
103 return 0;
104 // Found object
105 unsigned int zero = 0; // Needed for boost 1.40 (can't just put 0 in next line)
106 Text.erase(m1->position(zero), (*m1)[0].str().length());
107 return 1;
108}
109
121template <typename T> int StrFullCut(std::string &Text, const boost::regex &Re, std::vector<T> &Aout) {
122 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
123 boost::sregex_iterator empty;
124 if (m1 == empty)
125 return 0;
126
127 logger.information() << "SFC :: \n";
128 Aout.clear();
129 unsigned int zero = 0; // Needed for boost 1.40
130 const size_t M0 = m1->position(zero);
131 size_t ML = M0;
132 for (; m1 != empty; m1++) {
133 for (unsigned int index = 1; index < m1->size(); index++) {
134 T tmp;
135 if (!Mantid::Kernel::Strings::convert((*m1)[index].str(), tmp))
136 return 0;
137 Aout.emplace_back(tmp);
138 }
139 ML = m1->position(zero) + (*m1)[0].str().length();
140 }
141 logger.information() << "SFC :: " << M0 << " " << ML << '\n';
142 // Found object
143 Text.erase(M0, ML);
144 return 1;
145}
146
160template <> int StrFullCut(std::string &Text, const boost::regex &Re, std::vector<std::string> &Aout) {
161 boost::sregex_iterator m1(Text.begin(), Text.end(), Re);
162 boost::sregex_iterator empty;
163 if (m1 == empty)
164 return 0;
165
166 unsigned int zero = 0; // Needed for boost 1.40
167 const auto M0 = static_cast<int>(m1->position(zero));
168 int ML = M0;
169 for (; m1 != empty; m1++) {
170 ML = static_cast<int>(m1->position(zero) + (*m1)[0].str().length());
171 for (unsigned int index = 1; index < m1->size(); index++)
172 Aout.emplace_back((*m1)[index].str());
173 }
174 logger.information() << "SFC :: " << M0 << " " << ML << '\n';
175 // Found object
176 Text.erase(M0, ML);
177 return 1;
178}
179
191template <typename T> int StrFullSplit(const std::string &text, const boost::regex &Re, std::vector<T> &Aout) {
192 boost::sregex_iterator m1(text.begin(), text.end(), Re);
193 boost::sregex_iterator empty;
194 for (; m1 != empty; m1++)
195 for (unsigned int index = 1; index < m1->size(); index++) {
196 T tmp;
197 if (!Mantid::Kernel::Strings::convert((*m1)[index].str(), tmp))
198 return static_cast<int>(Aout.size());
199 Aout.emplace_back(tmp);
200 }
201 return static_cast<int>(Aout.size());
202}
203
215template <typename T> int StrSingleSplit(const std::string &text, const boost::regex &Re, std::vector<T> &Aout) {
216 boost::sregex_iterator m1(text.begin(), text.end(), Re);
217 boost::sregex_iterator empty;
218 if (m1 != empty)
219 for (unsigned int index = 1; index < m1->size(); index++) {
220 T tmp;
221 if (!Mantid::Kernel::Strings::convert((*m1)[index].str(), tmp))
222 return static_cast<int>(Aout.size());
223 Aout.emplace_back(tmp);
224 }
225
226 return static_cast<int>(Aout.size());
227}
228
241template <> int StrSingleSplit(const std::string &text, const boost::regex &Re, std::vector<std::string> &Aout) {
242 boost::sregex_iterator m1(text.begin(), text.end(), Re);
243 boost::sregex_iterator empty;
244 if (m1 != empty) {
245 for (unsigned int index = 1; index < m1->size(); index++)
246 Aout.emplace_back((*m1)[index].str());
247 return 1;
248 }
249 return 0;
250}
251
259DLLExport int findPattern(std::istream &fh, const boost::regex &Re, std::string &Out) {
260 char ss[512]; // max of 512
261 boost::cmatch ans;
262
263 int cnt = 1;
264 fh.getline(ss, 512, '\n');
265 while (!fh.fail() && !boost::regex_search(ss, ans, Re, boost::match_default)) {
266 fh.getline(ss, 512, '\n');
267 cnt++;
268 }
269 if (fh.fail())
270 return 0;
271 Out = ss;
272 return cnt;
273}
274
282template <typename T> int findComp(std::istream &fh, const boost::regex &Re, T &Out) {
283 char ss[512]; // max of 512
284 boost::cmatch ans;
285
286 int cnt(1);
287 fh.getline(ss, 512, '\n');
288 while (!fh.fail() && !boost::regex_search(ss, ans, Re, boost::match_default)) {
289 cnt++;
290 fh.getline(ss, 512, '\n');
291 }
292 if (ans[0].matched) {
293 std::string xout(ans[1].first, ans[1].second);
295 return cnt;
296 }
297 return 0;
298}
299
307template <> DLLExport int findComp(std::istream &fh, const boost::regex &Re, std::string &Out) {
308 char ss[512]; // max of 512
309 boost::cmatch ans;
310
311 int cnt(1);
312 fh.getline(ss, 512, '\n');
313 while (!fh.fail() && !boost::regex_search(ss, ans, Re, boost::match_default)) {
314 cnt++;
315 fh.getline(ss, 512, '\n');
316 }
317 if (ans[0].matched) {
318 Out = std::string(ans[1].first, ans[1].second);
319 return cnt;
320 }
321 return 0;
322}
323
325
326template DLLExport int StrFullCut(std::string &, const boost::regex &, std::string &, const int);
327template DLLExport int StrFullCut(std::string &, const boost::regex &, int &, const int);
328template DLLExport int StrFullCut(std::string &, const boost::regex &, double &, const int);
329
330template DLLExport int StrFullSplit(const std::string &, const boost::regex &, std::vector<int> &);
331template DLLExport int StrFullSplit(const std::string &, const boost::regex &, std::vector<double> &);
332template DLLExport int StrFullSplit(const std::string &, const boost::regex &, std::vector<std::string> &);
333
334template DLLExport int StrSingleSplit(const std::string &, const boost::regex &, std::vector<int> &);
335template DLLExport int StrSingleSplit(const std::string &, const boost::regex &, std::vector<double> &);
336
337template DLLExport int StrComp(const char *, const boost::regex &, double &, const int);
338template DLLExport int StrComp(const char *, const boost::regex &, int &, const int);
339template DLLExport int StrComp(const std::string &, const boost::regex &, double &, const int);
340template DLLExport int StrComp(const std::string &, const boost::regex &, int &, const int);
341
342template DLLExport int findComp(std::istream &, const boost::regex &, int &);
343
345
346} // namespace Mantid::Kernel::Strings
gsl_vector * tmp
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
int count
counter
Definition: Matrix.cpp:37
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
Holds support functions for strings.
Definition: RegexStrings.h:16
MANTID_KERNEL_DLL int findPattern(std::istream &, const boost::regex &, std::string &)
Finds a pattern in a file.
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.
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:665