sandbox
Loading...
Searching...
No Matches
alias.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_FILESYSTEM_ALIAS_HPP_
3#define LIBSBX_FILESYSTEM_ALIAS_HPP_
4
5#include <string>
6#include <string_view>
7
9
10namespace sbx::filesystem {
11
12template<utility::character Char>
14public:
15
16 using char_type = Char;
17 using string_type = std::basic_string<char_type>;
18 using string_view_type = std::basic_string_view<char_type>;
19 using size_type = std::size_t;
20 using hash_type = std::size_t;
21
23 : _value{_normalize(string_view_type{})} { }
24
25 explicit basic_alias(string_view_type alias)
26 : _value{_normalize(alias)} { }
27
28 static auto root() -> basic_alias {
29 return basic_alias{string_view_type{}};
30 }
31
32 auto string() const & noexcept -> const string_type& {
33 return _value;
34 }
35
36 auto string() && noexcept -> string_type {
37 return std::move(_value);
38 }
39
40 auto view() const noexcept -> string_view_type {
41 return _value;
42 }
43
44 auto length() const noexcept -> size_type {
45 return _value.length();
46 }
47
48 auto hash() const noexcept -> hash_type {
49 // return static_cast<hash_type>(utility::fnv1a_hash<char_type, std::uint64_t>{}(_value));
50 return std::hash<string_type>{}(_value);
51 }
52
53 auto operator==(const basic_alias& other) const noexcept -> bool {
54 return _value == other._value;
55 }
56
57private:
58
59 static auto _normalize(string_view_type alias) -> string_type {
60 auto normalized = string_type{alias};
61 auto whitespace = string_view_type{reinterpret_cast<const char_type*>(" \t\n\r")};
62
63 auto begin = normalized.find_first_not_of(whitespace);
64
65 if (begin == string_type::npos) {
66 normalized.clear();
67 } else if (begin > 0) {
68 normalized.erase(0, begin);
69 }
70
71 if (!normalized.empty()) {
72 auto end = normalized.find_last_not_of(whitespace);
73
74 if (end != string_type::npos && end + 1 < normalized.size()) {
75 normalized.erase(end + 1);
76 }
77 }
78
79 if (normalized.find(reinterpret_cast<const char_type*>("://")) != string_type::npos) {
80 return normalized;
81 }
82
83 if (normalized.empty()) {
84 normalized = reinterpret_cast<const char_type*>("/");
85 }
86
87 if (normalized.front() != static_cast<char_type>('/')) {
88 normalized.insert(normalized.begin(), static_cast<char_type>('/'));
89 }
90
91 while (normalized.size() > 1 && normalized.back() == static_cast<char_type>('/')) {
92 normalized.pop_back();
93 }
94
95 if (normalized.back() != static_cast<char_type>('/')) {
96 normalized.push_back(static_cast<char_type>('/'));
97 }
98
99 return normalized;
100 }
101
102 string_type _value;
103
104}; // class basic_alias
105
107
108} // namespace sbx::filesystem
109
110template<sbx::utility::character Char>
111struct std::hash<sbx::filesystem::basic_alias<Char>> {
112
113 auto operator()(const sbx::filesystem::basic_alias<Char>& alias) const noexcept -> std::size_t {
114 return alias.hash();
115 }
116
117}; // struct std::hash<sbx::filesystem::basic_alias<Char>>
118
119#endif // LIBSBX_FILESYSTEM_ALIAS_HPP_
Definition: alias.hpp:13