ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

Enum key stripping

Open dovdiienko-el3 opened this issue 2 years ago • 3 comments

Could you add an option to strip the prefix from the enumeration keys. For example given following C code:

enum abc_some_enum {
  abc_some_enum_key1,
  abc_some_enum_key2,
  abc_some_enum_key3,
};

The expected C# output is following:

enum abc_some_enum {
  key1,
  key2,
  key3,
};

My best guess is that generator should cut maximum common prefix from the enum key name.

dovdiienko-el3 avatar Jul 04 '23 18:07 dovdiienko-el3

This can be achieved via remapping today.

A new option to explicitly trim out some prefixes would also be possible, but there are many cases this won't work. This includes where the prefix is non-obvious, where it is instead a common postfix, where the first letter after the prefix is a number, etc.

I'll leave this open and up-for-grabs in case someone wants to do it, but I expect it would need to look like: -config prefix-strip-enum

Alternatively, or in addition, the existing --prefixStrip could be expanded to support specifying a common prefix to remove per enum.

tannergooding avatar Nov 18 '23 17:11 tannergooding

In many cases the prefix is the enum member type name. So, in order to keep it simple, I suggest --config strip-enum-type-from-member-name which would simply strip the enum member type name (and following underscore) from the enum member name. That would satisfy the above example.

I am working on this now, will submit PR soon.

fredrikhr avatar Feb 06 '24 16:02 fredrikhr

It's worth noting one of the "best" ways to consume these types is simply to do using static abc_some_enum; at which point you can just use abc_some_enum_key1 as is, unqualified.

This is ultimately very consistent with C/C++ (which can help with porting) and fits in if C# ever gets the proposed feature to allow contextual access to enum members without having to qualify the type name.

tannergooding avatar Feb 06 '24 18:02 tannergooding