minishell
ft_string.h File Reference
#include <stddef.h>
#include <stdbool.h>
Include dependency graph for ft_string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

size_t ft_strlen (const char *s)
 
size_t ft_strlcpy (char *dst, const char *src, size_t dstsize)
 
size_t ft_strlcat (char *dst, const char *src, size_t dstsize)
 
char * ft_strchr (const char *s, int c)
 
char * ft_strrchr (const char *s, int c)
 
int ft_strncmp (const char *s1, const char *s2, size_t n)
 
char * ft_strnstr (const char *haystack, const char *needle, size_t len)
 
int ft_atoi (const char *str)
 
bool ft_atoi_strict (const char *str, const char **endptr, int *dst)
 
char * ft_strdup (const char *s1)
 
char * ft_substr (char const *s, unsigned int start, size_t len)
 
char * ft_strjoin (char const *s1, char const *s2)
 
char * ft_strtrim (char const *s1, char const *set)
 
char ** ft_split (char const *s, char c)
 
char * ft_itoa (int n)
 
char * ft_strmapi (char const *s, char(*f)(unsigned int, char))
 
void ft_striteri (char *s, void(*f)(unsigned int, char *))
 
long ft_strtol (const char *str, char **endptr, int base)
 
char * ft_strndup (const char *s1, size_t n)
 
size_t ft_strnlen (const char *str, size_t max_len)
 
int get_numstr_base (char *buf, size_t num, int base, bool is_upper)
 

Function Documentation

◆ ft_atoi()

int ft_atoi ( const char *  str)

Definition at line 16 of file ft_atoi.c.

17 {
18  return ((int)ft_strtol(str, NULL, 10));
19 }
long ft_strtol(const char *str, char **endptr, int base)
Definition: ft_strtol.c:69
Here is the call graph for this function:

◆ ft_atoi_strict()

bool ft_atoi_strict ( const char *  str,
const char **  endptr,
int *  dst 
)

Definition at line 32 of file ft_atoi_strict.c.

33 {
34  int sign;
35 
36  if (str == NULL || dst == NULL)
37  return (false);
38  if (*endptr != NULL)
39  *endptr = str;
40  sign = 1;
41  if (*str == '-')
42  sign = -1;
43  if (*str == '-' || *str == '+')
44  str++;
45  if (*endptr != NULL)
46  *endptr = str;
47  if (!ft_isdigit(*str))
48  return (false);
49  *dst = 0;
50  while (set_value(*str, dst, sign))
51  str++;
52  if (*endptr != NULL)
53  *endptr = str;
54  return (true);
55 }
static bool set_value(char c, int *value, int sign)
int ft_isdigit(int c)
Definition: ft_isdigit.c:15
Here is the call graph for this function:

◆ ft_itoa()

char* ft_itoa ( int  n)

Definition at line 48 of file ft_itoa.c.

49 {
50  char num_str_buf[NUM_STR_BUF_LEN];
51  int buf_pos;
52  long long_n;
53 
54  if (n == 0)
55  return (ft_strdup("0"));
56  buf_pos = 0;
57  long_n = ft_absl(n);
58  while (long_n != 0)
59  {
60  num_str_buf[buf_pos++] = (long_n % 10) + '0';
61  long_n /= 10;
62  }
63  if (n < 0)
64  num_str_buf[buf_pos++] = '-';
65  num_str_buf[buf_pos] = '\0';
66  return (reverse_str(ft_strdup(num_str_buf)));
67 }
static long ft_absl(long v)
Definition: ft_itoa.c:40
static char * reverse_str(char *str)
Definition: ft_itoa.c:18
#define NUM_STR_BUF_LEN
Definition: ft_itoa.c:16
char * ft_strdup(const char *s1)
Definition: ft_strdup.c:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_split()

char** ft_split ( char const *  s,
char  c 
)

Definition at line 51 of file ft_split.c.

52 {
53  char **p_p_ret;
54  size_t p_p_ret_index;
55  const char *p_top_to_dup;
56 
57  if (s == NULL)
58  return (ft_calloc(1, sizeof(void *)));
59  while (*s != '\0' && *s == c)
60  s++;
61  p_p_ret = ft_calloc(count_char(s, c) + 2, sizeof(void *));
62  p_p_ret_index = 0;
63  while (p_p_ret != NULL && *s != '\0')
64  {
65  p_top_to_dup = s;
66  while (*s != '\0' && *s != c)
67  s++;
68  p_p_ret[p_p_ret_index]
69  = ft_substr(p_top_to_dup, 0, (size_t)s - (size_t)p_top_to_dup);
70  if (check_and_handle_alloc_error(p_p_ret, p_p_ret_index++))
71  return (NULL);
72  while (*s != '\0' && *s == c)
73  s++;
74  }
75  return (p_p_ret);
76 }
void * ft_calloc(size_t count, size_t size)
Definition: ft_calloc.c:29
static int check_and_handle_alloc_error(char **p_p_ret, size_t p_p_ret_index)
Definition: ft_split.c:38
static size_t count_char(const char *s, char c)
Definition: ft_split.c:17
char * ft_substr(char const *s, unsigned int start, size_t len)
Definition: ft_substr.c:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_strchr()

char* ft_strchr ( const char *  s,
int  c 
)

Definition at line 15 of file ft_strchr.c.

16 {
17  while (*s != (char)c && *s != '\0')
18  s++;
19  if (*s == (char)c)
20  return ((char *)s);
21  else
22  return (NULL);
23 }
Here is the caller graph for this function:

◆ ft_strdup()

char* ft_strdup ( const char *  s1)

Definition at line 16 of file ft_strdup.c.

17 {
18  size_t s1_len;
19  char *p_ret;
20  char *p_ret_top;
21 
22  s1_len = ft_strlen(s1);
23  p_ret = (char *)malloc(s1_len + 1);
24  p_ret_top = p_ret;
25  if (p_ret == NULL)
26  return (NULL);
27  while (*s1 != '\0')
28  *p_ret++ = *s1++;
29  *p_ret = '\0';
30  return (p_ret_top);
31 }
size_t ft_strlen(const char *s)
Definition: ft_strlen.c:15
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_striteri()

void ft_striteri ( char *  s,
void(*)(unsigned int, char *)  f 
)

Definition at line 16 of file ft_striteri.c.

17 {
18  size_t index;
19 
20  if (s == NULL || f == NULL)
21  return ;
22  index = 0;
23  while (*s != '\0')
24  f(index++, s++);
25 }

◆ ft_strjoin()

char* ft_strjoin ( char const *  s1,
char const *  s2 
)

Definition at line 17 of file ft_strjoin.c.

18 {
19  size_t s1_len;
20  size_t s2_len;
21  char *p_ret;
22 
23  if (s1 == NULL)
24  s1_len = 0;
25  else
26  s1_len = ft_strlen(s1);
27  if (s2 == NULL)
28  s2_len = 0;
29  else
30  s2_len = ft_strlen(s2);
31  p_ret = malloc(s1_len + s2_len + 1);
32  if (p_ret == NULL)
33  return (NULL);
34  ft_memmove(p_ret, s1, s1_len);
35  ft_memmove(p_ret + s1_len, s2, s2_len);
36  p_ret[s1_len + s2_len] = '\0';
37  return (p_ret);
38 }
void * ft_memmove(void *dst, const void *src, size_t n)
Definition: ft_memmove.c:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_strlcat()

size_t ft_strlcat ( char *  dst,
const char *  src,
size_t  dstsize 
)

Definition at line 15 of file ft_strlcat.c.

16 {
17  size_t src_len;
18  size_t dest_len;
19 
20  dest_len = ft_strnlen(dest, size);
21  src_len = ft_strlen(src);
22  if (dest_len >= size)
23  return (size + src_len);
24  size -= dest_len;
25  dest += dest_len;
26  while (*src != '\0' && --size > 0)
27  *(dest++) = *(src++);
28  *dest = '\0';
29  return (src_len + dest_len);
30 }
size_t ft_strnlen(const char *str, size_t max_len)
Definition: ft_strnlen.c:16
Here is the call graph for this function:

◆ ft_strlcpy()

size_t ft_strlcpy ( char *  dst,
const char *  src,
size_t  dstsize 
)

Definition at line 15 of file ft_strlcpy.c.

16 {
17  size_t src_size;
18 
19  src_size = ft_strlen(src);
20  if (size == 0)
21  return (src_size);
22  if ((src_size + 1) < size)
23  size = src_size + 1;
24  while (--size > 0)
25  *(dest++) = *(src++);
26  *dest = '\0';
27  return (src_size);
28 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_strlen()

size_t ft_strlen ( const char *  s)

Definition at line 15 of file ft_strlen.c.

16 {
17  size_t len;
18 
19  len = 0;
20  while (*str != '\0')
21  {
22  str++;
23  len++;
24  }
25  return (len);
26 }
Here is the caller graph for this function:

◆ ft_strmapi()

char* ft_strmapi ( char const *  s,
char(*)(unsigned int, char)  f 
)

Definition at line 15 of file ft_strmapi.c.

16 {
17  char *p_ret;
18  size_t index;
19 
20  if (s == NULL || *s == '\0')
21  return (ft_strdup(""));
22  p_ret = ft_strdup(s);
23  if (f == NULL || p_ret == NULL)
24  return (p_ret);
25  index = 0;
26  while (*s != '\0')
27  {
28  p_ret[index] = f(index, *s++);
29  index++;
30  }
31  return (p_ret);
32 }
Here is the call graph for this function:

◆ ft_strncmp()

int ft_strncmp ( const char *  s1,
const char *  s2,
size_t  n 
)

Definition at line 16 of file ft_strncmp.c.

17 {
18  if (n == 0)
19  return (0);
20  while (--n > 0 && *s1 == *s2 && *s1 != '\0')
21  {
22  s1++;
23  s2++;
24  }
25  return (*((unsigned char *)s1) - *((unsigned char *)s2));
26 }
Here is the caller graph for this function:

◆ ft_strndup()

char* ft_strndup ( const char *  s1,
size_t  n 
)

Definition at line 18 of file ft_strndup.c.

19 {
20  size_t s1_len;
21  char *p_ret;
22  char *p_ret_top;
23 
24  if (n <= 0)
25  return (ft_strdup(""));
26  s1_len = ft_minp(ft_strlen(s1), n);
27  p_ret = (char *)malloc(s1_len + 1);
28  p_ret_top = p_ret;
29  if (p_ret == NULL)
30  return (NULL);
31  while (s1_len-- > 0)
32  *p_ret++ = *s1++;
33  *p_ret = '\0';
34  return (p_ret_top);
35 }
size_t ft_minp(size_t a, size_t b)
Definition: ft_min.c:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_strnlen()

size_t ft_strnlen ( const char *  str,
size_t  max_len 
)

Definition at line 16 of file ft_strnlen.c.

17 {
18  size_t len;
19 
20  len = 0;
21  while (len < max_len && *str != '\0')
22  {
23  str++;
24  len++;
25  }
26  return (len);
27 }
Here is the caller graph for this function:

◆ ft_strnstr()

char* ft_strnstr ( const char *  haystack,
const char *  needle,
size_t  len 
)

Definition at line 15 of file ft_strnstr.c.

16 {
17  size_t pos;
18 
19  if (*needle == '\0')
20  return ((char *)haystack);
21  while (len > 0 && *haystack != '\0')
22  {
23  if (*haystack == *needle)
24  {
25  pos = 0;
26  while (needle[pos] != '\0'
27  && len > pos && haystack[pos] == needle[pos])
28  pos++;
29  if (needle[pos] == '\0')
30  return ((char *)haystack);
31  }
32  haystack++;
33  len--;
34  }
35  return (NULL);
36 }

◆ ft_strrchr()

char* ft_strrchr ( const char *  s,
int  c 
)

Definition at line 15 of file ft_strrchr.c.

16 {
17  const char *p_c_found;
18 
19  p_c_found = NULL;
20  while (*s != '\0')
21  {
22  if (*s == (char)c)
23  p_c_found = s;
24  s++;
25  }
26  if (p_c_found != NULL)
27  return ((char *)p_c_found);
28  else if (c == '\0')
29  return ((char *)s);
30  else
31  return (NULL);
32 }

◆ ft_strtol()

long ft_strtol ( const char *  str,
char **  endptr,
int  base 
)

Definition at line 69 of file ft_strtol.c.

70 {
71  long sign;
72  long value;
73 
74  sign = 1;
75  value = 0;
76  if (endptr != NULL)
77  *endptr = (char *)str;
78  if (!((2 <= base && base <= 36) || base == 0))
79  return (0);
80  while (((9 <= *str && *str <= 13) || *str == ' ') && *str != '\0')
81  str++;
82  if (*str == '-')
83  sign = -1;
84  else if (*str != '+' && !(('a' <= *str && *str <= 'z')
85  || ('A' <= *str && *str <= 'Z') || ('0' <= *str && *str <= '9')))
86  return (0);
87  if (*str == '+' || *str == '-')
88  str++;
89  base = get_base(&str, base);
90  while (set_value(*str, &value, sign, base))
91  str++;
92  if (endptr != NULL)
93  *endptr = (char *)str;
94  return (value);
95 }
static int get_base(const char **p_str, const int base)
Definition: ft_strtol.c:52
static int set_value(char c, long *p_value, int sign, int base)
Definition: ft_strtol.c:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_strtrim()

char* ft_strtrim ( char const *  s1,
char const *  set 
)

Definition at line 16 of file ft_strtrim.c.

17 {
18  size_t s1_len;
19  const char *s1_end;
20 
21  if (s1 == NULL)
22  return (NULL);
23  if (set == NULL || *s1 == '\0')
24  return (ft_strdup(s1));
25  s1_len = ft_strlen(s1);
26  s1_end = s1 + s1_len - 1;
27  while (*s1 != '\0' && ft_strchr(set, *s1) != NULL)
28  s1++;
29  if (*s1 == '\0')
30  return (ft_strdup(""));
31  while (ft_strchr(set, *s1_end) != NULL)
32  s1_end--;
33  return (ft_strndup(s1, (size_t)s1_end - (size_t)s1 + 1));
34 }
char * ft_strchr(const char *s, int c)
Definition: ft_strchr.c:15
char * ft_strndup(const char *s1, size_t n)
Definition: ft_strndup.c:18
Here is the call graph for this function:

◆ ft_substr()

char* ft_substr ( char const *  s,
unsigned int  start,
size_t  len 
)

Definition at line 16 of file ft_substr.c.

17 {
18  if (s == NULL)
19  return (NULL);
20  if (len == 0 || ft_strlen(s) <= start)
21  return (ft_strdup(""));
22  return (ft_strndup(s + start, len));
23 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_numstr_base()

int get_numstr_base ( char *  buf,
size_t  num,
int  base,
bool  is_upper 
)

Definition at line 17 of file get_numstr_base.c.

18 {
19  int tmp;
20  int last_len;
21  char c;
22 
23  if (num == 0)
24  return (0);
25  last_len = get_numstr_base(buf, num / base, base, is_upper);
26  tmp = num % base;
27  if (tmp < 10)
28  c = '0' + tmp;
29  else if (is_upper)
30  c = 'A' + tmp - 10;
31  else
32  c = 'a' + tmp - 10;
33  buf[last_len] = c;
34  return (last_len + 1);
35 }
int get_numstr_base(char *buf, size_t num, int base, bool is_upper)
Here is the call graph for this function:
Here is the caller graph for this function: