Skip to content

Commit

Permalink
Remove const and const ref qualifiers on stringviews
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 14, 2025
1 parent c3298b4 commit 5ebc2f0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
12 changes: 6 additions & 6 deletions include/SemVer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ namespace OpenShock {
inline bool operator>(const SemVer& other) const { return !(*this <= other); }
inline bool operator>=(const SemVer& other) const { return !(*this < other); }

bool operator==(const std::string_view& other) const;
inline bool operator!=(const std::string_view& other) const { return !(*this == other); }
bool operator<(const std::string_view& other) const;
inline bool operator<=(const std::string_view& other) const { return *this < other || *this == other; }
inline bool operator>(const std::string_view& other) const { return !(*this <= other); }
inline bool operator>=(const std::string_view& other) const { return !(*this < other); }
bool operator==(std::string_view other) const;
inline bool operator!=(std::string_view other) const { return !(*this == other); }
bool operator<(std::string_view other) const;
inline bool operator<=(std::string_view other) const { return *this < other || *this == other; }
inline bool operator>(std::string_view other) const { return !(*this <= other); }
inline bool operator>=(std::string_view other) const { return !(*this < other); }

bool isValid() const;

Expand Down
20 changes: 10 additions & 10 deletions include/util/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace OpenShock {
bool FormatToString(std::string& out, const char* format, ...);

constexpr std::string_view StringTrimLeft(const std::string_view view) {
constexpr std::string_view StringTrimLeft(std::string_view view) {
if (view.empty()) {
return view;
}
Expand All @@ -23,7 +23,7 @@ namespace OpenShock {

return view.substr(pos);
}
constexpr std::string_view StringTrimRight(const std::string_view view) {
constexpr std::string_view StringTrimRight(std::string_view view) {
if (view.empty()) {
return view;
}
Expand All @@ -35,14 +35,14 @@ namespace OpenShock {

return view.substr(0, pos + 1);
}
constexpr std::string_view StringTrim(const std::string_view view) {
constexpr std::string_view StringTrim(std::string_view view) {
return StringTrimLeft(StringTrimRight(view));
}
constexpr bool StringStartsWith(const std::string_view view, std::string_view prefix) {
constexpr bool StringStartsWith(std::string_view view, std::string_view prefix) {
return view.size() >= prefix.size() && view.substr(0, prefix.size()) == prefix;
}
template<std::size_t N>
constexpr bool TryStringSplit(const std::string_view view, char delimiter, std::string_view (&out)[N]) {
constexpr bool TryStringSplit(std::string_view view, char delimiter, std::string_view (&out)[N]) {
std::size_t pos = 0;
std::size_t idx = 0;
while (pos < view.size() && idx < N) {
Expand All @@ -58,12 +58,12 @@ namespace OpenShock {

return idx == N;
}
std::vector<std::string_view> StringSplit(const std::string_view view, char delimiter, std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
std::vector<std::string_view> StringSplit(const std::string_view view, bool (*predicate)(char delimiter), std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
std::vector<std::string_view> StringSplitNewLines(const std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
std::vector<std::string_view> StringSplitWhiteSpace(const std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
std::vector<std::string_view> StringSplit(std::string_view view, char delimiter, std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
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());
std::vector<std::string_view> StringSplitNewLines(std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t>::max());
std::vector<std::string_view> StringSplitWhiteSpace(std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t>::max());

bool StringIEquals(const std::string_view a, const std::string_view b);
bool StringIEquals(std::string_view a, std::string_view b);

String StringToArduinoString(std::string_view view);
} // namespace OpenShock
4 changes: 2 additions & 2 deletions src/SemVer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ bool SemVer::operator<(const SemVer& other) const
return build < other.build;
}

bool SemVer::operator==(const std::string_view& other) const
bool SemVer::operator==(std::string_view other) const
{
SemVer otherSemVer;
if (!OpenShock::TryParseSemVer(other, otherSemVer)) {
Expand All @@ -308,7 +308,7 @@ bool SemVer::operator==(const std::string_view& other) const
return *this == otherSemVer;
}

bool SemVer::operator<(const std::string_view& other) const
bool SemVer::operator<(std::string_view other) const
{
SemVer otherSemVer;
if (!OpenShock::TryParseSemVer(other, otherSemVer)) {
Expand Down
10 changes: 5 additions & 5 deletions src/util/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool OpenShock::FormatToString(std::string& out, const char* format, ...) {
return true;
}

std::vector<std::string_view> OpenShock::StringSplit(const std::string_view view, char delimiter, std::size_t maxSplits) {
std::vector<std::string_view> OpenShock::StringSplit(std::string_view view, char delimiter, std::size_t maxSplits) {
if (view.empty()) {
return {};
}
Expand All @@ -85,7 +85,7 @@ std::vector<std::string_view> OpenShock::StringSplit(const std::string_view view
return result;
}

std::vector<std::string_view> OpenShock::StringSplit(const std::string_view view, bool (*predicate)(char delimiter), std::size_t maxSplits) {
std::vector<std::string_view> OpenShock::StringSplit(std::string_view view, bool (*predicate)(char delimiter), std::size_t maxSplits) {
if (view.empty()) {
return {};
}
Expand All @@ -111,19 +111,19 @@ std::vector<std::string_view> OpenShock::StringSplit(const std::string_view view
return result;
}

std::vector<std::string_view> OpenShock::StringSplitNewLines(const std::string_view view, std::size_t maxSplits) {
std::vector<std::string_view> OpenShock::StringSplitNewLines(std::string_view view, std::size_t maxSplits) {
return StringSplit(
view, [](char c) { return c == '\r' || c == '\n'; }, maxSplits
);
}

std::vector<std::string_view> OpenShock::StringSplitWhiteSpace(const std::string_view view, std::size_t maxSplits) {
std::vector<std::string_view> OpenShock::StringSplitWhiteSpace(std::string_view view, std::size_t maxSplits) {
return StringSplit(
view, [](char c) { return isspace(c) != 0; }, maxSplits
);
}

bool OpenShock::StringIEquals(const std::string_view a, const std::string_view b)
bool OpenShock::StringIEquals(std::string_view a, std::string_view b)
{
if (a.size() != b.size()) return false;
return strncasecmp(a.data(), b.data(), a.size()) == 0;
Expand Down

1 comment on commit 5ebc2f0

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.3) reports: 2 file(s) not formatted
  • include/util/StringUtils.h
  • src/util/StringUtils.cpp

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.