SayoriOS 0.3.3

string.c
1 //
2 // Created by ndraey on 04.10.23.
3 //
4
5 #include "portability.h"
6 #include "lib/stdlib.h"
7 #include "lib/string.h"
8 #include "lib/stdio.h"
9 #include "../include/string.h"
10 #include "io/ports.h"
11
12 string_t* string_new() {
13 string_t* string = calloc(sizeof *string, 1);
14
15 if(!string)
16 return 0;
17
18 string->data = calloc(1, 1);
19 string->length = 0;
20
21 return string;
22 }
23
24 void string_append_charptr(string_t* string, const char* concatenable) {
25 if(!string || !concatenable)
26 return;
27
28 size_t len = strlen(concatenable);
29
30 size_t alloc_len = len + string->length;
31
32 char* memory = (char*)realloc(string->data, alloc_len + 1);
33
34 if(!memory)
35 return;
36
37 memory[alloc_len] = 0;
38
39 string->data = memory;
40
41 if(string->data[0] == 0) {
42 strcpy(string->data, concatenable);
43 } else {
44 strcat(string->data, concatenable);
45 }
46
47 string->length = alloc_len;
48 }
49
50 void string_append(string_t* string, const string_t* concatenable) {
51 if(!string || !concatenable)
52 return;
53
54 string_append_charptr(string, concatenable->data);
55 }
56
57 void string_crop(string_t* string, size_t start, size_t end) {
58 if(!string || start >= end || end > string->length)
59 return;
60
61 size_t new_length = end - start;
62
63 char* data = calloc(new_length + 1, 1);
64
65 if(!data)
66 return;
67
68 data[new_length] = 0;
69
70 memcpy(data, string->data + start, new_length);
71
72 free(string->data);
73
74 string->data = data;
75 string->length = new_length;
76 }
77
78 void string_append_char(string_t* string, char ch) {
79 if(!string)
80 return;
81
82 char* data = realloc(string->data, string->length + 2);
83
84 if(!data)
85 return;
86
87 data[string->length] = ch;
88 data[string->length + 1] = 0;
89
90 string->length++;
91
92 string->data = data;
93 }
94
95 string_t* string_from_charptr(const char* chars) {
96 string_t* string = calloc(sizeof(string_t), 1);
97
98 if(!string) {
99 return 0;
100 }
101
102 qemu_log("charptr to string: %s (%x)", chars, chars);
103
104 string->length = strlen(chars);
105 string->data = calloc(string->length + 1, 1);
106
107 memcpy(string->data, chars, string->length);
108 //string->data[string->length] = 0;
109
110 return string;
111 }
112
113 string_t* string_from_sized_charptr(const char* chars, size_t length) {
114 string_t* string = calloc(sizeof *string, 1);
115
116 if(!string)
117 return 0;
118
119 string->data = malloc(length + 1);
120 string->length = length;
121
122 memcpy(string->data, chars, length);
123 string->data[string->length] = 0;
124
125 return string;
126 }
127
128
129 vector_t* string_split(string_t* string, const char* delimiter) {
130 if(!string || !delimiter)
131 return 0;
132
133 vector_t* vec = vector_new();
134
135 if(!vec)
136 return 0;
137
138 char* curptr = string->data;
139 size_t delim_len = strlen(delimiter);
140
141 char* el = strstr(curptr, delimiter);
142
143 qemu_log("strstr(%s, %x): %x", curptr, delimiter, el);
144
145 if(!el) { // If no occurrences, just add whole string and return vector.
146 string_t* str = string_new();
147
148 string_append_charptr(str, curptr);
149
150 vector_push_back(vec, (size_t)str);
151
152 return vec;
153 }
154
155 while(el) {
156 el = strstr(curptr, delimiter);
157 size_t len = el - curptr;
158
159 if(el == 0)
160 len = strlen(curptr);
161
162 string_t* substring = string_from_sized_charptr(curptr, len);
163
164 vector_push_back(vec, (size_t)substring);
165
166 curptr = el + delim_len;
167 }
168
169 return vec;
170 }
171
172 void string_split_free(vector_t* vec) {
173 if(!vec)
174 return;
175
176 for(size_t i = 0; i < vec->size; i++) {
177 string_destroy((string_t*)vec->data[i]);
178 }
179
180 vector_destroy(vec);
181 }
182
183 string_t* string_clone(string_t* str) {
184 if(!str)
185 return 0;
186
187 string_t* str2 = string_new();
188
189 if(!str2)
190 return 0;
191
192 string_append(str2, str);
193
194 return str2;
195 }
196
197 void string_reverse(string_t *str) {
198 if(!str)
199 return;
200
201 size_t len = str->length;
202
203 for(size_t i = 0; i < len / 2; i++) {
204 char tmp = str->data[i];
205 str->data[i] = str->data[len - i - 1];
206 str->data[len - i - 1] = tmp;
207 }
208 }
209
210 string_t* string_from_integer(int number) {
211 string_t* str = string_new();
212 bool is_negative = false;
213
214 if(!str)
215 return 0;
216
217 if(number == 0) {
218 string_append_char(str, '0');
219
220 return str;
221 }
222
223 if(number < 0) {
224 number = -number;
225
226 is_negative = true;
227 }
228
229 while(number > 0) {
230 string_append_char(str, (char)((number % 10) + '0'));
231 number /= 10;
232 }
233
234 if(is_negative) {
235 string_append_char(str, '-');
236 }
237
238 string_reverse(str);
239
240 return str;
241 }
242
243 void string_destroy(string_t *string) {
244 if(string->data)
245 free(string->data);
246
247 free(string);
248 }
strlen
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
strcpy
int strcpy(char *dest, const char *src)
Копирование строк
Definition: string.c:282
strcat
char * strcat(char *destination, const char *source)
Объединение строк
Definition: string.c:463
string
Definition: string.h:10
vector
Definition: vector.h:7