Created
March 29, 2018 19:18
-
-
Save alfwatt/279b694f702d0d6910cd259ce427dfc3 to your computer and use it in GitHub Desktop.
scountf(char*) - counts the number of replacement strings in a format string, sorta
This file contains 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
static int | |
scountf(const char * format) | |
{ | |
int count = 0; | |
int index = 0; | |
char this, next; | |
while ((this = format[index])) { | |
next = format[index++]; | |
if (this == '%' && next != '%') { // skip %% | |
count++; | |
} | |
else if (next == '*') { // surrender | |
return -1; | |
} | |
} | |
return count; | |
} | |
/* Alf Watt - [email protected] - assigned to the public domain */ |
If you're using the GNU C library, you can checkout my answer on SO: https://stackoverflow.com/a/66624722/11293716
Or the manual page directly for a built-in solution: https://www.gnu.org/software/libc/manual/html_node/Parsing-a-Template-String.html
And please don't use this gist in production, if you don't want to surrender (;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
80% of the time it works 100% of the time