C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
parse_raw_identifier_name.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#ifndef _MSC_VER
26#include "../../char8_t_remediation.hpp"
27#endif
28
29#include "../identifier_param.hpp"
30#include "../literal_string.hpp"
31#include "extract_keyword.hpp"
32#include "extraction_param.hpp"
33#include "get_signature_suffix_size.hpp"
34#include "language_tokens.hpp"
35
36#include <cstddef>
37#include <string_view>
38
39#ifdef _MSC_VER
40#include <tuple>
41#endif
42
43namespace essence::meta::detail {
49 constexpr std::string_view get_short_identifier_name(std::string_view raw_name) noexcept {
50 return extract_keyword<find_mode_type::full_match_reverse>(
51 raw_name, language_tokens::scope, extraction_param{.ensure_correctness = false});
52 }
53
61 constexpr std::string_view parse_raw_identifier_name(
62 std::string_view prefix, std::string_view signature, const identifier_param& param = {}) noexcept {
63#ifdef _MSC_VER
64 const auto keyword = prefix;
65#elif defined(__llvm__) && defined(__clang__)
66 const std::string_view keyword{param.type ? U8("[T = ") : U8("[Value = ")};
67#elif defined(__GNUC__)
68 const std::string_view keyword{param.type ? U8("[with T = ") : U8("[with auto Value = ")};
69#else
70#error "Unsupported compiler."
71#endif
72
73 const auto result = extract_keyword<find_mode_type::full_match>(signature, keyword,
74 extraction_param{
75 .preview_first_character = param.preview_first_character,
76 .ensure_correctness = param.ensure_correctness,
77 .suffix_size = get_signature_suffix_size(signature),
78 .extra_size_func = [](std::string_view str, std::size_t prefix_size) -> std::size_t {
79#ifdef _MSC_VER
80 // Skips 'enum', 'class', 'struct' for MSVC.
81 return std::apply(
82 [&, size = std::size_t{}](const auto&... args) mutable {
83 return ((str.compare(prefix_size, args.size(), args) == 0 ? (size = args.size()) : 0U), ...,
84 size);
85 },
86 language_tokens::type_prefixes);
87#else
88 // Skips the possible '&' token for GCC and Clang.
89 return str[prefix_size] == language_tokens::reference.front();
90#endif
91 },
92 });
93
94 if (param.shortened && !param.preview_first_character) {
95 return get_short_identifier_name(result);
96 }
97
98 // ReSharper disable once CppDFALocalValueEscapesFunction
99 return result;
100 }
101} // namespace essence::meta::detail