Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
uritemplate.h
1#ifndef SOURCEMETA_CORE_URITEMPLATE_H_
2#define SOURCEMETA_CORE_URITEMPLATE_H_
3
4#ifndef SOURCEMETA_CORE_URITEMPLATE_EXPORT
5#include <sourcemeta/core/uritemplate_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/uritemplate_error.h>
10#include <sourcemeta/core/uritemplate_router.h>
11#include <sourcemeta/core/uritemplate_token.h>
12// NOLINTEND(misc-include-cleaner)
13
14#include <cstddef> // std::size_t
15#include <cstdint> // std::uint64_t
16#include <functional> // std::function
17#include <optional> // std::optional
18#include <string> // std::string
19#include <string_view> // std::string_view
20#include <tuple> // std::tuple
21#include <type_traits> // std::void_t
22#include <vector> // std::vector
23
32
33namespace sourcemeta::core {
34
37using URITemplateValue = std::optional<
38 std::tuple<std::string_view, std::optional<std::string_view>, bool>>;
39
42enum class URITemplateExpansionMode : std::uint8_t {
54};
55
60 std::optional<std::pair<URITemplateToken, std::size_t>>;
61
65class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplate {
66public:
75 URITemplate(const std::string_view source);
76
89 [[nodiscard]] static auto is_uritemplate(std::string_view input) noexcept
90 -> bool;
91
106 [[nodiscard]] static auto is_literal(char32_t codepoint) noexcept -> bool;
107
109 [[nodiscard]] auto size() const noexcept -> std::uint64_t;
110
112 [[nodiscard]] auto empty() const noexcept -> bool;
113
115 [[nodiscard]] auto at(std::size_t index) const & -> const URITemplateToken &;
116
118 [[nodiscard]] auto at(std::size_t index) && -> URITemplateToken;
119
121 [[nodiscard]] auto begin() const noexcept
122 -> std::vector<URITemplateToken>::const_iterator;
123
125 [[nodiscard]] auto end() const noexcept
126 -> std::vector<URITemplateToken>::const_iterator;
127
130 [[nodiscard]] auto
131 expand(const std::function<URITemplateValue(std::string_view)> &callback,
133 -> std::string;
134
136 template <typename Container,
137 typename = std::void_t<typename Container::key_type>>
138 [[nodiscard]] auto expand(
139 const Container &variables,
141 -> std::string {
142 return this->expand(
143 [&variables](const std::string_view name) -> URITemplateValue {
144 const auto iterator{find_variable(variables, name)};
145 if (iterator == variables.end()) {
146 return std::nullopt;
147 }
148 return std::make_tuple(std::string_view{iterator->second},
149 std::nullopt, false);
150 },
151 mode);
152 }
153
154private:
155 // Prefer a heterogeneous lookup so the key is not materialized when the
156 // container supports it
157 template <typename Container>
158 static auto find_variable(const Container &variables,
159 const std::string_view name) {
160 if constexpr (requires { variables.find(name); }) {
161 return variables.find(name);
162 } else {
163 return variables.find(typename Container::key_type{name});
164 }
165 }
166
167// Exporting symbols that depends on the standard C++ library is considered
168// safe.
169// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
170#if defined(_MSC_VER)
171#pragma warning(push)
172#pragma warning(disable : 4251)
173#endif
174 std::vector<URITemplateToken> tokens_;
175#if defined(_MSC_VER)
176#pragma warning(pop)
177#endif
178};
179
180} // namespace sourcemeta::core
181
182#endif
Definition uri.h:44
auto size() const noexcept -> std::uint64_t
Get the number of tokens in the template.
static auto is_literal(char32_t codepoint) noexcept -> bool
auto empty() const noexcept -> bool
Check if the template is empty.
auto expand(const std::function< URITemplateValue(std::string_view)> &callback, URITemplateExpansionMode mode=URITemplateExpansionMode::URI) const -> std::string
static auto is_uritemplate(std::string_view input) noexcept -> bool
auto at(std::size_t index) const &-> const URITemplateToken &
Get the token at the given index.
URITemplate(const std::string_view source)
std::optional< std::pair< URITemplateToken, std::size_t > > URITemplateParseResult
Definition uritemplate.h:59
std::variant< URITemplateTokenLiteral, URITemplateTokenVariable, URITemplateTokenReservedExpansion, URITemplateTokenFragmentExpansion, URITemplateTokenLabelExpansion, URITemplateTokenPathExpansion, URITemplateTokenPathParameterExpansion, URITemplateTokenQueryExpansion, URITemplateTokenQueryContinuationExpansion > URITemplateToken
Definition uritemplate_token.h:184
std::optional< std::tuple< std::string_view, std::optional< std::string_view >, bool > > URITemplateValue
Definition uritemplate.h:37
URITemplateExpansionMode
Definition uritemplate.h:42
@ IRI
Definition uritemplate.h:53