Compare commits
13 Commits
b768ac1a14
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ac98cb1f5 | ||
|
|
71806cb923 | ||
|
|
e95f55c07f | ||
|
|
85423f4211 | ||
|
|
f662d7f750 | ||
|
|
de28530259 | ||
|
|
60c72d7560 | ||
|
|
ae9787ba6d | ||
|
|
8edf428fed | ||
|
|
3cf0b6ecb9 | ||
|
|
aa35d294f2 | ||
|
|
413e149003 | ||
|
|
36379f56b0 |
11
Makefile
11
Makefile
@@ -12,7 +12,7 @@ _DEP = libft.h
|
||||
DEPS = $(_DEP:%.h=$(IDIR)/%.h)
|
||||
|
||||
CFLAGS = -I$(IDIR)
|
||||
CFLAGS += -Wall -Wextra -Werror -g3
|
||||
CFLAGS += -Wall -Wextra -Werror -g
|
||||
|
||||
SRCS = ft_memset.c \
|
||||
ft_bzero.c \
|
||||
@@ -37,6 +37,7 @@ SRCS = ft_memset.c \
|
||||
ft_tolower.c \
|
||||
\
|
||||
ft_strlen.c \
|
||||
ft_nbrlen.c \
|
||||
ft_strchr.c \
|
||||
ft_strrchr.c \
|
||||
ft_strchrset.c \
|
||||
@@ -75,6 +76,7 @@ SRCS = ft_memset.c \
|
||||
ft_atoll_superscript.c \
|
||||
ft_atof.c \
|
||||
ft_itoa.c \
|
||||
ft_itoa_static.c \
|
||||
ft_utoa.c \
|
||||
ft_atoibase.c \
|
||||
\
|
||||
@@ -120,18 +122,19 @@ SRCS = ft_memset.c \
|
||||
pf_utils.c \
|
||||
\
|
||||
ft_abs.c \
|
||||
ft_labs.c \
|
||||
ft_fabs.c \
|
||||
ft_greater.c \
|
||||
ft_smaller.c \
|
||||
ft_sign.c \
|
||||
ft_sign_f.c \
|
||||
ft_modf.c \
|
||||
ft_fsign.c \
|
||||
ft_pow.c \
|
||||
ft_sqrt.c \
|
||||
ft_round.c \
|
||||
ft_free_tab.c \
|
||||
\
|
||||
ft_arrint.c
|
||||
ft_arrint.c \
|
||||
ft_superscript.c
|
||||
|
||||
|
||||
ODIR = ./builds
|
||||
|
||||
@@ -27,6 +27,7 @@ void *ft_memmove(void *dst, const void *src, size_t len);
|
||||
void *ft_memchr(const void *s, int c, 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_nbrlen(int nbr);
|
||||
int ft_isalpha(int c);
|
||||
int ft_isdigit(int c);
|
||||
int ft_isdigit_superscript(const char *input, int *size);
|
||||
@@ -60,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_split(char const *s, char c);
|
||||
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_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
|
||||
@@ -122,17 +124,18 @@ void ft_putnbrendl(int n);
|
||||
void ft_putnbrendl_fd(int n, int fd);
|
||||
char *ft_concat_free(char *str1, char *str2);
|
||||
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_smaller(int a, int b);
|
||||
int ft_sign(int i);
|
||||
int ft_sign_f(double i);
|
||||
double ft_modf(double x, double *int_part);
|
||||
int ft_fsign(double i);
|
||||
double ft_pow(double base, int exponent);
|
||||
double ft_round(double x);
|
||||
int ft_sqrt(int i);
|
||||
double ft_sqrt(double i, double precision);
|
||||
void ft_free_tab(char **tab);
|
||||
|
||||
int ft_arrint(int *intarr, int comp, size_t size);
|
||||
const char *ft_superscript(char c);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#include "libft.h"
|
||||
|
||||
/**
|
||||
* returns the absolute value of an int number
|
||||
*/
|
||||
|
||||
int ft_abs(int n)
|
||||
{
|
||||
if (n < 0)
|
||||
n *= -1;
|
||||
else if (n == 0)
|
||||
n = 0; // for -0
|
||||
return (n);
|
||||
}
|
||||
|
||||
@@ -32,8 +32,10 @@ long long ft_atoll_superscript(const char *str)
|
||||
int digit;
|
||||
if (superscript_size == 2)
|
||||
{
|
||||
// ² (U+00B2) or ³ (U+00B3)
|
||||
if ((uint8_t)str[i + 1] == 0xB2)
|
||||
// ¹ (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;
|
||||
|
||||
@@ -1,8 +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);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "libft.h"
|
||||
|
||||
int ft_sign_f(double i)
|
||||
int ft_fsign(double i)
|
||||
{
|
||||
if (i < 0)
|
||||
return (-1);
|
||||
else if (i == 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
@@ -1,7 +1,28 @@
|
||||
#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 (², ³, ⁰-⁹).
|
||||
* 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.
|
||||
*/
|
||||
@@ -12,42 +33,40 @@ int ft_isdigit_superscript(const char *input, int *size)
|
||||
*size = 0; // Default to 0 if not a superscript digit
|
||||
}
|
||||
|
||||
// 2-byte UTF-8 superscript digits: ² (U+00B2) and ³ (U+00B3) ---
|
||||
// In UTF-8, 2-byte characters start with a byte in the range 0xC0-0xDF.
|
||||
// For superscript ² and ³:
|
||||
// - First byte: 0xC2 (binary: 11000010)
|
||||
// - Second byte: 0xB2 (²) or 0xB3 (³) (binary: 10110010 or 10110011)
|
||||
if ((uint8_t)*input == 0xC2) // Check if first byte is 0xC2 (start of 2-byte UTF-8)
|
||||
// --- Check for 2-byte superscript digits (¹, ², ³) ---
|
||||
if ((uint8_t)*input == TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
|
||||
{
|
||||
// Check if the second byte matches ² (0xB2) or ³ (0xB3)
|
||||
if ((uint8_t)*(input + 1) == 0xB2 || (uint8_t)*(input + 1) == 0xB3)
|
||||
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 ³)
|
||||
return 1; // Valid superscript digit (¹, ², or ³)
|
||||
}
|
||||
}
|
||||
// 3-byte UTF-8 superscript digits: ⁰ (U+2070) to ⁹ (U+2079) ---
|
||||
// In UTF-8, 3-byte characters start with a byte in the range 0xE0-0xEF.
|
||||
// For superscript ⁰-⁹:
|
||||
// - First byte: 0xE2 (binary: 11100010)
|
||||
// - Second byte: 0x81 (binary: 10000001)
|
||||
// - Third byte: 0xB0 (⁰) to 0xB9 (⁹) (binary: 10110000 to 10111001)
|
||||
else if ((uint8_t)*input == 0xE2) // Check if first byte is 0xE2 (start of 3-byte UTF-8)
|
||||
// --- Check for 3-byte superscript digits (⁰, ⁴-⁹) ---
|
||||
else if ((uint8_t)*input == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
|
||||
{
|
||||
// Check if the second byte is 0x81 (part of the 3-byte sequence for ⁰-⁹)
|
||||
if ((uint8_t)*(input + 1) == 0x81)
|
||||
if ((uint8_t)*(input + 1) == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2)
|
||||
{
|
||||
// Check if the third byte is in the range 0xB0-0xB9 (⁰ to ⁹)
|
||||
if ((uint8_t)*(input + 2) >= 0xB0 && (uint8_t)*(input + 2) <= 0xB9)
|
||||
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 1; // Valid superscript digit (⁰, ⁴-⁹)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#include "libft.h"
|
||||
|
||||
/**
|
||||
* Splits a double into its integer and fractional parts,
|
||||
* it returns the fractional part,
|
||||
* and stores the integer part in the in_part param (if not null)
|
||||
* e.g.:
|
||||
* -3.7 → -3.0 and -0.7 (returns -0.7)
|
||||
*/
|
||||
double ft_modf(double x, double *int_part)
|
||||
{
|
||||
// extract the integer part by casting to long long
|
||||
long long integer = (long long)x;
|
||||
if (int_part != NULL)
|
||||
{
|
||||
*int_part = (double)integer;
|
||||
}
|
||||
|
||||
// compute the fractional part
|
||||
double frac_part = x - *int_part;
|
||||
|
||||
return frac_part;
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -1,15 +1,37 @@
|
||||
#include "libft.h"
|
||||
|
||||
/*
|
||||
** return the square root of nb
|
||||
** or the integer value of it
|
||||
* return the square root of nb
|
||||
* 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;
|
||||
while ((i*i) < nb)
|
||||
i++;
|
||||
return (i);
|
||||
// Newton-Raphson
|
||||
double guess = x;
|
||||
double prev_guess;
|
||||
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 total_len;
|
||||
size_t i;
|
||||
|
||||
// Calculate total length needed
|
||||
size_t total_len = 0;
|
||||
for (size_t i = 0; i < n; i++)
|
||||
total_len = 0;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
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')
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** return length of of string
|
||||
** return length of of string, excluding the terminal '\0'
|
||||
*/
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
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];
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
#include "libft.h"
|
||||
|
||||
/*
|
||||
** return the square root of nb
|
||||
** or the integer value of it
|
||||
*/
|
||||
int ft_sqrt(int nb)
|
||||
{
|
||||
int i;
|
||||
// /*
|
||||
// ** return the square root of nb
|
||||
// ** or the integer value of it
|
||||
// */
|
||||
// int ft_sqrt(int nb)
|
||||
// {
|
||||
// int i;
|
||||
|
||||
i = 0;
|
||||
while ((i*i) < nb)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
// i = 0;
|
||||
// while ((i*i) < nb)
|
||||
// i++;
|
||||
// return (i);
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user