23#ifndef VCL_MISC_STRING_H
24#define VCL_MISC_STRING_H
26#include <vclib/math/min_max.h>
46inline std::string::const_iterator findCaseInsensitive(
47 const std::string& input,
48 const std::string& substr)
52 std::string::const_iterator it = std::search(
57 [](
char ch1,
char ch2) {
58 return std::toupper(ch1) == std::toupper(ch2);
72std::string toString(T val)
75 if constexpr (std::is_pointer_v<T>) {
76 const void* address =
static_cast<const void*
>(val);
82 return std::to_string(val);
93inline bool containsCaseInsensitive(
94 const std::string& input,
95 const std::string& substr)
97 return findCaseInsensitive(input, substr) != input.end();
100inline constexpr std::string toLower(
const std::string& s)
103 std::transform(s.begin(), s.end(), ret.begin(), ::tolower);
107inline constexpr std::string toUpper(
const std::string& s)
110 std::transform(s.begin(), s.end(), ret.begin(), [](
unsigned char c) {
111 return std::toupper(c);
124inline constexpr std::string camelCaseToSnakeCase(
const std::string& s)
127 for (
size_t i = 0; i < s.size(); i++) {
128 if (i > 0 && std::isupper(s[i]))
130 ret += std::tolower(s[i]);
144inline void removeCarriageReturn(std::string& s)
146 if (s.size() > 0 && s[s.size() - 1] ==
'\r')
147 s = s.substr(0, s.size() - 1);
162inline uint levenshteinDist(
const std::string& str1,
const std::string& str2)
164 std::vector<std::vector<uint>> d(
165 str1.size() + 1, std::vector<uint>(str2.size() + 1));
167 if (str1.size() == 0)
169 if (str2.size() == 0)
172 for (uint i = 0; i <= str1.size(); i++)
174 for (uint j = 0; j <= str2.size(); j++)
177 for (uint i = 1; i <= str1.size(); i++) {
178 for (uint j = 1; j <= str2.size(); j++) {
179 uint cost = (str2[j - 1] == str1[i - 1]) ? 0 : 1;
181 uint deletion = d[i - 1][j] + 1;
182 uint insertion = d[i][j - 1] + 1;
183 uint substitution = d[i - 1][j - 1] + cost;
185 d[i][j] =
vcl::min(deletion, insertion, substitution);
189 return d[str1.size()][str2.size()];
201inline uint
distance(
const std::string& str1,
const std::string& str2)
203 return levenshteinDist(str1, str2);
auto distance(const VertexType &v, const PointType &p)
Computes the distance between a Vertex and a 3D point.
Definition element.h:50
constexpr auto min(const T &p1, const T &p2)
Returns the minimum between the two parameters.
Definition min_max.h:42