14 const int result = std::stoi(s, &idx);
15 if (idx == s.size()) {
20 }
catch (
const std::exception&) {
29 const Size result = std::stoul(s, &idx);
30 if (idx == s.size()) {
35 }
catch (
const std::exception&) {
44 const float result = std::stof(s, &idx);
45 if (idx == s.size()) {
50 }
catch (
const std::exception&) {
59 const double result = std::stod(s, &idx);
60 if (idx == s.size()) {
65 }
catch (
const std::exception&) {
70 bool startsWith(
const std::string& s,
const std::string& start) {
71 return s.size() >= start.size() && s.substr(0, start.size()) == start;
74 std::string
trim(
const std::string& s) {
76 for (; i1 < s.size(); ++i1) {
82 for (; i2 > 0; --i2) {
83 if (s[i2 - 1] !=
' ') {
88 for (
Size i = i1; i < i2; ++i) {
89 trimmed.push_back(s[i]);
97 if (c >=
'A' && c <=
'Z') {
104 std::string
replaceFirst(
const std::string& source,
const std::string& old,
const std::string& s) {
105 const std::size_t n = source.find(old);
106 if (n == std::string::npos) {
109 std::string replaced = source;
110 replaced.replace(n, old.size(), s);
114 std::string
replaceAll(
const std::string& source,
const std::string& old,
const std::string& s) {
115 std::string current = source;
118 const std::size_t n = current.find(old, pos);
119 if (n == std::string::npos) {
122 current.replace(n, old.size(), s);
128 const std::string emptyChars =
" \t\r";
129 const std::string canBreakChars =
".,;\n" + emptyChars;
130 std::string result = s;
131 std::size_t lastLineBreak = 0;
132 std::size_t lastSpaceNum = 0;
133 bool commaFound =
false;
135 for (std::size_t n = 0; n < result.size();) {
137 std::size_t pos = result.find_first_of(canBreakChars, n);
138 if (pos == std::string::npos) {
141 if (result[pos] ==
'\n') {
149 if (pos - lastLineBreak <= lineWidth) {
156 while (n < result.size() && emptyChars.find(result[n]) != std::string::npos) {
162 result.insert(n++,
"\n");
164 if (commaFound && lastSpaceNum > 0) {
165 result.insert(n, std::string(lastSpaceNum,
' '));
169 const std::size_t comma = result.find(
"- ", lastLineBreak);
171 const std::size_t colon = result.find(
": ", comma);
173 std::size_t spaceNum = colon + 2 - lastLineBreak;
174 result.insert(n, std::string(spaceNum,
' '));
176 lastSpaceNum = spaceNum;
184 while (n < result.size() && emptyChars.find(result[n]) != std::string::npos) {
196 while ((n2 = s.find(delimiter, n1 + 1)) != std::string::npos) {
197 parts.
push(s.substr(n1 + 1, n2 - n1 - 1));
201 parts.
push(s.substr(n1 + 1));
206 const std::size_t n = s.find(delimiter);
207 if (n == std::string::npos) {
211 parts[0] = s.substr(0, n);
212 parts[1] = s.substr(n + 1);
217 static Array<std::string> capitalizationBlacklist{
"and",
"or",
"of",
"for",
"to",
"et",
"al" };
219 static bool shouldCapitalize(
const std::string& s) {
220 for (
const std::string& b : capitalizationBlacklist) {
221 if (s.size() < b.size()) {
224 if (s.substr(0, b.size()) == b && (s.size() == b.size() || s[b.size()] ==
' ')) {
232 std::string result = input;
233 for (
Size i = 0; i < result.size(); ++i) {
234 if (i == 0 || (result[i - 1] ==
' ' && shouldCapitalize(result.substr(i)))) {
235 result[i] = toupper(result[i]);
242 for (
const std::string& name : initial) {
248 std::string tested = name;
250 for (
Size postfix = 1; postfix < 999; ++postfix) {
251 if (names.find(tested) == names.end()) {
252 names.insert(tested);
256 tested = name +
" (" + std::to_string(postfix) +
")";
uint32_t Size
Integral type used to index arrays (by default).
#define NAMESPACE_SPH_END
const NothingType NOTHING
std::string replaceFirst(const std::string &source, const std::string &old, const std::string &s)
Replaces first occurence of string with a new string.
std::string setLineBreak(const std::string &s, const Size lineWidth)
Inserts to string so that no line is longer than given limit.
std::string trim(const std::string &s)
Removes all leading and trailing spaces from a string.
NAMESPACE_SPH_BEGIN Optional< std::string > fromString(const std::string &s)
Converts a string to given type.
std::string replaceAll(const std::string &source, const std::string &old, const std::string &s)
Replaces all occurences of string with a new string.
Array< std::string > split(const std::string &s, const char delimiter)
Splits a string into an array of string using given delimiter.
Pair< std::string > splitByFirst(const std::string &s, const char delimiter)
Splits a string into two parts, using first occurence of given delimiter.
std::string capitalize(const std::string &input)
Capitalizes first letters of all words in the string, except for words like 'and',...
bool startsWith(const std::string &s, const std::string &start)
Checks if the given string starts with given substring.
std::string lowercase(const std::string &s)
Converts all uppercase characters to their lowercase variants. Other characters are unchanged.
Object providing safe access to continuous memory of data.
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Array with fixed number of allocated elements.
UniqueNameManager()=default
std::string getName(const std::string &name)