Mantid
Loading...
Searching...
No Matches
Strings.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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 +
7#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidKernel/DllConfig.h"
15#include "MantidKernel/System.h"
16
17#ifndef Q_MOC_RUN
18#include <boost/lexical_cast.hpp>
19#endif
20#include <iosfwd>
21#include <map>
22#include <set>
23#include <sstream>
24#include <string>
25#include <vector>
26
27namespace Mantid {
28namespace Kernel {
29
33namespace Strings {
34
35//------------------------------------------------------------------------------------------------
52template <typename ITERATOR_TYPE>
53DLLExport std::string simpleJoin(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator) {
54 std::ostringstream output;
55 ITERATOR_TYPE it;
56 for (it = begin; it != end;) {
57 output << *it;
58 it++;
59 if (it != end)
60 output << separator;
61 }
62 return output.str();
63}
64
65//------------------------------------------------------------------------------------------------
82template <typename ITERATOR_TYPE>
83DLLExport std::string
84join(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator,
85 typename std::enable_if<!(std::is_same<typename std::iterator_traits<ITERATOR_TYPE>::iterator_category,
86 std::random_access_iterator_tag>::value)>::type * = nullptr) {
87 return simpleJoin(begin, end, separator);
88}
89
90//------------------------------------------------------------------------------------------------
109template <typename ITERATOR_TYPE>
110DLLExport std::string
111join(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator,
112 typename std::enable_if<(std::is_same<typename std::iterator_traits<ITERATOR_TYPE>::iterator_category,
113 std::random_access_iterator_tag>::value)>::type * = nullptr) {
114
115 // Get max number of threads
116 int nmaxThreads = static_cast<int>(PARALLEL_GET_MAX_THREADS);
117
118 // Define minimum size for using threading
119 int min_size = 500 * nmaxThreads;
120
121 // Get the distance between begining and end
122 int dist = static_cast<int>(std::distance(begin, end));
123
124 if (dist < min_size) {
125
126 // If the input array is small, use the simpler function to avoid
127 // unnecessary overhead from generating the parallel section
128 return simpleJoin(begin, end, separator);
129
130 } else {
131
132 // Allocate vector space
133 std::vector<std::string> output(nmaxThreads);
134 size_t stream_size = 0;
135
136 // Actual number of threads in the current region
137 int nThreads = 1;
138#pragma omp parallel reduction(+ : stream_size)
139 {
140 nThreads = static_cast<int>(PARALLEL_NUMBER_OF_THREADS);
141 int idThread = static_cast<int>(PARALLEL_THREAD_NUMBER);
142
143 // Initialise ostringstream
144 std::ostringstream thread_stream;
145
146/* To make sure the loop is done in the right order, we use schedule(static).
147
148 From the OpenMP documentation:
149 "When schedule(static, chunk_size) is specified, iterations are divided into
150 chunks of size chunk_size, and the chunks are assigned to the threads in the
151 team in a round-robin fashion **in the order of the thread number**."
152
153 "When no chunk_size is specified, the iteration space is divided into chunks
154 that are approximately equal in size, and at most one chunk is distributed
155 to each thread."
156*/
157#pragma omp for schedule(static)
158 for (int i = 0; i < dist; i++) {
159 thread_stream << separator << *(begin + i);
160 }
161 output[idThread] = thread_stream.str();
162 stream_size += output[idThread].length();
163 }
164
165 // Reserve space in memory for output string
166 std::string master_string = output[0].erase(0, separator.length());
167 master_string.reserve(stream_size - separator.length());
168
169 // Concatenate the contributions from the remaning threads
170 for (int i = 1; i < nThreads; i++) {
171 master_string += output[i];
172 }
173
174 return master_string;
175 }
176}
177
178//------------------------------------------------------------------------------------------------
195template <typename ITERATOR_TYPE>
196DLLExport std::string joinCompress(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator = ",",
197 const std::string &listSeparator = "-") {
198
199 if (begin == end) {
200 return "";
201 }
202 std::stringstream result;
203
204 ITERATOR_TYPE i = begin;
205 // Always include the first value
206 result << *begin;
207 // move on to the next value
208 ITERATOR_TYPE previousValue = i;
209 ++i;
210
211 std::string currentSeparator = separator;
212 for (; i != end; ++i) {
213 // if it is one higher than the last value
214 if (*i == (*previousValue + 1)) {
215 currentSeparator = listSeparator;
216 } else {
217 if (currentSeparator == listSeparator) {
218 // add the last value that was the end of the list
219 result << currentSeparator;
220 result << *previousValue;
221 currentSeparator = separator;
222 }
223 // add the current value
224 result << currentSeparator;
225 result << *i;
226 }
227 previousValue = i;
228 }
229 // if we have got to the end and part of a list output the last value
230 if (currentSeparator == listSeparator) {
231 result << currentSeparator;
232 result << *previousValue;
233 }
234 return result.str();
235}
237MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length);
238
240MANTID_KERNEL_DLL std::string replace(const std::string &input, const std::string &find_what,
241 const std::string &replace_with);
244MANTID_KERNEL_DLL std::string replaceAll(const std::string &input, const std::string &charStr,
245 const std::string &substitute);
246
248MANTID_KERNEL_DLL std::string toLower(const std::string &input);
249
251MANTID_KERNEL_DLL std::string toUpper(const std::string &input);
252
254MANTID_KERNEL_DLL int confirmStr(const std::string &S, const std::string &fullPhrase);
256MANTID_KERNEL_DLL int extractWord(std::string &Line, const std::string &Word, const int cnt = 4);
258MANTID_KERNEL_DLL int endsWithInt(const std::string &word);
259
261MANTID_KERNEL_DLL std::string removeSpace(const std::string &CLine);
263MANTID_KERNEL_DLL std::string fullBlock(const std::string &A);
265MANTID_KERNEL_DLL std::string strip(const std::string &A);
267MANTID_KERNEL_DLL void stripComment(std::string &A);
269MANTID_KERNEL_DLL int isEmpty(const std::string &A);
271MANTID_KERNEL_DLL bool skipLine(const std::string &line);
274MANTID_KERNEL_DLL std::string getLine(std::istream &fh);
277MANTID_KERNEL_DLL void getLine(std::istream &fh, std::string &Line);
279MANTID_KERNEL_DLL std::string peekLine(std::istream &fh);
281MANTID_KERNEL_DLL int getPartLine(std::istream &fh, std::string &Out, std::string &Excess, const int spc = 256);
282
284template <typename T> int convPartNum(const std::string &A, T &out);
285
287template <typename T> int convert(const std::string &A, T &out);
289template <typename T> int convert(const char *A, T &out);
290
292template <typename T> std::string toString(const T &value);
293
295template <typename T> std::string toString(const std::vector<T> &value);
296
298template <typename T> std::string toString(const std::set<T> &value);
299
300template <typename T> int setValues(const std::string &Line, const std::vector<int> &Index, std::vector<T> &Out);
301
303template <typename T> int sectPartNum(std::string &A, T &out);
304
306template <typename T> int section(std::string &A, T &out);
308template <typename T> int section(char *cA, T &out);
309
311template <typename T> int sectionMCNPX(std::string &A, T &out);
312
314MANTID_KERNEL_DLL void writeMCNPX(const std::string &Line, std::ostream &OX);
315
317MANTID_KERNEL_DLL std::vector<std::string> StrParts(const std::string &Ln);
318
320MANTID_KERNEL_DLL std::map<std::string, std::string>
321splitToKeyValues(const std::string &input, const std::string &keyValSep = "=", const std::string &listSep = ",");
322
324template <template <typename T, typename A> class V, typename T, typename A>
325int writeFile(const std::string &Fname, const T &step, const V<T, A> &Y);
326template <template <typename T, typename A> class V, typename T, typename A>
327int writeFile(const std::string &Fname, const V<T, A> &X, const V<T, A> &Y);
328template <template <typename T, typename A> class V, typename T, typename A>
329int writeFile(const std::string &Fname, const V<T, A> &X, const V<T, A> &Y, const V<T, A> &Err);
330
332float getVAXnum(const float A);
333
335MANTID_KERNEL_DLL void readToEndOfLine(std::istream &in, bool ConsumeEOL);
337MANTID_KERNEL_DLL std::string getWord(std::istream &in, bool consumeEOL);
340MANTID_KERNEL_DLL size_t split_path(const std::string &path, std::vector<std::string> &path_components);
341
343MANTID_KERNEL_DLL std::string loadFile(const std::string &filename);
344
346MANTID_KERNEL_DLL int isMember(const std::vector<std::string> &group, const std::string &candidate);
347
350MANTID_KERNEL_DLL std::vector<int> parseRange(const std::string &str, const std::string &elemSep = ",",
351 const std::string &rangeSep = "-");
352
355template <typename Integer> std::vector<std::vector<Integer>> parseGroups(const std::string &str) {
356 std::vector<std::vector<Integer>> groups;
357
358 // Local helper functions.
359 auto translateAdd = [&groups](const std::string &str) {
360 const auto tokens = Kernel::StringTokenizer(
362 std::vector<Integer> currentGroup;
363 currentGroup.reserve(tokens.count());
364 std::transform(tokens.cbegin(), tokens.cend(), std::back_inserter(currentGroup),
365 [](const auto &t) { return boost::lexical_cast<Integer>(t); });
366 groups.emplace_back(std::move(currentGroup));
367 };
368
369 auto translateSumRange = [&groups](const std::string &str) {
370 // add a group with the numbers in the range
371 const auto tokens = Kernel::StringTokenizer(
373 if (tokens.count() != 2)
374 throw std::runtime_error("Malformed range (-) operation.");
375 Integer first = boost::lexical_cast<Integer>(tokens[0]);
376 Integer last = boost::lexical_cast<Integer>(tokens[1]);
377 if (first > last)
378 std::swap(first, last);
379 // add all the numbers in the range to the output group
380 std::vector<Integer> group;
381 group.reserve(last - first + 1);
382 for (Integer i = first; i <= last; ++i)
383 group.emplace_back(i);
384 if (!group.empty())
385 groups.emplace_back(std::move(group));
386 };
387
388 auto translateRange = [&groups](const std::string &str) {
389 // add a group per number
390 const auto tokens = Kernel::StringTokenizer(
392 if (tokens.count() != 2)
393 throw std::runtime_error("Malformed range (:) operation.");
394 Integer first = boost::lexical_cast<Integer>(tokens[0]);
395 Integer last = boost::lexical_cast<Integer>(tokens[1]);
396 if (first > last)
397 std::swap(first, last);
398 // add all the numbers in the range to separate output groups
399 for (Integer i = first; i <= last; ++i) {
400 groups.emplace_back(1, i);
401 }
402 };
403
404 try {
405 // split into comma separated groups, each group potentially containing
406 // an operation (+-:) that produces even more groups.
408 for (const auto &token : tokens) {
409 // Look for the various operators in the string. If one is found then
410 // do the necessary translation into groupings.
411 if (token.find('+') != std::string::npos) {
412 translateAdd(token);
413 } else if (token.find('-') != std::string::npos) {
414 translateSumRange(token);
415 } else if (token.find(':') != std::string::npos) {
416 translateRange(token);
417 } else if (!token.empty()) {
418 // contains a single number, just add it as a new group
419 groups.emplace_back(1, boost::lexical_cast<Integer>(token));
420 }
421 }
422 } catch (boost::bad_lexical_cast &) {
423 throw std::runtime_error("Cannot parse numbers from string: '" + str + "'");
424 }
425
426 return groups;
427}
428
430MANTID_KERNEL_DLL std::istream &extractToEOL(std::istream &is, std::string &str);
431
432} // NAMESPACE Strings
433
434} // NAMESPACE Kernel
435
436} // NAMESPACE Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
#define PARALLEL_THREAD_NUMBER
#define PARALLEL_NUMBER_OF_THREADS
#define PARALLEL_GET_MAX_THREADS
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
Impliments a line.
Definition: Line.h:43
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
Holds support functions for strings.
Definition: RegexStrings.h:16
MANTID_KERNEL_DLL std::string removeSpace(const std::string &CLine)
strip all spaces
Definition: Strings.cpp:300
MANTID_KERNEL_DLL std::string toLower(const std::string &input)
Converts string to all lowercase.
Definition: Strings.cpp:116
MANTID_KERNEL_DLL std::istream & extractToEOL(std::istream &is, std::string &str)
Extract a line from input stream, discarding any EOL characters encountered.
Definition: Strings.cpp:1137
int sectPartNum(std::string &A, T &out)
Convert and cut a string.
Definition: Strings.cpp:527
MANTID_KERNEL_DLL size_t split_path(const std::string &path, std::vector< std::string > &path_components)
function parses a path, found in input string "path" and returns vector of the folders contributed in...
Definition: Strings.cpp:970
MANTID_KERNEL_DLL int confirmStr(const std::string &S, const std::string &fullPhrase)
determine if a character group exists in a string
Definition: Strings.cpp:239
float getVAXnum(const float A)
Convert a VAX number to x86 little eindien.
Definition: Strings.cpp:491
DLLExport std::string joinCompress(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator=",", const std::string &listSeparator="-")
Join a set or vector of (something that turns into a string) together into one string,...
Definition: Strings.h:196
MANTID_KERNEL_DLL std::vector< int > parseRange(const std::string &str, const std::string &elemSep=",", const std::string &rangeSep="-")
Parses a number range, e.g.
Definition: Strings.cpp:1071
MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length)
Converts long strings into "start ... end".
Definition: Strings.cpp:54
int convPartNum(const std::string &A, T &out)
Takes a character string and evaluates the first [typename T] object.
Definition: Strings.cpp:639
MANTID_KERNEL_DLL int isMember(const std::vector< std::string > &group, const std::string &candidate)
checks if the candidate is the member of the group
Definition: Strings.cpp:1050
DLLExport std::string join(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator, typename std::enable_if<!(std::is_same< typename std::iterator_traits< ITERATOR_TYPE >::iterator_category, std::random_access_iterator_tag >::value)>::type *=nullptr)
Join a set or vector of (something that turns into a string) together into one string,...
Definition: Strings.h:84
MANTID_KERNEL_DLL std::string replace(const std::string &input, const std::string &find_what, const std::string &replace_with)
Return a string with all matching occurence-strings.
Definition: Strings.cpp:74
int section(std::string &A, T &out)
Convert and cut a string.
Definition: Strings.cpp:573
MANTID_KERNEL_DLL std::string peekLine(std::istream &fh)
Peek at a line without extracting it from the stream.
Definition: Strings.cpp:344
MANTID_KERNEL_DLL std::string replaceAll(const std::string &input, const std::string &charStr, const std::string &substitute)
Return a string with all occurrences of the characters in the input replaced by the replace string.
Definition: Strings.cpp:94
std::vector< std::vector< Integer > > parseGroups(const std::string &str)
Parses unsigned integer groups, e.g.
Definition: Strings.h:355
MANTID_KERNEL_DLL std::string fullBlock(const std::string &A)
strip pre/post spaces
Definition: Strings.cpp:389
MANTID_KERNEL_DLL bool skipLine(const std::string &line)
Determines if a string starts with a #.
Definition: Strings.cpp:408
int sectionMCNPX(std::string &A, T &out)
Convert and cut a string for MCNPX.
Definition: Strings.cpp:603
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
Definition: Strings.cpp:28
MANTID_KERNEL_DLL std::vector< std::string > StrParts(std::string, const boost::regex &)
Split a line into component parts.
MANTID_KERNEL_DLL std::string toUpper(const std::string &input)
Converts string to all uppercase.
Definition: Strings.cpp:124
MANTID_KERNEL_DLL int endsWithInt(const std::string &word)
Get an int from the end of a word.
Definition: Strings.cpp:210
MANTID_KERNEL_DLL void writeMCNPX(const std::string &Line, std::ostream &OX)
Write file in standard MCNPX input form.
Definition: Strings.cpp:421
MANTID_KERNEL_DLL std::string getLine(std::istream &fh)
Get a line and strip comments Use only for a single call.
Definition: Strings.cpp:319
MANTID_KERNEL_DLL void stripComment(std::string &A)
strip trailling comments
Definition: Strings.cpp:370
MANTID_KERNEL_DLL int extractWord(std::string &Line, const std::string &Word, const int cnt=4)
Get a word from a string.
Definition: Strings.cpp:186
MANTID_KERNEL_DLL std::map< std::string, std::string > splitToKeyValues(const std::string &input, const std::string &keyValSep="=", const std::string &listSep=",")
Splits a string into key value pairs.
Definition: Strings.cpp:469
int setValues(const std::string &Line, const std::vector< int > &Index, std::vector< T > &Out)
Call to read in various values in position x1,x2,x3 from the line.
Definition: Strings.cpp:844
MANTID_KERNEL_DLL int isEmpty(const std::string &A)
Determines if a string is only spaces.
Definition: Strings.cpp:359
int writeFile(const std::string &Fname, const T &step, const V< T, A > &Y)
Write a set of containers to a file.
Definition: Strings.cpp:763
DLLExport std::string simpleJoin(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator)
Join a set or vector of (something that turns into a string) together into one string,...
Definition: Strings.h:53
MANTID_KERNEL_DLL int getPartLine(std::istream &fh, std::string &Out, std::string &Excess, const int spc=256)
get a part of a long line
Definition: Strings.cpp:262
Helper class which provides the Collimation Length for SANS instruments.