-
Notifications
You must be signed in to change notification settings - Fork 0
/
sutils.c
90 lines (72 loc) · 1.47 KB
/
sutils.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
#include "sutils.h"
#include <string.h>
char* skipWS(char* s) {
while ((*s)==' ' || (*s)=='\t') {
s++;
}
return s;
}
void trimEndingChar(char* s, char c) {
char* p=s+strlen(s)-1;
while ( p>=s && ((*p)==c) ) {
p--;
}
(*(++p))=0;
}
void trimEndingWS(char* s) {
char* p=s+strlen(s)-1;
while ( p>=s && ((*p)==' ' || (*p)=='\t') ) {
p--;
}
(*(++p))=0;
}
int strStartsWith(char* s, char* start) {
while ((*start)!=0 && (*s)!=0 && (*start)==(*s)) {
*start++; *s++;
}
return ((*start)==0);
}
int endsWith(char* s, char* end) {
if (strlen(end)>strlen(s)) {
return 0;
} else {
char* sEnd=s+strlen(s)-strlen(end);
return (strcmp(sEnd, end)==0);
}
}
char *itoa(char *buf, int n)
{
int radix = 10;
char *ret = buf;
char tmp[33];
int i = 0, j, r;
/* validate the conversion number base. */
if ((radix >= 2) && (radix <= 36)) {
if ((radix == 10) && (n < 0)) {
/* negative integer value. */
*buf++ = '-';
n = -n;
}
do {
/* calculate the current digit. */
r = (int)((unsigned int)n % radix);
tmp[i++] = ((r < 10) ? (r + '0') : (r - 10 + 'a'));
} while ((n /= radix) != 0);
/* reverse the buffer string. */
for (--i, j = 0; (i >= 0); --i, ++j) buf[j] = tmp[i];
buf[j] = 0;
}
return (ret);
}
void strReplaceChar(char* str, char s, char d) {
while (*str!=0) {
if (*str==s) *str=d;
str++;
}
}
void toUpperCase(char* s) {
while (*s!=0) {
*s=toupper(*s);
s++;
}
}