🔢 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:
commit
1a09167546
6 changed files with 99 additions and 8 deletions
40
convert/ft_atou.c
Normal file
40
convert/ft_atou.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -6,13 +6,15 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/31 14:57:24 by rparodi #+# #+# */
|
/* 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
|
#ifndef CONVERT_H
|
||||||
# define CONVERT_H
|
# define CONVERT_H
|
||||||
|
|
||||||
|
# include <stddef.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts integer to string
|
* @brief Converts integer to string
|
||||||
*
|
*
|
||||||
|
|
@ -29,6 +31,14 @@ char *ft_itoa(int n);
|
||||||
*/
|
*/
|
||||||
int ft_atoi(const char *nptr);
|
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
|
* @brief Converts string to long long integer
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
|
/* 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)
|
int main(void)
|
||||||
{
|
{
|
||||||
const char *str[] = {"", "0", "42", "-42", "+-42", "-2147483648",
|
const char *str[] = {"", "0", "42", "-42", "+-42", "-2147483648",
|
||||||
"2147483647"};
|
"2147483647", "\v\t13", "\r\n7"};
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 7)
|
while (i < 9)
|
||||||
{
|
{
|
||||||
if (ft_atoi(str[i]) != atoi(str[i]))
|
if (ft_atoi(str[i]) != atoi(str[i]))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
|
/* 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)
|
int main(void)
|
||||||
{
|
{
|
||||||
const char *str[] = {"", "0", "42", "-42", "+-42", "-2147483648",
|
const char *str[] = {"", "0", "42", "-42", "+-42", "-2147483648",
|
||||||
"2147483647", "9223372036854775807", "-9223372036854775808"};
|
"2147483647", "\v\t13", "\r\n7"};
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
|
||||||
41
test/convert/test_atou.c
Normal file
41
test/convert/test_atou.c
Normal 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");
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */
|
/* 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)
|
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];
|
char result[1024];
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue