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

View File

@@ -0,0 +1,65 @@
#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);
}