Visual Computing Library
Loading...
Searching...
No Matches
string.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MISC_STRING_H
24#define VCL_MISC_STRING_H
25
26#include <vclib/math/min_max.h>
27
28#include <algorithm>
29#include <cctype>
30#include <sstream>
31#include <string>
32#include <vector>
33
34namespace vcl {
35
46inline std::string::const_iterator findCaseInsensitive(
47 const std::string& input,
48 const std::string& substr)
49{
50 // link: https://stackoverflow.com/a/19839371/5851101
51
52 std::string::const_iterator it = std::search(
53 input.begin(),
54 input.end(),
55 substr.begin(),
56 substr.end(),
57 [](char ch1, char ch2) {
58 return std::toupper(ch1) == std::toupper(ch2);
59 });
60 return it;
61}
62
71template<typename T>
72std::string toString(T val)
73{
74 // if T is a pointer
75 if constexpr (std::is_pointer_v<T>) {
76 const void* address = static_cast<const void*>(val);
77 std::stringstream ss;
78 ss << address;
79 return ss.str();
80 }
81 else {
82 return std::to_string(val);
83 }
84}
85
93inline bool containsCaseInsensitive(
94 const std::string& input,
95 const std::string& substr)
96{
97 return findCaseInsensitive(input, substr) != input.end();
98}
99
100inline constexpr std::string toLower(const std::string& s)
101{
102 std::string ret(s);
103 std::transform(s.begin(), s.end(), ret.begin(), ::tolower);
104 return ret;
105}
106
107inline constexpr std::string toUpper(const std::string& s)
108{
109 std::string ret(s);
110 std::transform(s.begin(), s.end(), ret.begin(), [](unsigned char c) {
111 return std::toupper(c);
112 });
113 return ret;
114}
115
124inline constexpr std::string camelCaseToSnakeCase(const std::string& s)
125{
126 std::string ret;
127 for (size_t i = 0; i < s.size(); i++) {
128 if (i > 0 && std::isupper(s[i]))
129 ret += '_';
130 ret += std::tolower(s[i]);
131 }
132 return ret;
133}
134
144inline void removeCarriageReturn(std::string& s)
145{
146 if (s.size() > 0 && s[s.size() - 1] == '\r')
147 s = s.substr(0, s.size() - 1);
148}
149
162inline uint levenshteinDist(const std::string& str1, const std::string& str2)
163{
164 std::vector<std::vector<uint>> d(
165 str1.size() + 1, std::vector<uint>(str2.size() + 1));
166
167 if (str1.size() == 0)
168 return str2.size();
169 if (str2.size() == 0)
170 return str1.size();
171
172 for (uint i = 0; i <= str1.size(); i++)
173 d[i][0] = i;
174 for (uint j = 0; j <= str2.size(); j++)
175 d[0][j] = j;
176
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;
180
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;
184
185 d[i][j] = vcl::min(deletion, insertion, substitution);
186 }
187 }
188
189 return d[str1.size()][str2.size()];
190}
191
201inline uint distance(const std::string& str1, const std::string& str2)
202{
203 return levenshteinDist(str1, str2);
204}
205
206} // namespace vcl
207
208#endif // VCL_MISC_STRING_H
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