/
string.c
211 lines (154 loc) · 4.39 KB
/
string.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
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
char *make_string_copy(const char *orig)
{
char *copy;
if (orig==NULL)
return NULL;
copy = calloc(strlen(orig)+1, sizeof(char));
strcpy(copy, orig);
return copy;
}
char *make_string_copy_len(const char *orig, size_t len)
{
char *copy;
if (orig==NULL)
return NULL;
len = MINN(len, strlen(orig));
copy = calloc(len+1, sizeof(char));
strncpy(copy, orig, len);
return copy;
}
char *make_string_copy_between_ptrs(const char *start, const char *end) // copies a string between two points. The char that end points to is excluded
{
return make_string_copy_len(start, end-start);
}
char **make_string_array_copy(const char **orig, const size_t count)
{
size_t i;
char **copy;
copy = calloc(count, sizeof(char *));
if (copy==NULL)
return NULL;
for (i=0; i < count; i++)
copy[i] = make_string_copy(orig[i]);
return copy;
}
void strcpy_then_free(char *dest, char *src)
{
strcpy(dest, src);
free(src);
}
char *replace_char(char *str, char find, char replace) // ASCII replacement of one char with another
{
char *current_pos = strchr(str, find);
while (current_pos)
{
*current_pos = replace;
current_pos = strchr(current_pos+1, find);
}
return str;
}
char *string_tolower(char *str)
{
char *p = str;
if (str==NULL)
return NULL;
while (*p)
{
*p = tolower(*p);
p++;
}
return str;
}
int vstrlenf(const char *format, va_list args)
{
int len;
va_list args_copy;
va_copy(args_copy, args);
len = vsnprintf(NULL, 0, format, args_copy); // gets the printed length without actually printing
va_end(args_copy);
return len;
}
char *vsprintf_realloc(char **string, size_t *alloc_count, const int append, const char *format, va_list args)
{
int len0=0, len1;
size_t zero=0;
char *p=NULL;
if (string==NULL) // if there's no string then we create one
string = &p; // so that ultimately it's p that will be returned
if (alloc_count==NULL) // if alloc_count isn't provided
alloc_count = &zero; // use 0 which will realloc string to an adequate size
len1 = vstrlenf(format, args);
if (string)
if (append && *string)
len0 = strlen(*string);
alloc_enough(string, len0+len1+1, alloc_count, sizeof(char), (*alloc_count)==0 ? 1. : 1.5);
vsnprintf(&(*string)[len0], *alloc_count - len0, format, args);
return *string;
}
char *sprintf_realloc(char **string, size_t *alloc_count, const int append, const char *format, ...) // like sprintf but expands the string alloc if needed
{
va_list args;
char *p=NULL;
if (string==NULL) // if there's no string then we create one
string = &p;
va_start(args, format);
vsprintf_realloc(string, alloc_count, append, format, args);
va_end(args);
return *string;
}
char *vsprintf_alloc(const char *format, va_list args) // like vsprintf but allocates a new string
{
int len;
char *str;
len = vstrlenf(format, args);
str = calloc(len+1, sizeof(char));
vsnprintf(str, len+1, format, args);
return str;
}
char *sprintf_ret(char *str, const char *format, ...) // like sprintf but returns the string
{
va_list args;
va_start(args, format);
vsprintf(str, format, args);
va_end(args);
return str;
}
char **string_array_insert_lines(char **array, int *linecount, char **ins, int ins_count, int ins_pos) // destroys array and makes a new one with lines inserted
{
int i;
buffer_t s={0};
ins_pos = MINN(ins_pos, *linecount);
// Add the first part of the array line by line to s
for (i=0; i < ins_pos; i++)
bufprintf(&s, "%s\n", array[i]);
// Add the lines to insert to s
for (i=0; i < ins_count; i++)
bufprintf(&s, "%s\n", ins[i]);
// Add the rest of the array to s
for (i=ins_pos; i < *linecount; i++)
bufprintf(&s, "%s\n", array[i]);
free_2d(array, 1);
return arrayise_text(s.buf, linecount);
}
char **string_array_insert_line(char **array, int *linecount, char *ins, int ins_pos)
{
return string_array_insert_lines(array, linecount, &ins, 1, ins_pos);
}
char **string_array_replace_line(char **array, int *linecount, char *repl, int repl_pos) // destroys array and makes a new one with line replaced
{
int i;
buffer_t s={0};
// Copy every line except switch the line to replace
for (i=0; i < *linecount; i++)
bufprintf(&s, "%s\n", (i==repl_pos) ? repl : array[i]);
free_2d(array, 1);
return arrayise_text(s.buf, linecount);
}
int find_string_in_string_array(const char *string, const char **array, const int count)
{
int i;
for (i=0; i < count; i++)
if (strcmp(string, array[i]) == 0)
return i;
return -1;
}