string view ahora soporta caracteres anchos y chop by type con una funcion por parametro

This commit is contained in:
2026-11-15 18:45:26 -03:00
parent d8903b05de
commit fb4017ecb1
4 changed files with 239 additions and 16 deletions
+52 -14
View File
@@ -5,20 +5,6 @@
#include <stdio.h>
#include <string.h>
/*
* @brief Create a string view from a const char* and set their length
*
* @details This string view provides a view of a string. It does not allocate
* memory, therefore, it lives only while the source is alive. The
* length is a bytes number of the string. It does not represents the number of
* characters.
*
* @param str pointer from the original string. It can be NULL.
*
* @return a StringView with the view of original string with corresponding
* length.
*/
StringView sv_from_cstr(const char *str) {
if (str == NULL) {
return SV_NULL;
@@ -26,6 +12,38 @@ StringView sv_from_cstr(const char *str) {
return (StringView){.text = (char *)str, .length = strlen(str)};
}
size_t sv_chars_count(StringView *sv) {
if (sv == NULL)
return 0;
if (sv->text == NULL)
return 0;
size_t count = 0, i = 0;
while (i < sv->length) {
count++;
// Consulto si es un caracter ancho.
char cbyte = *(sv->text + i) & SV_START_WIDE_CHAR;
if (cbyte == (char)SV_START_WIDE_CHAR) {
// Consulto cuantos bytes tiene el caracter ancho.
char cbyte2 = *(sv->text + i) & SV_BYTES_2_WIDE_CHAR;
char cbyte3 = *(sv->text + i) & SV_BYTES_3_WIDE_CHAR;
char cbyte4 = *(sv->text + i) & SV_BYTES_4_WIDE_CHAR;
if (cbyte4 == (char)SV_BYTES_4_WIDE_CHAR) {
i += 4;
continue;
} else if (cbyte3 == (char)SV_BYTES_3_WIDE_CHAR) {
i += 3;
continue;
} else if (cbyte2 == (char)SV_BYTES_2_WIDE_CHAR) {
i += 2;
continue;
}
}
i++;
}
return count;
}
void sv_left(StringView *sv, size_t l) {
if (sv == NULL) {
return;
@@ -122,6 +140,26 @@ StringView sv_chop_by_delim(StringView *sv, char delim) {
return returnsv;
}
StringView sv_chop_by_type(StringView *sv, int (*checktype)(int)) {
StringView returnsv = SV_NULL;
size_t find = 0;
while (find < sv->length && checktype(*(sv->text + find))) {
find++;
}
if (find > sv->length) {
return returnsv;
}
returnsv.text = sv->text;
returnsv.length = find;
sv->text += find + 1;
sv->length = sv->length - find;
return returnsv;
}
bool sv_equals(StringView *sv1, StringView *sv2) {
if (sv1 == NULL)
return false;