-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrequal.h
212 lines (202 loc) · 6.32 KB
/
strequal.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// returns the length of str, terminated by term
int lenofstr(char *str, char term) {
int stn = 0;
while (str[stn] != term)
{ stn++; }
return stn;
}
// checks if str1, terminated by term1, is equal to
// str2, terminated by term2
int strequal(char *str1, char term1, char *str2, char term2) {
int sti = 0;
while (str1[sti] != term1 && str2[sti] != term2)
{ sti++; }
if (str1[sti] != term1 || str2[sti] != term2)
{ return 0; }
else { return 1; }
}
// copies str1, terminated by term1, char by char, to str2,
// then writes terminating char term2, and returns num chars copied
int strtostr(char *str1, char term1, char *str2, char term2) {
int sti = 0;
while (str1[sti] != term1)
{ str2[sti] = str1[sti]; sti++; }
str2[sti] = term2;
return sti;
}
// appends end to str and returns new str length
int strappend(char *str, char term, char *end, char eterm) {
int sti = lenofstr(str, term);
int stj = 0;
while (end[stj] != eterm)
{ str[sti + stj] = end[stj]; stj++; }
str[sti + stj] = term;
return sti + stj;
}
// appends two ends to str and returns new str length
int strappendend(char *str, char term, char *end1, char *end2, char eterm) {
strappend(str, term, end1, eterm);
return strappend(str, term, end2, eterm);
}
// appends three ends to str and returns new str length
int strappendendend(char *str, char term, char *end1, char *end2, char *end3, char eterm) {
strappend(str, term, end1, eterm);
strappend(str, term, end2, eterm);
return strappend(str, term, end3, eterm);
}
// finds the first occurrence of the snip delimeter, terminates
// the string before it and returns the string after it
char *strsnip(char *str, char *snip) {
int sniplen = 0;
while (snip[sniplen] != '\0')
{ sniplen++; }
int i = 0;
while (str[i] != '\0') {
int j = 0;
while (str[i - j] == snip[sniplen - j - 1]) {
if (sniplen == j + 1) {
str[i - j] = '\0';
return &str[i + 1];
} else {
j++;
if (i - j < 0)
{ break; }
}
}
i++;
}
return NULL;
}
/*
int strequal(char *str1, char *str2) {
int sti = 0;
while (str1[sti] != '\0' && str2[sti] != '\0') {
if (str1[sti] != str2[sti])
{ return 0; }
sti++;
}
return (str1[sti] == '\0' && str2[sti] == '\0') ? 1 : 0;
}
*/
int strstartswith(char *str, char *strstart) {
int sti = -1;
while (strstart[++sti] != '\0') {
if (str[sti] != strstart[sti])
{ return 0; }
}
return 1;
}
int strendswith(char *str, char *strend) {
int sti = -1;
while (str[++sti] != '\0') { }
int stj = -1;
while (strend[++stj] != '\0') { }
while ((sti >= 0) && (stj >= 0)) {
if (str[sti] != strend[stj])
{ return 0; }
sti--;
stj--;
}
return 1;
}
/*
int strsnap(char *str, char *strsnap) {
int sti = -1;
while (str[++sti] != '\0') { }
int stj = 0;
while (strsnap[stj] != '\0')
{ str[sti] = strsnap[stj]; sti++; stj++; }
str[sti] = '\0';
return 1;
}
*/
// copies snap to the end of str and returns str
char *strsnap(char *str, char *snap) {
int i = 0;
while (str[i] != '\0')
{ i++; }
int j = 0;
while (snap[j] != '\0') {
str[i + j] = snap[j];
j++;
}
str[i + j] = '\0';
return str;
}
// copies an int of type (B)inary, (H)exadecimal,
// (U)nsigned or (S)igned from the beginning of str
int strheadint(char *str, char type) {
int sti = 0;
int stval = 0;
int stneg = 0;
if (type == 'B') { // binary
while (str[sti] == '0' || str[sti] == '1')
{ stval = (stval << 1) | ((str[sti] == '1') ? 1 : 0); sti++; }
return stval;
} else if (type == 'H') { // hexadecimal
while ((str[sti] >= '0' && str[sti] <= '9') ||
(str[sti] >= 'A' && str[sti] <= 'F') ||
(str[sti] >= 'a' && str[sti] <= 'f')) {
if (str[sti] >= '0' && str[sti] <= '9')
{ stval = (stval << 4) | (int)(str[sti] - '0'); }
else if (str[sti] >= 'A' && str[sti] <= 'F')
{ stval = (stval << 4) | (int)(str[sti] - 'A' + 10); }
else if (str[sti] >= 'a' && str[sti] <= 'f')
{ stval = (stval << 4) | (int)(str[sti] - 'a' + 10); }
sti++;
}
return stval;
} else if (type == 'U') { // unsigned integer
while (str[sti] >= '0' && str[sti] <= '9')
{ stval = (stval * 10) + (int)(str[sti] - '0'); sti++; }
return stval;
} else if (type == 'S') { // signed integer
if (str[0] == '-')
{ stneg = 1; str = &str[1]; }
while (str[sti] >= '0' && str[sti] <= '9')
{ stval = (stval * 10) + (int)(str[sti] - '0'); sti++; }
return (stneg == 1) ? (0 - stval) : stval;
}
}
// copies an int of type (B)inary, (H)exadecimal,
// (U)nsigned or (S)igned from the end of str
int strtailint(char *str, char type) {
int stlen = 0;
while (str[stlen] != '\0')
{ stlen++; }
int sti = 1;
int stval = 0;
int stmul = 1;
if (type == 'B') { // binary
while ((stlen - sti >= 0) &&
(str[stlen - sti] == '0' || str[stlen - sti] == '1'))
{ stval = stval | (((str[stlen - sti] == '1') ? 1 : 0) << (sti - 1)); sti++; }
return stval;
} else if (type == 'H') { // hexadecimal
while ((stlen - sti >= 0) &&
((str[stlen - sti] >= '0' && str[stlen - sti] <= '9') ||
(str[stlen - sti] >= 'A' && str[stlen - sti] <= 'F') ||
(str[stlen - sti] >= 'a' && str[stlen - sti] <= 'f'))) {
if (str[stlen - sti] >= '0' && str[stlen - sti] <= '9')
{ stval = stval | (((int)(str[stlen - sti] - '0')) << (4 * (sti - 1))); }
else if (str[stlen - sti] >= 'A' && str[stlen - sti] <= 'F')
{ stval = stval | (((int)(str[stlen - sti] - 'A' + 10)) << (4 * (sti - 1))); }
else if (str[stlen - sti] >= 'a' && str[stlen - sti] <= 'f')
{ stval = stval | (((int)(str[stlen - sti] - 'a' + 10)) << (4 * (sti - 1))); }
sti++;
}
return stval;
} else if (type == 'U') { // unsigned integer
while ((stlen - sti >= 0) &&
(str[stlen - sti] >= '0' && str[stlen - sti] <= '9'))
{ stval = stval + (((int)(str[stlen - sti] - '0')) * stmul); stmul *= 10; sti++; }
return stval;
} else if (type == 'S') { // signed integer
while ((stlen - sti >= 0) &&
(str[stlen - sti] >= '0' && str[stlen - sti] <= '9'))
{ stval = stval + (((int)(str[stlen - sti] - '0')) * stmul); stmul *= 10; sti++; }
if ((stlen - sti >= 0) && (str[stlen - sti] == '-'))
{ return 0 - stval; } else { return stval; }
}
return 0;
}