-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbstr.h
85 lines (79 loc) · 1.23 KB
/
bstr.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
/*
bstrcpy and bstrcat are like their C library counterparts strcpy and strcat
but return a pointer to the null-terminator instead of one to the
destination.
This allows for faster chaining of concatenation because the search for
the null-terminator is avoided.
sample:
char buf[];
char *end;
end = bstrcpy(buf, "hello");
end = bstrcat(end, "world");
bstrcat(end, ".\n");
*/
static char *bstrcpy(char *dst, const char *src)
{
while(*src) {
*dst++ = *src++;
}
*dst = 0;
return dst;
}
static char *bstrcat(char *dst, const char *src)
{
while(*dst) {
dst++;
}
while(*src) {
*dst++ = *src++;
}
*dst = 0;
return dst;
}
/*
Returns pointer to last occurence of c in s
or NULL if it was not found
*/
static char *findlast(const char *s, char c)
{
char *p = (char *)s;
while(*p) {
p++;
}
p--;
while(p != s) {
if(*p == c) {
return p;
}
p--;
}
return NULL;
}
/*
Checks if s ends with c
*/
static int endswith(const char *s, char c)
{
while(*s) {
s++;
}
s--;
return *s == c;
}
/*
stou is like atoi but unsigned
*/
static unsigned stou(const char *s)
{
char *p = s;
unsigned r = 0;
while(*p) {
if(*p < '0' || *p > '9') {
return 0;
}
r *= 10;
r += *p - '0';
p++;
}
return r;
}