UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
string.cpp
1
41// UFO
42#include <ufo/utility/string.hpp>
43
44// STL
45#include <algorithm>
46#include <cstddef>
47#include <sstream>
48#include <string>
49#include <vector>
50
51std::vector<std::string> ufo::split(std::string const& s, char delimiter)
52{
53 std::vector<std::string> result;
54 std::string token;
55 std::istringstream stream(s);
56 while (std::getline(stream, token, delimiter)) {
57 result.push_back(token);
58 }
59 return result;
60}
61
62std::string ufo::join(std::vector<std::string> const& strings, char delimiter)
63{
64 if (strings.empty()) {
65 return "";
66 }
67
68 std::string result = strings.front();
69 for (std::size_t i{1}; strings.size() > i; ++i) {
70 result += delimiter;
71 result += strings[i];
72 }
73 return result;
74}
75
76bool ufo::startsWith(std::string const& s, std::string const& prefix)
77{
78 return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
79}
80
81bool ufo::endsWith(std::string const& s, std::string const& suffix)
82{
83 return s.size() >= suffix.size() &&
84 std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
85}
86
87bool ufo::contains(std::string const& s, std::string const& sub)
88{
89 return s.find(sub) != std::string::npos;
90}
91
92std::string ufo::tolower(std::string s)
93{
94 std::transform(s.begin(), s.end(), s.begin(),
95 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
96 return s;
97}
98
99std::string ufo::toupper(std::string s)
100{
101 std::transform(s.begin(), s.end(), s.begin(),
102 [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
103 return s;
104}
105
106void ufo::ltrim(std::string& s)
107{
108 s.erase(s.begin(), std::find_if(s.begin(), s.end(),
109 [](unsigned char ch) { return !std::isspace(ch); }));
110}
111
112void ufo::rtrim(std::string& s)
113{
114 s.erase(std::find_if(s.rbegin(), s.rend(),
115 [](unsigned char ch) { return !std::isspace(ch); })
116 .base(),
117 s.end());
118}
119
120void ufo::trim(std::string& s)
121{
122 rtrim(s);
123 ltrim(s);
124}
125
126std::string ufo::ltrimCopy(std::string s)
127{
128 ltrim(s);
129 return s;
130}
131
132std::string ufo::rtrimCopy(std::string s)
133{
134 rtrim(s);
135 return s;
136}
137
138std::string ufo::trimCopy(std::string s)
139{
140 trim(s);
141 return s;
142}
std::vector< std::string > split(std::string const &s, char delimiter)
Splits a string into a vector of substrings using a delimiter.
Definition string.cpp:51
void ltrim(std::string &s)
Removes leading whitespace from the string (in-place).
Definition string.cpp:106
bool startsWith(std::string const &s, std::string const &prefix)
Checks if a string starts with a given prefix.
Definition string.cpp:76
bool endsWith(std::string const &s, std::string const &suffix)
Checks if a string ends with a given suffix.
Definition string.cpp:81
void rtrim(std::string &s)
Removes trailing whitespace from the string (in-place).
Definition string.cpp:112
std::string toupper(std::string s)
Converts all characters in the string to uppercase.
Definition string.cpp:99
std::string join(std::vector< std::string > const &strings, char delimiter)
Joins a vector of strings into a single string with a delimiter.
Definition string.cpp:62
std::string ltrimCopy(std::string s)
Returns a copy of the string with leading whitespace removed.
Definition string.cpp:126
std::string tolower(std::string s)
Converts all characters in the string to lowercase.
Definition string.cpp:92
std::string trimCopy(std::string s)
Returns a copy of the string with leading and trailing whitespace removed.
Definition string.cpp:138
std::string rtrimCopy(std::string s)
Returns a copy of the string with trailing whitespace removed.
Definition string.cpp:132
constexpr bool contains(A const &a, B const &b)
Checks if a shape contains another shape.
Definition contains.hpp:63
void trim(std::string &s)
Removes leading and trailing whitespace from the string (in-place).
Definition string.cpp:120