C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
string.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#if !defined(CPP_ESSENCE_HEADER_ONLY) || !CPP_ESSENCE_HEADER_ONLY
26#include "abi/string.hpp"
27#endif
28
29#include "char.hpp"
30#include "char8_t_remediation.hpp"
31#include "hashing.hpp"
32
33#include <algorithm>
34#include <cstddef>
35#include <string_view>
36
37namespace essence {
38 inline constexpr std::string_view ascii_blank_chars{U8(" \t")};
39
43 struct string_hash {
44 using hash_type = std::hash<std::string_view>;
45 using is_transparent = void;
46
47 std::size_t operator()(const char* str) const {
48 return hash_type{}(str);
49 }
50
51 std::size_t operator()(std::string_view str) const {
52 return hash_type{}(str);
53 }
54
55 std::size_t operator()(const std::string& str) const {
56 return hash_type{}(str);
57 }
58 };
59
64 using is_transparent = void;
65
66 std::size_t operator()(const char* str) const {
67 return (*this)(std::string_view{str});
68 }
69
70 std::size_t operator()(const std::string& str) const {
71 return (*this)(std::string_view{str});
72 }
73
74 std::size_t operator()(std::string_view value) const {
75 std::size_t result{};
76
77 std::ranges::for_each(value, [&](char c) { hash_combine(result, to_lower(c)); });
78
79 return result;
80 }
81 };
82
87 constexpr bool operator()(std::string_view left, std::string_view right) const {
88 return left.size() == right.size() && std::ranges::equal(left, right, [](char c1, char c2) {
89 return c1 == c2 || to_lower(c1) == to_lower(c2);
90 });
91 }
92 };
93
99 constexpr bool operator()(std::string_view left, std::string_view right) const {
100 return std::ranges::lexicographical_compare(
101 left, right, [](char c1, char c2) { return to_lower(c1) < to_lower(c2); });
102 }
103 };
104
111 constexpr std::string_view trim_left(std::string_view str, std::string_view group = ascii_blank_chars) noexcept {
112 if (const auto index = str.find_first_not_of(group); index != std::string_view::npos) {
113 return str.substr(index);
114 }
115
116 return str;
117 }
118
125 constexpr std::string_view trim_right(std::string_view str, std::string_view group = ascii_blank_chars) noexcept {
126 return str.substr(0, str.find_last_not_of(group) + 1);
127 }
128
135 constexpr std::string_view trim(std::string_view str, std::string_view group = ascii_blank_chars) noexcept {
136 return trim_right(trim_left(str, group), group);
137 }
138
139#if !defined(CPP_ESSENCE_HEADER_ONLY) || !CPP_ESSENCE_HEADER_ONLY
145 ES_API(CPPESSENCE) abi::string to_lower(std::string_view str);
146
153 ES_API(CPPESSENCE) abi::string to_upper(std::string_view str);
154#endif
155
156} // namespace essence
Checks whether the first string is lexicographically less than the second string, in a case-insensiti...
Definition string.hpp:98
A case-insensitive equality comparer for two strings.
Definition string.hpp:86
A case-insensitive hash function for const char*, std::string_view and std::string.
Definition string.hpp:63
A hash function for const char*, std::string_view and std::string.
Definition string.hpp:43