Skip to content

Commit

Permalink
Merge pull request #556 from fredrikhr/strip-enum-member-type-name
Browse files Browse the repository at this point in the history
Refactor Enum member type escaping
  • Loading branch information
tannergooding authored May 24, 2024
2 parents 7e48890 + f3237de commit 549da90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)

var accessSpecifier = AccessSpecifier.None;
var name = GetRemappedCursorName(enumConstantDecl);
var escapedName = EscapeName(name);
var typeName = GetTargetTypeName(enumConstantDecl, out _);
var isAnonymousEnum = false;
var parentName = "";
Expand All @@ -295,10 +294,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)
parentName = _outputBuilder.Name;
}

if (Config.StripEnumMemberTypeName)
{
escapedName = PrefixAndStrip(escapedName, parentName, trimChar: '_');
}
var escapedName = EscapeAndStripEnumMemberName(name, parentName);

var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator;
var flags = ValueFlags.Constant;
Expand Down
14 changes: 14 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,20 @@ private string EscapeAndStripMethodName(string name)
return EscapeName(name);
}

private string EscapeAndStripEnumMemberName(string name, string enumTypeName)
{
if (Config.StripEnumMemberTypeName)
{
var escapedName = PrefixAndStrip(name, enumTypeName, trimChar: '_');
if (escapedName.Length > 0 && char.IsAsciiDigit(escapedName[0]))
{
escapedName = '_' + escapedName;
}
return escapedName;
}
return EscapeName(name);
}

internal static string EscapeCharacter(char value) => value switch {
'\0' => @"\0",
'\\' => @"\\",
Expand Down

0 comments on commit 549da90

Please sign in to comment.