-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
92 lines (83 loc) · 2.05 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: deleusis <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 17:40:50 by deleusis #+# #+# */
/* Updated: 2022/02/26 19:42:12 by deleusis ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void ft_putchar_fd(char c, int fd)
{
write (fd, &c, 1);
}
void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
{
write (fd, "-2147483648", 11);
}
else
{
if (n < 0)
{
n = -n;
ft_putchar_fd ('-', fd);
}
if (n < 10)
ft_putchar_fd ((n + '0'), fd);
else
{
ft_putnbr_fd (n / 10, fd);
ft_putnbr_fd (n % 10, fd);
}
}
}
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
return (0);
while (s1[i] != '\0' && s1[i] == s2[i] && i < n - 1)
i++;
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
static int ft_char_in_set(char c, char *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char *s1, char *set)
{
char *str;
size_t i;
size_t start;
size_t end;
if (!s1 || !set)
return (NULL);
start = 0;
while (s1[start] && ft_char_in_set(s1[start], set))
start++;
end = ft_strlen(s1);
while (end > start && ft_char_in_set(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(*s1) * (end - start + 1));
if (!str)
return (NULL);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = 0;
return (str);
}