-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
118 lines (112 loc) · 1.93 KB
/
string.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
#include <type.h>
void clear(void *buf,u32 len)
{
char *tmp=buf;
for (u32 i=0;i<len;i++)
tmp[i]=0;
return;
}
u32 strlen(char *a)
{
int i=0;
while (*a++)
i++;
return i;
}
int strcmp(char *a,char *b)
{
char *p=a,*q=b;
int ret=0;
while(*p && *q)
{
if (*p<*q)
{
ret=-1;
goto end;
}
else if (*p>*q)
{
ret=1;
goto end;
}
else
{
p++;
q++;
}
}
if (p-a<q-b)
ret=-1;
else if (p-a==q-b)
ret=0;
else
ret=1;
end:
return ret;
}
u32 strequ(char *a,char *b)
{
char *p=a,*q=b;
while (*p && *q)
{
if (*p++ != *q++)
break;
}
return (*p==*q) ? 1 : 0 ;
}
char *strstr(char * haystack, char * needle){
int haystack_len = strlen(haystack);
int ret=0;
int haystack_pos=0,needle_pos=0;
int needle_len = strlen(needle);
int move[27];
if (needle_len > haystack_len)
{
return -1;
}
if (needle_len == 0 )
{
return 0;
}
for (int i=0;i<26;i++)
{
move[i]=-1;
}
for (int i=needle_len-1;i>=0;i--)
{
if (move[needle[i]-'a']==-1)
{
move[needle[i]-'a']=needle_len-i;
}
}
for (int i=0;i<26;i++)
if (move[i]==-1)
move[i]=needle_len+1;
while (haystack_pos < haystack_len && haystack_pos + needle_len-1 < haystack_len)
{
needle_pos = 0;
while (needle_pos<needle_len && haystack_pos + needle_len-1 < haystack_len)
{
if (needle[needle_pos]!=haystack[haystack_pos+needle_pos])
{
if (haystack_pos+needle_len < haystack_len)
{
haystack_pos += move[haystack[haystack_pos+needle_len]-'a'];
break;
}
else goto end;
}
else
{
needle_pos++;
}
}
if (needle_pos==needle_len)
{
break;
}
}
end:
ret = (needle_pos == needle_len) ? haystack_pos : -1;
return (ret == -1) ? (char *)0 : haystack_pos+ret;
}