Conversation
Notices
-
32784 (saxophone3784@clubcyberia.co)'s status on Monday, 08-Jul-2024 06:18:32 JST 32784 @crunklord420 I want fastest and most autistic way in C to parse through CL arguments to find all numbers. Is this correct?
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]){
int k;
for (argc; argc>1; argc--) {
k = 0;
while (*(argv[argc-1]+k)) {
if (!isdigit(*(argv[argc-1]+k))) {
break;
}
k++;
}
if (!*(argv[argc-1]+k)) printf("%.5f\n",atof(argv[argc-1]));
}
}-
CrunkLord420 (crunklord420@clubcyberia.co)'s status on Monday, 08-Jul-2024 06:18:32 JST CrunkLord420 @Saxophone3784 assuming it's a non-negative, non-floating number. Also it depends on the exact implementation of isdigit in the stdlib which might include localization insanity.
I'm pretty sure all the string to number functions handle trash data. They just return 0 or something. -
CrunkLord420 (crunklord420@clubcyberia.co)'s status on Monday, 08-Jul-2024 07:00:45 JST CrunkLord420 @Saxophone3784 if you want robust, use a library that will do variable type checking for you. If you want fast just do the least amount of error checking possible. You don't want to implement a million edge cases and unicode and whatever. -
32784 (saxophone3784@clubcyberia.co)'s status on Monday, 08-Jul-2024 07:00:46 JST 32784 @crunklord420 yeah I put it in if to check for the specific chars to cover the negative and float cases.
atof would return 0 if it is trash, but how do i know if it is trash or number 0 in input?
Is there a better way to do this?
I am trying to do is implement a function that gets a set of numbers from which 1 number is missing. Say input is "50 a 100". Depending on which position the number is missing(2nd in this case), diff things are supposed to happen. For that i first need detection. -
CrunkLord420 (crunklord420@clubcyberia.co)'s status on Monday, 08-Jul-2024 07:02:31 JST CrunkLord420 @Saxophone3784 https://github.com/cofyc/argparse/blob/master/argparse.c#L50
-