From 61f8dcdbaeeee2bd1e9b8201584a00c3c1cd002a Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 22 Sep 2021 23:06:29 +0200 Subject: [PATCH] Add string_set_char function --- README.md | 7 ++++++- m-string.h | 9 +++++++++ tests/test-mstring.c | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 18a4887f..bc29c46c 100644 --- a/README.md +++ b/README.md @@ -5030,7 +5030,12 @@ reallocation of the underlying array of char has to be performed. ##### char string\_get\_char(const string\_t v, size\_t index) -Return the byte 'index' of the string 'v'. +Return the byte at position 'index' of the string 'v'. +'index' shall be within the allowed range of bytes of the string. + +##### void string\_set\_char(string\_t v, size\_t index, const char c) + +Set the byte at position 'index' of the string 'v' to 'c'. 'index' shall be within the allowed range of bytes of the string. ##### bool string\_empty\_p(const string\_t v) diff --git a/m-string.h b/m-string.h index 752c688e..bc85021b 100644 --- a/m-string.h +++ b/m-string.h @@ -239,6 +239,15 @@ string_get_char(const string_t v, size_t index) return string_get_cstr(v)[index]; } +/* Set the selected character of the string */ +static inline void +string_set_char(string_t v, size_t index, const char c) +{ + M_STR1NG_CONTRACT (v); + M_ASSERT_INDEX(index, string_size(v)); + m_str1ng_get_cstr(v)[index] = c; +} + /* Test if the string is empty or not */ static inline bool string_empty_p(const string_t v) diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 5c9a6a07..7d08f53b 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -133,6 +133,9 @@ static void test0(void) string_set_str (s1, "Hello, world!"); assert (string_size(s1) == 13); assert (string_get_char(s1, 1) == 'e'); + string_set_char(s1, 1, 'E'); + assert (string_get_char(s1, 1) == 'E'); + string_set_char(s1, 1, 'e'); string_clean(s1); assert (string_size(s1) == 0);