galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
StringUtils.cpp
Go to the documentation of this file.
1
7
8#include <algorithm>
9#include <cctype>
10
11#include "StringUtils.hpp"
12
13namespace galaxy
14{
15 namespace strutils
16 {
17 std::vector<std::string> split(std::string_view input, std::string_view delim) noexcept
18 {
19 std::size_t start = 0;
20 std::size_t end = 0;
21
22 std::vector<std::string> splits;
23 while ((start = input.find_first_not_of(delim, end)) != std::string::npos)
24 {
25 end = input.find(delim, start);
26
27 const auto split = input.substr(start, end - start);
28 if (!split.empty())
29 {
30 splits.emplace_back(split);
31 }
32 }
33
34 return splits;
35 }
36
37 void replace_first(std::string& input, std::string_view to_replace, std::string_view replace_with) noexcept
38 {
39 const auto pos = input.find(to_replace);
40
41 if (pos != std::string::npos)
42 {
43 input.replace(pos, to_replace.length(), replace_with);
44 }
45 }
46
47 void replace_all(std::string& input, std::string_view to_replace, std::string_view replace_with) noexcept
48 {
49 std::size_t pos = 0;
50
51 while (pos != std::string::npos)
52 {
53 pos = input.find(to_replace, pos);
54
55 if (pos != std::string::npos)
56 {
57 input.replace(pos, to_replace.length(), replace_with);
58 }
59 }
60 }
61
62 bool begins_with(const std::string& input, const std::string& find) noexcept
63 {
64 return (input.rfind(find, 0) == 0);
65 }
66
67 void ltrim(std::string& input) noexcept
68 {
69 // clang-format off
70 input.erase(input.begin(), std::find_if(input.begin(), input.end(), [](const auto ch) {
71 return !std::isspace(ch);
72 }));
73 }
74
75 void rtrim(std::string& input)noexcept
76 {
77 input.erase(std::find_if(input.rbegin(), input.rend(), [](const auto ch) {
78 return !std::isspace(ch);
79 }).base(), input.end());
80 // clang-format on
81 }
82
83 void trim(std::string& input) noexcept
84 {
85 rtrim(input);
86 ltrim(input);
87 }
88
89 void make_single_spaced(std::string& input) noexcept
90 {
91 const auto trim_from = std::unique(input.begin(), input.end(), [](const auto lhs, const auto rhs) {
92 return (lhs == rhs) && (lhs == ' ');
93 });
94
95 input.erase(trim_from, input.end());
96 input.shrink_to_fit();
97 }
98 } // namespace strutils
99} // namespace galaxy
void make_single_spaced(std::string &input) noexcept
Make a string single spaced.
void trim(std::string &input) noexcept
Trim both ends of string.
void rtrim(std::string &input) noexcept
Trim string from start.
bool begins_with(const std::string &input, const std::string &find) noexcept
Check if string begins with another string.
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.
void replace_all(std::string &input, std::string_view to_replace, std::string_view replace_with) noexcept
Replaces all occurrences of a string.
void replace_first(std::string &input, std::string_view to_replace, std::string_view replace_with) noexcept
Replace first occurrence of a string.
void ltrim(std::string &input) noexcept
Trim string from end.
Timer.hpp galaxy.
Definition Async.hpp:17