This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmystring.c
97 lines (77 loc) · 1.96 KB
/
mystring.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
97
// Sun Jul 21 09:34:39 DST 2019
// Copyright Michiel van Wessem
#include <string.h>
// returns a pointer to the last occurance in str1 of any of the characters that are part of str2, or a null pointer if there are no matches
const char* strrpbrk(const char* str1, const char* str2)
{
if (str1 == NULL || str2 == NULL || *str1 == '\0' || *str2 == '\0')
return NULL;
for (size_t i = strlen(str1) - 1;; i--)
{
for (const char* p = str2; *p != '\0'; p++)
if (*p == str1[i])
return &(str1[i]);
if (i == 0)
return NULL;
}
}
// returns the length of the last portion of str1 which consists only of characters that are part of str2
size_t strrspn(const char* str1, const char* str2)
{
if (str1 == NULL || str2 == NULL || *str1 == '\0' || *str2 == '\0')
return 0;
const size_t l1 = strlen(str1);
for (size_t i = l1 - 1;; i--)
{
for (const char* p = str2; *p != str1[i]; p++)
if (*p == '\0')
return l1 - 1 - i;
if (i == 0)
return l1;
}
}
// returns the length of the last portion of str1 which consists only of characters that are not part of str2
size_t strrcspn(const char* str1, const char* str2)
{
if (str1 == NULL || str2 == NULL || *str1 == '\0' || *str2 == '\0')
return 0;
const size_t l1 = strlen(str1);
for (size_t i = l1 - 1;; i--)
{
for (const char* p = str2; *p != '\0'; p++)
if (*p == str1[i])
return l1 - 1 - i;
if (i == 0)
return l1;
}
}
char* strrtok(char* s, const char* delim)
{
static char* save_ptr;
if (s == NULL)
s = save_ptr;
if (*s == '\0')
{
save_ptr = s;
return NULL;
}
char* p = s + strlen(s);
/* Scan trailing delimiters. */
p -= strrspn(s, delim);
if (p == s)
{
save_ptr = s;
return NULL;
}
*p = '\0';
/* Find the start of the token. */
char* start = p - strrcspn(s, delim);
if (start == s)
{
save_ptr = start;
return s;
}
*(start - 1) = '\0';
save_ptr = s;
return start;
}