RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Compare.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "RST/string/Char.hpp"
4
5#include <cstddef>
6#include <string_view>
7
8namespace RST::String {
12 [[nodiscard]] constexpr bool contains(std::string_view str, std::string_view needle) noexcept { return str.find(needle) != std::string_view::npos; }
13 [[nodiscard]] constexpr bool contains(std::string_view str, char c) noexcept { return str.find(c) != std::string_view::npos; }
14
15 [[nodiscard]] constexpr bool equalsIgnoreCase(std::string_view lhs, std::string_view rhs) noexcept
16 {
17 if (lhs.size() != rhs.size()) {
18 return false;
19 }
20 for (std::size_t i = 0; i < lhs.size(); ++i) {
21 if (toLower(lhs[i]) != toLower(rhs[i])) {
22 return false;
23 }
24 }
25 return true;
26 }
27
28 [[nodiscard]] constexpr bool startsWithIgnoreCase(std::string_view str, std::string_view prefix) noexcept
29 {
30 return str.size() >= prefix.size() && equalsIgnoreCase(str.substr(0, prefix.size()), prefix);
31 }
32
33 [[nodiscard]] constexpr bool endsWithIgnoreCase(std::string_view str, std::string_view suffix) noexcept
34 {
35 return str.size() >= suffix.size() && equalsIgnoreCase(str.substr(str.size() - suffix.size()), suffix);
36 }
37
39 [[nodiscard]] constexpr std::size_t findIgnoreCase(std::string_view str, std::string_view needle, std::size_t from = 0) noexcept
40 {
41 if (from > str.size() || needle.size() > str.size() - from) {
42 return std::string_view::npos;
43 }
44 for (std::size_t i = from; i <= str.size() - needle.size(); ++i) {
45 if (equalsIgnoreCase(str.substr(i, needle.size()), needle)) {
46 return i;
47 }
48 }
49 return std::string_view::npos;
50 }
51
52 [[nodiscard]] constexpr bool containsIgnoreCase(std::string_view str, std::string_view needle) noexcept
53 {
54 return findIgnoreCase(str, needle) != std::string_view::npos;
55 }
56
58 [[nodiscard]] constexpr int compareIgnoreCase(std::string_view lhs, std::string_view rhs) noexcept
59 {
60 const std::size_t common = lhs.size() < rhs.size() ? lhs.size() : rhs.size();
61 for (std::size_t i = 0; i < common; ++i) {
62 const auto a = static_cast<unsigned char>(toLower(lhs[i]));
63 const auto b = static_cast<unsigned char>(toLower(rhs[i]));
64 if (a != b) {
65 return a < b ? -1 : 1;
66 }
67 }
68 return lhs.size() == rhs.size() ? 0 : (lhs.size() < rhs.size() ? -1 : 1);
69 }
71}
String helpers: case, trimming, splitting, joining, replacing, parsing and hashing.
Definition Char.hpp:3
constexpr char toLower(char c) noexcept
Definition Char.hpp:16
constexpr bool endsWithIgnoreCase(std::string_view str, std::string_view suffix) noexcept
Definition Compare.hpp:33
constexpr std::size_t findIgnoreCase(std::string_view str, std::string_view needle, std::size_t from=0) noexcept
Position of the first needle at or after from, or std::string_view::npos.
Definition Compare.hpp:39
constexpr bool startsWithIgnoreCase(std::string_view str, std::string_view prefix) noexcept
Definition Compare.hpp:28
constexpr bool contains(std::string_view str, std::string_view needle) noexcept
Definition Compare.hpp:12
constexpr bool equalsIgnoreCase(std::string_view lhs, std::string_view rhs) noexcept
Definition Compare.hpp:15
constexpr int compareIgnoreCase(std::string_view lhs, std::string_view rhs) noexcept
Three-way comparison like strcmp: negative, zero or positive.
Definition Compare.hpp:58
constexpr bool containsIgnoreCase(std::string_view str, std::string_view needle) noexcept
Definition Compare.hpp:52
std::optional< T > parseNumber(std::string_view text, int base=10) noexcept
Parses text as a number, or returns std::nullopt if it is not entirely one.
Definition Convert.hpp:23