maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
ft_split.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_split.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rmikhayl <rmikhayl@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/12/17 17:25:26 by rmikhayl #+# #+# */
9/* Updated: 2023/12/17 17:25:26 by rmikhayl ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14
15/* *************************** ft_split ************************************* */
16/* Splits the input string 's' into an array of substrings using the */
17/* delimiter character 'c'. Returns the array, with a NULL-terminated */
18/* element marking the end. */
19/* */
20/* Edge Cases: */
21/* - If 's' is NULL, returns NULL. */
22/* - If 'c' is '\0', returns an array with 's' as the only element. */
23/* - If 's' does not contain 'c', returns an array with 's' as the only */
24/* element. */
25/* */
26/* Behavior: */
27/* - The function splits 's' into substrings wherever 'c' is encountered. */
28/* - Consecutive 'c' characters result in empty strings in the array. */
29/* - Leading and trailing 'c' characters are ignored. */
30/* ************************************************************************** */
31
32static int count_words(const char *s, char c)
33{
34 int count;
35 int in_word;
36
37 count = 0;
38 in_word = 0;
39 while (*s)
40 {
41 if (*s != c)
42 {
43 if (!in_word)
44 {
45 in_word = 1;
46 count++;
47 }
48 }
49 else
50 in_word = 0;
51 s++;
52 }
53 return (count);
54}
55
56static char *strndup(const char *s, size_t n)
57{
58 char *dup;
59
60 dup = malloc(n + 1);
61 if (!dup)
62 return (NULL);
63 ft_memcpy(dup, s, n);
64 dup[n] = '\0';
65 return (dup);
66}
67
68static void split_into_words(char **result, const char *s, char c)
69{
70 int i;
71 int in_word;
72 const char *start;
73
74 i = 0;
75 in_word = 0;
76 while (*s)
77 {
78 if (*s != c && !in_word)
79 {
80 start = s;
81 in_word = 1;
82 }
83 else if (*s == c && in_word)
84 {
85 result[i++] = strndup(start, s - start);
86 in_word = 0;
87 }
88 s++;
89 }
90 if (in_word)
91 result[i++] = strndup(start, s - start);
92 result[i] = NULL;
93}
94
95char **ft_split(const char *s, char c)
96{
97 char **result;
98 int word_count;
99
100 if (!s)
101 return (NULL);
102 word_count = count_words(s, c);
103 result = malloc(sizeof(char *) * (word_count + 1));
104 if (!result)
105 return (NULL);
106 split_into_words(result, s, c);
107 return (result);
108}
static void split_into_words(char **result, const char *s, char c)
Definition ft_split.c:68
static char * strndup(const char *s, size_t n)
Definition ft_split.c:56
char ** ft_split(const char *s, char c)
Definition ft_split.c:95
static int count_words(const char *s, char c)
Definition ft_split.c:32
void * ft_memcpy(void *dest, const void *src, size_t n)
Definition ft_memcpy.c:22