From 442b0e7ceadffa6ff380d6c7d5a89af3f09bd66a Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 10 Sep 2022 09:59:06 +0200 Subject: [PATCH] Reduce size of struct string_it_t on 64 bits by storing an offset to the next character instead of a pointer. An offset can be stored as a BYTE instead of a PTR since UT-8 can use at most 4 characters per code point. --- m-string.h | 14 ++++++++------ tests/test-mstring.c | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/m-string.h b/m-string.h index 083b5f6f..c453e6d4 100644 --- a/m-string.h +++ b/m-string.h @@ -114,9 +114,9 @@ typedef uint32_t m_string_unicode_t; /* Iterator on a string over UTF8 encoded characters */ typedef struct m_string_it_s { - m_string_unicode_t u; + m_string_unicode_t u; // Decoded Unicode character for the iterator + int next_ptr; // Offset to the next character const char *ptr; - const char *next_ptr; } m_string_it_t[1]; /* PREFIX: @@ -1695,9 +1695,9 @@ m_string_it(m_string_it_t it, const m_string_t str) M_STR1NG_CONTRACT(str); M_ASSERT(it != NULL); it->ptr = m_string_get_cstr(str); - it->next_ptr = it->ptr; + it->next_ptr = 0; it->u = 0; -} +} /* Set the iterator to the end of string The iterator references therefore nothing. @@ -1741,7 +1741,9 @@ m_string_end_p (m_string_it_t it) str++; } while (state != M_STR1NG_UTF8_STARTING && state != M_STR1NG_UTF8_ERROR && *str != 0); // Save where the current unicode value ends in the UTF8 steam - it->next_ptr = str; + // UTF-8 is maximum 4 characters + M_ASSERT( (str > it->ptr) && (str - it->ptr) <= 4); + it->next_ptr = (int) (str - it->ptr); // Save the decoded unicode value it->u = M_UNLIKELY (state == M_STR1NG_UTF8_ERROR) ? M_STRING_UNICODE_ERROR : u; return false; @@ -1764,7 +1766,7 @@ m_string_next (m_string_it_t it) M_ASSERT (it != NULL); // We already decoded the UTF8 stream // Read the decoded value - it->ptr = it->next_ptr; + it->ptr += it->next_ptr; } /* Return the unicode value associated to the iterator */ diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 144e3756..ef1504e2 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -46,7 +46,7 @@ static void test_utf8_basic(void) assert (b == false); assert (it->u == i); assert (string_get_cref(it) == i); - assert (*it->next_ptr == 0); + assert (it->ptr[it->next_ptr] == 0); assert (m_str1ng_utf8_length(buf) == 1); assert (m_str1ng_utf8_valid_str_p(buf) == true); /* Higher level access */