Merge branch 'master' into discrete-tests

This commit is contained in:
Gregory Lirent 2022-06-04 00:34:41 +03:00
commit 6fc4bcb379
4 changed files with 182 additions and 0 deletions

View File

@ -17,6 +17,12 @@ extern size_t string_to_lower (vtype_string* x) LIBCDSB_nt__ LIBCDSB_nn1__;
extern size_t string_to_upper (vtype_string* x) LIBCDSB_nt__ LIBCDSB_nn1__;
extern size_t string_capitalize(vtype_string* x) LIBCDSB_nt__ LIBCDSB_nn1__;
extern size_t string_reverse (vtype_string* x) LIBCDSB_nt__ LIBCDSB_nn1__;
extern size_t string_align_center(vtype_string* x, size_t padsize, int padchr) LIBCDSB_nt__ LIBCDSB_nn1__;
extern size_t string_align_right (vtype_string* x, size_t padsize, int padchr) LIBCDSB_nt__ LIBCDSB_nn1__;
extern size_t string_align_left (vtype_string* x, size_t padsize, int padchr) LIBCDSB_nt__ LIBCDSB_nn1__;
extern int string_compare_case_insensitive(const vtype_string* s0, const vtype_string* s1) LIBCDSB_cmpattr__;

142
src/string/extra-align.c Normal file
View File

@ -0,0 +1,142 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
/*#####################################################################################################################*/
static char* string_info(const vtype_string* s, size_t* size, size_t* nmemb) {
char* p;
char* v;
if (is_null(s->buffer) || !*s->buffer) {
*size = *nmemb = 0;
return s->buffer;
}
*size = *nmemb = strasciilen(s->buffer);
p = s->buffer + *nmemb;
if (!*p) return p;
while (*(v = next_char(p))) {
++*size;
*nmemb += v - p;
p = v;
}
++*size;
*nmemb += v - p;
return p;
}
static int fetch_pad(char buffer[4], int chr) {
char* p;
if (chr) {
p = tochar_unicode(buffer, chr);
chr = !is_null(p) ? p - buffer : 0;
}
if (!chr) {
*buffer = ' ';
chr = 1;
}
return chr;
}
/*#####################################################################################################################*/
size_t string_align_center(vtype_string* x, size_t n, int pc) {
char s[4];
size_t l;
size_t ls;
size_t rs;
char* p;
string_info(x, &ls, &l);
if (ls < n) {
pc = fetch_pad(s, pc);
ls = n - ls;
rs = ls / 2;
} else return ls;
x->buffer = p = realloc(x->buffer, l + ls*pc + 1);
memmove(x->buffer + (ls -= rs)*pc, x->buffer, l);
for (size_t i = 0; i < ls; ++i) {
p = memcpy(p, s, pc) + pc;
}
p += l;
for (size_t i = 0; i < rs; ++i) {
p = memcpy(p, s, pc) + pc;
}
*p = 0;
return n;
}
size_t string_align_right(vtype_string* x, size_t n, int pc) {
char s[4];
size_t l;
size_t ls;
char* p;
string_info(x, &ls, &l);
if (ls < n) {
pc = fetch_pad(s, pc);
ls = n - ls;
} else return ls;
x->buffer = p = realloc(x->buffer, ++l + ls*pc);
memmove(x->buffer + ls*pc, x->buffer, l);
for (size_t i = 0; i < ls; ++i) {
p = memcpy(p, s, pc) + pc;
}
return n;
}
size_t string_align_left(vtype_string* x, size_t n, int pc) {
char s[4];
size_t l;
size_t rs;
char* p;
string_info(x, &rs, &l);
if (rs < n) {
pc = fetch_pad(s, pc);
rs = n - rs;
} else return rs;
x->buffer = realloc(x->buffer, l + rs*pc + 1);
p = x->buffer + l;
for (size_t i = 0; i < rs; ++i) {
p = memcpy(p, s, pc) + pc;
}
*p = 0;
return n;
}

View File

@ -175,3 +175,35 @@ size_t string_capitalize(str_t* x) {
return n;
}
/*#####################################################################################################################*/
size_t string_reverse(str_t* x) {
char *t, *p, *v;
size_t n;
if (is_null(x->buffer) || !*x->buffer)
return 0;
n = strlen(x->buffer);
t = malloc(n + 1);
p = t + n;
v = x->buffer;
n = 0;
while (p > t) {
int cs = charsize(v);
if (cs > 1) {
p = memcpy(p - cs, v, cs);
v += cs;
} else *(--p) = *(v++);
}
free(x->buffer);
x->buffer = t;
return n;
}

View File

@ -24,6 +24,7 @@ size_t string_replace_cstring_cstring(str_t* x, const char* a, const char* d, si
if (x->buffer == a) {
x->buffer = realloc(x->buffer, dn + 1);
memcpy(x->buffer, d, dn);
x->buffer[dn] = 0;
return 1;
}
@ -68,6 +69,7 @@ size_t string_replace_cstring_char(str_t* x, const char* a, int dc, size_t maxn)
if (x->buffer == a) {
x->buffer = realloc(x->buffer, dn + 1);
memcpy(x->buffer, d, dn);
x->buffer[dn] = 0;
return 1;
} else if (x->buffer < a && a < x->buffer + n) {