🔢 Conversion Utilities Expansion

##  New Functionality
-  Added the `ft_atou` function in `ft_atou.c` to convert a string to `size_t` (unsigned integer), with built-in whitespace handling and null pointer safety.  
- 📝 Declared `ft_atou` in `convert.h` with a detailed Doxygen-style comment explaining usage, return value, and parameters.  

## 🧪 Test Coverage Improvements
- 🆕 Introduced `test_atou.c` to verify `ft_atou` against `strtoull`, covering edge cases such as empty strings, whitespace, and invalid input.  
- 🔍 Expanded tests in `test_atoi.c`, `test_atoll.c`, and `test_itoa.c` to include more diverse scenarios (extra whitespace, varied integer ranges) to improve robustness and reliability.  

## 🛠️ Codebase Maintenance
- 📦 Included `<stddef.h>` in `convert.h` to ensure `size_t` is available in function declarations.  

##  Impact
- New unsigned integer conversion capability.  
- Stronger and more comprehensive test suite for all conversion functions.  
- Cleaner and safer header definitions.
This commit is contained in:
Raphaël 2025-09-08 10:26:12 +02:00 committed by GitHub
commit 1a09167546
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 99 additions and 8 deletions

40
convert/ft_atou.c Normal file
View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atou.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 17:22:41 by rparodi #+# #+# */
/* Updated: 2025/09/08 10:16:01 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
static int ft_check_space(int c)
{
if (c == 32 || (c >= 9 && c <= 13))
return (1);
return (0);
}
size_t ft_atou(const char *nptr)
{
size_t i;
size_t number;
if (!nptr)
return (0);
i = 0;
number = 0;
while (nptr[i])
{
if (nptr[i] >= '0' && nptr[i] <= '9')
number = (number * 10) + nptr[i] - '0';
else
break ;
i++;
}
return (number);
}

View file

@ -6,13 +6,15 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 14:57:24 by rparodi #+# #+# */
/* Updated: 2025/09/05 16:07:54 by rparodi ### ########.fr */
/* Updated: 2025/09/08 10:17:01 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CONVERT_H
# define CONVERT_H
# include <stddef.h>
/**
* @brief Converts integer to string
*
@ -29,6 +31,14 @@ char *ft_itoa(int n);
*/
int ft_atoi(const char *nptr);
/**
* @brief Converts string to size_t
*
* @param nptr the string that will be converted
* @return The unsigned long long on the string
*/
size_t ft_atou(const char *nptr);
/**
* @brief Converts string to long long integer
*

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
/* Updated: 2025/09/05 15:05:21 by rparodi ### ########.fr */
/* Updated: 2025/09/08 10:07:43 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,11 +19,11 @@
int main(void)
{
const char *str[] = {"", "0", "42", "-42", "+-42", "-2147483648",
"2147483647"};
"2147483647", "\v\t13", "\r\n7"};
size_t i;
i = 0;
while (i < 7)
while (i < 9)
{
if (ft_atoi(str[i]) != atoi(str[i]))
{

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
/* Updated: 2025/09/05 15:05:31 by rparodi ### ########.fr */
/* Updated: 2025/09/08 10:08:15 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@
int main(void)
{
const char *str[] = {"", "0", "42", "-42", "+-42", "-2147483648",
"2147483647", "9223372036854775807", "-9223372036854775808"};
"2147483647", "\v\t13", "\r\n7"};
size_t i;
i = 0;

41
test/convert/test_atou.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_atou.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
/* Updated: 2025/09/08 10:20:37 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "color.h"
#include "convert.h"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *str[] = {"", "0", "42", "-42", "+-42",
"2147483647", "\v\t13", "\r\n7"};
size_t i;
char *end;
i = 0;
while (i < 9)
{
if (ft_atou(str[i]) != strtoull(str[i], &end, 10))
{
dprintf(2, "%s✘ Found %zu, expected %llu%s\n", CLR_RED,
ft_atou(str[i]), strtoull(str[i], &end, 10), RESET);
exit(1);
}
else
printf("%s✔%s ", CLR_GREEN, RESET);
i++;
}
ft_atou(NULL);
printf("%s✔%s", CLR_GREEN, RESET);
puts("\n");
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
/* Updated: 2025/09/05 16:10:48 by rparodi ### ########.fr */
/* Updated: 2025/09/08 10:09:06 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@
int main(void)
{
const int integer[] = {0, 42, -42, + -42, -2147483648, 2147483647};
const int integer[] = {0, 42, -42, +-42, -2147483648, 2147483647, 13, 7};
char result[1024];
size_t i;