Mantid
Loading...
Searching...
No Matches
Strings.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 <Poco/Path.h>
12
13#include <boost/algorithm/string.hpp>
14#include <memory>
15
16#include <fstream>
17
18using std::size_t;
19
21
22//------------------------------------------------------------------------------------------------
28std::string loadFile(const std::string &filename) {
29 std::string retVal;
30 std::string str;
31 std::ifstream in;
32 in.open(filename.c_str());
33 getline(in, str);
34 while (in) {
35 retVal += str + "\n";
36 getline(in, str);
37 }
38 in.close();
39 return retVal;
40}
41
42// ------------------------------------------------------------------------------------------------
54std::string shorten(const std::string &input, const size_t max_length) {
55 const std::string ellipsis = " ... ";
56 const size_t ellipsisSize = ellipsis.size();
57 // limit too small or input too small, return input string
58 if ((max_length == 0) || (input.size() < ellipsisSize + 2) || (input.size() <= max_length))
59 return input;
60
61 const size_t end_length = (max_length - ellipsisSize) / 2;
62 std::string retVal = input.substr(0, end_length) + ellipsis + input.substr(input.size() - end_length, end_length);
63 return retVal;
64}
65
66//------------------------------------------------------------------------------------------------
74std::string replace(const std::string &input, const std::string &find_what, const std::string &replace_with) {
75 std::string output = input;
76 std::string::size_type pos = 0;
77 while ((pos = output.find(find_what, pos)) != std::string::npos) {
78 output.erase(pos, find_what.length());
79 output.insert(pos, replace_with);
80 pos += replace_with.length();
81 }
82 return output;
83}
84
94MANTID_KERNEL_DLL std::string replaceAll(const std::string &input, const std::string &charStr,
95 const std::string &substitute) {
96 std::string replaced;
97 replaced.reserve(input.size());
98 std::string::const_iterator iend = input.end();
99 for (std::string::const_iterator itr = input.begin(); itr != iend; ++itr) {
100 char inputChar = (*itr);
101 if (charStr.find_first_of(inputChar) == std::string::npos) // Input string
102 // char is not
103 // one of those
104 // to be replaced
105 {
106 replaced.push_back(inputChar);
107 } else {
108 replaced.append(substitute);
109 }
110 }
111 return replaced;
112}
113
116MANTID_KERNEL_DLL std::string toLower(const std::string &input) {
117 std::string output(input);
118 std::transform(output.begin(), output.end(), output.begin(), ::tolower);
119 return output;
120}
121
124MANTID_KERNEL_DLL std::string toUpper(const std::string &input) {
125 std::string output(input);
126 std::transform(output.begin(), output.end(), output.begin(), ::toupper);
127 return output;
128}
129
130//------------------------------------------------------------------------------------------------
138void printHex(std::ostream &OFS, const int n) {
139 std::ios_base::fmtflags PrevFlags = OFS.flags();
140 OFS << "Ox";
141 OFS.width(8);
142 OFS.fill('0');
143 hex(OFS);
144 OFS << n;
145 OFS.flags(PrevFlags);
146}
147
148//------------------------------------------------------------------------------------------------
154std::string stripMultSpc(const std::string &Line) {
155 std::string Out;
156 int spc(1);
157 int lastReal(-1);
158 for (unsigned int i = 0; i < Line.length(); i++) {
159 if (Line[i] != ' ' && Line[i] != '\t' && Line[i] != '\r' && Line[i] != '\n') {
160 lastReal = i;
161 spc = 0;
162 Out += Line[i];
163 } else if (!spc) {
164 spc = 1;
165 Out += ' ';
166 }
167 }
168 lastReal++;
169 if (lastReal < static_cast<int>(Out.length()))
170 Out.erase(lastReal);
171 return Out;
172}
173
174//------------------------------------------------------------------------------------------------
175//------------------------------------------------------------------------------------------------
186int extractWord(std::string &Line, const std::string &Word, const int cnt) {
187 if (Word.empty())
188 return 0;
189
190 size_t minSize(cnt > static_cast<int>(Word.size()) ? Word.size() : cnt);
191 std::string::size_type pos = Line.find(Word.substr(0, minSize));
192 if (pos == std::string::npos)
193 return 0;
194 // Pos == Start of find
195 size_t LinePt = minSize + pos;
196 for (; minSize < Word.size() && LinePt < Line.size() && Word[minSize] == Line[LinePt]; LinePt++, minSize++) {
197 }
198
199 Line.erase(pos, LinePt - (pos - 1));
200 return 1;
201}
202
203//------------------------------------------------------------------------------------------------
210int endsWithInt(const std::string &word) {
211 if (word.empty())
212 return -1;
213 int out = -1;
214 // Find the index of the first number in the string (if any)
215 auto firstNumber = int(word.size());
216 for (int i = int(word.size()) - 1; i >= 0; i--) {
217 char c = word[i];
218 if ((c > '9') || (c < '0'))
219 break;
220 firstNumber = i;
221 }
222 // Convert the string of decimals to an int
223 if (firstNumber < int(word.size())) {
224 std::string part = word.substr(firstNumber, word.size() - firstNumber);
225 if (!convert(part, out))
226 return -1;
227 }
228 return out;
229}
230
231//------------------------------------------------------------------------------------------------
239int confirmStr(const std::string &S, const std::string &fullPhrase) {
240 const size_t nS(S.length());
241 const size_t nC(fullPhrase.length());
242 if (nS > nC || nS == 0)
243 return 0;
244 for (size_t i = 0; i < nS; i++)
245 if (S[i] != fullPhrase[i])
246 return 0;
247 return 1;
248}
249
250//------------------------------------------------------------------------------------------------
262int getPartLine(std::istream &fh, std::string &Out, std::string &Excess, const int spc) {
263 // std::string Line;
264 if (fh.good()) {
265 auto ss = new char[spc + 1];
266 const auto clen = static_cast<int>(spc - Out.length());
267 fh.getline(ss, clen, '\n');
268 ss[clen + 1] = 0; // incase line failed to read completely
269 Out += static_cast<std::string>(ss);
270 delete[] ss;
271 // remove trailing comments
272 std::string::size_type pos = Out.find_first_of("#!");
273 if (pos != std::string::npos) {
274 Out.erase(pos);
275 return 0;
276 }
277 if (fh.gcount() == clen - 1) // cont line
278 {
279 pos = Out.find_last_of("\t ");
280 if (pos != std::string::npos) {
281 Excess = Out.substr(pos, std::string::npos);
282 Out.erase(pos);
283 } else
284 Excess.erase(0, std::string::npos);
285 fh.clear();
286 return 1;
287 }
288 return 0;
289 }
290 return -1;
291}
292
293//------------------------------------------------------------------------------------------------
300std::string removeSpace(const std::string &CLine) {
301 std::string Out;
302 char prev = 'x';
303 for (char character : CLine) {
304 if (!isspace(character) || prev == '\\') {
305 Out += character;
306 prev = character;
307 }
308 }
309 return Out;
310}
311
312//------------------------------------------------------------------------------------------------
319std::string getLine(std::istream &fh) {
320 std::string line;
321 getLine(fh, line);
322 return line;
323}
324
325//------------------------------------------------------------------------------------------------
332void getLine(std::istream &fh, std::string &Line) {
333 if (std::getline(fh, Line)) {
334 // remove trailing comments
335 auto pos = Line.find_first_of("#!");
336 if (pos != std::string::npos)
337 Line.erase(pos);
338 }
339}
340
344std::string peekLine(std::istream &fh) {
345 std::string str;
346 std::streampos pos = fh.tellg();
347 getline(fh, str);
348 fh.seekg(pos);
349
350 return strip(str);
351}
352
353//------------------------------------------------------------------------------------------------
359int isEmpty(const std::string &A) {
360 std::string::size_type pos = A.find_first_not_of(" \t");
361 return (pos != std::string::npos) ? 0 : 1;
362}
363
364//------------------------------------------------------------------------------------------------
370void stripComment(std::string &A) {
371 std::string::size_type posA = A.find("$ ");
372 std::string::size_type posB = A.find("# ");
373 std::string::size_type posC = A.find('!');
374 if (posA > posB)
375 posA = posB;
376 if (posA > posC)
377 posA = posC;
378 if (posA != std::string::npos)
379 A.erase(posA, std::string::npos);
380}
381
382//------------------------------------------------------------------------------------------------
389std::string fullBlock(const std::string &A) { return strip(A); }
390
391//------------------------------------------------------------------------------------------------
397std::string strip(const std::string &A) {
398 std::string result(A);
399 boost::trim(result);
400 return result;
401}
402
408bool skipLine(const std::string &line) {
409 // Empty or comment
410 return (line.empty() || boost::starts_with(line, "#"));
411}
412
413//------------------------------------------------------------------------------------------------
421void writeMCNPX(const std::string &Line, std::ostream &OX) {
422 const int MaxLine(72);
423 std::string::size_type pos(0);
424 std::string X = Line.substr(0, MaxLine);
425 std::string::size_type posB = X.find_last_of(" ,");
426 int spc(0);
427 while (posB != std::string::npos && static_cast<int>(X.length()) >= MaxLine - spc) {
428 pos += posB + 1;
429 if (!isspace(X[posB]))
430 posB++;
431 const std::string Out = X.substr(0, posB);
432 if (!isEmpty(Out)) {
433 if (spc)
434 OX << std::string(spc, ' ');
435 OX << X.substr(0, posB) << '\n';
436 }
437 spc = 8;
438 X = Line.substr(pos, MaxLine - spc);
439 posB = X.find_last_of(" ,");
440 }
441 if (!isEmpty(X)) {
442 if (spc)
443 OX << std::string(spc, ' ');
444 OX << X << '\n';
445 }
446}
447
448//------------------------------------------------------------------------------------------------
454std::vector<std::string> StrParts(const std::string &Ln) {
457 return tokenizer.asVector();
458}
459
469std::map<std::string, std::string> splitToKeyValues(const std::string &input, const std::string &keyValSep,
470 const std::string &listSep) {
471 std::map<std::string, std::string> keyValues;
472 const int splitOptions =
474 Mantid::Kernel::StringTokenizer listSplitter(input, listSep);
475 for (const auto &iter : listSplitter) {
476 Mantid::Kernel::StringTokenizer keyValSplitter(iter, keyValSep, splitOptions);
477 if (keyValSplitter.count() == 2) {
478 keyValues[keyValSplitter[0]] = keyValSplitter[1];
479 }
480 }
481
482 return keyValues;
483}
484
485//------------------------------------------------------------------------------------------------
491float getVAXnum(const float A) {
492 union {
493 // char a[4];
494 float f;
495 int ival;
496 } Bd;
497
498 int sign, expt, fmask;
499 float frac;
500 double onum;
501
502 Bd.f = A;
503 sign = (Bd.ival & 0x8000) ? -1 : 1;
504 expt = ((Bd.ival & 0x7f80) >> 7); // reveresed ?
505 if (!expt)
506 return 0.0;
507
508 fmask = ((Bd.ival & 0x7f) << 16) | ((Bd.ival & 0xffff0000) >> 16);
509 expt -= 128;
510 fmask |= 0x800000;
511 frac = static_cast<float>(fmask) / 0x1000000;
512 onum = frac * static_cast<float>(sign) * pow(2.0, expt);
513 return static_cast<float>(onum);
514}
515
516//------------------------------------------------------------------------------------------------
527template <typename T> int sectPartNum(std::string &A, T &out) {
528 if (A.empty())
529 return 0;
530
531 std::istringstream cx;
532 T retval;
533 cx.str(A);
534 cx.clear();
535 cx >> retval;
536 const std::streamoff xpt = cx.tellg();
537 if (xpt < 0)
538 return 0;
539 A.erase(0, static_cast<unsigned int>(xpt));
540 out = retval;
541 return 1;
542}
543
544//------------------------------------------------------------------------------------------------
553template <typename T> int section(char *cA, T &out) {
554 if (!cA)
555 return 0;
556 std::string sA(cA);
557 const int item(section(sA, out));
558 if (item) {
559 strcpy(cA, sA.c_str());
560 return 1;
561 }
562 return 0;
563}
564
573template <typename T> int section(std::string &A, T &out) {
574 if (A.empty())
575 return 0;
576 std::istringstream cx;
577 T retval;
578 cx.str(A);
579 cx.clear();
580 cx >> retval;
581 if (cx.fail())
582 return 0;
583 const std::streamoff xpt = cx.tellg();
584 const auto xc = static_cast<char>(cx.get());
585 if (!cx.fail() && !isspace(xc))
586 return 0;
587 A.erase(0, static_cast<unsigned int>(xpt));
588 out = retval;
589 return 1;
590}
591
603template <typename T> int sectionMCNPX(std::string &A, T &out) {
604 if (A.empty())
605 return 0;
606 std::istringstream cx;
607 T retval;
608 cx.str(A);
609 cx.clear();
610 cx >> retval;
611 if (!cx.fail()) {
612 const std::streamoff xpt = cx.tellg();
613 if (xpt < 0) {
614 return 0;
615 }
616 const auto xc = static_cast<char>(cx.get());
617 if (!cx.fail() && !isspace(xc) && (xc != '-' || xpt < 5)) {
618 return 0;
619 }
620 A.erase(0, static_cast<unsigned int>(xpt));
621 out = retval;
622 return 1;
623 }
624 return 0;
625}
626
627//------------------------------------------------------------------------------------------------
639template <typename T> int convPartNum(const std::string &A, T &out) {
640 if (A.empty())
641 return 0;
642 std::istringstream cx;
643 T retval;
644 cx.str(A);
645 cx.clear();
646 cx >> retval;
647 // If we have reached the end of the stream, then we need to clear the error
648 // as
649 // it will cause the tellg() call to return -1. Not pretty but works for now.
650 cx.clear();
651 const std::streamoff xpt = cx.tellg();
652 if (xpt < 0)
653 return 0;
654 out = retval;
655 return static_cast<int>(xpt);
656}
657
658//------------------------------------------------------------------------------------------------
665template <typename T> int convert(const std::string &A, T &out) {
666 if (A.empty())
667 return 0;
668 std::istringstream cx;
669 T retval;
670 cx.str(A);
671 cx.clear();
672 cx >> retval;
673 if (cx.fail())
674 return 0;
675 const auto clast = static_cast<char>(cx.get());
676 if (!cx.fail() && !isspace(clast))
677 return 0;
678 out = retval;
679 return 1;
680}
681
682//------------------------------------------------------------------------------------------------
689template <typename T> int convert(const char *A, T &out) {
690 // No string, no conversion
691 if (!A)
692 return 0;
693 std::string Cx = A;
694 return convert(Cx, out);
695}
696
697//------------------------------------------------------------------------------------------------
703template <typename T> std::string toString(const T &value) {
704 std::ostringstream mess;
705 mess << value;
706 return mess.str();
707}
708
715template <typename T> std::string toString(const std::vector<T> &value) {
716 std::ostringstream mess;
717 auto it = value.begin();
718 auto last = value.end();
719 T start;
720 T stop;
721 for (; it != last; ++it) {
722 start = *(it);
723 stop = start;
724 for (; it != last; ++it) {
725 if (it + 1 == last)
726 break;
727 else if ((stop + static_cast<T>(1)) == *(it + 1))
728 stop = *(it + 1);
729 else
730 break;
731 }
732 mess << start;
733 if (start != stop)
734 mess << "-" << stop;
735 if (it + 1 != last)
736 mess << ",";
737 }
738 return mess.str();
739}
740
741template <typename T> std::string toString(const std::set<T> &value) {
742 return toString(std::vector<T>(value.begin(), value.end()));
743}
744
745template <> MANTID_KERNEL_DLL std::string toString(const UnitLabel &value) { return value; }
746
750template <> MANTID_KERNEL_DLL std::string toString(const std::vector<std::string> &value) {
751 return join(value.begin(), value.end(), ",");
752}
753
754//------------------------------------------------------------------------------------------------
762template <template <typename T, typename A> class V, typename T, typename A>
763int writeFile(const std::string &Fname, const T &step, const V<T, A> &Y) {
764 V<T, A> Ex; // Empty vector
765 V<T, A> X; // Empty vector
766 for (unsigned int i = 0; i < Y.size(); i++)
767 X.emplace_back(i * step);
768
769 return writeFile(Fname, X, Y, Ex);
770}
771
772//------------------------------------------------------------------------------------------------
780template <template <typename T, typename A> class V, typename T, typename A>
781int writeFile(const std::string &Fname, const V<T, A> &X, const V<T, A> &Y) {
782 V<T, A> Ex; // Empty vector/list
783 return writeFile(Fname, X, Y, Ex); // don't need to specific ??
784}
785
786//------------------------------------------------------------------------------------------------
798template <template <typename T, typename A> class V, typename T, typename A>
799int writeFile(const std::string &Fname, const V<T, A> &X, const V<T, A> &Y, const V<T, A> &Err) {
800 const size_t Npts(X.size() > Y.size() ? Y.size() : X.size());
801 const size_t Epts(Npts > Err.size() ? Err.size() : Npts);
802
803 std::ofstream FX;
804
805 FX.open(Fname.c_str());
806 if (!FX.good())
807 return -1;
808
809 FX << "# " << Npts << " " << Epts << '\n';
810 FX.precision(10);
811 FX.setf(std::ios::scientific, std::ios::floatfield);
812 auto xPt = X.cbegin();
813 auto yPt = Y.cbegin();
814 auto ePt = (Epts ? Err.cbegin() : Y.cbegin());
815
816 // Double loop to include/exclude a short error stack
817 size_t eCount = 0;
818 for (; eCount < Epts; eCount++) {
819 FX << (*xPt) << " " << (*yPt) << " " << (*ePt) << '\n';
820 ++xPt;
821 ++yPt;
822 ++ePt;
823 }
824 for (; eCount < Npts; eCount++) {
825 FX << (*xPt) << " " << (*yPt) << " 0.0\n";
826 ++xPt;
827 ++yPt;
828 }
829 FX.close();
830 return 0;
831}
832
833//------------------------------------------------------------------------------------------------
844template <typename T> int setValues(const std::string &Line, const std::vector<int> &Index, std::vector<T> &Out) {
845 if (Index.empty())
846 return 0;
847
848 if (Out.size() != Index.size())
849 return -1;
850 // throw ColErr::MisMatch<int>(Index.size(),Out.size(),
851 // "Mantid::Kernel::Strings::setValues");
852
853 std::string modLine = Line;
854 std::vector<int> sIndex(Index); // Copy for sorting
855 std::vector<int> OPt(Index.size());
856 for (unsigned int i = 0; i < Index.size(); i++)
857 OPt[i] = i;
858
859 // mathFunc::crossSort(sIndex,OPt);
860
861 using iVecIter = std::vector<int>::const_iterator;
862 std::vector<int>::const_iterator sc = sIndex.begin();
863 std::vector<int>::const_iterator oc = OPt.begin();
864 int cnt(0);
865 T value;
866 std::string dump;
867 while (sc != sIndex.end() && *sc < 0) {
868 ++sc;
869 ++oc;
870 }
871
872 while (sc != sIndex.end()) {
873 if (*sc == cnt) {
874 if (!section(modLine, value))
875 return static_cast<int>(-1 - distance(static_cast<iVecIter>(sIndex.begin()), sc));
876 // this loop handles repeat units
877 do {
878 Out[*oc] = value;
879 ++sc;
880 ++oc;
881 } while (sc != sIndex.end() && *sc == cnt);
882 } else {
883 if (!section(modLine, dump))
884 return static_cast<int>(-1 - distance(static_cast<iVecIter>(sIndex.begin()), sc));
885 }
886 cnt++; // Add only to cnt [sc/oc in while loop]
887 }
888 // Success since loop only gets here if sc is exhaused.
889 return 0;
890}
891
892//-----------------------------------------------------------------------------------------------
900std::string getWord(std::istream &in, bool consumeEOL) {
901 std::string ret;
902 char nextch = 0;
903
904 // Skip leading spaces
905 do {
906 nextch = static_cast<char>(in.get());
907 } while (nextch == ' ');
908
909 // Return an empty string on EOL; optionally consume it
910 if (nextch == '\n' || nextch == '\r') {
911 if (!consumeEOL) {
912 in.unget();
913 } else if ((nextch == '\n' && in.peek() == '\r') || (nextch == '\r' && in.peek() == '\n')) {
914 // Handle CRLF and LFCR on Unix by consuming both
915 in.ignore();
916 }
917
918 return ret;
919 } else { // Non-EOL and non-space character
920 in.unget(); // Put it back on stream
921 }
922
923 // Get next word if stream is still valid
924 if (in.good())
925 in >> ret;
926
927 // Optionally consume EOL character
928 if (consumeEOL) {
929 nextch = static_cast<char>(in.get());
930
931 // Handle CRLF and LFCR on Unix by consuming both
932 if (nextch == '\n' || nextch == '\r') {
933 if ((nextch == '\n' && in.peek() == '\r') || (nextch == '\r' && in.peek() == '\n')) {
934 in.ignore();
935 }
936 } else {
937 in.unget();
938 }
939 }
940
941 return ret;
942}
943
944//-----------------------------------------------------------------------------------------------
951void readToEndOfLine(std::istream &in, bool ConsumeEOL) {
952 while (in.good() && getWord(in, false).length() > 0)
953 getWord(in, false);
954 if (!ConsumeEOL)
955 return;
956 getWord(in, true);
957}
958
970size_t split_path(const std::string &path, std::vector<std::string> &path_components) {
971 if (path.empty()) {
972 path_components.resize(0);
973 return 0;
974 }
975 // convert Windows path into the unix one
976 std::string working_path(path);
977 for (size_t i = 0; i < path.size(); i++) {
978 if (working_path[i] < 0x20 || working_path[i] > 0x7E)
979 working_path[i] = '_';
980 if (working_path[i] == '\\')
981 working_path[i] = '/';
982 if (working_path[i] == ' ')
983 working_path[i] = '_';
984 }
985
986 // path start with relative character, and we need to convert it into full
987 // path
988 if (path[0] == '.') {
989 // get absolute path using working directory as base;
990 Poco::Path absol;
991 absol = absol.absolute();
992 working_path = absol.toString(Poco::Path::PATH_UNIX) + working_path;
993 }
994 // as poco splt using regular expressions is doing some rubbish, we need to do
995 // split manually
996 // code below implements perl split(/\\//,string) commamd. (\\ has been
997 // converted to / above)
998 std::list<int64_t> split_pos;
999 split_pos.emplace_back(-1);
1000 size_t path_size = working_path.size();
1001 for (size_t i = 0; i < path_size; i++) {
1002 if (working_path[i] == '/') {
1003 split_pos.emplace_back(i);
1004 }
1005 }
1006 split_pos.emplace_back(path_size);
1007 // allocate target vector to keep folder structure and fill it in
1008 size_t n_folders = split_pos.size() - 1;
1009 path_components.resize(n_folders);
1010 auto it1 = split_pos.begin();
1011 auto it2 = it1;
1012 ++it2;
1013
1014 int64_t ic(0);
1015 for (; it2 != split_pos.end(); ++it2) {
1016 std::string folder = working_path.substr(*it1 + 1, *it2 - *it1 - 1);
1017 if (folder.empty() || (folder.size() == 1 && folder == ".")) { // skip self-references and double slashes;
1018 it1 = it2;
1019 continue;
1020 }
1021 // reprocess up-references;
1022 if (folder == "..") {
1023 ic--;
1024 if (ic < 0)
1025 throw(std::invalid_argument("path contains relative references to a "
1026 "folder outside of the seach tree"));
1027 it1 = it2;
1028 continue;
1029 }
1030 path_components[ic] = folder;
1031 ic++;
1032 it1 = it2;
1033 }
1034
1035 n_folders = size_t(ic);
1036 path_components.resize(n_folders);
1037 return n_folders;
1038}
1039
1050int isMember(const std::vector<std::string> &group, const std::string &candidate) {
1051 int num(-1);
1052 for (size_t i = 0; i < group.size(); i++) {
1053 if (candidate == group[i]) {
1054 num = int(i);
1055 return num;
1056 }
1057 }
1058 return num;
1059}
1060
1071std::vector<int> parseRange(const std::string &str, const std::string &elemSep, const std::string &rangeSep) {
1072 using Tokenizer = Mantid::Kernel::StringTokenizer;
1073
1074 Tokenizer elements;
1075
1076 if (elemSep.find(' ') != std::string::npos) {
1077 // If element separator contains space character it's a special case,
1078 // because in that case
1079 // it is allowed to have element separator inside a range, e.g. "4 - 5", but
1080 // not "4,-5"
1081 Tokenizer ranges(str, rangeSep, Tokenizer::TOK_TRIM);
1082 std::string new_str = join(ranges.begin(), ranges.end(), rangeSep.substr(0, 1));
1083 elements = Tokenizer(new_str, elemSep, Tokenizer::TOK_IGNORE_EMPTY | Tokenizer::TOK_TRIM);
1084 } else {
1085 elements = Tokenizer(str, elemSep, Tokenizer::TOK_IGNORE_EMPTY | Tokenizer::TOK_TRIM);
1086 }
1087
1088 std::vector<int> result;
1089
1090 // Estimation of the resulting number of elements
1091 result.reserve(elements.count());
1092
1093 for (const auto &elementString : elements) {
1094 // See above for the reason space is added
1095 Tokenizer rangeElements(elementString, rangeSep, Tokenizer::TOK_TRIM);
1096
1097 size_t noOfRangeElements = rangeElements.count();
1098
1099 // A single element
1100 if (noOfRangeElements == 1) {
1101 int element;
1102 if (convert(rangeElements[0], element) != 1)
1103 throw std::invalid_argument("Invalid element: " + elementString);
1104 result.emplace_back(element);
1105 }
1106 // A pair
1107 else if (noOfRangeElements == 2) {
1108 int start, end;
1109
1110 if (convert(rangeElements[0], start) != 1 || convert(rangeElements[1], end) != 1)
1111 throw std::invalid_argument("Invalid range: " + elementString);
1112
1113 if (start >= end)
1114 throw std::invalid_argument("Range boundaries are reversed: " + elementString);
1115
1116 for (int i = start; i <= end; i++)
1117 result.emplace_back(i);
1118 }
1119 // Error - e.g. "--""
1120 else {
1121 throw std::invalid_argument("Multiple range separators: " + elementString);
1122 }
1123 }
1124
1125 return result;
1126}
1127
1137std::istream &extractToEOL(std::istream &is, std::string &str) {
1138 // Empty the string
1139 str = "";
1140 char c('\0');
1141 while (is.get(c)) {
1142 if (c == '\r') {
1143 c = static_cast<char>(is.peek());
1144 if (c == '\n') {
1145 // Extract this as well
1146 is.get();
1147 }
1148 break;
1149 } else if (c == '\n') {
1150 break;
1151 } else {
1152 // Accumulate the string
1153 str += c;
1154 }
1155 }
1156 return is;
1157}
1158
1160template MANTID_KERNEL_DLL int section(std::string &, double &);
1161template MANTID_KERNEL_DLL int section(std::string &, float &);
1162template MANTID_KERNEL_DLL int section(std::string &, int &);
1163template MANTID_KERNEL_DLL int section(std::string &, std::string &);
1164
1165template MANTID_KERNEL_DLL int sectPartNum(std::string &, double &);
1166template MANTID_KERNEL_DLL int sectPartNum(std::string &, int &);
1167template MANTID_KERNEL_DLL int sectionMCNPX(std::string &, double &);
1168
1169template MANTID_KERNEL_DLL int convert(const std::string &, double &);
1170template MANTID_KERNEL_DLL int convert(const std::string &, float &);
1171template MANTID_KERNEL_DLL int convert(const std::string &, std::string &);
1172template MANTID_KERNEL_DLL int convert(const std::string &, int &);
1173template MANTID_KERNEL_DLL int convert(const std::string &, std::size_t &);
1174template MANTID_KERNEL_DLL int convert(const std::string &, bool &);
1175template MANTID_KERNEL_DLL int convert(const char *, std::string &);
1176template MANTID_KERNEL_DLL int convert(const char *, double &);
1177template MANTID_KERNEL_DLL int convert(const char *, int &);
1178template MANTID_KERNEL_DLL int convert(const char *, std::size_t &);
1179template MANTID_KERNEL_DLL int convert(const char *, bool &);
1180
1181template MANTID_KERNEL_DLL std::string toString(const double &value);
1182template MANTID_KERNEL_DLL std::string toString(const float &value);
1183template MANTID_KERNEL_DLL std::string toString(const int &value);
1184template MANTID_KERNEL_DLL std::string toString(const uint16_t &value);
1185template MANTID_KERNEL_DLL std::string toString(const size_t &value); // Matches uint64_t on Linux 64 & Win 64
1186#if defined(__APPLE__) || (defined(_WIN32) && !defined(_WIN64)) || \
1187 (defined(__GNUC__) && !defined(__LP64__)) // Mac or 32-bit compiler
1188template MANTID_KERNEL_DLL std::string toString(const uint64_t &value);
1189#endif
1190template MANTID_KERNEL_DLL std::string toString(const std::string &value);
1191
1192template MANTID_KERNEL_DLL std::string toString(const std::vector<int> &value);
1193template MANTID_KERNEL_DLL std::string toString(const std::vector<size_t> &value);
1194
1195// this block should generate the vector ones as well
1196template MANTID_KERNEL_DLL std::string toString(const std::set<int> &value);
1197template MANTID_KERNEL_DLL std::string toString(const std::set<int16_t> &value);
1198template MANTID_KERNEL_DLL std::string toString(const std::set<size_t> &value); // Matches uint64_t on Linux 64 & Win 64
1199#if defined(__APPLE__) || (defined(_WIN32) && !defined(_WIN64)) || \
1200 (defined(__GNUC__) && !defined(__LP64__)) // Mac or 32-bit compiler
1201template MANTID_KERNEL_DLL std::string toString(const std::set<uint64_t> &value);
1202#endif
1203
1204template MANTID_KERNEL_DLL int convPartNum(const std::string &, double &);
1205template MANTID_KERNEL_DLL int convPartNum(const std::string &, int &);
1206
1207template MANTID_KERNEL_DLL int setValues(const std::string &, const std::vector<int> &, std::vector<double> &);
1208
1209template MANTID_KERNEL_DLL int writeFile(const std::string &, const double &, const std::vector<double> &);
1210template MANTID_KERNEL_DLL int writeFile(const std::string &, const std::vector<double> &, const std::vector<double> &,
1211 const std::vector<double> &);
1212template MANTID_KERNEL_DLL int writeFile(const std::string &, const std::vector<double> &, const std::vector<double> &);
1213template MANTID_KERNEL_DLL int writeFile(const std::string &, const std::vector<float> &, const std::vector<float> &);
1214template MANTID_KERNEL_DLL int writeFile(const std::string &, const std::vector<float> &, const std::vector<float> &,
1215 const std::vector<float> &);
1217
1218} // namespace Mantid::Kernel::Strings
double value
The value of the point.
Definition: FitMW.cpp:51
Impliments a line.
Definition: Line.h:43
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
const TokenVec & asVector()
Returns a vector of tokenized strings.
std::size_t count() const
Get the total number of tokens.
A base-class for the a class that is able to return unit labels in different representations.
Definition: UnitLabel.h:20
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
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 strip(const std::string &A)
strip pre/post spaces
Definition: Strings.cpp:397
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
void printHex(std::ostream &OFS, const int n)
Function to convert a number into hex output (and leave the stream un-changed)
Definition: Strings.cpp:138
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
MANTID_KERNEL_DLL void readToEndOfLine(std::istream &in, bool ConsumeEOL)
Eat everything from the stream until the next EOL.
Definition: Strings.cpp:951
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
std::string stripMultSpc(const std::string &Line)
Removes the multiple spaces in the line.
Definition: Strings.cpp:154
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
int convert(const std::string &A, T &out)
Convert a string into a number.
Definition: Strings.cpp:665
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
std::string toString(const T &value)
Convert a number to a string.
Definition: Strings.cpp:703
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 std::string getWord(std::istream &in, bool consumeEOL)
Returns the next word in the stream.
Definition: Strings.cpp:900
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
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