Compare commits
16 Commits
b64ede50af
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ac98cb1f5 | ||
|
|
71806cb923 | ||
|
|
e95f55c07f | ||
|
|
85423f4211 | ||
|
|
f662d7f750 | ||
|
|
de28530259 | ||
|
|
60c72d7560 | ||
|
|
ae9787ba6d | ||
|
|
8edf428fed | ||
|
|
3cf0b6ecb9 | ||
|
|
aa35d294f2 | ||
|
|
413e149003 | ||
|
|
36379f56b0 | ||
|
|
b768ac1a14 | ||
|
|
78f6a2d9ba | ||
|
|
f93c635234 |
17
Makefile
17
Makefile
@@ -12,7 +12,7 @@ _DEP = libft.h
|
|||||||
DEPS = $(_DEP:%.h=$(IDIR)/%.h)
|
DEPS = $(_DEP:%.h=$(IDIR)/%.h)
|
||||||
|
|
||||||
CFLAGS = -I$(IDIR)
|
CFLAGS = -I$(IDIR)
|
||||||
CFLAGS += -Wall -Wextra -Werror -g3
|
CFLAGS += -Wall -Wextra -Werror -g
|
||||||
|
|
||||||
SRCS = ft_memset.c \
|
SRCS = ft_memset.c \
|
||||||
ft_bzero.c \
|
ft_bzero.c \
|
||||||
@@ -27,6 +27,7 @@ SRCS = ft_memset.c \
|
|||||||
\
|
\
|
||||||
ft_isalpha.c \
|
ft_isalpha.c \
|
||||||
ft_isdigit.c \
|
ft_isdigit.c \
|
||||||
|
ft_isdigit_superscript.c \
|
||||||
ft_isalnum.c \
|
ft_isalnum.c \
|
||||||
ft_isnumber.c \
|
ft_isnumber.c \
|
||||||
ft_isascii.c \
|
ft_isascii.c \
|
||||||
@@ -36,6 +37,7 @@ SRCS = ft_memset.c \
|
|||||||
ft_tolower.c \
|
ft_tolower.c \
|
||||||
\
|
\
|
||||||
ft_strlen.c \
|
ft_strlen.c \
|
||||||
|
ft_nbrlen.c \
|
||||||
ft_strchr.c \
|
ft_strchr.c \
|
||||||
ft_strrchr.c \
|
ft_strrchr.c \
|
||||||
ft_strchrset.c \
|
ft_strchrset.c \
|
||||||
@@ -67,9 +69,14 @@ SRCS = ft_memset.c \
|
|||||||
ft_split.c \
|
ft_split.c \
|
||||||
\
|
\
|
||||||
ft_atoi.c \
|
ft_atoi.c \
|
||||||
|
ft_atoi_superscript.c \
|
||||||
ft_atol.c \
|
ft_atol.c \
|
||||||
|
ft_atol_superscript.c \
|
||||||
|
ft_atoll.c \
|
||||||
|
ft_atoll_superscript.c \
|
||||||
ft_atof.c \
|
ft_atof.c \
|
||||||
ft_itoa.c \
|
ft_itoa.c \
|
||||||
|
ft_itoa_static.c \
|
||||||
ft_utoa.c \
|
ft_utoa.c \
|
||||||
ft_atoibase.c \
|
ft_atoibase.c \
|
||||||
\
|
\
|
||||||
@@ -115,13 +122,19 @@ SRCS = ft_memset.c \
|
|||||||
pf_utils.c \
|
pf_utils.c \
|
||||||
\
|
\
|
||||||
ft_abs.c \
|
ft_abs.c \
|
||||||
|
ft_labs.c \
|
||||||
|
ft_fabs.c \
|
||||||
ft_greater.c \
|
ft_greater.c \
|
||||||
ft_smaller.c \
|
ft_smaller.c \
|
||||||
ft_sign.c \
|
ft_sign.c \
|
||||||
|
ft_fsign.c \
|
||||||
|
ft_pow.c \
|
||||||
ft_sqrt.c \
|
ft_sqrt.c \
|
||||||
|
ft_round.c \
|
||||||
ft_free_tab.c \
|
ft_free_tab.c \
|
||||||
\
|
\
|
||||||
ft_arrint.c
|
ft_arrint.c \
|
||||||
|
ft_superscript.c
|
||||||
|
|
||||||
|
|
||||||
ODIR = ./builds
|
ODIR = ./builds
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#define LIBFT_H
|
#define LIBFT_H
|
||||||
#include <unistd.h> // for write
|
#include <unistd.h> // for write
|
||||||
#include <stdlib.h> // for malloc and free
|
#include <stdlib.h> // for malloc and free
|
||||||
|
#include <stdint.h> // for uint8_t for utf8 characters
|
||||||
|
#include <limits.h> // for INT_MAX, LONG_MAX, etc
|
||||||
#include "ft_gnl.h"
|
#include "ft_gnl.h"
|
||||||
#include "ft_printf.h"
|
#include "ft_printf.h"
|
||||||
|
|
||||||
@@ -25,8 +27,10 @@ void *ft_memmove(void *dst, const void *src, size_t len);
|
|||||||
void *ft_memchr(const void *s, int c, size_t n);
|
void *ft_memchr(const void *s, int c, size_t n);
|
||||||
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
||||||
size_t ft_strlen(const char *str);
|
size_t ft_strlen(const char *str);
|
||||||
|
size_t ft_nbrlen(int nbr);
|
||||||
int ft_isalpha(int c);
|
int ft_isalpha(int c);
|
||||||
int ft_isdigit(int c);
|
int ft_isdigit(int c);
|
||||||
|
int ft_isdigit_superscript(const char *input, int *size);
|
||||||
int ft_isnumber(char *nb);
|
int ft_isnumber(char *nb);
|
||||||
int ft_isalnum(int c);
|
int ft_isalnum(int c);
|
||||||
int ft_isascii(int c);
|
int ft_isascii(int c);
|
||||||
@@ -42,7 +46,11 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
|||||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||||
char *ft_strnstr(const char *b, const char *l, size_t s);
|
char *ft_strnstr(const char *b, const char *l, size_t s);
|
||||||
int ft_atoi(const char *str);
|
int ft_atoi(const char *str);
|
||||||
|
int ft_atoi_superscript(const char *str);
|
||||||
long ft_atol(const char *str);
|
long ft_atol(const char *str);
|
||||||
|
long ft_atol_superscript(const char *str);
|
||||||
|
long long ft_atoll(const char *str);
|
||||||
|
long long ft_atoll_superscript(const char *str);
|
||||||
double ft_atof(const char *str);
|
double ft_atof(const char *str);
|
||||||
void *ft_calloc(size_t count, size_t size);
|
void *ft_calloc(size_t count, size_t size);
|
||||||
char *ft_strdup(const char *s1);
|
char *ft_strdup(const char *s1);
|
||||||
@@ -53,6 +61,7 @@ size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n
|
|||||||
char *ft_strtrim(char const *s1, char const *set);
|
char *ft_strtrim(char const *s1, char const *set);
|
||||||
char **ft_split(char const *s, char c);
|
char **ft_split(char const *s, char c);
|
||||||
char *ft_itoa(long int n);
|
char *ft_itoa(long int n);
|
||||||
|
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len);
|
||||||
char *ft_utoa(unsigned long int n);
|
char *ft_utoa(unsigned long int n);
|
||||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||||
|
|
||||||
@@ -65,9 +74,6 @@ void ft_putstr_fd(const char *s, int fd);
|
|||||||
void ft_putnbr_fd(int n, int fd);
|
void ft_putnbr_fd(int n, int fd);
|
||||||
void ft_putnbrbase_fd(int nbr, const char *base, int fd);
|
void ft_putnbrbase_fd(int nbr, const char *base, int fd);
|
||||||
|
|
||||||
// void ft_putendl(char const *str);
|
|
||||||
// void ft_putendl_fd(char *s, int fd);
|
|
||||||
|
|
||||||
typedef struct s_list
|
typedef struct s_list
|
||||||
{
|
{
|
||||||
void *content;
|
void *content;
|
||||||
@@ -75,19 +81,6 @@ typedef struct s_list
|
|||||||
struct s_list *next;
|
struct s_list *next;
|
||||||
} t_list;
|
} t_list;
|
||||||
|
|
||||||
// t_list *ft_lstnew(void *content);
|
|
||||||
// void ft_lstadd_front(t_list **alst, t_list *n);
|
|
||||||
// void *ft_lstadd_back(t_list **alst, t_list *n);
|
|
||||||
// int ft_lstsize(t_list *lst);
|
|
||||||
// t_list *ft_lstlast(t_list *lst);
|
|
||||||
// void ft_lstdelone(t_list *lst, void (*del)(void *));
|
|
||||||
// void ft_lstclear(t_list **lst, void (*del)(void *));
|
|
||||||
// void ft_lstiter(t_list *lst, void (*f)(void *));
|
|
||||||
// t_list *ft_lstmap(t_list *l, void *(*f)(void *), void (*d)(void *));
|
|
||||||
|
|
||||||
// void ft_lstremove(t_list *lst, void (*del)(void *));
|
|
||||||
// t_list *ft_lstfind(t_list *lst, void *to_find, int (*comp)(void *, void *));
|
|
||||||
|
|
||||||
t_list *ft_lstcreate(void *content);
|
t_list *ft_lstcreate(void *content);
|
||||||
void *ft_lstpush_back(t_list **lst, t_list *new);
|
void *ft_lstpush_back(t_list **lst, t_list *new);
|
||||||
void ft_lstpush_front(t_list **alst, t_list *new);
|
void ft_lstpush_front(t_list **alst, t_list *new);
|
||||||
@@ -131,12 +124,18 @@ void ft_putnbrendl(int n);
|
|||||||
void ft_putnbrendl_fd(int n, int fd);
|
void ft_putnbrendl_fd(int n, int fd);
|
||||||
char *ft_concat_free(char *str1, char *str2);
|
char *ft_concat_free(char *str1, char *str2);
|
||||||
int ft_abs(int n);
|
int ft_abs(int n);
|
||||||
|
long int ft_labs(long int n);
|
||||||
|
double ft_fabs(double n);
|
||||||
int ft_greater(int a, int b);
|
int ft_greater(int a, int b);
|
||||||
int ft_smaller(int a, int b);
|
int ft_smaller(int a, int b);
|
||||||
int ft_sign(int i);
|
int ft_sign(int i);
|
||||||
int ft_sqrt(int i);
|
int ft_fsign(double i);
|
||||||
|
double ft_pow(double base, int exponent);
|
||||||
|
double ft_round(double x);
|
||||||
|
double ft_sqrt(double i, double precision);
|
||||||
void ft_free_tab(char **tab);
|
void ft_free_tab(char **tab);
|
||||||
|
|
||||||
int ft_arrint(int *intarr, int comp, size_t size);
|
int ft_arrint(int *intarr, int comp, size_t size);
|
||||||
|
const char *ft_superscript(char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_abs(int n)
|
/**
|
||||||
|
* returns the absolute value of an int number
|
||||||
|
*/
|
||||||
|
|
||||||
|
int ft_abs(int n)
|
||||||
{
|
{
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
n *= -1;
|
n *= -1;
|
||||||
|
else if (n == 0)
|
||||||
|
n = 0; // for -0
|
||||||
return (n);
|
return (n);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,27 +14,13 @@
|
|||||||
|
|
||||||
int ft_atoi(const char *str)
|
int ft_atoi(const char *str)
|
||||||
{
|
{
|
||||||
long nbr;
|
long long result = ft_atoll(str);
|
||||||
int i;
|
|
||||||
int negatif;
|
|
||||||
|
|
||||||
i = 0;
|
// clamp to int range
|
||||||
negatif = 1;
|
if (result > INT_MAX)
|
||||||
nbr = 0;
|
return INT_MAX;
|
||||||
|
if (result < INT_MIN)
|
||||||
|
return INT_MIN;
|
||||||
|
|
||||||
// Skip leading whitespace
|
return (int)result;
|
||||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
|
||||||
i++;
|
|
||||||
|
|
||||||
// Handle optional sign
|
|
||||||
if (str[i] == '-')
|
|
||||||
negatif = -1;
|
|
||||||
if (str[i] == '+' || str[i] == '-')
|
|
||||||
i++;
|
|
||||||
|
|
||||||
// parse integer
|
|
||||||
while (str[i] >= '0' && str[i] <= '9')
|
|
||||||
nbr = nbr * 10 + (str[i++] - '0');
|
|
||||||
|
|
||||||
return (nbr * negatif);
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
srcs/ft_atoi_superscript.c
Normal file
16
srcs/ft_atoi_superscript.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a string of superscript digits into an int.
|
||||||
|
* Returns the integer value, or INT_MAX/INT_MIN on overflow.
|
||||||
|
*/
|
||||||
|
int ft_atoi_superscript(const char *str)
|
||||||
|
{
|
||||||
|
long long result = ft_atoll_superscript(str);
|
||||||
|
if (result > INT_MAX)
|
||||||
|
return INT_MAX;
|
||||||
|
if (result < INT_MIN)
|
||||||
|
return INT_MIN;
|
||||||
|
return (int)result;
|
||||||
|
}
|
||||||
@@ -3,27 +3,13 @@
|
|||||||
|
|
||||||
long ft_atol(const char *str)
|
long ft_atol(const char *str)
|
||||||
{
|
{
|
||||||
long long nbr;
|
long long result = ft_atoll(str);
|
||||||
int i;
|
|
||||||
int negatif;
|
|
||||||
|
|
||||||
i = 0;
|
// clamp to long range
|
||||||
negatif = 1;
|
if (result > LONG_MAX)
|
||||||
nbr = 0;
|
return LONG_MAX;
|
||||||
|
if (result < LONG_MIN)
|
||||||
|
return LONG_MIN;
|
||||||
|
|
||||||
// Skip leading whitespace
|
return (long)result;
|
||||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
|
||||||
i++;
|
|
||||||
|
|
||||||
// Handle optional sign
|
|
||||||
if (str[i] == '-')
|
|
||||||
negatif = -1;
|
|
||||||
if (str[i] == '+' || str[i] == '-')
|
|
||||||
i++;
|
|
||||||
|
|
||||||
// parse integer
|
|
||||||
while (str[i] >= '0' && str[i] <= '9')
|
|
||||||
nbr = nbr * 10 + (str[i++] - '0');
|
|
||||||
|
|
||||||
return (nbr * negatif);
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
srcs/ft_atol_superscript.c
Normal file
16
srcs/ft_atol_superscript.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a string of superscript digits into a long.
|
||||||
|
* Returns the integer value, or LONG_MAX/LONG_MIN on overflow.
|
||||||
|
*/
|
||||||
|
long ft_atol_superscript(const char *str)
|
||||||
|
{
|
||||||
|
long long result = ft_atoll_superscript(str);
|
||||||
|
if (result > LONG_MAX)
|
||||||
|
return LONG_MAX;
|
||||||
|
if (result < LONG_MIN)
|
||||||
|
return LONG_MIN;
|
||||||
|
return (long)result;
|
||||||
|
}
|
||||||
39
srcs/ft_atoll.c
Normal file
39
srcs/ft_atoll.c
Normal 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);
|
||||||
|
}
|
||||||
67
srcs/ft_atoll_superscript.c
Normal file
67
srcs/ft_atoll_superscript.c
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
|
||||||
|
#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+00B9), ² (U+00B2), or ³ (U+00B3)
|
||||||
|
if ((uint8_t)str[i + 1] == 0xB9)
|
||||||
|
digit = 1;
|
||||||
|
else 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);
|
||||||
|
}
|
||||||
14
srcs/ft_fabs.c
Normal file
14
srcs/ft_fabs.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the absolute value of a double number
|
||||||
|
*/
|
||||||
|
|
||||||
|
double ft_fabs(double n)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
n *= -1;
|
||||||
|
else if (n == 0)
|
||||||
|
n = 0; // for -0
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
10
srcs/ft_fsign.c
Normal file
10
srcs/ft_fsign.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_fsign(double i)
|
||||||
|
{
|
||||||
|
if (i < 0)
|
||||||
|
return (-1);
|
||||||
|
else if (i == 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
74
srcs/ft_isdigit_superscript.c
Normal file
74
srcs/ft_isdigit_superscript.c
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
// --- UTF-8 byte sequence macros for 2-byte superscript digits ---
|
||||||
|
#define TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1 0xC2
|
||||||
|
|
||||||
|
// Superscript digits: ¹, ², ³
|
||||||
|
#define SUPERSCRIPT_1_BYTE2 0xB9
|
||||||
|
#define SUPERSCRIPT_2_BYTE2 0xB2
|
||||||
|
#define SUPERSCRIPT_3_BYTE2 0xB3
|
||||||
|
|
||||||
|
// --- UTF-8 byte sequence macros for 3-byte superscript digits ---
|
||||||
|
#define THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1 0xE2
|
||||||
|
#define THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2 0x81
|
||||||
|
|
||||||
|
// Superscript digits: ⁰, ⁴, ⁵, ⁶, ⁷, ⁸, ⁹
|
||||||
|
#define SUPERSCRIPT_0_BYTE3 0xB0
|
||||||
|
#define SUPERSCRIPT_4_BYTE3 0xB4
|
||||||
|
#define SUPERSCRIPT_5_BYTE3 0xB5
|
||||||
|
#define SUPERSCRIPT_6_BYTE3 0xB6
|
||||||
|
#define SUPERSCRIPT_7_BYTE3 0xB7
|
||||||
|
#define SUPERSCRIPT_8_BYTE3 0xB8
|
||||||
|
#define SUPERSCRIPT_9_BYTE3 0xB9
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the UTF-8 character at `input` is a superscript digit (¹, ², ³, ⁰-⁹).
|
||||||
|
* If it is, sets `*size` to the number of bytes in the character (2 or 3).
|
||||||
|
* Returns 1 if true, 0 otherwise.
|
||||||
|
*/
|
||||||
|
int ft_isdigit_superscript(const char *input, int *size)
|
||||||
|
{
|
||||||
|
if (size != NULL)
|
||||||
|
{
|
||||||
|
*size = 0; // Default to 0 if not a superscript digit
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Check for 2-byte superscript digits (¹, ², ³) ---
|
||||||
|
if ((uint8_t)*input == TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
|
||||||
|
{
|
||||||
|
uint8_t second_byte = (uint8_t)*(input + 1);
|
||||||
|
if (second_byte == SUPERSCRIPT_1_BYTE2 ||
|
||||||
|
second_byte == SUPERSCRIPT_2_BYTE2 ||
|
||||||
|
second_byte == SUPERSCRIPT_3_BYTE2)
|
||||||
|
{
|
||||||
|
if (size != NULL)
|
||||||
|
{
|
||||||
|
*size = 2; // 2-byte character
|
||||||
|
}
|
||||||
|
return 1; // Valid superscript digit (¹, ², or ³)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// --- Check for 3-byte superscript digits (⁰, ⁴-⁹) ---
|
||||||
|
else if ((uint8_t)*input == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
|
||||||
|
{
|
||||||
|
if ((uint8_t)*(input + 1) == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2)
|
||||||
|
{
|
||||||
|
uint8_t third_byte = (uint8_t)*(input + 2);
|
||||||
|
if (third_byte == SUPERSCRIPT_0_BYTE3 ||
|
||||||
|
third_byte == SUPERSCRIPT_4_BYTE3 ||
|
||||||
|
third_byte == SUPERSCRIPT_5_BYTE3 ||
|
||||||
|
third_byte == SUPERSCRIPT_6_BYTE3 ||
|
||||||
|
third_byte == SUPERSCRIPT_7_BYTE3 ||
|
||||||
|
third_byte == SUPERSCRIPT_8_BYTE3 ||
|
||||||
|
third_byte == SUPERSCRIPT_9_BYTE3)
|
||||||
|
{
|
||||||
|
if (size != NULL)
|
||||||
|
{
|
||||||
|
*size = 3; // 3-byte character
|
||||||
|
}
|
||||||
|
return 1; // Valid superscript digit (⁰, ⁴-⁹)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
80
srcs/ft_itoa_static.c
Normal file
80
srcs/ft_itoa_static.c
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/11/25 13:59:01 by hulamy #+# #+# */
|
||||||
|
/* Updated: 2020/02/19 15:44:04 by hulamy ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a long integer to its string representation and stores it in a pre-allocated buffer.
|
||||||
|
*
|
||||||
|
* @param nbr The long integer to convert.
|
||||||
|
* @param buffer The buffer to store the resulting string. Must be pre-allocated.
|
||||||
|
* @param buff_len The size of the buffer.
|
||||||
|
* Must be at least as large as the length of the number (including sign).
|
||||||
|
*
|
||||||
|
* @return The buffer (if successful), or NULL if the buffer is too small or invalid.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len)
|
||||||
|
{
|
||||||
|
int is_negative;
|
||||||
|
const char *min_str;
|
||||||
|
size_t min_len;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (buffer == NULL || buff_len == 0)
|
||||||
|
{
|
||||||
|
return NULL; // Invalid buffer or length
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle LONG_MIN separately
|
||||||
|
if (nbr == LONG_MIN)
|
||||||
|
{
|
||||||
|
min_str = "-9223372036854775808";
|
||||||
|
min_len = ft_strlen(min_str);
|
||||||
|
if (buff_len < min_len)
|
||||||
|
{
|
||||||
|
return NULL; // Buffer too small for LONG_MIN
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i < min_len)
|
||||||
|
{
|
||||||
|
buffer[i] = min_str[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
buffer[min_len] = '\0'; // Null-terminate
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine if the number is negative
|
||||||
|
is_negative = nbr < 0;
|
||||||
|
nbr = ft_labs(nbr);
|
||||||
|
|
||||||
|
// Write the null terminator at the end
|
||||||
|
buffer[buff_len - 1] = '\0';
|
||||||
|
|
||||||
|
// Write the digits in reverse order
|
||||||
|
i = buff_len - 2;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
buffer[i] = (nbr % 10) + '0';
|
||||||
|
nbr /= 10;
|
||||||
|
i--;
|
||||||
|
} while (nbr != 0);
|
||||||
|
|
||||||
|
// Add the negative sign if needed
|
||||||
|
if (is_negative)
|
||||||
|
{
|
||||||
|
buffer[0] = '-';
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
14
srcs/ft_labs.c
Normal file
14
srcs/ft_labs.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the absolute value of a long int number
|
||||||
|
*/
|
||||||
|
|
||||||
|
long int ft_labs(long int n)
|
||||||
|
{
|
||||||
|
if (n <= 0)
|
||||||
|
n *= -1;
|
||||||
|
else if (n == 0)
|
||||||
|
n = 0; // for -0
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
18
srcs/ft_nbrlen.c
Normal file
18
srcs/ft_nbrlen.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
// return length of int
|
||||||
|
// eg. 123 -> 3, -123 -> 4
|
||||||
|
|
||||||
|
size_t ft_nbrlen(int nbr)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int is_negative;
|
||||||
|
|
||||||
|
is_negative = nbr < 0;
|
||||||
|
len = (nbr < 0) ? 2 : 1;
|
||||||
|
while (nbr /= 10)
|
||||||
|
len++;
|
||||||
|
|
||||||
|
return len + is_negative;
|
||||||
|
}
|
||||||
19
srcs/ft_pow.c
Normal file
19
srcs/ft_pow.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the value of 'base' raised to the power of 'exponent'
|
||||||
|
*/
|
||||||
|
double ft_pow(double base, int exponent)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
double result;
|
||||||
|
|
||||||
|
result = 1.0;
|
||||||
|
i = 0;
|
||||||
|
while (i < exponent)
|
||||||
|
{
|
||||||
|
result *= base;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
28
srcs/ft_round.c
Normal file
28
srcs/ft_round.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* round x to the nearest integer,
|
||||||
|
* but round halfway cases away from zero, regardless of the current rounding direction
|
||||||
|
*/
|
||||||
|
double ft_round(double x)
|
||||||
|
{
|
||||||
|
// Handle special cases
|
||||||
|
if (x != x)
|
||||||
|
{ // Check for NaN (NaN is not equal to itself)
|
||||||
|
return x; // Return NaN
|
||||||
|
}
|
||||||
|
if (x == (double)LLONG_MAX + 1.0 || x == (double)LLONG_MIN - 1.0)
|
||||||
|
{
|
||||||
|
return x; // Return ±Infinity or the original value if overflow would occur
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle positive and negative numbers
|
||||||
|
if (x >= 0)
|
||||||
|
{
|
||||||
|
return (double)(long long)(x + 0.5);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return (double)(long long)(x - 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_sign(int i)
|
int ft_sign(int i)
|
||||||
{
|
{
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|||||||
@@ -1,15 +1,37 @@
|
|||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return the square root of nb
|
* return the square root of nb
|
||||||
** or the integer value of it
|
* Newton–Raphson method : https://en.wikipedia.org/wiki/Newton%27s_method#Use_of_Newton's_method_to_compute_square_roots
|
||||||
*/
|
*/
|
||||||
int ft_sqrt(int nb)
|
double ft_sqrt(double x, double precision)
|
||||||
{
|
{
|
||||||
int i;
|
if (x < 0)
|
||||||
|
return -1; // handle negative numbers
|
||||||
|
if (x == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
i = 0;
|
// Newton-Raphson
|
||||||
while ((i*i) < nb)
|
double guess = x;
|
||||||
i++;
|
double prev_guess;
|
||||||
return (i);
|
do
|
||||||
|
{
|
||||||
|
prev_guess = guess;
|
||||||
|
guess = (guess + x / guess) / 2.0;
|
||||||
|
} while (ft_fabs(prev_guess - guess) > precision);
|
||||||
|
return guess;
|
||||||
|
|
||||||
|
// // binary search
|
||||||
|
// double low = 0;
|
||||||
|
// double high = x;
|
||||||
|
// double mid;
|
||||||
|
// while (high - low > precision)
|
||||||
|
// {
|
||||||
|
// mid = (low + high) / 2.0;
|
||||||
|
// if (mid * mid < x)
|
||||||
|
// low = mid;
|
||||||
|
// else
|
||||||
|
// high = mid;
|
||||||
|
// }
|
||||||
|
// return (low + high) / 2.0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,17 @@
|
|||||||
|
|
||||||
size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n)
|
size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n)
|
||||||
{
|
{
|
||||||
|
size_t total_len;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
// Calculate total length needed
|
// Calculate total length needed
|
||||||
size_t total_len = 0;
|
total_len = 0;
|
||||||
for (size_t i = 0; i < n; i++)
|
i = 0;
|
||||||
|
while (i < n)
|
||||||
{
|
{
|
||||||
total_len += ft_strlen(srcs[i]);
|
total_len += ft_strlen(srcs[i]);
|
||||||
|
ft_printf("--ft_strlen(srcs[%i]):%i--", i, ft_strlen(srcs[i]));
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if buffer is large enough (include space for '\0')
|
// Check if buffer is large enough (include space for '\0')
|
||||||
|
|||||||
@@ -11,12 +11,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return length of of string
|
** return length of of string, excluding the terminal '\0'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
size_t ft_strlen(const char *str)
|
size_t ft_strlen(const char *str)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
|
|||||||
32
srcs/ft_superscript.c
Normal file
32
srcs/ft_superscript.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a single digit character (0-9) to its superscript string representation.
|
||||||
|
*
|
||||||
|
* @param c The digit character (e.g., '0', '1', ..., '9').
|
||||||
|
* @return A static string containing the superscript representation of the digit.
|
||||||
|
* Returns NULL if the input is not a digit.
|
||||||
|
*/
|
||||||
|
const char *ft_superscript(char c)
|
||||||
|
{
|
||||||
|
// Array of superscript digit strings (UTF-8 literals)
|
||||||
|
static const char *superscript_digits[10] = {
|
||||||
|
"\xE2\x81\xB0", // ⁰ (U+2070)
|
||||||
|
"\xC2\xB9", // ¹ (U+00B9)
|
||||||
|
"\xC2\xB2", // ² (U+00B2)
|
||||||
|
"\xC2\xB3", // ³ (U+00B3)
|
||||||
|
"\xE2\x81\xB4", // ⁴ (U+2074)
|
||||||
|
"\xE2\x81\xB5", // ⁵ (U+2075)
|
||||||
|
"\xE2\x81\xB6", // ⁶ (U+2076)
|
||||||
|
"\xE2\x81\xB7", // ⁷ (U+2077)
|
||||||
|
"\xE2\x81\xB8", // ⁸ (U+2078)
|
||||||
|
"\xE2\x81\xB9" // ⁹ (U+2079)
|
||||||
|
};
|
||||||
|
|
||||||
|
if (c < '0' || c > '9')
|
||||||
|
{
|
||||||
|
return NULL; // Not a digit
|
||||||
|
}
|
||||||
|
int digit = c - '0';
|
||||||
|
return superscript_digits[digit];
|
||||||
|
}
|
||||||
@@ -12,28 +12,56 @@
|
|||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_atoi(const char *str)
|
// int ft_atoi(const char *str)
|
||||||
{
|
// {
|
||||||
long long nbr;
|
// long long nbr; // Use `long long` to detect overflow before it happens in `int`
|
||||||
int i;
|
// int i; // Current position in the string
|
||||||
int n;
|
// int n; // Sign multiplier: 1 (positive) or -1 (negative)
|
||||||
|
|
||||||
i = 0;
|
// i = 0;
|
||||||
n = 1;
|
// n = 1;
|
||||||
nbr = 0;
|
// nbr = 0;
|
||||||
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
|
|
||||||
i++;
|
// // skip leading whitespace
|
||||||
if (str[i] == '-')
|
// while (ft_isspace(str[i]))
|
||||||
n = -1;
|
// i++;
|
||||||
if (str[i] == '+' || str[i] == '-')
|
|
||||||
i++;
|
// // handle optional sign
|
||||||
while (str[i] >= '0' && str[i] <= '9')
|
// if (str[i] == '-')
|
||||||
{
|
// n = -1;
|
||||||
if ((nbr >= 922337203685477580
|
// if (str[i] == '+' || str[i] == '-')
|
||||||
&& ((str[i] > 8 && n < 0) || (str[i] > 7 && n > 0))))
|
// i++;
|
||||||
return ((n > 0) ? -1 : 0);
|
|
||||||
else
|
// // convert digits to integer
|
||||||
nbr = nbr * 10 + (str[i++] - '0');
|
// while (str[i] >= '0' && str[i] <= '9')
|
||||||
}
|
// {
|
||||||
return (nbr * n);
|
// // --- Overflow Check ---
|
||||||
}
|
// // LLONG_MAX = 9223372036854775807 (maximum value for a 64-bit signed integer)
|
||||||
|
// // If `nbr` is already >= 922337203685477580 (LLONG_MAX / 10),
|
||||||
|
// // then multiplying by 10 and adding another digit could overflow.
|
||||||
|
// //
|
||||||
|
// // Cases:
|
||||||
|
// // - For positive numbers: If `nbr >= 922337203685477580` and the next digit > 7,
|
||||||
|
// // then `nbr * 10 + digit` would exceed LLONG_MAX (e.g., 9223372036854775808).
|
||||||
|
// // - For negative numbers: If `nbr >= 922337203685477580` and the next digit > 8,
|
||||||
|
// // then `nbr * 10 + digit` would exceed LLONG_MIN (e.g., -9223372036854775809).
|
||||||
|
// //
|
||||||
|
// // Why 7 and 8?
|
||||||
|
// // - LLONG_MAX = 9223372036854775807 → Last digit is 7.
|
||||||
|
// // So, if `nbr = 922337203685477580` and the next digit is > 7, overflow occurs.
|
||||||
|
// // - LLONG_MIN = -9223372036854775808 → Last digit is 8.
|
||||||
|
// // So, if `nbr = 922337203685477580` and the next digit is > 8, underflow occurs.
|
||||||
|
// if ((nbr >= 922337203685477580 && ((str[i] > 8 && n < 0) || (str[i] > 7 && n > 0))))
|
||||||
|
// {
|
||||||
|
// // Overflow: Return -1 for positive, 0 for negative (mimics INT_MAX/INT_MIN behavior)
|
||||||
|
// return ((n > 0) ? -1 : 0);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // convert digit to integer : `str[i] - '0'` converts the ASCII character to its numeric value (e.g., '5' → 5)
|
||||||
|
// nbr = nbr * 10 + (str[i] - '0');
|
||||||
|
// i++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // apply sign and return
|
||||||
|
// return (nbr * n);
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
/*
|
// /*
|
||||||
** return the square root of nb
|
// ** return the square root of nb
|
||||||
** or the integer value of it
|
// ** or the integer value of it
|
||||||
*/
|
// */
|
||||||
int ft_sqrt(int nb)
|
// int ft_sqrt(int nb)
|
||||||
{
|
// {
|
||||||
int i;
|
// int i;
|
||||||
|
|
||||||
i = 0;
|
// i = 0;
|
||||||
while ((i*i) < nb)
|
// while ((i*i) < nb)
|
||||||
i++;
|
// i++;
|
||||||
return (i);
|
// return (i);
|
||||||
}
|
// }
|
||||||
|
|||||||
Reference in New Issue
Block a user