|
| 1 | +// Copyright Dominic (DNKpp) Koepke 2025 - 2025. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// https://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#ifndef CTNP_PRETTIFY_HPP |
| 7 | +#define CTNP_PRETTIFY_HPP |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "ctnp/Lexer.hpp" |
| 12 | +#include "ctnp/PrintVisitor.hpp" |
| 13 | +#include "ctnp/config/Config.hpp" |
| 14 | +#include "ctnp/parsing/Parser.hpp" |
| 15 | + |
| 16 | +namespace ctnp |
| 17 | +{ |
| 18 | + namespace detail |
| 19 | + { |
| 20 | + [[nodiscard]] |
| 21 | + constexpr std::string_view remove_template_details(std::string_view name) noexcept |
| 22 | + { |
| 23 | + if (name.ends_with(']')) |
| 24 | + { |
| 25 | + auto rest = name | std::views::reverse | std::views::drop(1); |
| 26 | + if (auto const openingIter = std::ranges::find(rest, '['); |
| 27 | + openingIter != rest.end()) |
| 28 | + { |
| 29 | + auto const end = std::ranges::find_if_not( |
| 30 | + openingIter + 1, |
| 31 | + rest.end(), |
| 32 | + lexing::is_space); |
| 33 | + name = std::string_view{name.cbegin(), end.base()}; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return name; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + template <print_iterator OutIter> |
| 42 | + constexpr OutIter prettify_type(OutIter out, std::string_view name) |
| 43 | + { |
| 44 | + static_assert(parsing::parser_visitor<PrintVisitor<OutIter>>); |
| 45 | + |
| 46 | + PrintVisitor<OutIter> visitor{std::move(out)}; |
| 47 | + parsing::Parser parser{std::ref(visitor), name}; |
| 48 | + parser.parse_type(); |
| 49 | + |
| 50 | + return visitor.out(); |
| 51 | + } |
| 52 | + |
| 53 | + template <print_iterator OutIter> |
| 54 | + constexpr OutIter prettify_function(OutIter out, std::string_view name) |
| 55 | + { |
| 56 | + name = detail::remove_template_details(name); |
| 57 | + |
| 58 | + static_assert(parsing::parser_visitor<PrintVisitor<OutIter>>); |
| 59 | + |
| 60 | + PrintVisitor<OutIter> visitor{std::move(out)}; |
| 61 | + parsing::Parser parser{std::ref(visitor), name}; |
| 62 | + parser.parse_function(); |
| 63 | + |
| 64 | + return visitor.out(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#endif |
0 commit comments