-
Notifications
You must be signed in to change notification settings - Fork 1
/
String.c
executable file
·200 lines (151 loc) · 3.83 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
/* String.c */
#include "String.h"
String * newString(int size)
{
String * string = (String *) malloc(sizeof(String));
string->chars = (char *) malloc((size + 1) * sizeof(char));
string->length = size;
string->chars[size] = EOS;
return string;
}
String * constructString(char * charArray, int size)
{
int i, c = 0;
for (i = 0; i < size; i++) {
if (charArray[i] == EOS || charArray[i] == EOL) {
break;
}
c++;
}
String * string = newString(c);
for (i = 0; i < c; i++) {
string->chars[i] = charArray[i];
}
return string;
}
char * asCharArray(String * source)
{
char * char_array = copyCharArray(source->chars, source->length);
return char_array;
}
char * copyCharArray(char * source, int size)
{
int i;
char * result = (char *) malloc((size + 1) * sizeof(char));
for (i = 0; i < size; i++) {
result[i] = source[i];
}
result[i] = EOS;
return result;
}
String ** explode(List * separators, String * string)
{
bool isSeparator;
int begin = 0, quantity = 0, i, j;
Node * separator;
String ** strings;
// Fase de contagem
for (i = 0; i < string->length; i++) {
char c = string->chars[i];
isSeparator = false;
separator = separators->head;
while (separator != NULL) {
if (c == (char) separator->value) {
isSeparator = true;
break;
}
separator = separator->next;
}
if (isSeparator) {
if (i > begin) {
quantity++;
begin = i + 1;
}
}
}
// Fase de alocação
strings = (String **) malloc(quantity * sizeof(String *));
// Fase de criação
for (i = 0, j = 0; i < string->length; i++) {
char c = string->chars[i];
isSeparator = false;
separator = separators->head;
while (separator != NULL) {
if (c == separator->value) {
isSeparator = true;
break;
}
separator = separator->next;
}
if (isSeparator) {
if (i > begin) {
strings[j] = substringFromString(string, begin, i);
begin = i + 1;
}
}
}
return strings;
}
void implode(String * separator, String ** strings)
{}
String * concatStrings(String * string1, String * string2)
{
int i, j;
String * string = newString(string1->length + string2->length);
for (i = 0, j = 0; i < string1->length; i++, j++) {
string->chars[j] = string1->chars[i];
}
for (i = 0; i < string1->length; i++, j++) {
string->chars[j] = string2->chars[i];
}
return string;
}
char charAt(String * string, int position)
{
if (!(string->length > position)) {
return EOS;
}
return string->chars[position];
}
int stringLength(char * charArray)
{
int c = 0;
while (charArray[c++] != EOS) {}
return c;
}
String * substringFromString(String * source, int begin, int end)
{
int i, j;
int size = (end - begin) + 1;
if (!(size > 0)) {
return NULL;
}
if (!(end <= source->length)) {
return NULL;
}
String * string = newString(size);
for (i = 0, j = begin; j < end; i++, j++) {
string->chars[i] = source->chars[j];
}
return string;
}
String * substringFromCharArray(char * source, int begin, int end)
{
int i, j;
int size = (end - begin) + 1;
if (!(size > 0)) {
return NULL;
}
if (!(end < stringLength(source))) {
return NULL;
}
String * string = newString(size);
for (i = 0, j = begin; j < end; i++, j++) {
string->chars[i] = source[j];
}
return string;
}
void printString(String * string)
{
printf("%s\n", string->chars);
}