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 str
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 std::string 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 return input;
47 }
48
49 std::string replace_all(std::string input, std::string_view to_replace, std::string_view replace_with) noexcept
50 {
51 std::size_t pos = 0;
52
53 while (pos != std::string::npos)
54 {
55 pos = input.find(to_replace, pos);
56
57 if (pos != std::string::npos)
58 {
59 input.replace(pos, to_replace.length(), replace_with);
60 }
61 }
62
63 return input;
64 }
65
66 bool begins_with(const std::string& input, const std::string& find) noexcept
67 {
68 return (input.rfind(find, 0) == 0);
69 }
70
71 std::string ltrim(std::string input) noexcept
72 {
73 // clang-format off
74 input.erase(input.begin(), std::find_if(input.begin(), input.end(), [](const auto ch) {
75 return !std::isspace(ch);
76 }));
77
78 return input;
79 }
80
81 std::string rtrim(std::string input)noexcept
82 {
83 input.erase(std::find_if(input.rbegin(), input.rend(), [](const auto ch) {
84 return !std::isspace(ch);
85 }).base(), input.end());
86 // clang-format on
87
88 return input;
89 }
90
91 std::string trim(std::string input) noexcept
92 {
93 input = rtrim(input);
94 input = ltrim(input);
95
96 return input;
97 }
98
99 std::string make_single_spaced(std::string input) noexcept
100 {
101 const auto trim_from = std::unique(input.begin(), input.end(), [](const auto lhs, const auto rhs) {
102 return (lhs == rhs) && (lhs == ' ');
103 });
104
105 input.erase(trim_from, input.end());
106 input.shrink_to_fit();
107
108 return input;
109 }
110 } // namespace str
111} // namespace galaxy
std::string rtrim(std::string input) noexcept
Trim string from start.
std::string replace_all(std::string input, std::string_view to_replace, std::string_view replace_with) noexcept
Replaces all occurrences of a string.
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.
std::string ltrim(std::string input) noexcept
Trim string from end.
bool begins_with(const std::string &input, const std::string &find) noexcept
Check if string begins with another string.
std::string trim(std::string input) noexcept
Trim both ends of string.
std::string make_single_spaced(std::string input) noexcept
Make a string single spaced.
std::string replace_first(std::string input, std::string_view to_replace, std::string_view replace_with) noexcept
Replace first occurrence of a string.
Timer.hpp galaxy.
Definition Timer.cpp:18