flag espace ok

This commit is contained in:
Hugo LAMY
2020-03-12 20:56:44 +01:00
parent 01aab8912c
commit 02bd116005
6 changed files with 102 additions and 45 deletions

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 12:07:47 by hulamy #+# #+# */
/* Updated: 2020/03/10 16:26:47 by hulamy ### ########.fr */
/* Updated: 2020/03/12 20:35:10 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -62,5 +62,6 @@ char *ft_flag_transform(char *s, char *print, char *type, int *size);
char *ft_plus(char *s, char *print, char *type);
char *ft_sharp(char *s, char *print, char *type);
char *ft_sharp_again(char *s, char *print, char *type);
char *ft_space(char *s, char *print, char *type, int *size);
#endif

106
main.c
View File

@@ -391,10 +391,12 @@ int main(int ac, char **av)
// PRINT("%05.d - %05.0d - %05.1d - %05.2d - %05.3d - %05.4d - %05.5d - %05.6d", 123, 123, 123, 123, 123, 123, 123, 123)
// PRINT("%06.d - %06.0d - %06.1d - %06.2d - %06.3d - %06.4d - %06.5d - %06.6d", 123, 123, 123, 123, 123, 123, 123, 123)
// PRINT(diuxXcsp)
// PRINT(h hh l ll)
// PRINT(0-)
// PRINT(#' +)
// (diuxXcsp)
// (h hh l ll)
// (0-)
// (#' +)
// PRINT("%+10.7i % 010i %-#7.10x %7p %10c %7.10s", 122, -122, 122, "122", 122, "-122")
}
/* ////////////////////////////////////////////////////////////////// */
@@ -895,6 +897,7 @@ int main(int ac, char **av)
printf("flag '#'\n");
printf("----------------------------------------------------------------\n\n");
unsigned int uu = 4;
PRINT("%#X", 8645);
PRINT("%#.7X", 8645);
PRINT("%#010.7X", 8645);
@@ -903,10 +906,19 @@ int main(int ac, char **av)
PRINT("%#.10X", 8645);
PRINT("%#10.10X", 8645);
PRINT("%#010X", 8645);
unsigned int uu = 4;
PRINT("%#X", (unsigned int)&uu)
PRINT("%#x", (unsigned int)&uu)
PRINT("%#lx", (unsigned long int)&uu)
PRINT("%-#X", 8645);
PRINT("%-#.7X", 8645);
PRINT("%-#10.7X", 8645);
PRINT("%-#10X", 8645);
PRINT("%-#.10X", 8645);
PRINT("%-#10.10X", 8645);
PRINT("%-#X", (unsigned int)&uu)
PRINT("%-#x", (unsigned int)&uu)
PRINT("%-#lx", (unsigned long int)&uu)
}
if (ac == 2 || !strcmp(av[2], "'"))
@@ -949,6 +961,24 @@ int main(int ac, char **av)
PRINT("% 05.4d", 12);
PRINT("% 010d", 645);
PRINT("% 010d", -645);
PRINT("%- d", 12);
PRINT("%- d", -12);
PRINT("%- 5d", 12);
PRINT("%- 5d", -12);
PRINT("%- 1d", -12);
PRINT("%- 2d", -12);
PRINT("%- 3d", -12);
PRINT("%- 4d", -12);
PRINT("%- 1d", 12);
PRINT("%- 2d", 12);
PRINT("%- 3d", 12);
PRINT("%- 4d", 12);
PRINT("%- .4d", 12);
PRINT("%- 3.4d", 12);
PRINT("%- 5.4d", -12);
PRINT("%- 5.4d", 12);
PRINT("%- 8d", 645);
}
if (ac == 2 || !strcmp(av[2], "+"))
@@ -1044,8 +1074,8 @@ int main(int ac, char **av)
printf("----------------------------------------------------------------\n\n");
PRINT("%i", 33333);
PRINT("%.0f", 33333.0);
PRINT("%#.0f", 33333.0);
// PRINT("%.0f", 33333.0);
// PRINT("%#.0f", 33333.0);
PRINT("%08i", 33333);
PRINT("%-8i", 33333);
PRINT("%+i", 33333);
@@ -1056,9 +1086,9 @@ int main(int ac, char **av)
printf("'0' and '-' not compatible -------------------------------------\n\n");
PRINT("%0000i", 33333);
PRINT("%0#.0f", 33333.0);
PRINT("%#0.0f", 33333.0);
PRINT("%0#0.0f", 33333.0);
// PRINT("%0#.0f", 33333.0);
// PRINT("%#0.0f", 33333.0);
// PRINT("%0#0.0f", 33333.0);
PRINT("%0+8i", 33333);
PRINT("%+08i", 33333);
PRINT("%0+08i", 33333);
@@ -1073,9 +1103,9 @@ int main(int ac, char **av)
printf("'0' and '-' not compatible -------------------------------------\n\n");
PRINT("%----i", 33333);
PRINT("%-#.0f", 33333.0);
PRINT("%#-.0f", 33333.0);
PRINT("%-#-.0f", 33333.0);
// PRINT("%-#.0f", 33333.0);
// PRINT("%#-.0f", 33333.0);
// PRINT("%-#-.0f", 33333.0);
PRINT("%-+8i", 33333);
PRINT("%+-8i", 33333);
PRINT("%-+-8i", 33333);
@@ -1088,29 +1118,29 @@ int main(int ac, char **av)
printf("\nrepetition of flag '#' -----------------------------------------\n");
PRINT("%####f", 33333.0);
PRINT("%#+.0f", 33333.0);
PRINT("%+#.0f", 33333.0);
PRINT("%#+#.0f", 33333.0);
PRINT("%#08f", 33333.0);
PRINT("%0#8f", 33333.0);
PRINT("%#0#8f", 33333.0);
PRINT("%#-8f", 33333.0);
PRINT("%-#8f", 33333.0);
PRINT("%#-#8f", 33333.0);
PRINT("%#'f", 33333.0);
PRINT("%'#f", 33333.0);
PRINT("%#'#f", 33333.0);
PRINT("%# f", 33333.0);
PRINT("% #f", 33333.0);
PRINT("%# #f", 33333.0);
// PRINT("%####f", 33333.0);
// PRINT("%#+.0f", 33333.0);
// PRINT("%+#.0f", 33333.0);
// PRINT("%#+#.0f", 33333.0);
// PRINT("%#08f", 33333.0);
// PRINT("%0#8f", 33333.0);
// PRINT("%#0#8f", 33333.0);
// PRINT("%#-8f", 33333.0);
// PRINT("%-#8f", 33333.0);
// PRINT("%#-#8f", 33333.0);
// PRINT("%#'f", 33333.0);
// PRINT("%'#f", 33333.0);
// PRINT("%#'#f", 33333.0);
// PRINT("%# f", 33333.0);
// PRINT("% #f", 33333.0);
// PRINT("%# #f", 33333.0);
printf("\nrepetition of flag ''' -----------------------------------------\n\n");
PRINT("%''''i", 33333);
PRINT("%'#.0f", 33333.0);
PRINT("%#'.0f", 33333.0);
PRINT("%'#'.0f", 33333.0);
// PRINT("%'#.0f", 33333.0);
// PRINT("%#'.0f", 33333.0);
// PRINT("%'#'.0f", 33333.0);
PRINT("%'08i", 33333);
PRINT("%0'8i", 33333);
PRINT("%'0'8i", 33333);
@@ -1128,9 +1158,9 @@ int main(int ac, char **av)
printf("'+' and ' ' not compatible -------------------------------------\n\n");
PRINT("% i", 33333);
PRINT("% #.0f", 33333.0);
PRINT("%# .0f", 33333.0);
PRINT("% # .0f", 33333.0);
// PRINT("% #.0f", 33333.0);
// PRINT("%# .0f", 33333.0);
// PRINT("% # .0f", 33333.0);
PRINT("% 08i", 33333);
PRINT("%0 8i", 33333);
PRINT("% 0 8i", 33333);
@@ -1145,9 +1175,9 @@ int main(int ac, char **av)
printf("'+' and ' ' not compatible -------------------------------------\n\n");
PRINT("%++++i", 33333);
PRINT("%+#.0f", 33333.0);
PRINT("%#+.0f", 33333.0);
PRINT("%+#+.0f", 33333.0);
// PRINT("%+#.0f", 33333.0);
// PRINT("%#+.0f", 33333.0);
// PRINT("%+#+.0f", 33333.0);
PRINT("%+08i", 33333);
PRINT("%0+8i", 33333);
PRINT("%+0+8i", 33333);

View File

@@ -1 +1 @@
123 - 123 - 123 - 123 - 123 - 0123 - 00123 - 000123
0x7ffee2d2e8a8

View File

@@ -1 +1 @@
123 - 123 - 123 - 123 - 123 - 0123 - 00123 - 000123
0x7ffee2d2e8a8

View File

@@ -65,10 +65,10 @@ char *ft_precision(char *s, char *print, char *type)
if ((tmp = ft_strchr(s, '.')))
{
precision = ft_atoi(tmp + 1);
*tmp = '\0';
// *tmp = '\0';
while (*s && ft_strchr("#- +'0", *(++s)))
if (*s == '0')
*s = '.';
*s = '/';
i = 0;
if (precision == 0 && !ft_strcmp(print, "0"))
print[0] = '\0';
@@ -146,9 +146,9 @@ char *ft_width(char *s, char *print, int *size, char *type)
zero = 0;
if (print[0] == '\0' && ft_strchr(type, 'c'))
zero = 1;
while (*tmp != '\0' && ft_strchr("%#- +'0.", *tmp))
while (*tmp != '\0' && ft_strchr("%#- +'0/", *tmp))
tmp++;
if (*tmp == '\0')
if (*tmp == '\0' || *tmp == '.')
{
*size = ft_strlen(print) + zero;
return (print);
@@ -159,6 +159,7 @@ char *ft_width(char *s, char *print, int *size, char *type)
print = width_flags(print, s, *size, zero);
else
*size = ft_strlen(print) + zero;
tmp[0] = '1';
return (print);
}
@@ -179,6 +180,7 @@ char *ft_flag_transform(char *s, char *print, char *type, int *size)
print = ft_sharp(s, print, type);
print = ft_width(s, print, size, type);
print = ft_sharp_again(s, print, type);
print = ft_space(s, print, type, size);
if (ft_strchr(type, 'p'))
{
print = ft_concat_free(ft_strdup("0x"), print);

View File

@@ -37,3 +37,27 @@ char *ft_sharp_again(char *s, char *print, char *type)
return (print);
}
char *ft_space(char *s, char *print, char *type, int *size)
{
int i = 0;
if (print[0] == ' ' || !ft_strchr(s, ' ') || !ft_strchrset(type, "diuxX"))
return (print);
while (print[i] == ' ')
i++;
if (print[i] == '-' || print[i] == '+')
return (print);
if (ft_strchr(s, '.') || (i == 0 && print[i] != '0'))
{
print = ft_concat_free(ft_strdup(" "), print);
*size += 1;
}
else
print[i] = ' ';
if (ft_strchr(s, '-') && print[*size - 1] == ' ')
{
print[*size] = '\0';
*size -= 1;
}
return (print);
}