-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjstr2.c
91 lines (79 loc) · 2.55 KB
/
jstr2.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct String {
uint64_t length; // should always equal strlen(contents)
char* contents; // should always have allocated space of length + 1, including null terminator
};
typedef struct String String;
String new_String(char* init_contents) {
uint64_t size = strlen(init_contents);
char* contents = malloc(size + 1);
strcpy(contents, init_contents);
String r = { size, contents };
return r;
}
// This plus is *just* the heap-allocating version now
String plus(String s1, String s2) {
uint64_t new_size = s1.length + s2.length + 1;
char* new_contents = calloc(new_size, sizeof(char));
strncpy(new_contents, s1.contents, s1.length);
strncpy(new_contents + s1.length, s2.contents, s2.length);
new_contents[new_size - 1] = 0;
String r = { new_size - 1, new_contents };
return r;
}
String join(String strs[], int count, String delimiter) {
String s = new_String("");
for(int i = 0; i < count; i += 1) {
s = plus(s, strs[i]);
if(i < count - 1) {
s = plus(s, delimiter);
}
}
return s;
}
int main() {
String apple = new_String("apple");
String banana = new_String("banana");
String strawberry = new_String("strawberry");
String fruit[] = { apple, banana, strawberry };
String comma = new_String(", ");
String fruitlist = join(fruit, 3, comma);
printf("%s\n", fruitlist.contents);
}
String joinfree(String strs[], int count, String delimiter) {
String s = new_String("");
for(int i = 0; i < count; i += 1) {
char* before_plus = s.contents;
s = plus(s, strs[i]);
free(before_plus);
if(i < count - 1) {
char* before_plus = s.contents;
s = plus(s, delimiter);
free(before_plus);
}
}
return s;
}
String join_nogarbage(String strs[], int count, String delimiter) {
int total_length = 0;
for(int i = 0; i < count; i += 1) {
total_length += strs[i].length;
if(i < count - 1) { total_length += delimiter.length; }
}
char* new_contents = malloc(total_length + 1);
int index = 0;
for(int i = 0; i < count; i += 1) {
strcpy(new_contents + index, strs[i].contents);
if(i < count - 1) {
strcpy(new_contents + index + strs[i].length, delimiter.contents);
}
index += strs[i].length + delimiter.length;
}
new_contents[total_length - 1] = 0;
String r = { total_length, new_contents };
return r;
}