66#include < limits>
77#include < string>
88#include < string_view>
9+ #include < utility>
910#include < vector>
1011
1112namespace OpenShock {
@@ -17,7 +18,7 @@ namespace OpenShock {
1718 }
1819
1920 std::size_t pos = 0 ;
20- while (pos < view.size () && isspace (view[pos])) {
21+ while (pos < view.size () && isspace (view[pos]) != 0 ) {
2122 ++pos;
2223 }
2324
@@ -29,7 +30,7 @@ namespace OpenShock {
2930 }
3031
3132 std::size_t pos = view.size () - 1 ;
32- while (pos > 0 && isspace (view[pos])) {
33+ while (pos > 0 && isspace (view[pos]) != 0 ) {
3334 --pos;
3435 }
3536
@@ -38,16 +39,79 @@ namespace OpenShock {
3839 constexpr std::string_view StringTrim (std::string_view view) {
3940 return StringTrimLeft (StringTrimRight (view));
4041 }
42+
43+ constexpr bool StringStartsWith (std::string_view view, char prefix) {
44+ return !view.empty () && view.front () == prefix;
45+ }
4146 constexpr bool StringStartsWith (std::string_view view, std::string_view prefix) {
4247 return view.size () >= prefix.size () && view.substr (0 , prefix.size ()) == prefix;
4348 }
4449 constexpr bool StringEndsWith (std::string_view view, char suffix)
4550 {
46- return view.size () >= 1 && view.back () == suffix;
51+ return ! view.empty () && view.back () == suffix;
4752 }
4853 constexpr bool StringEndsWith (std::string_view view, std::string_view suffix) {
4954 return view.size () >= suffix.size () && view.substr (view.size () - suffix.size (), view.size ()) == suffix;
5055 }
56+
57+ constexpr std::string_view StringRemovePrefix (std::string_view view, char prefix) {
58+ if (StringStartsWith (view, prefix)) view.remove_prefix (1 );
59+ return view;
60+ }
61+ constexpr std::string_view StringRemovePrefix (std::string_view view, std::string_view prefix) {
62+ if (StringStartsWith (view, prefix)) view.remove_prefix (prefix.length ());
63+ return view;
64+ }
65+ constexpr std::string_view StringRemoveSuffix (std::string_view view, char suffix) {
66+ if (StringStartsWith (view, suffix)) view.remove_suffix (1 );
67+ return view;
68+ }
69+ constexpr std::string_view StringRemoveSuffix (std::string_view view, std::string_view suffix) {
70+ if (StringStartsWith (view, suffix)) view.remove_prefix (suffix.length ());
71+ return view;
72+ }
73+
74+ constexpr std::string_view StringBeforeFirst (std::string_view view, char delimiter) {
75+ size_t pos = view.find (delimiter);
76+ if (pos == std::string_view::npos) return view;
77+ return view.substr (0 , pos);
78+ }
79+ constexpr std::string_view StringBeforeFirst (std::string_view view, std::string_view delimiter) {
80+ size_t pos = view.find (delimiter);
81+ if (pos == std::string_view::npos) return view;
82+ return view.substr (0 , pos);
83+ }
84+ constexpr std::string_view StringBeforeLast (std::string_view view, char delimiter) {
85+ size_t pos = view.find_last_of (delimiter);
86+ if (pos == std::string_view::npos) return view;
87+ return view.substr (0 , pos);
88+ }
89+ constexpr std::string_view StringBeforeLast (std::string_view view, std::string_view delimiter) {
90+ size_t pos = view.find_last_of (delimiter);
91+ if (pos == std::string_view::npos) return view;
92+ return view.substr (0 , pos);
93+ }
94+ constexpr std::string_view StringAfterFirst (std::string_view view, char delimiter) {
95+ size_t pos = view.find (delimiter);
96+ if (pos == std::string_view::npos) return view;
97+ return view.substr (pos + 1 );
98+ }
99+ constexpr std::string_view StringAfterFirst (std::string_view view, std::string_view delimiter) {
100+ size_t pos = view.find (delimiter);
101+ if (pos == std::string_view::npos) return view;
102+ return view.substr (pos + delimiter.length ());
103+ }
104+ constexpr std::string_view StringAfterLast (std::string_view view, char delimiter) {
105+ size_t pos = view.find_last_of (delimiter);
106+ if (pos == std::string_view::npos) return view;
107+ return view.substr (pos + 1 );
108+ }
109+ constexpr std::string_view StringAfterLast (std::string_view view, std::string_view delimiter) {
110+ size_t pos = view.find_last_of (delimiter);
111+ if (pos == std::string_view::npos) return view;
112+ return view.substr (pos + delimiter.length ());
113+ }
114+
51115 template <std::size_t N>
52116 constexpr bool TryStringSplit (std::string_view view, char delimiter, std::string_view (&out)[N]) {
53117 std::size_t pos = 0 ;
@@ -69,8 +133,26 @@ namespace OpenShock {
69133 std::vector<std::string_view> StringSplit (std::string_view view, bool (*predicate)(char delimiter), std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
70134 std::vector<std::string_view> StringSplitNewLines (std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t >::max());
71135 std::vector<std::string_view> StringSplitWhiteSpace (std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t >::max());
136+ constexpr std::pair<std::string_view, std::string_view> StringSplitByFirst (std::string_view view, char delimiter) {
137+ size_t pos = view.find (delimiter);
138+ return std::make_pair (view.substr (0 , pos), pos == std::string_view::npos ? std::string_view () : view.substr (pos + 1 ));
139+ }
140+ constexpr std::pair<std::string_view, std::string_view> StringSplitByFirst (std::string_view view, std::string_view delimiter) {
141+ size_t pos = view.find (delimiter);
142+ return std::make_pair (view.substr (0 , pos), pos == std::string_view::npos ? std::string_view () : view.substr (pos + delimiter.length ()));
143+ }
144+ constexpr std::pair<std::string_view, std::string_view> StringSplitByLast (std::string_view view, char delimiter) {
145+ size_t pos = view.find_last_of (delimiter);
146+ return std::make_pair (view.substr (0 , pos), pos == std::string_view::npos ? std::string_view () : view.substr (pos + 1 ));
147+ }
148+ constexpr std::pair<std::string_view, std::string_view> StringSplitByLast (std::string_view view, std::string_view delimiter) {
149+ size_t pos = view.find_last_of (delimiter);
150+ return std::make_pair (view.substr (0 , pos), pos == std::string_view::npos ? std::string_view () : view.substr (pos + delimiter.length ()));
151+ }
72152
73153 bool StringIEquals (std::string_view a, std::string_view b);
154+ bool StringIContains (std::string_view haystack, std::string_view needle);
155+ bool StringHasPrefixIC (std::string_view view, std::string_view prefix);
74156
75157 String StringToArduinoString (std::string_view view);
76158} // namespace OpenShock
0 commit comments