-
Notifications
You must be signed in to change notification settings - Fork 3
/
string.c
65 lines (55 loc) · 1.06 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
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include "string.h"
#include "debug.h"
int snprintcat(char *buf, int size, char *fmt, ...)
{
va_list ap;
int len;
int n;
len = strnlen(buf, size);
va_start(ap, fmt);
n = vsnprintf(buf + len, size - len, fmt, ap);
va_end(ap);
return n;
}
char *string_tolower(char *str)
{
char *ptr = str;
while (*ptr != '\0') {
*ptr = tolower(*ptr);
ptr++;
}
return str;
}
int string_find_last(const char *str, int c)
{
int len = strlen(str);
for (int i = len; i >= 0; i--) {
if (str[i] == c) {
return i;
}
}
return -1;
}
void string_truncate(char *str, size_t len, size_t max)
{
size_t n;
if (len <= max || len < 3 || max < 3)
return;
n = max - 3;
strlcpy(str + n, "...", len - n);
}
char *string_trim_whitespace(char *str)
{
char *p, *q;
p = str;
q = p + strlen(str);
while (isspace(*p))
p++;
while (q > p && isspace(*(q - 1)))
q--;
*q = '\0';
return p;
}