mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-23 15:56:59 +08:00
fix(dropdown): lv_dropdown_get_option_index shouldn't match strings of different lengths (#4191)
This commit is contained in:
@@ -404,11 +404,13 @@ int32_t lv_dropdown_get_option_index(lv_obj_t * obj, const char * option)
|
||||
uint32_t char_i = 0;
|
||||
uint32_t opt_i = 0;
|
||||
const char * start = opts;
|
||||
const size_t option_len = lv_strlen(option); /*avoid recomputing this multiple times in the loop*/
|
||||
|
||||
while(start[0] != '\0') {
|
||||
for(char_i = 0; (start[char_i] != '\n') && (start[char_i] != '\0'); char_i++);
|
||||
|
||||
if(memcmp(start, option, LV_MIN(lv_strlen(option), char_i)) == 0) return opt_i;
|
||||
if(option_len == char_i &&
|
||||
memcmp(start, option, option_len) == 0) return opt_i; /*cannot match exactly unless they are the same length*/
|
||||
start = &start[char_i];
|
||||
if(start[0] == '\n') start++;
|
||||
opt_i++;
|
||||
|
||||
@@ -441,5 +441,17 @@ void test_dropdown_should_list_on_top(void)
|
||||
TEST_ASSERT_EQUAL_INT(2, lv_obj_get_index(list));
|
||||
}
|
||||
|
||||
/* See #4191 */
|
||||
void test_dropdown_get_options_should_check_lengths(void)
|
||||
{
|
||||
lv_obj_t * dd = lv_dropdown_create(lv_scr_act());
|
||||
lv_dropdown_set_options(dd, "abc\nab");
|
||||
TEST_ASSERT_EQUAL_INT(1, lv_dropdown_get_option_index(dd, "ab"));
|
||||
|
||||
lv_dropdown_set_options(dd, "Option 1\nOption 2\nOption 3\nOption");
|
||||
TEST_ASSERT_EQUAL_INT(3, lv_dropdown_get_option_index(dd, "Option"));
|
||||
TEST_ASSERT_EQUAL_INT(-1, lv_dropdown_get_option_index(dd, "Option "));
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user