Skip to content

Commit

Permalink
Merge pull request #29 from Savapitech/put_science
Browse files Browse the repository at this point in the history
Put science
  • Loading branch information
savalet authored Oct 30, 2024
2 parents ad051cd + 377d3f5 commit 3ed1fe4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/my.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef enum {
char *baby_revstr(char *);
int baby_getnbr(char const *);
int baby_put_nbr(int, flags_t *, int);
int baby_put_hex(size_t nb, flags_t *);
int baby_put_hex(size_t nb, flags_t *, int);
int baby_putchar(char);
int baby_putstr(char *);
int baby_stridx(char const *, char);
Expand Down
19 changes: 12 additions & 7 deletions src/handler/put_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

#include "my.h"

int baby_put_hex(size_t nb, flags_t *flags)
int baby_put_hex(size_t nb, flags_t *flags, int i)
{
size_t result = (size_t)nb;
char str[64];
int i = 0;

for (; nb != 0; i++) {
result = nb % 16;
Expand All @@ -25,15 +24,14 @@ int baby_put_hex(size_t nb, flags_t *flags)
baby_revstr(str);
baby_strcpy(flags->spec_buff.str, str);
flags->spec_buff.count = i;
return 0;
return i;
}

static
int baby_put_hex_upc(size_t nb, flags_t *flags)
int baby_put_hex_upc(size_t nb, flags_t *flags, int i)
{
size_t result = (size_t)nb;
char str[64];
int i = 0;

for (; nb != 0; i++) {
result = nb % 16;
Expand All @@ -52,6 +50,7 @@ int baby_put_hex_upc(size_t nb, flags_t *flags)

void printf_put_hex(flags_t *flags)
{
int i = 0;
size_t nb = (size_t)va_arg(flags->args, void *);

if (flags->precision == 0 && nb == 0)
Expand All @@ -61,8 +60,14 @@ void printf_put_hex(flags_t *flags)
flags->spec_buff.count = 1;
return;
}
if (flags->precision > 0) {
if (flags->flags & FLAGS_PAD_RIGHT)
flags->flags &= ~FLAGS_PAD_RIGHT;
flags->flags |= FLAGS_PAD_ZERO;
flags->width = flags->precision;
}
if (isupper(flags->spec))
baby_put_hex_upc(nb, flags);
baby_put_hex_upc(nb, flags, i);
else
baby_put_hex(nb, flags);
baby_put_hex(nb, flags, i);
}
6 changes: 3 additions & 3 deletions src/handler/put_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void printf_put_pointer(flags_t *flags)
char *old_spec_buff_str;

old_spec_buff_str = flags->spec_buff.str;
flags->spec_buff.str = "0x";
flags->spec_buff.str += 2;
baby_put_hex(src, flags);
flags->spec_buff.str[0] = '0';
flags->spec_buff.str[1] = 'x';
baby_put_hex(src, flags, 3);
flags->spec_buff.str = old_spec_buff_str;
}
4 changes: 2 additions & 2 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ int main(void)
{
int a = 42;

my_printf("%5.3s pad 3: %5.3s %d\n", "hello", "hello", 42);
printf("%5.3s pad 3: %5.3s %d\n", "hello", "helloo", 42);
my_printf("%.5f\n", 33.01);
printf("%.5f\n", 33.01);
}

0 comments on commit 3ed1fe4

Please sign in to comment.