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.
This commit is contained in:
Patrick Pelissier committed 2022-09-10 09:59:06 +02:00
1 parent 0b853c6d52
commit 442b0e7cea
2 files changed
+9 -7

No files matched your search

+8 -6
View File
@@ -114,9 +114,9 @@ typedef uint32_t m_string_unicode_t;
/* Iterator on a string over UTF8 encoded characters */ /* Iterator on a string over UTF8 encoded characters */
typedef struct m_string_it_s { 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 *ptr;
const char *next_ptr;
} m_string_it_t[1]; } m_string_it_t[1];
/* PREFIX: /* PREFIX:
@@ -1695,9 +1695,9 @@ m_string_it(m_string_it_t it, const m_string_t str)
M_STR1NG_CONTRACT(str); M_STR1NG_CONTRACT(str);
M_ASSERT(it != NULL); M_ASSERT(it != NULL);
it->ptr = m_string_get_cstr(str); it->ptr = m_string_get_cstr(str);
it->next_ptr = it->ptr; it->next_ptr = 0;
it->u = 0; it->u = 0;
} }
/* Set the iterator to the end of string /* Set the iterator to the end of string
The iterator references therefore nothing. The iterator references therefore nothing.
@@ -1741,7 +1741,9 @@ m_string_end_p (m_string_it_t it)
str++; str++;
} while (state != M_STR1NG_UTF8_STARTING && state != M_STR1NG_UTF8_ERROR && *str != 0); } while (state != M_STR1NG_UTF8_STARTING && state != M_STR1NG_UTF8_ERROR && *str != 0);
// Save where the current unicode value ends in the UTF8 steam // 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 // Save the decoded unicode value
it->u = M_UNLIKELY (state == M_STR1NG_UTF8_ERROR) ? M_STRING_UNICODE_ERROR : u; it->u = M_UNLIKELY (state == M_STR1NG_UTF8_ERROR) ? M_STRING_UNICODE_ERROR : u;
return false; return false;
@@ -1764,7 +1766,7 @@ m_string_next (m_string_it_t it)
M_ASSERT (it != NULL); M_ASSERT (it != NULL);
// We already decoded the UTF8 stream // We already decoded the UTF8 stream
// Read the decoded value // Read the decoded value
it->ptr = it->next_ptr; it->ptr += it->next_ptr;
} }
/* Return the unicode value associated to the iterator */ /* Return the unicode value associated to the iterator */
+1 -1
View File
@@ -46,7 +46,7 @@ static void test_utf8_basic(void)
assert (b == false); assert (b == false);
assert (it->u == i); assert (it->u == i);
assert (string_get_cref(it) == 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_length(buf) == 1);
assert (m_str1ng_utf8_valid_str_p(buf) == true); assert (m_str1ng_utf8_valid_str_p(buf) == true);
/* Higher level access */ /* Higher level access */