-
Notifications
You must be signed in to change notification settings - Fork 2
/
str.h
77 lines (62 loc) · 1.49 KB
/
str.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
#ifndef __STR_H
#define __STR_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tools.h"
#include "zmalloc.h"
#define STR_HEAD(s) ((struct strinfo *)((s)-(sizeof(struct strinfo))))
//expand str memeory dynamically,
//if the memory is large than 1M,add 1M every time,
//otherwise * 2 erveytime.
#define MAX_DOUBLE_MEMORY 1024 * 1024
//the struct of str
//cap is the allocated memory
//len is the length of str
//buf is the buf
struct strinfo {
size_t len;
size_t cap;
char buf[];
};
//create a new str with length
char* newstr(const char* init);
//empty a str with length
void cleanstr(char* s);
//free str memory
void freestr(char*s);
//copy function
char* strnumcpy(char* s,const char* n, size_t len);
//init a empty str
char* emptystr();
//init str via len
char* newstrlen(const char* init, size_t len);
char* strnumcat(char* s, const char* n, size_t len);
//parse command to args
char **parseCommand(const char *line, int* argc);
//tests
void parse_test();
void str_test();
//set str length
static inline void setlen(const char* s, int newlen) {
STR_HEAD(s)->len = newlen;
}
//set str length
static inline void setcap(const char* s, int newcap) {
STR_HEAD(s)->cap = newcap;
}
//get str length
static inline size_t getlen(const char* s) {
return STR_HEAD(s)->len;
}
//test asset and print info
static inline void test_cond(const char* s,bool c) {
if(c) {
printf("test %s passed\n",s);
} else {
printf("test %s failed\n",s);
}
}
#endif