-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.h
270 lines (254 loc) · 7.82 KB
/
strings.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
#ifndef STRINGS_H
#define STRINGS_H
#include "arrays.h"
#include "arrays_char.h"
#include <string.h>
#include "types.h"
#include <stdarg.h>
// used for min_3
#include "math_helpers.h"
/**String**/
typedef struct {
Array* char_array;
} String;
const char* str_cstr(const String* const);
size_t str_len(const String* const s);
bool str_empty(const String* const s);
char str_at(const String* const s, size_t offset);
void str_add_string(String *s, const char *c_str);
void str_cpy(String* s, const String* const input);
String* str_create(size_t initsize) {
// one more for \0
initsize++;
String* s = NEW(String);
s->char_array = NULL;
s->char_array = array_create(sizeof(char), initsize);
array_push_char(s->char_array, '\0');
return s;
}
String* str_from_cstr(const char* chars) {
String* s = str_create(strlen(chars));
str_add_string(s, chars);
return s;
}
String* str_clone(const String* const s) {
String* new_str = str_create(s->char_array->length);
str_cpy(new_str, s);
return new_str;
}
//PRIVATE!!!
// opens string for writing by removing the leading \0
// this function is not reentrant
void str_open(String* s) {
if(str_at(s, str_len(s)) != '\0')
log_warn("Warning, given string ends with %c instead of \\0", str_at(s, str_len(s)));
else {
// remove the trailing \0
array_pop_back(s->char_array);
}
}
void str_close(String* s) {
if(str_at(s, str_len(s)) == '\0')
log_warn("Warning, given string %s already ends with \\0", str_cstr(s));
else {
// remove the trailing \0
array_push_char(s->char_array, '\0');
}
}
/**
* @brief str_reserve reserves memory given by size. This can be used
* to prevent multiple realloc calls in loops etc.
* @param s string
* @param size new size. Note that this does not increase length of the string,
* just reserved the memory size
*/
void str_reserve(String* s, size_t size) {
// +1 for \0
array_reserve(s->char_array, size+1);
}
void str_add_char(String* s, const char ch) {
str_open(s);
// insert char
array_push_char(s->char_array, ch);
// add trailing '\0'
str_close(s);
}
void str_add_string(String* s, const char* c_str) {
size_t len = strlen(c_str);
str_reserve(s, len+s->char_array->length);
// remove the trailing \0
array_pop_back(s->char_array);
while((*c_str)!='\0') {
array_push_char(s->char_array, *c_str);
++c_str;
}
array_push_char(s->char_array, '\0');
}
void str_add_string_class(String* s, const String* const str) {
str_add_string(s, str_cstr(str));
}
void str_sort(String* s, int(*comparator)(const void*, const void*, size_t)) {
str_open(s);
array_sort(s->char_array, comparator);
str_close(s);
}
/** Set char at position **/
void str_set_char(String* s, const char chr, size_t pos) {
if(pos >= s->char_array->length-1) {
exit(22);
}
array_set(s->char_array, pos, &chr);
}
/** Copies string `input` to string `s`**/
void str_cpy(String* s, const String* const input) {
s->char_array->length = 0;
str_reserve(s, str_len(input));
char chr;
AR_FOREACH(chr, input->char_array, char) {
array_push_char(s->char_array, chr);
}
}
/** Retrieves const pointer to contents */
const char* str_cstr(const String* const s) {
return (const char*)s->char_array->array;
}
size_t str_len(const String* const s) {
return s->char_array->length-1;
}
bool str_is_empty(const String* const s) {
return s->char_array->length<2;
}
char str_at(const String* const s, size_t offset) {
return array_get_char(s->char_array, offset);
}
char str_last_char(const String* const s) {
return str_at(s, str_len(s)-1);
}
/**
* Returns POINTER to character at given offset. ONLY FOR IMMEDIATE USE!!!
*/
char* str_at_ref(const String* const s, size_t offset) {
if(offset<str_len(s))
return ((char*)s->char_array->array)+offset;
else {
chyba("Tried to access offset %zd in a string %zd characters long.", 113, offset, str_len(s));
}
}
int str_index_of_char(const String* const s, const char character) {
for(size_t i=0, l=str_len(s); i<l; ++i) {
if(str_at(s, i) == character) {
return i;
}
}
return -1;
}
bool str_contains_char(const String* const s, const char character) {
return str_index_of_char(s, character)>=0;
}
/** compares two strings. example for "a" and "b" this function returns 1 abs('a'-'b')
output is sum of absolute values of each char's differences.
if lengths differ, every extra character adds +127 to diff.
**/
int str_cmp(const String* const s1, const String* const s2) {
const size_t s1_len = str_len(s1);
const size_t s2_len = str_len(s2);
const size_t max_len = s1_len>s2_len?s1_len:s2_len;
const char* s1_str = str_cstr(s1);
const char* s2_str = str_cstr(s2);
int sum = 0;
for(size_t i=0; i<max_len; ++i) {
// we're out of s1's size already
if(i>s1_len)
sum+=127;
else if(i>s2_len)
sum+=127;
else {
char diff = s1_str[i]-s2_str[i];;
sum+=diff>=0?diff: -1*diff;
}
}
return sum;
}
int str_levenshtein_dist(const String* const s1, const String* const s2) {
const size_t s1_len = str_len(s1);
const size_t s2_len = str_len(s2);
const char* s1_str = str_cstr(s1);
const char* s2_str = str_cstr(s2);
int (*matrix)[s1_len+1][s2_len+1] = malloc(sizeof(float[s1_len+1][s2_len+1]));
// int** const matrix = (int**)calloc(sizeof(int*), s1_len+1);
// memset((void*)matrix, 0, sizeof(matrix));
// for(size_t i=0; i<=s1_len; ++i) {
// matrix[i] = (int*)calloc(sizeof(int), s1_len+2);
// }
memset(matrix, 0, sizeof(int[s1_len+1][s2_len+1]));
for(size_t i=0; i<=s1_len; ++i) {
(*matrix)[i][0] = (int)i;
}
for(size_t i=0; i<=s2_len; ++i) {
(*matrix)[0][i] = (int)i;
}
for(size_t j=1; j<=s2_len; ++j) {
for(size_t i=1; i<=s1_len; ++i) {
if( s1_str[i-1]==s2_str[j-1] ) {
(*matrix)[i][j] = (int)(*matrix)[i-1][j-1];
}
else {
(*matrix)[i][j] = (int)min_3(
(*matrix)[i-1][j ] + 1,
(*matrix)[i ][j-1] + 1,
(*matrix)[i-1][j-1] + 1
);
}
}
}
#ifdef HOME
// printf("Matrix:\n");
// for(size_t j=0; j<=s2_len; ++j) {
// for(size_t i=0; i<=s1_len; ++i) {
// printf("%3d ", (*matrix)[i][j]);
// }
// printf("\n\n");
// }
// printf("----------------\n");
#endif
// if( matrix[s1_len][s2_len]!=levenshtein(s1_str, s2_str) ) {
// chyba_debug("Levenshtein output does not match: %d vs %d", 112, matrix[s1_len][s2_len], levenshtein(s1_str, s2_str));
// }
int result = (*matrix)[s1_len][s2_len];
// for(size_t i=0; i<=s1_len; ++i) {
// free(matrix[i]);
// }
free(matrix);
return result;
}
void str_clear(String* s) {
array_clear(s->char_array);
str_add_char(s, '\0');
}
/**
* @brief str_destroy
* @param s
* @return NULL for convenience
*/
String* str_destroy(String* s) {
array_destroy(s->char_array);
free(s);
return NULL;
}
/** Varargs method to create matrix. Mess up the dimensions and you're fucked! **/
String* str_destroy_va( String* first, ... ) {
//log_info("Making %d by %d matrix, max indexes [%d, %d].", rows, cols, rows-1, cols-1);
va_list arguments;
/* Initializing arguments to store all values after num */
va_start ( arguments, first );
/* we still rely on the function caller to tell us how
* many there are */
str_destroy(first);
String* pointer = NULL;
while( (pointer = va_arg ( arguments, String* ))!=NULL ) {
str_destroy(pointer);
}
va_end ( arguments ); // Cleans up the list
return NULL;
}
#endif // STRINGS_H