CamelCase conversion to friendly name, i.e. Enum constants; Problems?

前端 未结 3 1864
小蘑菇
小蘑菇 2021-01-13 03:48

In my answer to this question, I mentioned that we used UpperCamelCase parsing to get a description of an enum constant not decorated with a Description attribute, but it wa

相关标签:
3条回答
  • 2021-01-13 04:04

    So while I agree with Hans Passant here, I have to say that I had to try my hand at making it one regex as an armchair regex user.

    (?<a>(?<!^)((?:[A-Z][a-z])|(?:(?<!^[A-Z]+)[A-Z0-9]+(?:(?=[A-Z][a-z])|$))|(?:[0-9]+)))
    

    Is what I came up with. It seems to pass all the tests you put forward in the question.

    So

    var result = Regex.Replace(camelCasedString, @"(?<a>(?<!^)((?:[A-Z][a-z])|(?:(?<!^[A-Z]+)[A-Z0-9]+(?:(?=[A-Z][a-z])|$))|(?:[0-9]+)))", @" ${a}");
    

    Does it in one pass.

    0 讨论(0)
  • 2021-01-13 04:04

    Let's say every case you come across works with this (you're asking us for examples that won't and then giving us some, so you don't even have a question left).

    This still binds UI to programmatic identifiers in a way that will make both programming and UI changes brittle.

    It still assumes your program will only be used in one language. Either your potential market it so small that just indexing an array of names would be scalable enough (e.g. a one-client bespoke or in-house project), or you are assuming you will never be successful enough to need to be available to other languages or other dialects of your first-chosen language.

    Does "well, it'll work as long as we're a failure" sound like a passing grade in balancing designs?

    Either code it to use resources, or else code it to pass the enum name blindly or use an array of names, as that at least will be modifiable afterwards.

    0 讨论(0)
  • 2021-01-13 04:17

    not that this directly answers the question, but why not test by taking the standard C# API and converting each class into a friendly name? It'd take some manual verification, but it'd give you a good list of standard names to test.

    0 讨论(0)
提交回复
热议问题