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
- debian bookworm: strcase-dev 0.1-3 Β· LGPL-2.1-or-later
- debian trixie: strcase-dev 0.1-3 Β· LGPL-2.1-or-later
- ubuntu jammy: strcase-dev 0.1-3 Β· LGPL-2.1-or-later
- ubuntu noble: strcase-dev 0.1-3 Β· LGPL-2.1-or-later
- ubuntu resolute: strcase-dev 0.1-3build1 Β· LGPL-2.1-or-later
- ubuntu stonking: strcase-dev 0.1-3build1 Β· LGPL-2.1-or-later
- Copyright (C) 2019 Renzo Davoli. University of Bologna. <renzo@cs.unibo.it>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms