Thursday, February 27, 2025

 Switch case on strings!


Interesting solution:    https://stackoverflow.com/a/51816519/112687 


#ifndef __SSWITCH_H__

#define __SSWITCH_H__

#include <string.h>

    #ifndef SWITCH_CASE_INIT

    #define SWITCH_CASE_INIT

    #define SWITCH(X)  for(char* __switch_p__=X,__switch_next__=1;__switch_p__;__switch_p__=0,__switch_next__=1) {{

    #define CASE(X)    } if (!__switch_next__ || !(__switch_next__ = strcmp(__switch_p__, X))) {

    #define DEFAULT    } {

    #define END        }}

    #endif

#endif


No comments: