-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
163 lines (134 loc) · 2.62 KB
/
str.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
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include "str.h"
#include "mem.h"
char *strchr_rev(const char *s, int ch, const char *start)
{
while(*s != ch){
if(s == start)
return NULL;
s--;
}
return (char *)s; /* *s == ch */
}
char *tim_strrevstr(char *restrict haystack, unsigned off, const char *restrict needle)
{
const size_t nlen = strlen(needle);
for(char *p = haystack + off;
p >= haystack;
p--)
{
if(!strncmp(p, needle, nlen))
return p;
}
return NULL;
}
char *tim_strstr(char *restrict haystack, size_t len, const char *restrict needle)
{
if(!*needle)
return NULL;
/* memstr */
for(size_t h = 0; h < len; h++)
if(haystack[h] == *needle)
/* can assume needle is 0-terminated */
for(size_t n = 0; n < len - h;){
if(haystack[h + n] != needle[n])
break;
n++;
if(!needle[n])
return haystack + h;
}
return NULL;
}
bool isallspace(const char *s, size_t len)
{
for(size_t i = 0; i < len; i++)
if(!isspace(s[i]))
return false;
return true;
}
bool isdigit_or_minus(char c)
{
return isdigit(c) || c == '-';
}
char *skipspace(const char *in)
{
for(; isspace(*in); in++);
return (char *)in;
}
static const struct paren
{
char opp;
bool lead;
} parens[] = {
['['] = { ']', true },
['{'] = { '}', true },
['('] = { ')', true },
['<'] = { '>', true },
[']'] = { '[', false },
['}'] = { '{', false },
[')'] = { '(', false },
['>'] = { '<', false },
};
static const struct paren *paren_lookup(char c)
{
unsigned char u = c;
if(u >= sizeof parens / sizeof *parens)
return &parens[0];
return &parens[u];
}
bool paren_match(char c, char *other)
{
return (*other = paren_lookup(c)->opp);
}
bool paren_left(char c)
{
return paren_lookup(c)->lead;
}
char paren_opposite(char c)
{
return paren_lookup(c)->opp;
}
void str_ltrim(char *s, size_t *pl)
{
size_t i;
for(i = 0; i < *pl && isspace(s[i]); i++);
if(i)
memmove(s, s + i, *pl -= i);
}
void str_rtrim(char *s, size_t *pl)
{
size_t i = *pl;
while(i > 0 && isspace(s[i - 1])){
s[i - 1] = '\0';
i--;
}
*pl = i;
}
char *expand_tilde(const char *in)
{
if(*in != '~')
return NULL;
const char *user = in + 1;
if(isalpha(*user)){
const char *user_end;
for(user_end = user; isalnum(*user_end); user_end++);
/* TODO */
return NULL;
}else{
/* plain ~ */
const char *home = getenv("HOME");
if(!home)
return NULL;
size_t homelen = strlen(home);
size_t userlen = strlen(user);
size_t len = homelen + userlen + 1;
char *ret = umalloc(len);
memcpy(ret, home, homelen);
memcpy(ret + homelen, user, userlen);
ret[homelen + userlen] = '\0';
return ret;
}
}