-
Notifications
You must be signed in to change notification settings - Fork 0
/
chararray.h
354 lines (313 loc) · 8.25 KB
/
chararray.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
*
* Copyright (c) 2011, Jue Ruan <ruanjue@gmail.com>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __STRING_RJ_H
#define __STRING_RJ_H
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"
#include "mem_share.h"
/**
* String
*/
#ifdef HUGE_STRING
typedef long int string_size_t;
#else
typedef int string_size_t;
#endif
typedef struct {
union { char *string; char *buffer; };
string_size_t size;
string_size_t capacity;
} String;
typedef struct {
char *string;
string_size_t size;
} VString;
define_list(VStrv, VString);
static inline String* init_string(string_size_t cap){
String *str;
str = (String*)malloc(sizeof(String));
str->size = 0;
str->capacity = (cap&0x1)? cap + 1 : cap + 2;
str->string = (char*)malloc(sizeof(char) * (str->capacity));
str->string[0] = 0;
return str;
}
static inline size_t string_obj_desc_cnt(void *obj, int idx){
return ((String*)obj)->size + 1;
idx = idx;
}
static const obj_desc_t string_obj_desc = {"string_obj_desc", sizeof(String), 1, {1}, {offsetof(String, string)}, {(obj_desc_t*)&OBJ_DESC_DATA}, string_obj_desc_cnt, NULL};
static inline unsigned int _string_size_roundup_power2(unsigned int v){
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return v + 1;
}
static inline void encap_string(String *str, string_size_t inc){
if(inc + str->size + 1 > str->capacity){
if(inc + str->size + 1 < 0xFFFFFFF){
str->capacity = _string_size_roundup_power2(inc + str->size + 1);
} else {
str->capacity += (((inc + str->size + 1) - str->capacity + 0xFFFFFFF - 1) / 0xFFFFFFF) * 0xFFFFFFF;
}
str->string = (char*)realloc(str->string, str->capacity);
}
}
static inline void recap_string(String *str, string_size_t cap){
if(cap <= str->size) return;
str->string = (char*)realloc(str->string, cap);
str->capacity = cap;
}
static inline void uc_string(String *str){
string_size_t i;
for(i=0;i<str->size;i++){
if(str->string[i] >= 'a' && str->string[i] <= 'z') str->string[i] = str->string[i] + 'A' - 'a';
}
}
static inline void lc_string(String *str){
string_size_t i;
for(i=0;i<str->size;i++){
if(str->string[i] >= 'A' && str->string[i] <= 'Z') str->string[i] = str->string[i] + 'a' - 'A';
}
}
static inline char* substr(char *string, string_size_t len, char *dst){
char *str;
if(len < 0) len = strlen(string);
if(dst != NULL) str = dst;
else str = (char*)malloc(sizeof(char) * (len + 1));
strncpy(str, string, len);
str[len] = '\0';
return str;
}
static inline char* catstr(string_size_t n_str, ...){
char *str, *s;
string_size_t i, len, inc;
va_list params;
len = 0;
str = NULL;
va_start(params, n_str);
for(i=0;i<n_str;i++){
s = va_arg(params, char*);
if(s == NULL) continue;
inc = strlen(s);
str = realloc(str, len + inc + 1);
memcpy(str + len, s, inc + 1);
len += inc;
}
va_end(params);
return str;
}
static inline void chomp_string(String *str){
if(str->size && str->string[str->size - 1] == '\n'){
str->size --;
str->string[str->size] = 0;
}
}
static inline void chomp_vstring(VString *str){
if(str->size && str->string[str->size - 1] == '\n'){
str->size --;
}
}
static inline void trim_string(String *str){
string_size_t i, j;
i = str->size - 1;
while(i >= 0 && (str->string[i] == '\n' || str->string[i] == '\t' || str->string[i] == ' ')) i--;
str->size = i + 1;
i = 0;
while(i < str->size && (str->string[i] == '\n' || str->string[i] == '\t' || str->string[i] == ' ')) i++;
if(i){
for(j=i;j<str->size;j++){ str->string[j-i] = str->string[j]; }
str->size -= i;
}
str->string[str->size] = 0;
}
static inline void trim_vstring(VString *str){
string_size_t i;
i = str->size - 1;
while(i >= 0 && (str->string[i] == '\n' || str->string[i] == '\t' || str->string[i] == ' ')) i--;
str->size = i + 1;
i = 0;
while(i < str->size && (str->string[i] == '\n' || str->string[i] == '\t' || str->string[i] == ' ')) i++;
str->string += i;
}
static inline void append_string(String *str, char *src, string_size_t offlen){
encap_string(str, offlen);
memcpy(str->string + str->size, src, offlen);
str->size += offlen;
str->string[str->size] = 0;
}
static inline void append_char_string(String *str, char c, string_size_t num){
encap_string(str, num);
while(num-- > 0){ str->string[str->size ++] = c; }
str->string[str->size] = 0;
}
static inline String* as_string(char *chs, string_size_t len){
String *str;
str = init_string(len);
memcpy(str->string, chs, len);
str->size = len;
str->string[len] = 0;
return str;
}
static inline VString* as_vstring(char *chs){
string_size_t len;
VString *str;
len = strlen(chs);
str = malloc(sizeof(VString));
str->string = chs;
str->size = len;
return str;
}
static inline void add_char_string(String *str, char ch){
encap_string(str, 1);
str->string[str->size++] = ch;
str->string[str->size] = 0;
}
#define push_string(str, ch) add_char_string(str, ch)
static inline void add_int_string(String *str, long long val){
string_size_t n;
long long v;
encap_string(str, 30);
if(val == 0){
str->string[str->size++] = '0';
} else {
if(val < 0){
val = - val;
str->string[str->size++] = '-';
}
v = val;
for(n=0;v;n++) v /= 10;
str->size += n;
v = val;
while(v){
str->string[--str->size] = '0' + (v % 10);
v /= 10;
}
str->size += n;
}
str->string[str->size] = 0;
}
static inline void clear_string(String *str){ str->size = 0; str->string[0] = 0; }
static inline string_size_t split_string(String *str, char separator, VStrv *vstrs){
VString *vstr;
string_size_t n_tab, i, s;
for(i=s=n_tab=0;i<str->size;i++){
if(str->string[i] == separator){
if(i > s){
str->string[i] = '\0';
vstr = next_ref_VStrv(vstrs);
vstr->string = str->string + s;
n_tab ++;
vstr->size = i - s;
}
s = i + 1;
}
}
if(i > s){
str->string[i] = '\0';
vstr = next_ref_VStrv(vstrs);
vstr->string = str->string + s;
n_tab ++;
vstr->size = i - s;
}
return n_tab;
}
static inline string_size_t split_vstring(VString *str, char separator, VStrv *vstrs, string_size_t cut){
VString *vstr;
string_size_t n_tab, i, s;
for(i=s=n_tab=0;i<str->size;i++){
if(str->string[i] == separator){
if(i > s){
if(cut) str->string[i] = '\0';
vstr = next_ref_VStrv(vstrs);
vstr->string = str->string + s;
n_tab ++;
vstr->size = i - s;
}
s = i + 1;
}
}
if(i > s){
if(cut) str->string[i] = '\0';
vstr = next_ref_VStrv(vstrs);
vstr->string = str->string + s;
n_tab ++;
vstr->size = i - s;
}
return n_tab;
}
static inline void reverse_string(String *str){
string_size_t i, j;
char c;
i = 0;
j = str->size - 1;
while(i < j){
swap_tmp(str->string[i], str->string[j], c);
i ++;
j --;
}
}
static inline void reverse_str(char *str, string_size_t len){
string_size_t i, j;
char c;
i = 0;
j = len - 1;
while(i < j){
swap_tmp(str[i], str[j], c);
i ++;
j --;
}
}
static inline void tidy_string(String *src, String *dst, char ch){
string_size_t i;
encap_string(dst, src->size);
for(i=0;i<src->size;i++){
if(src->string[i] != ch){
dst->string[dst->size ++] = src->string[i];
}
}
dst->string[dst->size] = 0;
}
static inline string_size_t occ_str(char *str, string_size_t len, char c){
string_size_t i, ret;
for(i=ret=0;i<len;i++){
if(str[i] == c) ret ++;
}
return ret;
}
static inline void trunc_string(String *str, string_size_t size){
if(size >= str->size || size < 0) return;
str->size = size;
str->string[size] = 0;
}
static inline String* clone_string(String *str){
String *clone;
clone = init_string(str->size);
append_string(clone, str->string, str->size);
return clone;
}
static inline void free_string(String *str){ free(str->string); free(str); }
static inline void free_vstring(VString *str){ free(str); }
#endif