From f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Tue, 29 Oct 2024 14:49:26 +0800 Subject: format * format * Minor test fixes * enable checking cpp format in ci --- .../compiler-core/slang-name-convention-util.cpp | 117 ++++++++++++--------- 1 file changed, 70 insertions(+), 47 deletions(-) (limited to 'source/compiler-core/slang-name-convention-util.cpp') diff --git a/source/compiler-core/slang-name-convention-util.cpp b/source/compiler-core/slang-name-convention-util.cpp index c3d08326c..cb44aa10e 100644 --- a/source/compiler-core/slang-name-convention-util.cpp +++ b/source/compiler-core/slang-name-convention-util.cpp @@ -7,14 +7,15 @@ namespace Slang { -/* static */NameConvention NameConventionUtil::inferConventionFromText(const UnownedStringSlice& slice) +/* static */ NameConvention NameConventionUtil::inferConventionFromText( + const UnownedStringSlice& slice) { // If no chars, or first char isn't alpha we don't know what it is if (slice.getLength() <= 0 || !CharUtil::isAlpha(slice[0])) { return NameConvention::Invalid; } - + typedef int Flags; struct Flag { @@ -33,9 +34,9 @@ namespace Slang { switch (c) { - case '-': flags |= Flag::Dash; break; - case '_': flags |= Flag::Underscore; break; - default: + case '-': flags |= Flag::Dash; break; + case '_': flags |= Flag::Underscore; break; + default: { if (CharUtil::isLower(c)) { @@ -61,62 +62,66 @@ namespace Slang // Use flags to determine what convention is used switch (flags) { - // We'll assume it's lower camel. - case Flag::Lower: return NameConvention::LowerCamel; - // We'll assume it's upper snake. It almost certainly isn't camel, and snake is more usual - // than kabab. - case Flag::Upper: return NameConvention::UpperSnake; - case Flag::Upper | Flag::Lower: + // We'll assume it's lower camel. + case Flag::Lower: return NameConvention::LowerCamel; + // We'll assume it's upper snake. It almost certainly isn't camel, and snake is more usual + // than kabab. + case Flag::Upper: return NameConvention::UpperSnake; + case Flag::Upper | Flag::Lower: { // Looks like camel, choose the right case based on first char - return CharUtil::isUpper(slice[0]) ? NameConvention::UpperCamel : NameConvention::LowerCamel; + return CharUtil::isUpper(slice[0]) ? NameConvention::UpperCamel + : NameConvention::LowerCamel; } - case Flag::Lower | Flag::Dash: return NameConvention::LowerKabab; - case Flag::Upper | Flag::Dash: return NameConvention::UpperKabab; - case Flag::Lower | Flag::Underscore: return NameConvention::LowerSnake; - case Flag::Upper | Flag::Underscore: return NameConvention::UpperSnake; - default: break; + case Flag::Lower | Flag::Dash: return NameConvention::LowerKabab; + case Flag::Upper | Flag::Dash: return NameConvention::UpperKabab; + case Flag::Lower | Flag::Underscore: return NameConvention::LowerSnake; + case Flag::Upper | Flag::Underscore: return NameConvention::UpperSnake; + default: break; } // Don't know what this style is return NameConvention::Invalid; } -/* static */NameStyle NameConventionUtil::inferStyleFromText(const UnownedStringSlice& slice) +/* static */ NameStyle NameConventionUtil::inferStyleFromText(const UnownedStringSlice& slice) { for (const char c : slice) { switch (c) { - case '-': return NameStyle::Kabab; - case '_': return NameStyle::Snake; - default: break; + case '-': return NameStyle::Kabab; + case '_': return NameStyle::Snake; + default: break; } } return NameStyle::Camel; } -/* static */void NameConventionUtil::split(NameStyle style, const UnownedStringSlice& slice, List& out) +/* static */ void NameConventionUtil::split( + NameStyle style, + const UnownedStringSlice& slice, + List& out) { switch (style) { - case NameStyle::Kabab: + case NameStyle::Kabab: { StringUtil::split(slice, '-', out); break; } - case NameStyle::Snake: + case NameStyle::Snake: { StringUtil::split(slice, '_', out); break; } - case NameStyle::Camel: + case NameStyle::Camel: { typedef CharUtil::Flags CharFlags; typedef CharUtil::Flag CharFlag; CharFlags prevFlags = 0; - const char*const end = slice.end(); + const char* const end = slice.end(); const char* start = slice.begin(); for (const char* cur = start; cur < end; ++cur) @@ -134,15 +139,18 @@ namespace Slang } else if ((prevFlags & CharFlag::Upper) && cur + 1 < end) { - // This works with capital or uncapitalized acronyms, but if we have two capitalized acronyms following each other - it can't split. - // - // For example + // This works with capital or uncapitalized acronyms, but if we have two + // capitalized acronyms following each other - it can't split. + // + // For example // "IAABBSystem" -> "IAABB", "System" - // - // If it only accepted lower case acronyms the logic could be changed such that the following could be produced - // "IAabbSystem" -> "I", "Aabb", "System" // - // Since Slang source largely goes with upper case acronyms, we work with the heuristic here.. + // If it only accepted lower case acronyms the logic could be changed such + // that the following could be produced "IAabbSystem" -> "I", "Aabb", + // "System" + // + // Since Slang source largely goes with upper case acronyms, we work with + // the heuristic here.. if (CharUtil::isLower(cur[1])) { @@ -151,7 +159,7 @@ namespace Slang } } } - + prevFlags = flags; } @@ -162,7 +170,7 @@ namespace Slang } break; } - case NameStyle::Unknown: + case NameStyle::Unknown: { out.add(slice); break; @@ -175,7 +183,12 @@ void NameConventionUtil::split(const UnownedStringSlice& slice, List 0) { @@ -226,15 +239,19 @@ void NameConventionUtil::split(const UnownedStringSlice& slice, List slices; @@ -286,10 +307,12 @@ void NameConventionUtil::split(const UnownedStringSlice& slice, List