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
93 [[nodiscard]] auto size() const noexcept -> std::uint64_t;
94
96 [[nodiscard]] auto empty() const noexcept -> bool;
97
99 [[nodiscard]] auto at(std::size_t index) const & -> const URITemplateToken &;
100
102 [[nodiscard]] auto at(std::size_t index) && -> URITemplateToken;
103
105 [[nodiscard]] auto begin() const noexcept
106 -> std::vector<URITemplateToken>::const_iterator;
107
109 [[nodiscard]] auto end() const noexcept
110 -> std::vector<URITemplateToken>::const_iterator;
111
114 [[nodiscard]] auto
115 expand(const std::function<URITemplateValue(std::string_view)> &callback,
117 -> std::string;
118
120 template <typename Container,
121 typename = std::void_t<typename Container::key_type>>
122 [[nodiscard]] auto expand(
123 const Container &variables,
125 -> std::string {
126 return this->expand(
127 [&variables](const std::string_view name) -> URITemplateValue {
128 const auto iterator{find_variable(variables, name)};
129 if (iterator == variables.end()) {
130 return std::nullopt;
131 } else {
132 return std::make_tuple(std::string_view{iterator->second},
133 std::nullopt, false);
134 }
135 },
136 mode);
137 }
138
139private:
140 // Prefer a heterogeneous lookup so the key is not materialized when the
141 // container supports it
142 template <typename Container>
143 static auto find_variable(const Container &variables,
144 const std::string_view name) {
145 if constexpr (requires { variables.find(name); }) {
146 return variables.find(name);
147 } else {
148 return variables.find(typename Container::key_type{name});
149 }
150 }
151
152// Exporting symbols that depends on the standard C++ library is considered
153// safe.
154// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
155#if defined(_MSC_VER)
156#pragma warning(disable : 4251)
157#endif
158 std::vector<URITemplateToken> tokens_;
159#if defined(_MSC_VER)
160#pragma warning(default : 4251)
161#endif
162};
163
164} // namespace sourcemeta::core
165
166#endif
Definition uri.h:43
auto size() const noexcept -> std::uint64_t
Get the number of tokens in the template.
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