-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_inspect.c
More file actions
147 lines (124 loc) · 3.06 KB
/
str_inspect.c
File metadata and controls
147 lines (124 loc) · 3.06 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <assert.h>
#include "strings.h"
/**
* _strlen - counts number of characters in a string upto
* the terminating null byte.
* @s: pointer to the string.
*
* Return: length of the string.
*/
intmax_t _strlen(const char *const s)
{
intmax_t i;
if (!s)
return (0);
for (i = 0; s[i]; ++i)
;
return (i);
}
/**
* _strncmp - compares the first `len` characters of two strings.
* @s1: pointer to the first string.
* @s2: pointer to the second string.
* @len: number of characters to compare, cannot be less than 0.
*
* Return: 0 if strings are equal,
* the difference of the first differing characters otherwise.
*/
short int _strncmp(const char *s1, const char *s2, intmax_t len)
{
if (!s1 || !s2 || len < 1)
return (0);
while (len > 0 && !(*s1 - *s2))
{
++s1;
++s2;
--len;
}
return (*s1 - *s2);
}
/**
* _strstr - searches for a substring in a given string.
* @haystack: the string.
* @needle: the substring to search for.
*
* Return: a `view_string` pointing to the start of substring if found,
* otherwise the `view_string` points to NULL.
*/
view_string _strstr(const char *const haystack, const char *const needle)
{
view_string s = {0};
intmax_t n_i = 0, h_i = 0;
if (!haystack || !needle)
return (s);
for (h_i = 0; haystack[h_i]; ++h_i)
{
for (n_i = 0; needle[n_i] && needle[n_i] == haystack[h_i]; n_i++)
;
if (needle[n_i] == '\0')
{
s.s = &haystack[h_i];
s.size = n_i;
return (s);
}
}
return (s);
}
#define _STRTOK_FILTER_SIZE 256U
/**
* initialise_filter - initialise a character filter for a given set of
* characters.
* @filter: pointer to the filter to initialise.
* @to_filter: pointer to a string of characters to initialise with.
*/
static void initialise_filter(
unsigned char *const filter, const char *to_filter)
{
assert(filter && to_filter);
_memset(filter, 0, _STRTOK_FILTER_SIZE);
while (*to_filter)
{
filter[(unsigned char)*to_filter] = 1;
++to_filter;
}
}
/**
* _strtok - returns substrings of `str` that do not contain any character
* in `delims`.
* @str: pointer to the string to tokenize.
* @delims: the delimiting characters.
*
* The most recent value of `delims` will be used if NULL is passed in its
* place from the second call onwards.
* If there are no more substrings, a token pointing to NULL will be returned.
* It is an error if `str` is not properly initialised or no delimeters were
* initially provided.
*
* Return: the initial part of the string ending with one of the characters in
* `delims`, NULL token on error.
*/
view_string _strtok(
view_string *const str, const char *const delims)
{
static unsigned char filter[_STRTOK_FILTER_SIZE];
char c = 0;
view_string token = {0};
if (!str)
return (token);
if (delims)
initialise_filter(filter, delims);
c = string_readc(str);
while (c > -1 && filter[(unsigned char)c] == 1)
c = string_readc(str);
if (c < 0)
return (token);
token.s = &str->s[str->i - 1];
while (c > -1 && filter[(unsigned char)c] == 0)
{
++token.size;
c = string_readc(str);
}
if (c > -1)
string_readp(str);
return (token);
}