Skip to content

Instantly share code, notes, and snippets.

@develhox
Last active August 9, 2025 22:09
Show Gist options
  • Save develhox/abfe15c40f2d9daebc35 to your computer and use it in GitHub Desktop.
Save develhox/abfe15c40f2d9daebc35 to your computer and use it in GitHub Desktop.
Switch operand implementation for the strings
#ifndef __SWITCHS_H__
#define __SWITCHS_H__
#include <string.h>
#include <regex.h>
#include <stdbool.h>
/** Begin a switch for the string x */
#define switchs(x) \
{ const char *ss__sw = (x); bool ss__done = false; bool ss__cont = false; \
regex_t ss__regex; regcomp(&ss__regex, ".*", 0); do {
/** Check if the string matches the cases argument (case sensitive) */
#define cases(x) } if ( ss__cont || !strcmp ( ss__sw, x ) ) \
{ ss__done = true; ss__cont = true;
/** Check if the string matches the icases argument (case insensitive) */
#define icases(x) } if ( ss__cont || !strcasecmp ( ss__sw, x ) ) { \
ss__done = true; ss__cont = true;
/** Check if the string matches the specified regular expression using regcomp(3) */
#define cases_re(x,flags) } regfree ( &ss__regex ); if ( ss__cont || ( \
0 == regcomp ( &ss__regex, x, flags ) && \
0 == regexec ( &ss__regex, ss__sw, 0, NULL, 0 ) ) ) { \
ss__done = true; ss__cont = true;
/** Default behaviour */
#define defaults } if ( !ss__done || ss__cont ) {
/** Close the switchs */
#define switchs_end } while ( 0 ); regfree(&ss__regex); }
#endif // __SWITCHS_H__
#include <stdio.h>
#include "switchs.h"
int main(int argc, char **argv) {
switchs(argv[1]) {
cases("foo")
cases("bar")
printf("foo or bar (case sensitive)\n");
break;
icases("pi")
printf("pi or Pi or pI or PI (case insensitive)\n");
break;
cases_re("^D.*",0)
printf("Something that start with D (case sensitive)\n");
break;
cases_re("^E.*",REG_ICASE)
printf("Something that start with E (case insensitive)\n");
break;
cases("1")
printf("1\n");
cases("2")
printf("2\n");
break;
defaults
printf("No match\n");
break;
} switchs_end;
return 0;
}
@mralexgray
Copy link

LOVE this. just comment out the regex line for embedded systems without glibc, etc!

@hueychen27
Copy link

Is this public domain?

@develhox
Copy link
Author

develhox commented Aug 4, 2025

Is this public domain?

Sure. You're free to use it

@hueychen27
Copy link

@develhox In the macro, switchs(x), can you change char *ss__sw = (x); to const char *ss__sw = (x); to prevent compiler warnings when passing in something with const as a qualifier of x? It shouldn't matter if the user did not pass const-qualified string too.

@develhox
Copy link
Author

develhox commented Aug 5, 2025

@hueychen27 done! thanks for the advice

@hueychen27
Copy link

hueychen27 commented Aug 9, 2025

@develhox Just a little note: You should add a LICENSE file to make it clear we can reuse and distribute it under some terms (preferably MIT license). Don't forget license headers, which can be concise, in the beginning of each source file as a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment