Add bxs_filter_visible() and react to color options in bxs_to_output() in 'bxstring' module

This commit is contained in:
Thomas Jensen 2023-05-09 21:09:00 +02:00
parent 241459e5b4
commit cd2ccdaed3
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
5 changed files with 89 additions and 2 deletions

View File

@ -336,7 +336,15 @@ char *bxs_to_output(bxstr_t *pString)
if (pString == NULL) {
return strdup("NULL");
}
return u32_strconv_to_output(pString->memory);
if (color_output_enabled) {
return u32_strconv_to_output(pString->memory);
}
uint32_t *vis = bxs_filter_visible(pString);
char *result = u32_strconv_to_output(vis);
BFREE(vis);
return result;
}
@ -370,6 +378,26 @@ int bxs_is_visible_char(bxstr_t *pString, size_t idx)
uint32_t *bxs_filter_visible(bxstr_t *pString)
{
uint32_t *result = NULL;
if (pString != NULL) {
if (pString->num_chars_invisible == 0) {
result = u32_strdup(pString->memory);
}
else {
result = (uint32_t *) calloc(pString->num_chars_visible + 1, sizeof(uint32_t));
for (size_t i = 0; i < pString->num_chars_visible; i++) {
set_char_at(result, i, pString->memory[pString->visible_char[i]]);
}
set_char_at(result, pString->num_chars_visible, char_nul);
}
}
return result;
}
int bxs_strcmp(bxstr_t *s1, bxstr_t *s2)
{
if (s1 == NULL) {

View File

@ -143,7 +143,7 @@ bxstr_t *bxs_rtrim(bxstr_t *pString);
/**
* Convert the string into boxes' output encoding for proper printing on stdout.
* @param pString the string to convert
* @return the same string in the target (output) encoding
* @return the same string in the target (output) encoding, for which new memory has been allocated
*/
char *bxs_to_output(bxstr_t *pString);
@ -166,6 +166,15 @@ int bxs_is_empty(bxstr_t *pString);
int bxs_is_visible_char(bxstr_t *pString, size_t idx);
/**
* Filter the given string so that only the visible characters are returned.
* @param pString the string to filter (may be NULL, in which case NULL is returned)
* @return a new UTF-32 string which contains only the visible characters from the given string, or NULL when the
* given string was NULL
*/
uint32_t *bxs_filter_visible(bxstr_t *pString);
/**
* Determine whether the given `pString` is a valid string under at least one condition. This will return `false` for
* strings which should really never occur anywhere.

View File

@ -22,6 +22,7 @@
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <unistr.h>
#include <cmocka.h>
#include <string.h>
@ -932,6 +933,49 @@ void test_bxs_is_visible_char(void **state)
void test_bxs_filter_visible(void **state)
{
UNUSED(state);
uint32_t *ustr32 = u32_strconv_from_arg("\x1b[38;5;203mX\x1b[0m \x1b[38;5;203mY\x1b[0m", "ASCII");
assert_non_null(ustr32);
bxstr_t *input = bxs_from_unicode(ustr32);
uint32_t *expected = u32_strconv_from_arg("X Y", "ASCII");
uint32_t *actual = bxs_filter_visible(input);
assert_int_equal(0, u32_strcmp(expected, actual));
actual = bxs_filter_visible(NULL);
assert_null(actual);
BFREE(actual);
BFREE(expected);
BFREE(ustr32);
bxs_free(input);
}
void test_bxs_filter_visible_none(void **state)
{
UNUSED(state);
uint32_t *ustr32 = u32_strconv_from_arg("plain", "ASCII");
assert_non_null(ustr32);
bxstr_t *input = bxs_from_unicode(ustr32);
uint32_t *expected = u32_strconv_from_arg("plain", "ASCII");
uint32_t *actual = bxs_filter_visible(input);
assert_int_equal(0, u32_strcmp(expected, actual));
BFREE(actual);
BFREE(expected);
BFREE(ustr32);
bxs_free(input);
}
void test_bxs_strcmp(void **state)
{
UNUSED(state);

View File

@ -69,6 +69,9 @@ void test_bxs_is_empty_null(void **state);
void test_bxs_is_visible_char(void **state);
void test_bxs_filter_visible(void **state);
void test_bxs_filter_visible_none(void **state);
void test_bxs_strcmp(void **state);
void test_bxs_valid_anywhere_error(void **state);

View File

@ -155,6 +155,9 @@ int main(void)
cmocka_unit_test_setup(test_bxs_to_output, beforeTest),
cmocka_unit_test_setup(test_bxs_is_empty_null, beforeTest),
cmocka_unit_test_setup(test_bxs_is_visible_char, beforeTest),
cmocka_unit_test_setup(test_bxs_filter_visible, beforeTest),
cmocka_unit_test_setup(test_bxs_filter_visible_none, beforeTest),
cmocka_unit_test_setup(test_bxs_strcmp, beforeTest),
cmocka_unit_test_setup(test_bxs_valid_anywhere_error, beforeTest),
cmocka_unit_test_setup(test_bxs_valid_in_filename_error, beforeTest),