Subversion Repositories Tewi

Rev

Rev 70 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 70 Rev 212
Line 1... Line 1...
1
/* $Id: string.c 70 2024-09-19 09:23:45Z nishi $ */
1
/* $Id: string.c 212 2024-10-02 17:44:55Z nishi $ */
2
 
2
 
3
#include <string.h>
3
#include <string.h>
4
#include <stdlib.h>
4
#include <stdlib.h>
5
#include <stdbool.h>
5
#include <stdbool.h>
6
#include <stdio.h>
6
#include <stdio.h>
7
#include <ctype.h>
7
#include <ctype.h>
8
 
8
 
9
char* cm_strcat(const char* a, const char* b) {
9
char* cm_strcat(const char* a, const char* b) {
-
 
10
	char* str;
10
	if(a == NULL) a = "";
11
	if(a == NULL) a = "";
11
	if(b == NULL) b = "";
12
	if(b == NULL) b = "";
12
	char* str = malloc(strlen(a) + strlen(b) + 1);
13
	str = malloc(strlen(a) + strlen(b) + 1);
13
	memcpy(str, a, strlen(a));
14
	memcpy(str, a, strlen(a));
14
	memcpy(str + strlen(a), b, strlen(b));
15
	memcpy(str + strlen(a), b, strlen(b));
15
	str[strlen(a) + strlen(b)] = 0;
16
	str[strlen(a) + strlen(b)] = 0;
16
	return str;
17
	return str;
17
}
18
}
Line 24... Line 25...
24
}
25
}
25
 
26
 
26
char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
27
char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
27
 
28
 
28
bool cm_endswith(const char* str, const char* end) {
29
bool cm_endswith(const char* str, const char* end) {
29
	if(strlen(str) < strlen(end)) return false;
-
 
30
	int i;
30
	int i;
-
 
31
	if(strlen(str) < strlen(end)) return false;
31
	for(i = strlen(str) - strlen(end); i < strlen(str); i++) {
32
	for(i = strlen(str) - strlen(end); i < strlen(str); i++) {
32
		if(str[i] != end[i - strlen(str) + strlen(end)]) return false;
33
		if(str[i] != end[i - strlen(str) + strlen(end)]) return false;
33
	}
34
	}
34
	return true;
35
	return true;
35
}
36
}
36
 
37
 
37
bool cm_nocase_endswith(const char* str, const char* end) {
38
bool cm_nocase_endswith(const char* str, const char* end) {
38
	if(strlen(str) < strlen(end)) return false;
-
 
39
	int i;
39
	int i;
-
 
40
	if(strlen(str) < strlen(end)) return false;
40
	for(i = strlen(str) - strlen(end); i < strlen(str); i++) {
41
	for(i = strlen(str) - strlen(end); i < strlen(str); i++) {
41
		if(tolower(str[i]) != tolower(end[i - strlen(str) + strlen(end)])) return false;
42
		if(tolower(str[i]) != tolower(end[i - strlen(str) + strlen(end)])) return false;
42
	}
43
	}
43
	return true;
44
	return true;
44
}
45
}
Line 73... Line 74...
73
}
74
}
74
 
75
 
75
char** cm_split(const char* str, const char* by) {
76
char** cm_split(const char* str, const char* by) {
76
	int i;
77
	int i;
77
	char** r = malloc(sizeof(*r));
78
	char** r = malloc(sizeof(*r));
78
	r[0] = NULL;
-
 
79
	char* b = malloc(1);
79
	char* b = malloc(1);
80
	b[0] = 0;
-
 
81
	char cbuf[2];
80
	char cbuf[2];
82
	cbuf[1] = 0;
-
 
83
	bool dq = false;
81
	bool dq = false;
84
	bool sq = false;
82
	bool sq = false;
-
 
83
	r[0] = NULL;
-
 
84
	b[0] = 0;
-
 
85
	cbuf[1] = 0;
85
	for(i = 0;; i++) {
86
	for(i = 0;; i++) {
86
		int j;
87
		int j;
87
		bool has = false;
88
		bool has = false;
88
		for(j = 0; by[j] != 0; j++) {
89
		for(j = 0; by[j] != 0; j++) {
89
			if(by[j] == str[i]) {
90
			if(by[j] == str[i]) {
Line 110... Line 111...
110
			if(str[i] == '"' && !sq) {
111
			if(str[i] == '"' && !sq) {
111
				dq = !dq;
112
				dq = !dq;
112
			} else if(str[i] == '\'' && !dq) {
113
			} else if(str[i] == '\'' && !dq) {
113
				sq = !sq;
114
				sq = !sq;
114
			} else {
115
			} else {
115
				cbuf[0] = str[i];
-
 
116
				char* tmp = b;
116
				char* tmp = b;
-
 
117
				cbuf[0] = str[i];
117
				b = cm_strcat(tmp, cbuf);
118
				b = cm_strcat(tmp, cbuf);
118
				free(tmp);
119
				free(tmp);
119
			}
120
			}
120
		}
121
		}
121
	}
122
	}
122
	free(b);
123
	free(b);
123
	return r;
124
	return r;
124
}
125
}
125
 
126
 
126
bool cm_strcaseequ(const char* a, const char* b) {
127
bool cm_strcaseequ(const char* a, const char* b) {
-
 
128
	int i;
127
	if(a == NULL) return false;
129
	if(a == NULL) return false;
128
	if(b == NULL) return false;
130
	if(b == NULL) return false;
129
	if(strlen(a) != strlen(b)) return false;
131
	if(strlen(a) != strlen(b)) return false;
130
	int i;
-
 
131
	for(i = 0; a[i] != 0; i++) {
132
	for(i = 0; a[i] != 0; i++) {
132
		if(tolower(a[i]) != tolower(b[i])) return false;
133
		if(tolower(a[i]) != tolower(b[i])) return false;
133
	}
134
	}
134
	return true;
135
	return true;
135
}
136
}
Line 152... Line 153...
152
}
153
}
153
 
154
 
154
char* cm_html_escape(const char* str) {
155
char* cm_html_escape(const char* str) {
155
	int i;
156
	int i;
156
	char* result = malloc(1);
157
	char* result = malloc(1);
157
	result[0] = 0;
-
 
158
	char cbuf[2];
158
	char cbuf[2];
-
 
159
	result[0] = 0;
159
	cbuf[1] = 0;
160
	cbuf[1] = 0;
160
	for(i = 0; str[i] != 0; i++) {
161
	for(i = 0; str[i] != 0; i++) {
161
		cbuf[0] = str[i];
162
		cbuf[0] = str[i];
162
		if(str[i] == '&') {
163
		if(str[i] == '&') {
163
			char* tmp = result;
164
			char* tmp = result;
Line 181... Line 182...
181
}
182
}
182
 
183
 
183
char* cm_url_escape(const char* str) {
184
char* cm_url_escape(const char* str) {
184
	int i;
185
	int i;
185
	char* result = malloc(1);
186
	char* result = malloc(1);
186
	result[0] = 0;
-
 
187
	char cbuf[2];
187
	char cbuf[2];
-
 
188
	result[0] = 0;
188
	cbuf[1] = 0;
189
	cbuf[1] = 0;
189
	for(i = 0; str[i] != 0; i++) {
190
	for(i = 0; str[i] != 0; i++) {
190
		cbuf[0] = str[i];
191
		cbuf[0] = str[i];
191
		if('!' <= str[i] && str[i] <= '@' && str[i] != '.' && str[i] != '-' && str[i] != '/' && !('0' <= str[i] && str[i] <= '9')) {
192
		if('!' <= str[i] && str[i] <= '@' && str[i] != '.' && str[i] != '-' && str[i] != '/' && !('0' <= str[i] && str[i] <= '9')) {
192
			char code[4];
193
			char code[4];
193
			sprintf(code, "%%%02X", str[i]);
-
 
194
			char* tmp = result;
194
			char* tmp = result;
-
 
195
			sprintf(code, "%%%02X", str[i]);
195
			result = cm_strcat(tmp, code);
196
			result = cm_strcat(tmp, code);
196
			free(tmp);
197
			free(tmp);
197
		} else {
198
		} else {
198
			char* tmp = result;
199
			char* tmp = result;
199
			result = cm_strcat(tmp, cbuf);
200
			result = cm_strcat(tmp, cbuf);