-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_utils.c
183 lines (153 loc) · 3.93 KB
/
string_utils.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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_utils.h"
#include "vector.h"
void string_join (
char** pieces
, int len
, const char* delimiter
, char* result
, int maxlen) {
bool first = true;
int totallen = 0;
int addlen;
int i;
for (i = 0; i < len; i++) {
if (pieces[i] != NULL) {
addlen = strlen(pieces[i]) + ((first) ? 0 : strlen(delimiter));
if (totallen + addlen >= maxlen) {
return;
}
if (first) {
strcpy(result, pieces[i]);
first = false;
} else {
strcat(result, delimiter);
strcat(result, pieces[i]);
}
totallen += addlen;
}
}
}
void string_join_english (char** pieces, int len, char* result, int maxlen) {
// First we find the last two elements.
int ultimate = -1;
int penultimate = -1;
int i;
for (i = len - 1; i > -1; i--) {
if (pieces[i] != NULL) {
ultimate = penultimate;
penultimate = i;
}
if (ultimate > -1) {
break;
}
}
char* join_last = NULL;
char* penultimate_preserve = NULL;
char* ultimate_preserve = NULL;
if (ultimate > -1) {
join_last = calloc(
strlen(pieces[penultimate]) + strlen(pieces[ultimate]) + 6
, sizeof(char));
strcpy(join_last, pieces[penultimate]);
strcat(join_last, " and ");
strcat(join_last, pieces[ultimate]);
penultimate_preserve = pieces[penultimate];
ultimate_preserve = pieces[ultimate];
pieces[penultimate] = join_last;
pieces[ultimate] = NULL;
}
string_join(pieces, len, ", ", result, maxlen);
if (join_last != NULL) {
free(join_last);
}
if (ultimate > -1) {
pieces[penultimate] = penultimate_preserve;
pieces[ultimate] = ultimate_preserve;
}
}
int strpos (const char* string, const char* substring) {
// Find the earliest occurrence of substring in string.
if (strlen(string) < strlen(substring)) {
return -1;
}
int start = -1;
int n = 0;
int i;
for (i = 0; i < strlen(string); i++) {
if (substring[n] == '\0') {
return start;
}
if (substring[n] == string[i]) {
if (n == 0) {
start = i;
}
n++;
} else {
n = 0;
start = -1;
}
}
return start;
}
const char* string_chomp (const char* string, const char* substring) {
int space_pos = strpos(string, substring);
int total_len = strlen(string);
if (space_pos > -1) {
return string + space_pos + 1;
} else {
return string + total_len;
}
}
void strsplit_construct_vector (const char* string, void* vector) {
size_t size = strlen(string) + 1;
char* alloc = calloc(size, sizeof(char));
strncpy(alloc, string, size);
vector_add((vector_t*) vector, &alloc);
}
/*
* Split the given string into multiple parts based on a given delimiter. Each
* part of the string is passed to a callback function.
*/
void string_split_cb (
strsplit_cb callback
, void* cb_data
, const char* string
, const char* delimiter) {
char buffer[STRSPLIT_MAX_ELEM_LEN];
int delimiter_len = strlen(delimiter);
int string_len = strlen(string);
int i = 0;
int n;
do {
// Find the first position of the delimiter in the string.
n = strpos(string + i, delimiter);
if (n < 0) {
n = string_len - i + 1;
}
if (n >= STRSPLIT_MAX_ELEM_LEN - 1) {
fprintf(stderr, "Substring element too long.\n");
} else if (n > 0) {
// Copy up to that delimiter into a new string and send it to
// the callback.
strncpy(buffer, string + i, n);
buffer[n] = '\0';
callback(buffer, cb_data);
}
// Advance the current position in the string past the delimiter.
i += n + delimiter_len;
} while (i < string_len);
}
/*
* Split a string and place the results in a vector. Just calls
* string_split_cb with a specific callback.
*/
void string_split (
vector_t* vector
, const char* string
, const char* delimiter) {
string_split_cb(&strsplit_construct_vector, vector, string, delimiter);
}