logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

STRCASE, strcase, strcase_tolower - Multiway branch (switch) for short strings

Authors

       Renzo Davoli <renzo@cs.unibo.it>

VirtualSquare                                      2019-01-11                                         strcase(3)

Bugs

       Bug reports should be addressed to <info@virtualsquare.org>

Description

strcase.h  provides  two  inline  functions  strcase,  strcase_tolower   and a macro STRCASE, all of them
       convert a string in a uint64_t integer value. Only the first8characters are significative.

       strcase_tolower works like strcase but it converts uppercase letters as if they were lowercase.

       strcase or strcase_tolower can be used in the expression of a switch statement to convert a string to  an
       integer type, STRCASE generates at compile time the integer constants for the case stanzas of the switch.

       Actually STRCASE (C language):

       *  requires the string to be specified as comma separated characters,

       *  supports  "strings"  using  only literals, digits and underscore. Other symbols can appear using their
          name, as in:
          STRCASE(slash,e,t,c)

       Regardless of its limitations this macro library is quite useful to write switch statements in C language
       using strings (almost) as if it were integers (as in the example below).  Although STRCASE  needs  commas
       between characters the string is still readable, and it is simple to add cases or change the tags.

       When the string contains only one char, the value of strcase is the code of the character (e.g. the value
       of strcase("a") as well as the value of STRCASE(a) is 'a')

       Strcase is a portable alternative to multi-character constants in C.

       Strcase is available for C++ users, too. STRCASE argument in C++ is a short string.

Example

       The following C program shows the use of strcase:

            #include <stdio.h>
            #include <strcase.h>

            int yes_or_not(const char *s) {
              switch (strcase_tolower(s)) {
                case STRCASE(y,e,s):
                case 'y':
                    return 1;
                case STRCASE(n,o):
                case 'n':
                    return 0;
                default:
                    return -1;
              }
            }

            int main(int argc,char *argv[]) {
              for(argc--, argv++; argc > 0; argc--, argv++)
                printf("%s %d\n", *argv, yes_or_not(*argv));
            }

       The same example can be translated in C++ as follows:

            #include <iostream>
            #include <strcase.h>

            using namespace std;

            int yes_or_not(const char *s) {
              switch (strcase_tolower(s)) {
                case STRCASE("yes"):
                case 'y':
                    return 1;
                case STRCASE("no"):
                case 'n':
                    return 0;
                default:
                    return -1;
              }
            }

            int main(int argc,char *argv[]) {
              for(argc--, argv++; argc > 0; argc--, argv++)
                cout << *argv << " " << yes_or_not(*argv) << endl;
            }

Name

       STRCASE, strcase, strcase_tolower - Multiway branch (switch) for short strings

Synopsis

#include<strcase.h>STRCASE(...)staticinlineuint64_tstrcase(constchar*s);staticinlineuint64_tstrcase_tolower(constchar*s);

See Also