diff --git a/common/core/desktop/src/kmx/kmx_xstring.cpp b/common/core/desktop/src/kmx/kmx_xstring.cpp index 921cca5af7..615f58ef07 100644 --- a/common/core/desktop/src/kmx/kmx_xstring.cpp +++ b/common/core/desktop/src/kmx/kmx_xstring.cpp @@ -179,10 +179,9 @@ PKMX_WCHAR km::kbp::kmx::decxstr(PKMX_WCHAR p, PKMX_WCHAR pStart) { for (int i = 1; i < 5; i++) { - // *(p - i) == UC_SENTINEL && next CODE_ right to UC_SENTINEL ( looked up in CODE__SIZE+1) has value i - if ((*(p - i) == UC_SENTINEL) && (CODE__SIZE[*(p - i + 1)] + 1 == i)) { + // p right of pstart && *(p - i) == UC_SENTINEL && next CODE_ right of UC_SENTINEL ( looked up in CODE__SIZE+1) has value i + if ((p >= pStart + i) && (*(p - i) == UC_SENTINEL) && (CODE__SIZE[*(p - i + 1)] + 1 == i)) return (p - (CODE__SIZE[*(p - i + 1)] + 1)); - } } } return p; diff --git a/common/core/desktop/tests/unit/kmnkbd/test_kmx_xstring.cpp b/common/core/desktop/tests/unit/kmnkbd/test_kmx_xstring.cpp index 4f088ac1eb..634f0fe351 100644 --- a/common/core/desktop/tests/unit/kmnkbd/test_kmx_xstring.cpp +++ b/common/core/desktop/tests/unit/kmnkbd/test_kmx_xstring.cpp @@ -30,7 +30,102 @@ void test_decxstr() { PKMX_WCHAR p_start; // pointer start of input PKMX_WCHAR p; // pointer end of input PKMX_WCHAR q; // pointer output - + + + // ------------------------------------------------------------------------------------------------------------------------------------------------------- + // more tests: check if we might end up left of pstart + // ------------------------------------------------------------------------------------------------------------------------------------------------------- + + p_start = (PKMX_WCHAR)u"\u0001"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (NULL )); + + p_start = (PKMX_WCHAR)u"\u0001\u0001"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\u0001\u0001\u0001"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0001"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0001\u0001"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 2)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0001\u0001\u0001"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 3)); + + p_start = (PKMX_WCHAR)u"\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (NULL)); + + p_start = (PKMX_WCHAR)u"\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\u0014\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\u0014\u0014\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\u0014\u0014\u0014\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p-2 )); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0014\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0014\u0014\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 5)); + + p_start = (PKMX_WCHAR)u"\uFFFF\u0014\u0014\u0014\u0014\u0014\u0014"; + p = find_ptr_to_last_character(p_start); + q = decxstr(p, p_start); + assert(q == (p - 1)); + // ------------------------------------------------------------------------------------------------------------------------------------------------------- // -- differences in pointer movement for new decxstr ---------------------------------------------------------------------------------------------------- // ----- OLD version of decxstr: 0x0A not used => return ( p - 1) , 0x0C not used => return ( p - 1) , 0x0E = 1 => return ( p - 3) @@ -1218,7 +1313,7 @@ void test_decxstr() { p = (PKMX_WCHAR)u"\uFFFF\u0004\u0062\U0001F609"; q = incxstr(p); assert(q == p + 2); -} + } constexpr const auto help_str = "\ test_kmx_xstring [--color]\n\ @@ -1240,4 +1335,4 @@ int main(int argc, char *argv []) { test_decxstr(); return 0; -} \ No newline at end of file +}