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