preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
String.cpp
Go to the documentation of this file.
1#include "String.hpp"
2#include <Eigen/Dense>
3#include <boost/algorithm/string/case_conv.hpp>
4#include <memory>
5#include <regex>
6#include <vector>
7#include "boost/algorithm/string/classification.hpp"
8#include "boost/algorithm/string/split.hpp"
9#include "utils/assertion.hpp"
10
11namespace precice::utils {
12
14 const std::string &text,
15 int linewidth,
16 int indentation)
17{
19 boost::algorithm::split(tokens, text, boost::algorithm::is_space());
20 PRECICE_ASSERT((int) tokens.size() > 0);
21 std::string wrapped;
22 int length = 0;
23 while (text[length] == ' ') {
24 wrapped += ' ';
25 length++;
26 }
27 for (int i = 0; i < static_cast<int>(tokens.size()) - 1; i++) {
28 length += static_cast<int>(tokens[i].length());
29 wrapped += tokens[i];
30 if (length + static_cast<int>(tokens[i + 1].length()) + 1 > linewidth) {
31 wrapped += '\n';
32 for (int ws = 0; ws < indentation; ws++) {
33 wrapped += ' ';
34 }
35 length = indentation;
36 } else {
37 wrapped += ' ';
38 length += 1;
39 }
40 }
41 wrapped += tokens.back();
42 return wrapped;
43}
44
46 std::string &filename,
47 const std::string &extension)
48{
49 size_t pos = filename.rfind(extension);
50 if ((pos == std::string::npos) || (pos != filename.size() - extension.size())) {
51 filename += extension;
52 }
53 return filename;
54}
55
57{
58 std::string str = value;
59 boost::algorithm::to_lower(str);
60 if (str == "1" or str == "yes" or str == "true" or str == "on")
61 return true;
62
63 return false;
64}
65
67{
68 std::string converted(wstr.length(), '\0');
69 // Buffer for the multibyte representation of a wchar
70 std::string mb(MB_CUR_MAX, '\0');
71 for (size_t i = 0; i != wstr.length(); ++i) {
72 // Converts a wchar to 1-MB_CUR_MAX chars
73 std::size_t ret = std::wctomb(&mb[0], wstr[i]);
74 converted[i] = (ret == 1) ? mb.front() : fill;
75 }
76 return converted;
77}
78
80{
81 const std::size_t len1 = s1.size(), len2 = s2.size();
82 using Matrix = Eigen::Matrix<std::size_t, Eigen::Dynamic, Eigen::Dynamic>;
83 Matrix distances(len1 + 1, len2 + 1);
84
85 distances(0, 0) = 0;
86 for (std::size_t i = 1; i <= len1; ++i)
87 distances(i, 0) = i;
88 for (std::size_t i = 1; i <= len2; ++i)
89 distances(0, i) = i;
90
91 for (std::size_t i = 1; i <= len1; ++i) {
92 for (std::size_t j = 1; j <= len2; ++j) {
93 auto deletionCost = distances(i - 1, j) + 1;
94 auto insertionCost = distances(i, j - 1) + 1;
95 auto substitutionCost = distances(i - 1, j - 1) + (s1[i - 1] == s2[j - 1] ? 0 : 1);
96 distances(i, j) = std::min({deletionCost, insertionCost, substitutionCost});
97 }
98 }
99
100 return distances(len1, len2);
101}
102
104{
105 std::regex kebabCaseRegex("^[a-z0-9]+(-[a-z0-9]+)*$");
106 return sv.empty() || std::regex_match(sv.begin(), sv.end(), kebabCaseRegex);
107}
108
109} // namespace precice::utils
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
T back(T... args)
T begin(T... args)
T empty(T... args)
T end(T... args)
T front(T... args)
T min(T... args)
contains precice-related utilities.
bool isKebabStyle(std::string_view sv)
Definition String.cpp:103
std::size_t editDistance(std::string_view s1, std::string_view s2)
Definition String.cpp:79
bool convertStringToBool(std::string const &value)
Evaluates a string to find out if it represents a bool.
Definition String.cpp:56
std::string truncate_wstring_to_string(std::wstring wstr, char fill)
Definition String.cpp:66
std::string wrapText(const std::string &text, int linewidth, int indentation)
Definition String.cpp:13
std::string & checkAppendExtension(std::string &filename, const std::string &extension)
Checks if filename has the given extension, if not appends it.
Definition String.cpp:45
T regex_match(T... args)
T rfind(T... args)
T size(T... args)
T wctomb(T... args)