adding atoi_superscript and isdigit_superscript

This commit is contained in:
hugogogo
2026-05-06 20:02:44 +02:00
parent b64ede50af
commit f93c635234
10 changed files with 269 additions and 82 deletions

39
srcs/ft_atoll.c Normal file
View File

@@ -0,0 +1,39 @@
#include "libft.h"
long long ft_atoll(const char *str)
{
long long nbr;
int i;
int sign;
i = 0;
sign = 1;
nbr = 0;
// skip leading whitespace
while (ft_isspace(str[i]))
i++;
// handle optional sign
if (str[i] == '-')
sign = -1;
if (str[i] == '+' || str[i] == '-')
i++;
// parse digits with overflow check for `long long`
while (str[i] >= '0' && str[i] <= '9')
{
// check if multiplying by 10 would overflow
if (nbr > LLONG_MAX / 10)
return (sign > 0) ? LLONG_MAX : LLONG_MIN;
// check if adding the next digit would overflow
if (nbr == LLONG_MAX / 10 && (str[i] - '0') > LLONG_MAX % 10)
return (sign > 0) ? LLONG_MAX : LLONG_MIN;
nbr = nbr * 10 + (str[i] - '0');
i++;
}
return (nbr * sign);
}