Branch data Line data Source code
1 : : #include "mem_internal.h"
2 : :
3 : : /**
4 : : * @brief Check whether one logical element is fully zero-valued
5 : : *
6 : : * The string helpers in libmem treat a terminator as one whole logical
7 : : * element whose every byte is zero. This helper centralizes that rule so
8 : : * callers that work with arbitrary element widths can share one fast check
9 : : * instead of repeating byte loops in multiple files
10 : : *
11 : : * @param element_view Pointer to the first byte of the logical element
12 : : * @param single_element_size Size of one logical element in bytes
13 : : * @return `true` when every byte in the element is zero; `false` otherwise
14 : : */
15 : 94190 : bool mem_is_zero_element(
16 : : const unsigned char *const element_view,
17 : : const size_t single_element_size)
18 : : {
19 [ + - - + ]: 94190 : if(element_view == NULL || single_element_size == 0)
20 : : {
21 : 0 : return(false);
22 : : }
23 : :
24 [ + + ]: 94190 : if(single_element_size == sizeof(char))
25 : : {
26 : : /* Fast path for one-byte elements: one zero byte already means one zero element */
27 [ + + ]: 94127 : if(element_view[0] == 0)
28 : : {
29 : 69628 : return(true);
30 : : }
31 : :
32 : 24499 : return(false);
33 : : }
34 : :
35 [ + + ]: 181 : for(size_t byte_index = 0; byte_index < single_element_size; ++byte_index)
36 : : {
37 [ + + ]: 156 : if(element_view[byte_index] != 0)
38 : : {
39 : 38 : return(false);
40 : : }
41 : : }
42 : :
43 : 25 : return(true);
44 : : }
|