190 lines
4.3 KiB
C
190 lines
4.3 KiB
C
#include "stringv.h"
|
|
#include <ctype.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
StringView sv_from_cstr(const char *str) {
|
|
if (str == NULL) {
|
|
return SV_NULL;
|
|
}
|
|
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;
|
|
}
|
|
if (sv->text == NULL) {
|
|
return;
|
|
}
|
|
if (sv->length < l) {
|
|
return;
|
|
}
|
|
sv->length = l;
|
|
}
|
|
|
|
void sv_right(StringView *sv, size_t r) {
|
|
if (sv == NULL) {
|
|
return;
|
|
}
|
|
if (sv->text == NULL) {
|
|
return;
|
|
}
|
|
if (sv->length < r) {
|
|
return;
|
|
}
|
|
size_t nlength = sv->length - r;
|
|
sv->text = sv->text + nlength;
|
|
sv->length = r;
|
|
}
|
|
|
|
void sv_ltrim(StringView *sv) {
|
|
if (sv == NULL)
|
|
return;
|
|
if (sv->length == 0)
|
|
return;
|
|
if (sv->text == NULL)
|
|
return;
|
|
|
|
size_t offset = 0;
|
|
while (offset < sv->length && isspace((int)*(sv->text + offset))) {
|
|
offset++;
|
|
}
|
|
|
|
if (offset > sv->length)
|
|
return;
|
|
sv->text += offset;
|
|
sv->length -= offset;
|
|
}
|
|
|
|
void sv_rtrim(StringView *sv) {
|
|
if (sv == NULL)
|
|
return;
|
|
if (sv->length == 0)
|
|
return;
|
|
if (sv->text == NULL)
|
|
return;
|
|
|
|
size_t offset = 0;
|
|
while (offset < sv->length &&
|
|
isspace((int)*(sv->text + sv->length - 1 - offset))) {
|
|
offset++;
|
|
}
|
|
|
|
if (offset > sv->length)
|
|
return;
|
|
sv->length -= offset;
|
|
}
|
|
|
|
void sv_trim(StringView *sv) {
|
|
sv_ltrim(sv);
|
|
sv_rtrim(sv);
|
|
}
|
|
|
|
int sv_index_of(StringView sv, char c) {
|
|
size_t offset = 0;
|
|
while (offset < sv.length && *(sv.text + offset) != c) {
|
|
offset++;
|
|
}
|
|
if (offset > sv.length)
|
|
return -1;
|
|
return offset;
|
|
}
|
|
|
|
StringView sv_chop_by_delim(StringView *sv, char delim) {
|
|
StringView returnsv = SV_NULL;
|
|
int find = sv_index_of(*sv, delim);
|
|
if (!find) {
|
|
return returnsv;
|
|
}
|
|
returnsv.text = sv->text;
|
|
returnsv.length = find;
|
|
|
|
sv->text += find + 1;
|
|
sv->length = sv->length - find;
|
|
|
|
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;
|
|
if (sv2 == NULL)
|
|
return false;
|
|
if (sv1->text == NULL)
|
|
return false;
|
|
if (sv2->text == NULL)
|
|
return false;
|
|
if (sv1->length != sv2->length)
|
|
return false;
|
|
return memcmp(sv1->text, sv2->text, sv1->length) == 0;
|
|
}
|
|
|
|
bool sv_starts_with(StringView *sv, StringView *prefix) {
|
|
if (sv->length < prefix->length)
|
|
return false;
|
|
StringView cut = {.text = sv->text, .length = prefix->length};
|
|
return sv_equals(&cut, prefix);
|
|
}
|
|
bool sv_ends_with(StringView *sv, StringView *suffix) {
|
|
if (sv->length < suffix->length)
|
|
return false;
|
|
StringView cut = {.text = sv->text, .length = sv->length};
|
|
sv_right(&cut, suffix->length);
|
|
return sv_equals(&cut, suffix);
|
|
}
|