Skip to content

Commit

Permalink
Rewrite str_to_int to my_strtol
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Dec 15, 2023
1 parent 19cab17 commit 91f0ab2
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 85 deletions.
2 changes: 1 addition & 1 deletion bonus/anthills/ex_3.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
3
3 # foo
2 5 0
##start
0 1 2
Expand Down
2 changes: 1 addition & 1 deletion lib/my/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SRC += string/my_strpbrk.c
SRC += string/my_strstr.c
SRC += string/reverse.c
SRC += string/str_identify.c
SRC += string/str_to_int.c
SRC += string/strtol.c
SRC += string/strcases.c
SRC += string/write_number.c

Expand Down
4 changes: 2 additions & 2 deletions lib/my/io/my_printf/flag_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void get_flags(printf_t *pr, printf_args_t *arg)
arg->flags = (struct flags_s){ 0 };
get_simple_flags(pr, arg);
if (IS_NUM(pr->format[pr->index]) || pr->format[pr->index] == '-') {
arg->flags.padding = str_to_int(pr->format + pr->index);
arg->flags.padding = my_strtol(pr->format + pr->index, NULL);
arg->flags.padded = true;
pr->index += (
(int)my_nbr_len_base(arg->flags.padding, BASE_DEC)
Expand All @@ -72,7 +72,7 @@ bool get_precision(printf_t *pr, printf_args_t *arg)
arg->precision = default_precision;
if (pr->format[pr->index] != '.')
return true;
tmp = str_to_int(pr->format + pr->index + 1);
tmp = my_strtol(pr->format + pr->index + 1, NULL);
if (tmp <= 0 && pr->format[pr->index + 1] == '-')
return false;
pr->index += 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/my/std/convert_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

char *convert_base(char const *nbr, char const *base_from, char const *base_to)
{
return my_nbr_to_base(str_to_int_base(nbr, base_from), base_to);
return my_nbr_to_base(my_strtol_base(nbr, NULL, base_from), base_to);
}
4 changes: 2 additions & 2 deletions lib/my/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ int my_showmem(char const *str, int size);
int my_showstr(char const *str);
int my_strcmp(char const *s1, char const *s2);
int my_strncmp(char const *s1, char const *s2, size_t n);
int64_t str_to_int(char const *str);
int64_t str_to_int_base(char const *str, char const *base);
long my_strtol(const char *str, const char **endptr);
long my_strtol_base(const char *str, const char **endptr, char const *base);
size_t my_memfind(const void *arr, const void *target, size_t n);
size_t my_strlen(char const *str);
size_t write_int_base(long long nbr, char const *base, char *buf);
Expand Down
75 changes: 0 additions & 75 deletions lib/my/string/str_to_int.c

This file was deleted.

57 changes: 57 additions & 0 deletions lib/my/string/strtol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
** EPITECH PROJECT, 2023
** lemin
** File description:
** my_strtol
*/

#include "string.h"

static
int index_in(char c, char const *base)
{
for (int i = 0; base[i]; i++)
if (base[i] == c)
return i;
return -1;
}

static
bool is_valid(char c, bool *negative, const char *base)
{
if (index_in(c, base) != -1)
return true;
if (c == '+')
return true;
if (c == '-') {
*negative = !*negative;
return true;
}
return false;
}

long my_strtol_base(const char *str, const char **endptr, char const *base)
{
long res = 0;
long len = (long)my_strlen(base);
bool negative = false;
size_t i = 0;

if (str == NULL || len == 0)
return 0;
for (; str[i]; i++) {
if (!is_valid(str[i], &negative, base))
break;
if (index_in(str[i], base) == -1)
continue;
res = res * len + -index_in(str[i], base);
}
if (endptr != NULL)
*endptr = str + i;
return (negative) ? res : -res;
}

long my_strtol(const char *str, const char **endptr)
{
return my_strtol_base(str, endptr, BASE_DEC);
}
2 changes: 1 addition & 1 deletion src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int get_command(char *line, anthill_t *anthill)
static
int get_ants_num(char *line, anthill_t *anthill)
{
long value = str_to_int(line);
long value = my_strtol(line, NULL);

if (value == INT64_MAX)
return RET_ERROR;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/room.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ room_t *get_room_data(char *line)
*ptr_x = '\0';
*ptr_y = '\0';
room.name = dup;
room.x = str_to_int(ptr_x + 1);
room.y = str_to_int(ptr_y + 1);
room.x = my_strtol(ptr_x + 1, NULL);
room.y = my_strtol(ptr_y + 1, NULL);
room.type = ROOM_NORMAL;
if (room.x == INT64_MAX || room.y == INT64_MAX)
return NULL;
Expand Down

0 comments on commit 91f0ab2

Please sign in to comment.