Giter VIP home page Giter VIP logo

Comments (4)

aantron avatar aantron commented on July 26, 2024

As I understand you, _from_string_nocase works for strings with underscores exactly as in your enum constants, e.g. _from_string_nocase("Foo_bar") == YourEnum::Foo_bar, assuming you have a suitable definition of YourEnum.

Better Enums doesn't try to replaces spaces by underscores, convert camel case to underscores, or do anything else "fancy" like that – mostly because any choice it could make for one user would be wrong for another user. You may be able to write some kind of function that transforms your input strings before passing them to _from_string_nocase or _from_string, for example, by replacing spaces with underscores.

from better-enums.

aantron avatar aantron commented on July 26, 2024

If converting input strings to match your enum constants is not an option for you, you can go in the other direction. At startup, or perhaps using some kind of crazy metaprogramming, you could iterate over all the constants and convert the outputs of _to_string to strings you can then match your input against. You can store these in your favorite lookup data structure, for good performance, if your enum is large enough to worry about that.

from better-enums.

Zerophase avatar Zerophase commented on July 26, 2024

Yeah, I just need to convert "FooBar" to "FOO_BAR". Yeah, I could write function that would do the conversion at runtime fairly easily. I was just hoping there was a means of defining a custom string representation for an enum. Oh, wait I could just assign an int value at compile time based on a hashed value for the expected string.

from better-enums.

aantron avatar aantron commented on July 26, 2024

hoping there was a means of defining a custom string representation for an enum

There isn't at the moment, but you can get the bytes making up the string. You could then write a custom comparison function against them without having to allocate a temporary intermediate string. The interface could also be adjusted, but so far, I think it is possible to get what you want without an excessive amount of trouble, given that this is a sort of "custom" need.

from better-enums.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.