65 lines
1.7 KiB
C
65 lines
1.7 KiB
C
|
|
#include "libft.h"
|
|
|
|
/**
|
|
* Parses a string of superscript digits (e.g., "²³⁴") into a long long.
|
|
* Returns the integer value, or LLONG_MAX/LLONG_MIN on overflow.
|
|
*/
|
|
long long ft_atoll_superscript(const char *str)
|
|
{
|
|
long long nbr = 0;
|
|
int i = 0;
|
|
int negatif = 1;
|
|
int superscript_size;
|
|
|
|
// Skip leading whitespace
|
|
while (ft_isspace(str[i]))
|
|
i++;
|
|
|
|
// Handle optional sign
|
|
if (str[i] == '-')
|
|
negatif = -1;
|
|
if (str[i] == '+' || str[i] == '-')
|
|
i++;
|
|
|
|
// Parse superscript digits
|
|
while (1)
|
|
{
|
|
if (!ft_isdigit_superscript(&str[i], &superscript_size))
|
|
break; // Not a superscript digit
|
|
|
|
// Extract the digit value (0-9) from the superscript character
|
|
int digit;
|
|
if (superscript_size == 2)
|
|
{
|
|
// ² (U+00B2) or ³ (U+00B3)
|
|
if ((uint8_t)str[i + 1] == 0xB2)
|
|
digit = 2;
|
|
else if ((uint8_t)str[i + 1] == 0xB3)
|
|
digit = 3;
|
|
else
|
|
break; // Invalid, should not happen
|
|
}
|
|
else if (superscript_size == 3)
|
|
{
|
|
// ⁰ (U+2070) to ⁹ (U+2079)
|
|
digit = (uint8_t)str[i + 2] - 0xB0; // 0xB0 → 0, 0xB1 → 1, ..., 0xB9 → 9
|
|
}
|
|
else
|
|
{
|
|
break; // Invalid, should not happen
|
|
}
|
|
|
|
// Check for overflow
|
|
if (nbr > LLONG_MAX / 10 ||
|
|
(nbr == LLONG_MAX / 10 && digit > LLONG_MAX % 10))
|
|
{
|
|
return (negatif > 0) ? LLONG_MAX : LLONG_MIN;
|
|
}
|
|
|
|
nbr = nbr * 10 + digit;
|
|
i += superscript_size; // Skip the entire UTF-8 character
|
|
}
|
|
|
|
return (nbr * negatif);
|
|
} |