-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.c
178 lines (145 loc) · 2.86 KB
/
tools.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tools.h"
#define DEBUG 0
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
char* Substring(char* string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if (pointer == NULL)
{
printf("Unable to allocate memory.\n");
/*exit(EXIT_FAILURE);*/
return NULL;
}
for (c = 0 ; c < position -1 ; c++)
string++;
for (c = 0 ; c < length ; c++)
{
*(pointer+c) = *string;
string++;
}
*(pointer+c) = '\0';
return pointer;
}
/* Detecting whether base is starts with str */
short StartsWith (char* base, char* str)
{
return (strstr(base, str) - base) == 0;
}
/* Detecting whether base is ends with str */
short EndsWith (char* base, char* str)
{
int blen = strlen(base);
int slen = strlen(str);
return (blen >= slen) && (0 == strcmp(base + blen - slen, str));
}
/* getting the first index of str in base */
int indexOf (char* base, char* str)
{
return indexOf_shift(base, str, 0);
}
int indexOf_shift (char* base, char* str, int startIndex)
{
int result;
int baselen = strlen(base);
/* str should not longer than base*/
if (strlen(str) > baselen || startIndex > baselen)
{
result = -1;
}
else
{
if (startIndex < 0 )
{
startIndex = 0;
}
char* pos = strstr(base+startIndex, str);
if (pos == NULL)
{
result = -1;
}
else
{
result = pos - base;
}
}
return result;
}
/** use two index to search in two part to prevent the worst case
* (assume search 'aaa' in 'aaaaaaaa', you cannot skip three char each time)
*/
int lastIndexOf (char* base, char* str)
{
int result;
/* str should not longer than base*/
if (strlen(str) > strlen(base))
{
result = -1;
}
else
{
int start = 0;
int endinit = strlen(base) - strlen(str);
int end = endinit;
int endtmp = endinit;
while(start != end)
{
start = indexOf_shift(base, str, start);
end = indexOf_shift(base, str, end);
/* not found from start */
if (start == -1)
{
end = -1; /* then break; */
}
else if (end == -1)
{
/* found from start
* but not found from end
* move end to middle
*/
if (endtmp == (start+1))
{
end = start; /* then break; */
}
else
{
end = endtmp - (endtmp - start) / 2;
if (end <= start)
{
end = start+1;
}
endtmp = end;
}
}
else
{
/* found from both start and end
* move start to end and
* move end to base - strlen(str)
*/
start = end;
end = endinit;
}
}
result = start;
}
return result;
}
void rand_str(char *dest, size_t length)
{
char charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (length-- > 0)
{
size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
*dest++ = charset[index];
}
*dest = '\0';
}