-
Notifications
You must be signed in to change notification settings - Fork 0
/
libft.c
96 lines (85 loc) · 2.22 KB
/
libft.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
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: meghribe <meghribe@student.42barcelona.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 22:07:48 by meghribe #+# #+# */
/* Updated: 2024/11/11 22:07:48 by meghribe ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* Calculates the length of a string */
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
/* Ascii to integer function */
int ft_atoi(const char *nptr)
{
int i;
int mult;
int num;
i = 0;
num = 0;
mult = 1;
while (ft_ischar(nptr[i], SPACE))
i++;
if (ft_ischar(nptr[i], SYMBOL))
if (nptr[i++] == '-')
mult = -1;
while (nptr[i] >= '0' && nptr[i] <= '9')
num = (num * 10) + (nptr[i++] - '0');
return (num * mult);
}
/* Returns substring from a string */
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t s_len;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (ft_strdup(""));
if (len > s_len - start)
len = s_len - start;
sub = (char *)malloc(sizeof(char) * (len + 1));
if (!sub)
return (NULL);
ft_strlcpy(sub, s + start, len + 1);
return (sub);
}
/* Copies a string */
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
if (size > 0)
{
while (src[i] != '\0' && i < (size - 1))
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
}
return (ft_strlen(src));
}
/* Duplicates a string */
char *ft_strdup(const char *s)
{
size_t len;
char *dup;
len = ft_strlen(s);
dup = (char *)malloc((len + 1) * sizeof(char));
if (!dup)
return (NULL);
ft_strlcpy(dup, s, len + 1);
return (dup);
}