Skip to content

Commit 82ac48e

Browse files
committed
reserved legible names are automatically generated
#improvement
1 parent e0a9479 commit 82ac48e

38 files changed

+1039
-138
lines changed

include/mrdocs/ADT/UnorderedStringMap.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace clang::mrdocs {
1919

20-
struct string_hash
20+
struct StringHash
2121
{
2222
using hash_type = std::hash<std::string_view>;
2323
using is_transparent = void;
@@ -27,7 +27,10 @@ struct string_hash
2727
};
2828

2929
template <class T>
30-
using UnorderedStringMap = std::unordered_map<std::string, T, string_hash, std::equal_to<>>;
30+
using UnorderedStringMap = std::unordered_map<std::string, T, StringHash, std::equal_to<>>;
31+
32+
template <class T>
33+
using UnorderedStringMultiMap = std::unordered_multimap<std::string, T, StringHash, std::equal_to<>>;
3134

3235
} // clang::mrdocs
3336

include/mrdocs/Metadata/Info.hpp

+53-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ tag_invoke(
5959
v = toString(kind);
6060
}
6161

62+
consteval
63+
std::underlying_type_t<InfoKind>
64+
countInfoKind()
65+
{
66+
std::underlying_type_t<InfoKind> count = 0;
67+
#define INFO(Type) count++;
68+
#include <mrdocs/Metadata/InfoNodesPascal.inc>
69+
return count;
70+
}
71+
6272
/** Base class with common properties of all symbols
6373
*/
6474
struct MRDOCS_VISIBLE Info
@@ -136,7 +146,49 @@ struct MRDOCS_VISIBLE Info
136146
{
137147
}
138148

139-
#define INFO(Type) constexpr bool is##Type() const noexcept { return Kind == InfoKind::Type; }
149+
#define INFO(Type) constexpr bool is##Type() const noexcept { \
150+
return Kind == InfoKind::Type; \
151+
}
152+
#include <mrdocs/Metadata/InfoNodesPascal.inc>
153+
154+
constexpr Info const& asInfo() const noexcept
155+
{
156+
return *this;
157+
}
158+
159+
constexpr Info& asInfo() noexcept
160+
{
161+
return *this;
162+
}
163+
164+
#define INFO(Type) \
165+
constexpr Type##Info const& as##Type() const noexcept { \
166+
if (Kind == InfoKind::Type) \
167+
return reinterpret_cast<Type##Info const&>(*this); \
168+
MRDOCS_UNREACHABLE(); \
169+
}
170+
#include <mrdocs/Metadata/InfoNodesPascal.inc>
171+
172+
#define INFO(Type) \
173+
constexpr Type##Info & as##Type() noexcept { \
174+
if (Kind == InfoKind::Type) \
175+
return reinterpret_cast<Type##Info&>(*this); \
176+
MRDOCS_UNREACHABLE(); \
177+
}
178+
#include <mrdocs/Metadata/InfoNodesPascal.inc>
179+
180+
#define INFO(Type) \
181+
constexpr Type##Info const* as##Type##Ptr() const noexcept { \
182+
if (Kind == InfoKind::Type) { return reinterpret_cast<Type##Info const*>(this); } \
183+
return nullptr; \
184+
}
185+
#include <mrdocs/Metadata/InfoNodesPascal.inc>
186+
187+
#define INFO(Type) \
188+
constexpr Type##Info * as##Type##Ptr() noexcept { \
189+
if (Kind == InfoKind::Type) { return reinterpret_cast<Type##Info *>(this); } \
190+
return nullptr; \
191+
}
140192
#include <mrdocs/Metadata/InfoNodesPascal.inc>
141193

142194
auto operator<=>(Info const&) const = default;

include/mrdocs/Metadata/Source.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ struct MRDOCS_DECL
127127
*/
128128
std::vector<Location> Loc;
129129

130+
constexpr SourceInfo const& asSourceInfo() const noexcept
131+
{
132+
return *this;
133+
}
134+
135+
constexpr SourceInfo& asSourceInfo() noexcept
136+
{
137+
return *this;
138+
}
139+
130140
constexpr virtual ~SourceInfo() = default;
131141

132142
auto operator<=>(SourceInfo const&) const = default;

include/mrdocs/Support/String.hpp

+277
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,283 @@ endsWithOneOf(std::string_view s, std::string_view chars) noexcept
118118
return !s.empty() && chars.find(s.back()) != std::string_view::npos;
119119
}
120120

121+
constexpr
122+
bool
123+
isLowerCase(char const c) noexcept
124+
{
125+
return c >= 'a' && c <= 'z';
126+
}
127+
128+
constexpr
129+
bool
130+
isLowerCase(std::string_view const s) noexcept
131+
{
132+
for (char const c : s)
133+
{
134+
if (!isLowerCase(c))
135+
{
136+
return false;
137+
}
138+
}
139+
return true;
140+
}
141+
142+
constexpr
143+
bool
144+
isUpperCase(char const c) noexcept
145+
{
146+
return c >= 'A' && c <= 'Z';
147+
}
148+
149+
constexpr
150+
bool
151+
isUpperCase(std::string_view const s) noexcept
152+
{
153+
for (char const c : s)
154+
{
155+
if (!isUpperCase(c))
156+
{
157+
return false;
158+
}
159+
}
160+
return true;
161+
}
162+
163+
constexpr
164+
char
165+
toLowerCase(char const c) noexcept
166+
{
167+
return isUpperCase(c) ? c + ('a' - 'A') : c;
168+
}
169+
170+
constexpr
171+
std::string
172+
toLowerCase(std::string_view const s) noexcept
173+
{
174+
std::string result;
175+
result.reserve(s.size());
176+
for (char const c : s)
177+
{
178+
result.push_back(toLowerCase(c));
179+
}
180+
return result;
181+
}
182+
183+
constexpr
184+
char
185+
toUpperCase(char const c) noexcept
186+
{
187+
return isLowerCase(c) ? c - ('a' - 'A') : c;
188+
}
189+
190+
constexpr
191+
std::string
192+
toUpperCase(std::string_view const s) noexcept
193+
{
194+
std::string result;
195+
result.reserve(s.size());
196+
for (char const c : s)
197+
{
198+
result.push_back(toUpperCase(c));
199+
}
200+
return result;
201+
}
202+
203+
constexpr
204+
bool
205+
isDigit(char const c) noexcept
206+
{
207+
return c >= '0' && c <= '9';
208+
}
209+
210+
constexpr
211+
bool
212+
isDigit(std::string_view const s) noexcept
213+
{
214+
for (char const c : s)
215+
{
216+
if (!isDigit(c))
217+
{
218+
return false;
219+
}
220+
}
221+
return true;
222+
}
223+
224+
constexpr
225+
bool
226+
isAlphabetic(char const c) noexcept
227+
{
228+
return isLowerCase(c) || isUpperCase(c);
229+
}
230+
231+
constexpr
232+
bool
233+
isAlphabetic(std::string_view const s) noexcept
234+
{
235+
for (char const c : s)
236+
{
237+
if (!isAlphabetic(c))
238+
{
239+
return false;
240+
}
241+
}
242+
return true;
243+
}
244+
245+
constexpr
246+
bool
247+
isAlphaNumeric(char const c) noexcept
248+
{
249+
return isAlphabetic(c) || isDigit(c);
250+
}
251+
252+
constexpr
253+
bool
254+
isAlphaNumeric(std::string_view const s) noexcept
255+
{
256+
for (char const c : s)
257+
{
258+
if (!isAlphaNumeric(c))
259+
{
260+
return false;
261+
}
262+
}
263+
return true;
264+
}
265+
266+
constexpr
267+
std::string
268+
toKebabCase(std::string_view const input)
269+
{
270+
std::string result;
271+
size_t extraSizeCount = 0;
272+
for (std::size_t i = 1; i < input.size(); ++i) {
273+
if (isUpperCase(input[i])) {
274+
++extraSizeCount;
275+
}
276+
}
277+
result.reserve(input.size() + extraSizeCount);
278+
for (size_t i = 0; i < input.size(); ++i) {
279+
if (char const c = input[i];
280+
isUpperCase(c))
281+
{
282+
if (i != 0) {
283+
result.push_back('-');
284+
}
285+
result.push_back(toLowerCase(c));
286+
}
287+
else if (isLowerCase(c) || isDigit(c))
288+
{
289+
result.push_back(c);
290+
}
291+
else
292+
{
293+
result.push_back('-');
294+
}
295+
}
296+
return result;
297+
}
298+
299+
constexpr
300+
std::string
301+
toSnakeCase(std::string_view const input)
302+
{
303+
std::string result;
304+
size_t extraSizeCount = 0;
305+
for (std::size_t i = 1; i < input.size(); ++i) {
306+
if (isUpperCase(input[i]))
307+
{
308+
++extraSizeCount;
309+
}
310+
}
311+
result.reserve(input.size() + extraSizeCount);
312+
for (size_t i = 0; i < input.size(); ++i) {
313+
if (char const c = input[i];
314+
isUpperCase(c))
315+
{
316+
if (i != 0)
317+
{
318+
result.push_back('_');
319+
}
320+
result.push_back(toLowerCase(c));
321+
}
322+
else if (isLowerCase(c) || isDigit(c))
323+
{
324+
result.push_back(c);
325+
}
326+
else
327+
{
328+
result.push_back('_');
329+
}
330+
}
331+
return result;
332+
}
333+
334+
constexpr
335+
std::string
336+
toCamelCase(std::string_view const input)
337+
{
338+
std::string result;
339+
result.reserve(input.size());
340+
bool forceUppercaseNext = false;
341+
for (char const c : input)
342+
{
343+
if (isAlphaNumeric(c))
344+
{
345+
if (result.empty())
346+
{
347+
result.push_back(toLowerCase(c));
348+
forceUppercaseNext = false;
349+
}
350+
else if (forceUppercaseNext)
351+
{
352+
result.push_back(toUpperCase(c));
353+
forceUppercaseNext = false;
354+
}
355+
else
356+
{
357+
result.push_back(c);
358+
}
359+
}
360+
else
361+
{
362+
forceUppercaseNext = true;
363+
}
364+
}
365+
return result;
366+
}
367+
368+
constexpr
369+
std::string
370+
toPascalCase(std::string_view const input)
371+
{
372+
std::string result;
373+
result.reserve(input.size());
374+
bool forceUppercaseNext = true;
375+
for (char const c : input)
376+
{
377+
if (isAlphaNumeric(c))
378+
{
379+
if (forceUppercaseNext)
380+
{
381+
result.push_back(toUpperCase(c));
382+
forceUppercaseNext = false;
383+
}
384+
else
385+
{
386+
result.push_back(c);
387+
}
388+
}
389+
else
390+
{
391+
forceUppercaseNext = true;
392+
}
393+
}
394+
return result;
395+
}
396+
397+
121398
} // clang::mrdocs
122399

123400
#endif

0 commit comments

Comments
 (0)