/
sawixy
/
Milk
Обзор
Документация
Войти
/
sawixy
/
Milk
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/string.c
62 строки
1 KB
sawixy
Added http parser/formatter
23 ноя 2025, 13:29
23 ноя 2025, 13:29
e32c48a
Код
Авторство
О чём код?
#include <collections/string.h> int string_utf8_chars(const char* str, int byte_len) { if (str == NULL) return 0; int chars = 0; for (int i = 0; i < byte_len; i++) { if ((str[i] & 0xC0) != 0x80) chars++; } return chars; } string string_new(char* text) { string s; if (text == NULL) { s.len_bytes = 0; s.len_chars = 0; s.data = NULL; return s; } s.len_bytes = strlen(text); s.len_chars = string_utf8_chars(text, s.len_bytes); s.data = malloc(s.len_bytes + 1); if (s.data != NULL) { memcpy(s.data, text, s.len_bytes + 1); } else { s.len_bytes = 0; s.len_chars = 0; } return s; } string string_concat(string s1, string s2) { string result; result.len_bytes = s1.len_bytes + s2.len_bytes; result.len_chars = s1.len_chars + s2.len_chars; result.data = malloc(result.len_bytes + 1); if (result.data != NULL) { memcpy(result.data, s1.data, s1.len_bytes); memcpy(result.data + s1.len_bytes, s2.data, s2.len_bytes + 1); } else { result.len_bytes = 0; result.len_chars = 0; } return result; } void string_free(string* str) { if (str != NULL && str->data != NULL) { free(str->data); str->data = NULL; str->len_bytes = 0; str->len_chars = 0; } } char* string_raw(string* str){ return str->data; }