-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.cpp
60 lines (45 loc) · 1.13 KB
/
str.cpp
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
#include "_shared.h"
#include "str.h"
#include "conversions.h"
int str::length(char* chars) {
int i = 0;
while (1) {
if (chars[i] == '\0') break;
i++;
}
return i;
}
char* str::join_some(char delim, char* a, char* b, char* c) {
bool use_delim = delim != '\0';
int a_size = length(a);
int b_size = length(b);
int c_size = length(c);
int alloc_size = 1 + a_size + b_size + use_delim;
if (c_size != 0) alloc_size += c_size + use_delim;
char* text = (char*)_shared_malloc(alloc_size);
int text_i = 0;
for (int i = 0; i < a_size; i++) {
text[text_i] = a[i];
text_i++;
}
if (use_delim) {
text[text_i] = delim;
text_i++;
}
for (int i = 0; i < b_size; i++) {
text[text_i] = b[i];
text_i++;
}
if (c_size != 0) {
if (use_delim) {
text[text_i] = delim;
text_i++;
}
for (int i = 0; i < c_size; i++) {
text[text_i] = c[i];
text_i++;
}
}
_shared_free(text);
return text;
}