Add u32_insert_space_at() to 'unicode' module

This commit is contained in:
Thomas Jensen 2023-06-13 22:13:24 +02:00
parent 6c40928ed0
commit aef8c56eed
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
5 changed files with 55 additions and 1 deletions

View File

@ -387,4 +387,23 @@ uint32_t *u32_strnrstr(const uint32_t *s1, const uint32_t *s2, const size_t s2_l
} }
void u32_insert_space_at(uint32_t **s, const size_t idx, const size_t n)
{
if (s == NULL || n == 0) {
return;
}
size_t len = u32_strlen(*s);
size_t x = idx;
if (idx > len) {
x = len;
}
*s = (uint32_t *) realloc(*s, (len + 1 + n) * sizeof(uint32_t));
u32_move(*s + x + n, *s + x, len - x);
u32_set(*s + x, char_space, n);
}
/* vim: set cindent sw=4: */ /* vim: set cindent sw=4: */

View File

@ -248,6 +248,15 @@ uint32_t *u32_nspaces(const size_t n);
uint32_t *u32_strnrstr(const uint32_t *s1, const uint32_t *s2, const size_t s2_len, int skip); uint32_t *u32_strnrstr(const uint32_t *s1, const uint32_t *s2, const size_t s2_len, int skip);
/**
* Insert `n` spaces at position `idx` into `s`. This modifies the given string.
* @param s the string to modify
* @param idx the position at which to insert. The character previously at this position will move to the right.
* @param n the number of spaces to insert
*/
void u32_insert_space_at(uint32_t **s, const size_t idx, const size_t n);
#endif #endif
/* vim: set cindent sw=4: */ /* vim: set cindent sw=4: */

View File

@ -116,7 +116,8 @@ int main(void)
cmocka_unit_test(test_is_allowed_in_shape), cmocka_unit_test(test_is_allowed_in_shape),
cmocka_unit_test(test_is_allowed_in_filename), cmocka_unit_test(test_is_allowed_in_filename),
cmocka_unit_test(test_is_allowed_in_kv_string), cmocka_unit_test(test_is_allowed_in_kv_string),
cmocka_unit_test(test_u32_strnrstr) cmocka_unit_test(test_u32_strnrstr),
cmocka_unit_test(test_u32_insert_space_at)
}; };
const struct CMUnitTest bxstring_tests[] = { const struct CMUnitTest bxstring_tests[] = {

View File

@ -26,6 +26,7 @@
#include <cmocka.h> #include <cmocka.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistr.h>
#include "boxes.h" #include "boxes.h"
#include "tools.h" #include "tools.h"
@ -183,4 +184,26 @@ void test_u32_strnrstr(void **state)
} }
void test_u32_insert_space_at(void **state)
{
UNUSED(state);
uint32_t *expected = u32_strconv_from_arg("x xxx ", "ASCII");
assert_non_null(expected);
uint32_t *s = u32_strconv_from_arg("xxxx", "ASCII");
assert_non_null(s);
u32_insert_space_at(NULL, 2, 3);
u32_insert_space_at(&s, 3, 0);
u32_insert_space_at(&s, 1, 1);
u32_insert_space_at(&s, 10000, 2);
assert_int_equal(0, u32_strcmp(expected, s));
BFREE(s);
BFREE(expected);
}
/* vim: set cindent sw=4: */ /* vim: set cindent sw=4: */

View File

@ -28,6 +28,8 @@ void test_is_allowed_in_shape(void **state);
void test_is_allowed_in_filename(void **state); void test_is_allowed_in_filename(void **state);
void test_is_allowed_in_kv_string(void **state); void test_is_allowed_in_kv_string(void **state);
void test_u32_strnrstr(void **state); void test_u32_strnrstr(void **state);
void test_u32_insert_space_at(void **state);
#endif #endif