strcase(3)

πŸ’… STRCASE β€” okay bestie, let's talk about strcase(3)

So this is the manual for strcase, and it's giving multiway branch (switch) for short strings energy. We're in section 3, we're in the Linux Programmer's Manual, and we are obsessed. Let's get into it. πŸ¦‹


✨ NAME ✨

STRCASE, strcase, strcase_tolower β€” multiway branch (switch) for short strings. That's it, that's the whole vibe. πŸ”₯


🌸 SYNOPSIS 🌸

#include <strcase.h>
STRCASE(...)
static inline uint64_t strcase(const char *s);
static inline uint64_t strcase_tolower(const char *s);

πŸ’– DESCRIPTION πŸ’–

Okay so listen. strcase.h hands you two inline functions β€” strcase and strcase_tolower β€” plus a macro called STRCASE, and all three of them convert a string into a uint64_t integer value. The catch? Only the first 8 characters are significative. Eight. That's the whole budget. Periodt. πŸ’€

strcase_tolower works exactly like strcase, except it converts uppercase letters as if they were lowercase. So "YES" and "yes"? Same energy, same integer. Slay.

strcase or strcase_tolower can be dropped right into the expression of a switch statement to convert a string to an integer type, and STRCASE generates, at compile time, the integer constants for the case stanzas of that switch. It's giving "strings but make it integers". ✨

Now, actually, STRCASE (the C language version) has some ✨quirks✨:

  • it requires the string to be specified as comma separated characters,
  • it 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 (like in the example below, bestie, keep scrolling πŸ‘‡). Although STRCASE needs commas between characters, the string is still readable, and it is simple to add cases or change the tags. Lowkey iconic.

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'). One char, one code, no drama. 🌈

Strcase is a portable alternative to multi-character constants in C. Not me being the responsible, portable choice. 🫢

Strcase is available for C++ users, too β€” no one is left out of the group chat. In C++, the STRCASE argument 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;
}

Two languages, one vibe. Ate. πŸ’…


πŸ™ˆ BUGS πŸ™ˆ

Found a bug? Don't suffer in silence, bestie. Bug reports should be addressed to <info@virtualsquare.org>. Spill the tea, they want to hear it. 😭


πŸ‘‘ AUTHORS πŸ‘‘

Renzo Davoli <renzo@cs.unibo.it> β€” the main character who wrote this. We stan. 🌟


Okay and that's a wrap on strcase(3) from the VirtualSquare crew, dated 2019-01-11. Thank you for coming to my TED talk, go write some slay switches, and remember: only the first 8 characters count, so make them count. Byeee πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from