This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_strtok.cpp
87 lines (63 loc) · 1.7 KB
/
m_strtok.cpp
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
#include <cstring>
char* m_strtok (char* str, const char* skp, unsigned int nom_lex_skip = 0);
int Skip (char** str, const char* skp);
void SkipNon (char** str, const char* skp);
int In (char s, const char* skp);
char* m_strtok (char* str, const char* skp, unsigned int nom_elem_skip) {
static char* prev_ptr = NULL;
if (str != NULL)
prev_ptr = str;
if (str == NULL && prev_ptr == NULL)
return NULL;
static unsigned int n_skip_now = 0;
n_skip_now += Skip(&prev_ptr, skp);
char* begin = prev_ptr;
SkipNon(&prev_ptr, skp);
if (*prev_ptr == '\0') {
prev_ptr = NULL;
if (*begin != '\0')
return begin;
else
return NULL;
}
char* end = prev_ptr;
*end = '\0';
prev_ptr++;
if (nom_elem_skip > 0) {
if (n_skip_now < nom_elem_skip) {
n_skip_now++;
begin = m_strtok(prev_ptr, skp, nom_elem_skip);
}
if (n_skip_now >= nom_elem_skip)
n_skip_now = 0;
if (n_skip_now > nom_elem_skip)
return NULL;
}
return begin;
}
int Skip (char** str, const char* skp) {
if (str == NULL)
return 0;
int i = 0;
for (i = 0; In(*((*str) + i), skp); i++)
if (*((*str) + i) == '\0')
break;
*str += i;
return i;
}
void SkipNon (char** str, const char* skp) {
if (str == NULL)
return;
int i = 0;
for (i = 0; !In(*((*str) + i), skp); i++)
if (*((*str) + i) == '\0')
break;
*str += i;
}
int In (char s, const char* skp) {
unsigned l_skp = strlen (skp);
for (unsigned i = 0; i < l_skp; i++)
if (s == skp[i])
return 1;
return 0;
}