1 | #ifndef MAD_STR_H |
---|
2 | #define MAD_STR_H |
---|
3 | |
---|
4 | // types |
---|
5 | |
---|
6 | struct int_array; |
---|
7 | struct char_array; |
---|
8 | struct char_p_array; |
---|
9 | |
---|
10 | // interface |
---|
11 | |
---|
12 | void mycpy(char* sout, const char* sin); |
---|
13 | char* mystrchr(char* string, char c); |
---|
14 | void mystrcpy(struct char_array* target, char* source); |
---|
15 | char* mystrstr(char* string, char* s); |
---|
16 | void myrepl(char* in, char* out, char* string_in, char* string_out); |
---|
17 | int mysplit(char* buf, struct char_p_array* list); |
---|
18 | |
---|
19 | void conv_char(const char* string, struct int_array* tint); |
---|
20 | void stolower_nq(char* s); |
---|
21 | char* strip(char* name); |
---|
22 | int supp_lt(char* inbuf, int flag); |
---|
23 | void supp_mul_char(char c, char* string); |
---|
24 | char* supp_tb(char* string); /* suppress trailing blanks in string */ |
---|
25 | int zero_string(char* string); /* returns 1 if string defaults to '0', else 0 */ |
---|
26 | char* buffer(char* string); /* obsolete, replaced by permbuff */ |
---|
27 | char* permbuff(char* string); /* copy string to permanent buffer */ |
---|
28 | char* tmpbuff(const char* string); /* copy string to temporary allocated buffer */ |
---|
29 | // int compare_no_case(char* string_1, char* string_2); // replaced by string_icmp |
---|
30 | int is_token(char* pb, char* string, int slen); |
---|
31 | char* join(char** it_list, int n); |
---|
32 | char* join_b(char** it_list, int n); |
---|
33 | char next_non_blank(char* string); |
---|
34 | int next_non_blank_pos(char* string); |
---|
35 | char* noquote(char* string); |
---|
36 | int quote_level(char* string, char* send); |
---|
37 | int square_to_colon(char* string); |
---|
38 | |
---|
39 | // inline functions |
---|
40 | |
---|
41 | #include <ctype.h> |
---|
42 | |
---|
43 | static inline int |
---|
44 | str_pos(const char s[], char c) |
---|
45 | { |
---|
46 | int i; |
---|
47 | for (i = 0; s[i]; i++) |
---|
48 | if (s[i] == c) return i; |
---|
49 | return -1; |
---|
50 | } |
---|
51 | |
---|
52 | static inline int |
---|
53 | char_cnt(char c, const char* s) |
---|
54 | /* returns number of occurrences of character c in string */ |
---|
55 | { |
---|
56 | int i, k = 0; |
---|
57 | for (i = 0; s[i]; i++) |
---|
58 | if(s[i] == c) k++; |
---|
59 | return k; |
---|
60 | } |
---|
61 | |
---|
62 | static inline int |
---|
63 | next_char(char c, char** toks, int start, int nitem) |
---|
64 | /* returns the number of the token starting with c after token start */ |
---|
65 | { |
---|
66 | int i; |
---|
67 | for (i = start; i < nitem; i++) |
---|
68 | if(*toks[i] == c) return i; |
---|
69 | return -1; |
---|
70 | } |
---|
71 | |
---|
72 | static inline void |
---|
73 | replace(char* buf, char in, char out) |
---|
74 | /* replaces character in by character out in string buf */ |
---|
75 | { |
---|
76 | int j; |
---|
77 | for (j = 0; buf[j]; j++) |
---|
78 | if (buf[j] == in) buf[j] = out; |
---|
79 | } |
---|
80 | |
---|
81 | static inline char* |
---|
82 | stolower(char* s) /* converts string to lower in place */ |
---|
83 | { |
---|
84 | int j; |
---|
85 | for (j = 0; s[j]; j++) { |
---|
86 | unsigned char c = s[j]; |
---|
87 | s[j] = tolower(c); |
---|
88 | } |
---|
89 | return s; |
---|
90 | } |
---|
91 | |
---|
92 | static inline char* |
---|
93 | stoupper(char* s) /* converts string to upper in place */ |
---|
94 | { |
---|
95 | int j; |
---|
96 | for (j = 0; s[j]; j++) { |
---|
97 | unsigned char c = s[j]; |
---|
98 | s[j] = toupper(c); |
---|
99 | } |
---|
100 | return s; |
---|
101 | } |
---|
102 | |
---|
103 | static inline int |
---|
104 | string_icmp(const char* s1, const char *s2) /* case insitive string compare */ |
---|
105 | { |
---|
106 | int j; |
---|
107 | for (j = 0; s1[j] && s2[j]; j++) { |
---|
108 | unsigned char c1 = s1[j], c2 = s2[j]; |
---|
109 | int ic1 = tolower(c1), ic2 = tolower(c2); |
---|
110 | if (ic1 != ic2) return ic2-ic1; |
---|
111 | } |
---|
112 | return s2[j]-s1[j]; |
---|
113 | } |
---|
114 | |
---|
115 | static inline int |
---|
116 | string_cnt(char c, int n, char* toks[]) |
---|
117 | /* returns number of strings in toks starting with character c */ |
---|
118 | { |
---|
119 | int i, k = 0; |
---|
120 | for (i = 0; i < n; i++) |
---|
121 | if(*toks[i] == c) k++; |
---|
122 | return k; |
---|
123 | } |
---|
124 | |
---|
125 | static inline void |
---|
126 | supp_char(char c, char* s) |
---|
127 | /* suppresses character c in string */ |
---|
128 | { |
---|
129 | char *p; |
---|
130 | for (p = s; *s; s++) |
---|
131 | if (*s != c) *p++ = *s; |
---|
132 | *p = '\0'; |
---|
133 | } |
---|
134 | |
---|
135 | static inline int |
---|
136 | all_blank(char* s) |
---|
137 | { |
---|
138 | int i; |
---|
139 | for (i = 0; s[i]; i++) |
---|
140 | if(s[i] != ' ') return 0; |
---|
141 | return 1; |
---|
142 | } |
---|
143 | |
---|
144 | #endif // MAD_STR_H |
---|
145 | |
---|