|
| 1 | +// Copyright 2025 Glyn Matthews. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt of copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#ifndef SKYR_URL_FORMAT_HPP |
| 7 | +#define SKYR_URL_FORMAT_HPP |
| 8 | + |
| 9 | +#include <format> |
| 10 | + |
| 11 | +#include <skyr/percent_encoding/percent_decode.hpp> |
| 12 | +#include <skyr/url.hpp> |
| 13 | + |
| 14 | +/// \file url_format.hpp |
| 15 | +/// Provides std::format support for skyr::url |
| 16 | +/// |
| 17 | +/// Format specifications: |
| 18 | +/// - {} or {:} - Full URL (default) |
| 19 | +/// - {:s} - Scheme (without ':') |
| 20 | +/// - {:h} - Hostname (punycode/ASCII) |
| 21 | +/// - {:hd} - Hostname (unicode decoded) |
| 22 | +/// - {:p} - Port (empty if default) |
| 23 | +/// - {:P} - Pathname (percent-encoded) |
| 24 | +/// - {:Pd} - Pathname (percent-decoded) |
| 25 | +/// - {:q} - Search/query (percent-encoded, with '?') |
| 26 | +/// - {:qd} - Search/query (percent-decoded) |
| 27 | +/// - {:f} - Fragment (percent-encoded, with '#') |
| 28 | +/// - {:fd} - Fragment (percent-decoded) |
| 29 | +/// - {:o} - Origin (scheme://host:port) |
| 30 | +/// |
| 31 | +/// The 'd' modifier decodes percent-encoding and punycode. |
| 32 | +/// If decoding fails, falls back to the encoded version. |
| 33 | +/// |
| 34 | +/// Examples: |
| 35 | +/// \code |
| 36 | +/// auto url = skyr::url("http://example.إختبار/π?name=John%20Doe"); |
| 37 | +/// std::println("{:h}", url); // example.xn--kgbechtv (punycode) |
| 38 | +/// std::println("{:hd}", url); // example.إختبار (unicode) |
| 39 | +/// std::println("{:P}", url); // /%CF%80 (encoded) |
| 40 | +/// std::println("{:Pd}", url); // /π (decoded) |
| 41 | +/// std::println("{:q}", url); // ?name=John%20Doe (encoded) |
| 42 | +/// std::println("{:qd}", url); // ?name=John Doe (decoded) |
| 43 | +/// \endcode |
| 44 | + |
| 45 | +template <> |
| 46 | +struct std::formatter<skyr::url> { |
| 47 | + enum class format_type { |
| 48 | + full, // Full URL (default) |
| 49 | + scheme, // s - scheme |
| 50 | + hostname, // h - hostname |
| 51 | + port, // p - port |
| 52 | + pathname, // P - pathname |
| 53 | + query, // q - query/search |
| 54 | + fragment, // f - fragment |
| 55 | + origin // o - origin |
| 56 | + }; |
| 57 | + |
| 58 | + format_type type_ = format_type::full; |
| 59 | + bool decode_ = false; // 'd' modifier for decoded output |
| 60 | + |
| 61 | + constexpr auto parse(std::format_parse_context& ctx) { |
| 62 | + auto it = ctx.begin(); |
| 63 | + const auto end = ctx.end(); |
| 64 | + |
| 65 | + if (it == end || *it == '}') { |
| 66 | + type_ = format_type::full; |
| 67 | + decode_ = false; |
| 68 | + return it; |
| 69 | + } |
| 70 | + |
| 71 | + // Parse format spec |
| 72 | + switch (*it) { |
| 73 | + case 's': |
| 74 | + type_ = format_type::scheme; |
| 75 | + ++it; |
| 76 | + break; |
| 77 | + case 'h': |
| 78 | + type_ = format_type::hostname; |
| 79 | + ++it; |
| 80 | + break; |
| 81 | + case 'p': |
| 82 | + type_ = format_type::port; |
| 83 | + ++it; |
| 84 | + break; |
| 85 | + case 'P': |
| 86 | + type_ = format_type::pathname; |
| 87 | + ++it; |
| 88 | + break; |
| 89 | + case 'q': |
| 90 | + type_ = format_type::query; |
| 91 | + ++it; |
| 92 | + break; |
| 93 | + case 'f': |
| 94 | + type_ = format_type::fragment; |
| 95 | + ++it; |
| 96 | + break; |
| 97 | + case 'o': |
| 98 | + type_ = format_type::origin; |
| 99 | + ++it; |
| 100 | + break; |
| 101 | + default: |
| 102 | + throw std::format_error("Invalid format specifier for skyr::url"); |
| 103 | + } |
| 104 | + |
| 105 | + // Check for 'd' (decode) modifier |
| 106 | + if (it != end && *it == 'd') { |
| 107 | + decode_ = true; |
| 108 | + ++it; |
| 109 | + } |
| 110 | + |
| 111 | + if (it != end && *it != '}') { |
| 112 | + throw std::format_error("Invalid format specifier for skyr::url"); |
| 113 | + } |
| 114 | + |
| 115 | + return it; |
| 116 | + } |
| 117 | + |
| 118 | + auto format(const skyr::url& url, std::format_context& ctx) const { |
| 119 | + switch (type_) { |
| 120 | + case format_type::full: |
| 121 | + return std::format_to(ctx.out(), "{}", url.href()); |
| 122 | + |
| 123 | + case format_type::scheme: |
| 124 | + return std::format_to(ctx.out(), "{}", url.scheme()); |
| 125 | + |
| 126 | + case format_type::hostname: |
| 127 | + if (decode_) { |
| 128 | + // Try to get unicode domain, fall back to ASCII if not available |
| 129 | + if (auto domain = url.u8domain()) { |
| 130 | + return std::format_to(ctx.out(), "{}", domain.value()); |
| 131 | + } |
| 132 | + } |
| 133 | + return std::format_to(ctx.out(), "{}", url.hostname()); |
| 134 | + |
| 135 | + case format_type::port: |
| 136 | + return std::format_to(ctx.out(), "{}", url.port()); |
| 137 | + |
| 138 | + case format_type::pathname: { |
| 139 | + auto pathname = url.pathname(); |
| 140 | + if (decode_) { |
| 141 | + // Try to percent-decode, fall back to encoded if decode fails |
| 142 | + if (auto decoded = skyr::percent_decode(pathname)) { |
| 143 | + return std::format_to(ctx.out(), "{}", decoded.value()); |
| 144 | + } |
| 145 | + } |
| 146 | + return std::format_to(ctx.out(), "{}", pathname); |
| 147 | + } |
| 148 | + |
| 149 | + case format_type::query: { |
| 150 | + auto search = url.search(); |
| 151 | + if (decode_ && !search.empty()) { |
| 152 | + // Decode the query string (skip the leading '?') |
| 153 | + auto query_part = search.substr(1); // Remove '?' |
| 154 | + if (auto decoded = skyr::percent_decode(query_part)) { |
| 155 | + return std::format_to(ctx.out(), "?{}", decoded.value()); |
| 156 | + } |
| 157 | + } |
| 158 | + return std::format_to(ctx.out(), "{}", search); |
| 159 | + } |
| 160 | + |
| 161 | + case format_type::fragment: { |
| 162 | + auto hash = url.hash(); |
| 163 | + if (decode_ && !hash.empty()) { |
| 164 | + // Decode the fragment (skip the leading '#') |
| 165 | + auto fragment_part = hash.substr(1); // Remove '#' |
| 166 | + if (auto decoded = skyr::percent_decode(fragment_part)) { |
| 167 | + return std::format_to(ctx.out(), "#{}", decoded.value()); |
| 168 | + } |
| 169 | + } |
| 170 | + return std::format_to(ctx.out(), "{}", hash); |
| 171 | + } |
| 172 | + |
| 173 | + case format_type::origin: |
| 174 | + return std::format_to(ctx.out(), "{}", url.origin()); |
| 175 | + |
| 176 | + default: |
| 177 | + return ctx.out(); |
| 178 | + } |
| 179 | + } |
| 180 | +}; |
| 181 | + |
| 182 | +#endif // SKYR_URL_FORMAT_HPP |
0 commit comments