Last active
August 19, 2026 08:32
-
-
Save develhox/abfe15c40f2d9daebc35 to your computer and use it in GitHub Desktop.
Switch operand implementation for the strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright 2015 Andrea Carron | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| #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__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright 2015 Andrea Carron | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| #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; | |
| } |
Is this public domain?
Author
Is this public domain?
Sure. You're free to use it
@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.
Author
@hueychen27 done! thanks for the advice
@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
LOVE this. just comment out the regex line for embedded systems without
glibc, etc!