Newer
Older
#include "MantidKernel/Strings.h"
#include "MantidKernel/UnitLabel.h"
#include <Poco/StringTokenizer.h>
#include <Poco/Path.h>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <fstream>
Janik Zikovsky
committed
using std::size_t;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace Mantid {
namespace Kernel {
namespace Strings {
//------------------------------------------------------------------------------------------------
/** Loads the entire contents of a text file into a string
*
* @param filename :: full path to file
* @return string contents of text file
*/
std::string loadFile(const std::string &filename) {
std::string retVal;
std::string str;
std::ifstream in;
in.open(filename.c_str());
getline(in, str);
while (in) {
retVal += str + "\n";
getline(in, str);
}
in.close();
return retVal;
}
//------------------------------------------------------------------------------------------------
/** Return a string with all matching occurence-strings
*
* @param input :: input string
* @param find_what :: will search for all occurences of this string
* @param replace_with :: ... and replace them with this.
* @return the modified string.
*/
std::string replace(const std::string &input, const std::string &find_what,
const std::string &replace_with) {
std::string output = input;
std::string::size_type pos = 0;
while ((pos = output.find(find_what, pos)) != std::string::npos) {
output.erase(pos, find_what.length());
output.insert(pos, replace_with);
pos += replace_with.length();
}
return output;
}
/**
* Return a string with all occurrences of the characters in the input replaced
* by the replace string
* @param input :: The input string to perform the replacement on
* @param charStr :: Each occurrence of ANY character in this string within the
* input string will be replaced by substitute
* @param substitute :: A substitute string
* @return A new string with the characters replaced
*/
MANTID_KERNEL_DLL std::string replaceAll(const std::string &input,
const std::string &charStr,
const std::string &substitute) {
std::string replaced;
replaced.reserve(input.size());
std::string::const_iterator iend = input.end();
for (std::string::const_iterator itr = input.begin(); itr != iend; ++itr) {
char inputChar = (*itr);
if (charStr.find_first_of(inputChar) == std::string::npos) // Input string
// char is not
// one of those
// to be replaced
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
replaced.push_back(inputChar);
} else {
replaced.append(substitute);
}
}
return replaced;
}
//------------------------------------------------------------------------------------------------
/**
* Function to convert a number into hex
* output (and leave the stream un-changed)
* @param OFS :: Output stream
* @param n :: Integer to convert
* \todo Change this to a stream operator
*/
void printHex(std::ostream &OFS, const int n) {
std::ios_base::fmtflags PrevFlags = OFS.flags();
OFS << "Ox";
OFS.width(8);
OFS.fill('0');
hex(OFS);
OFS << n;
OFS.flags(PrevFlags);
return;
}
//------------------------------------------------------------------------------------------------
/**
* Removes the multiple spaces in the line
* @param Line :: Line to process
* @return String with single space components
*/
std::string stripMultSpc(const std::string &Line) {
std::string Out;
int spc(1);
int lastReal(-1);
for (unsigned int i = 0; i < Line.length(); i++) {
if (Line[i] != ' ' && Line[i] != '\t' && Line[i] != '\r' &&
Line[i] != '\n') {
lastReal = i;
spc = 0;
Out += Line[i];
} else if (!spc) {
spc = 1;
Out += ' ';
}
}
lastReal++;
if (lastReal < static_cast<int>(Out.length()))
Out.erase(lastReal);
return Out;
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
/**
* Checks that as least cnt letters of
* works is part of the string. It is currently
* case sensitive. It removes the Word if found
* @param Line :: Line to process
* @param Word :: Word to use
* @param cnt :: Length of Word for significants [default =4]
* @retval 1 on success (and changed Line)
* @retval 0 on failure
*/
int extractWord(std::string &Line, const std::string &Word, const int cnt) {
if (Word.empty())
return 0;
size_t minSize(cnt > static_cast<int>(Word.size()) ? Word.size() : cnt);
std::string::size_type pos = Line.find(Word.substr(0, minSize));
if (pos == std::string::npos)
return 0;
// Pos == Start of find
size_t LinePt = minSize + pos;
for (; minSize < Word.size() && LinePt < Line.size() &&
Word[minSize] == Line[LinePt];
LinePt++, minSize++) {
}
Line.erase(pos, LinePt - (pos - 1));
return 1;
}
//------------------------------------------------------------------------------------------------
/** If a word ends with a number representing a positive integer, return
* the value of that int.
*
* @param word :: string possibly ending in a number
* @return the number, or -1 if it does not end in a number
*/
int endsWithInt(const std::string &word) {
if (word.empty())
return -1;
int out = -1;
// Find the index of the first number in the string (if any)
int firstNumber = int(word.size());
for (int i = int(word.size()) - 1; i >= 0; i--) {
char c = word[i];
if ((c > '9') || (c < '0'))
break;
firstNumber = i;
}
// Convert the string of decimals to an int
if (firstNumber < int(word.size())) {
std::string part = word.substr(firstNumber, word.size() - firstNumber);
if (!convert(part, out))
return -1;
}
return out;
}
//------------------------------------------------------------------------------------------------
/**
* Check to see if S is the same as the
* first part of a phrase. (case insensitive)
* @param S :: string to check
Loading
Loading full blame...