12#include "MantidKernel/DllConfig.h"
18#include <boost/lexical_cast.hpp>
52template <
typename ITERATOR_TYPE>
53DLLExport std::string
simpleJoin(ITERATOR_TYPE begin, ITERATOR_TYPE end,
const std::string &separator) {
54 std::ostringstream output;
56 for (it = begin; it != end;) {
82template <
typename ITERATOR_TYPE>
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) {
109template <
typename ITERATOR_TYPE>
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) {
119 int min_size = 500 * nmaxThreads;
122 int dist =
static_cast<int>(std::distance(begin, end));
124 if (dist < min_size) {
133 std::vector<std::string> output(nmaxThreads);
134 size_t stream_size = 0;
138#pragma omp parallel reduction(+ : stream_size)
144 std::ostringstream thread_stream;
157#pragma omp for schedule(static)
158 for (
int i = 0; i < dist; i++) {
159 thread_stream << separator << *(begin + i);
161 output[idThread] = thread_stream.str();
162 stream_size += output[idThread].length();
166 std::string master_string = output[0].erase(0, separator.length());
167 master_string.reserve(stream_size - separator.length());
171 for (
int i = 1; i < nThreads; i++) {
172 master_string += output[i];
175 return master_string;
196template <
typename ITERATOR_TYPE>
198 const std::string &listSeparator =
"-") {
203 std::stringstream result;
205 ITERATOR_TYPE i = begin;
209 ITERATOR_TYPE previousValue = i;
212 std::string currentSeparator = separator;
213 for (; i != end; ++i) {
215 if (*i == (*previousValue + 1)) {
216 currentSeparator = listSeparator;
218 if (currentSeparator == listSeparator) {
220 result << currentSeparator;
221 result << *previousValue;
222 currentSeparator = separator;
225 result << currentSeparator;
231 if (currentSeparator == listSeparator) {
232 result << currentSeparator;
233 result << *previousValue;
238MANTID_KERNEL_DLL std::string
shorten(
const std::string &input,
const size_t max_length);
241MANTID_KERNEL_DLL std::string
replace(
const std::string &input,
const std::string &find_what,
242 const std::string &replace_with);
245MANTID_KERNEL_DLL std::string
replaceAll(std::string
const &input,
char const to_replace,
char const substitute);
249MANTID_KERNEL_DLL std::string
replaceAll(
const std::string &input,
const std::string &charStr,
250 const std::string &substitute);
253MANTID_KERNEL_DLL std::string
toLower(
const std::string &input);
256MANTID_KERNEL_DLL std::string
toUpper(
const std::string &input);
259MANTID_KERNEL_DLL
bool endsWith(std::string
const &str, std::string
const &suffix);
262MANTID_KERNEL_DLL
int confirmStr(
const std::string &S,
const std::string &fullPhrase);
264MANTID_KERNEL_DLL
int extractWord(std::string &
Line,
const std::string &Word,
const int cnt = 4);
266MANTID_KERNEL_DLL
int endsWithInt(
const std::string &word);
269MANTID_KERNEL_DLL std::string
removeSpace(
const std::string &CLine);
271MANTID_KERNEL_DLL std::string
fullBlock(
const std::string &A);
273MANTID_KERNEL_DLL std::string strip(
const std::string &A);
279MANTID_KERNEL_DLL
int isEmpty(
const std::string &A);
281MANTID_KERNEL_DLL
bool skipLine(
const std::string &line);
284MANTID_KERNEL_DLL std::string
getLine(std::istream &fh);
287MANTID_KERNEL_DLL
void getLine(std::istream &fh, std::string &
Line);
289MANTID_KERNEL_DLL std::string
peekLine(std::istream &fh);
291MANTID_KERNEL_DLL
int getPartLine(std::istream &fh, std::string &Out, std::string &Excess,
const int spc = 256);
294template <
typename T>
int convPartNum(
const std::string &A, T &out);
297template <
typename T>
int convert(
const std::string &A, T &out);
299template <
typename T>
int convert(
const char *A, T &out);
305template <
typename T> std::string
toString(
const std::vector<T> &
value);
308template <
typename T> std::string
toString(
const std::set<T> &
value);
310template <
typename T>
int setValues(
const std::string &
Line,
const std::vector<int> &Index, std::vector<T> &Out);
313template <
typename T>
int sectPartNum(std::string &A, T &out);
316template <
typename T>
int section(std::string &A, T &out);
318template <
typename T>
int section(
char *cA, T &out);
321template <
typename T>
int sectionMCNPX(std::string &A, T &out);
324MANTID_KERNEL_DLL
void writeMCNPX(
const std::string &
Line, std::ostream &OX);
327MANTID_KERNEL_DLL std::vector<std::string>
StrParts(
const std::string &Ln);
330MANTID_KERNEL_DLL std::map<std::string, std::string>
331splitToKeyValues(
const std::string &input,
const std::string &keyValSep =
"=",
const std::string &listSep =
",");
334template <
template <
typename T,
typename A>
class V,
typename T,
typename A>
335int writeFile(
const std::string &Fname,
const T &step,
const V<T, A> &
Y);
336template <
template <
typename T,
typename A>
class V,
typename T,
typename A>
337int writeFile(
const std::string &Fname,
const V<T, A> &
X,
const V<T, A> &
Y);
338template <
template <
typename T,
typename A>
class V,
typename T,
typename A>
339int writeFile(
const std::string &Fname,
const V<T, A> &
X,
const V<T, A> &
Y,
const V<T, A> &Err);
345MANTID_KERNEL_DLL
void readToEndOfLine(std::istream &in,
bool ConsumeEOL);
347MANTID_KERNEL_DLL std::string getWord(std::istream &in,
bool consumeEOL);
350MANTID_KERNEL_DLL
size_t split_path(
const std::string &path, std::vector<std::string> &path_components);
353MANTID_KERNEL_DLL std::string
loadFile(
const std::string &filename);
356MANTID_KERNEL_DLL
int isMember(
const std::vector<std::string> &
group,
const std::string &candidate);
360MANTID_KERNEL_DLL std::vector<int>
parseRange(
const std::string &str,
const std::string &elemSep =
",",
361 const std::string &rangeSep =
"-");
365template <
typename Integer> std::vector<std::vector<Integer>>
parseGroups(
const std::string &str) {
366 std::vector<std::vector<Integer>> groups;
369 auto translateAdd = [&groups](
const std::string &str) {
372 std::vector<Integer> currentGroup;
373 currentGroup.reserve(tokens.count());
374 std::transform(tokens.cbegin(), tokens.cend(), std::back_inserter(currentGroup),
375 [](
const auto &t) { return boost::lexical_cast<Integer>(t); });
376 groups.emplace_back(std::move(currentGroup));
379 auto translateSumRange = [&groups](
const std::string &str) {
383 if (tokens.count() != 2)
384 throw std::runtime_error(
"Malformed range (-) operation.");
385 Integer first = boost::lexical_cast<Integer>(tokens[0]);
386 Integer last = boost::lexical_cast<Integer>(tokens[1]);
388 std::swap(first, last);
390 std::vector<Integer>
group;
391 group.reserve(last - first + 1);
392 for (Integer i = first; i <= last; ++i)
393 group.emplace_back(i);
395 groups.emplace_back(std::move(
group));
398 auto translateRange = [&groups](
const std::string &str) {
402 if (tokens.count() != 2)
403 throw std::runtime_error(
"Malformed range (:) operation.");
404 Integer first = boost::lexical_cast<Integer>(tokens[0]);
405 Integer last = boost::lexical_cast<Integer>(tokens[1]);
407 std::swap(first, last);
409 for (Integer i = first; i <= last; ++i) {
410 groups.emplace_back(1, i);
418 for (
const auto &token : tokens) {
421 if (token.find(
'+') != std::string::npos) {
423 }
else if (token.find(
'-') != std::string::npos) {
424 translateSumRange(token);
425 }
else if (token.find(
':') != std::string::npos) {
426 translateRange(token);
427 }
else if (!token.empty()) {
429 groups.emplace_back(1, boost::lexical_cast<Integer>(token));
432 }
catch (boost::bad_lexical_cast &) {
433 throw std::runtime_error(
"Cannot parse numbers from string: '" + str +
"'");
444MANTID_KERNEL_DLL std::string randomString(
const size_t len);
447MANTID_KERNEL_DLL std::istream &
extractToEOL(std::istream &is, std::string &str);
456MANTID_KERNEL_DLL std::string
strmakef(
char const *
const fmt, ...);
double value
The value of the point.
#define PARALLEL_THREAD_NUMBER
#define PARALLEL_NUMBER_OF_THREADS
#define PARALLEL_GET_MAX_THREADS
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
Holds support functions for strings.
MANTID_KERNEL_DLL void stripInPlace(std::string &A)
strip pre/post spaces
MANTID_KERNEL_DLL std::string removeSpace(const std::string &CLine)
strip all spaces
MANTID_KERNEL_DLL std::string toLower(const std::string &input)
Converts string to all lowercase.
MANTID_KERNEL_DLL std::istream & extractToEOL(std::istream &is, std::string &str)
Extract a line from input stream, discarding any EOL characters encountered.
MANTID_KERNEL_DLL std::string strmakef(char const *const fmt,...)
This is the constructor that std::string needed to have.
int sectPartNum(std::string &A, T &out)
Convert and cut a string.
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...
MANTID_KERNEL_DLL int confirmStr(const std::string &S, const std::string &fullPhrase)
determine if a character group exists in a string
float getVAXnum(const float A)
Convert a VAX number to x86 little eindien.
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,...
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.
MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length)
Converts long strings into "start ... end".
int convPartNum(const std::string &A, T &out)
Takes a character string and evaluates the first [typename T] object.
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
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,...
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.
int section(std::string &A, T &out)
Convert and cut a string.
MANTID_KERNEL_DLL std::string peekLine(std::istream &fh)
Peek at a line without extracting it from the stream.
std::vector< std::vector< Integer > > parseGroups(const std::string &str)
Parses unsigned integer groups, e.g.
MANTID_KERNEL_DLL std::string replaceAll(std::string const &input, char const to_replace, char const substitute)
Return a string with all occurrences of indicated character replaced by the new character.
MANTID_KERNEL_DLL std::string fullBlock(const std::string &A)
strip pre/post spaces
MANTID_KERNEL_DLL bool skipLine(const std::string &line)
Determines if a string starts with a #.
int sectionMCNPX(std::string &A, T &out)
Convert and cut a string for MCNPX.
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
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.
MANTID_KERNEL_DLL bool endsWith(std::string const &str, std::string const &suffix)
Checks if string ends with a suffix.
MANTID_KERNEL_DLL int endsWithInt(const std::string &word)
Get an int from the end of a word.
MANTID_KERNEL_DLL void writeMCNPX(const std::string &Line, std::ostream &OX)
Write file in standard MCNPX input form.
MANTID_KERNEL_DLL std::string getLine(std::istream &fh)
Get a line and strip comments Use only for a single call.
MANTID_KERNEL_DLL void stripComment(std::string &A)
strip trailling comments
MANTID_KERNEL_DLL int extractWord(std::string &Line, const std::string &Word, const int cnt=4)
Get a word from a string.
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.
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.
MANTID_KERNEL_DLL int isEmpty(const std::string &A)
Determines if a string is only spaces.
int writeFile(const std::string &Fname, const T &step, const V< T, A > &Y)
Write a set of containers to a file.
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,...
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
std::string toString(const T &value)
Convert values to strings.
Helper class which provides the Collimation Length for SANS instruments.