From b3261b8dc49746eb2fedd785e20c92c7bf2d7ebf Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Thu, 18 Jan 2024 09:26:58 +0700 Subject: [PATCH 1/2] refactor(core): move context helpers to a new module Fixes #10423. --- core/src/context.hpp | 5 ++ core/src/context_helpers.cpp | 58 +++++++++++++++++++ .../km_core_state_context_set_if_needed.cpp | 47 --------------- core/src/meson.build | 1 + .../unit/kmnkbd/test_actions_normalize.cpp | 6 +- 5 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 core/src/context_helpers.cpp diff --git a/core/src/context.hpp b/core/src/context.hpp index 36831f6d14..10ad878e9e 100644 --- a/core/src/context.hpp +++ b/core/src/context.hpp @@ -39,6 +39,11 @@ void context::push_marker(uint32_t marker) { emplace_back(km_core_context_item { KM_CORE_CT_MARKER, {0,}, {marker} }); } +// Context helper functions + +km_core_cp* get_context_as_string(km_core_context *context); +km_core_status set_context_from_string(km_core_context *context, km_core_cp const *new_context); + } // namespace core } // namespace km diff --git a/core/src/context_helpers.cpp b/core/src/context_helpers.cpp new file mode 100644 index 0000000000..8e6d2a1920 --- /dev/null +++ b/core/src/context_helpers.cpp @@ -0,0 +1,58 @@ +/* + Copyright: © 2018-2024 SIL International. + Description: Helper functions for context data type conversions + Create Date: 18 Jan 2024 + Authors: Marc Durdin + History: 18 Jan 2024 - MCD - Refactor from km_core_state_context_set_if_needed.cpp +*/ +#include + +#include "keyman_core.h" +#include "context.hpp" + +using namespace km::core; + +/** + * Retrieves the context as a km_core_cp string, dropping markers + */ +km_core_cp* km::core::get_context_as_string(km_core_context *context) { + size_t buf_size = 0; + km_core_context_item* context_items = nullptr; + + if(km_core_context_get(context, &context_items) != KM_CORE_STATUS_OK) { + return nullptr; + } + + if(km_core_context_items_to_utf16(context_items, nullptr, &buf_size) != KM_CORE_STATUS_OK) { + km_core_context_items_dispose(context_items); + return nullptr; + } + + km_core_cp *app_context_string = new km_core_cp[buf_size]; + + km_core_status status = km_core_context_items_to_utf16(context_items, app_context_string, &buf_size); + km_core_context_items_dispose(context_items); + + if(status != KM_CORE_STATUS_OK) { + return nullptr; + } + + return app_context_string; +} + +/** + * Updates the context from the new_context km_core_cp string + */ +km_core_status km::core::set_context_from_string(km_core_context *context, km_core_cp const *new_context) { + km_core_context_item* new_context_items = nullptr; + + km_core_status status = km_core_context_items_from_utf16(new_context, &new_context_items); + if (status != KM_CORE_STATUS_OK) { + return status; + } + + km_core_context_set(context, new_context_items); + km_core_context_items_dispose(new_context_items); + + return KM_CORE_STATUS_OK; +} diff --git a/core/src/km_core_state_context_set_if_needed.cpp b/core/src/km_core_state_context_set_if_needed.cpp index a6f6fde9c0..b5ac9f7a44 100644 --- a/core/src/km_core_state_context_set_if_needed.cpp +++ b/core/src/km_core_state_context_set_if_needed.cpp @@ -30,8 +30,6 @@ using namespace km::core; bool should_normalize(km_core_state *state); bool is_context_valid(km_core_cp const * context, km_core_cp const * cached_context); -km_core_cp* get_context_as_string(km_core_context *context); -km_core_status set_context_from_string(km_core_context *context, km_core_cp const *new_context); bool do_normalize_nfd(km_core_cp const * src, std::u16string &dst); km_core_context_status do_fail(km_core_context *app_context, km_core_context *cached_context, const char* error); @@ -139,51 +137,6 @@ bool is_context_valid(km_core_cp const * new_app_context, km_core_cp const * app return true; } -/** - * Retrieves the context as a km_core_cp string, dropping markers - */ -km_core_cp* get_context_as_string(km_core_context *context) { - size_t buf_size = 0; - km_core_context_item* context_items = nullptr; - - if(km_core_context_get(context, &context_items) != KM_CORE_STATUS_OK) { - return nullptr; - } - - if(km_core_context_items_to_utf16(context_items, nullptr, &buf_size) != KM_CORE_STATUS_OK) { - km_core_context_items_dispose(context_items); - return nullptr; - } - - km_core_cp *app_context_string = new km_core_cp[buf_size]; - - km_core_status status = km_core_context_items_to_utf16(context_items, app_context_string, &buf_size); - km_core_context_items_dispose(context_items); - - if(status != KM_CORE_STATUS_OK) { - return nullptr; - } - - return app_context_string; -} - -/** - * Updates the context from the new_context km_core_cp string - */ -km_core_status set_context_from_string(km_core_context *context, km_core_cp const *new_context) { - km_core_context_item* new_context_items = nullptr; - - km_core_status status = km_core_context_items_from_utf16(new_context, &new_context_items); - if (status != KM_CORE_STATUS_OK) { - return status; - } - - km_core_context_set(context, new_context_items); - km_core_context_items_dispose(new_context_items); - - return KM_CORE_STATUS_OK; -} - /** * Normalize the input string using ICU */ diff --git a/core/src/meson.build b/core/src/meson.build index 6f96041dac..9943c2fd07 100644 --- a/core/src/meson.build +++ b/core/src/meson.build @@ -44,6 +44,7 @@ endif kmx_files = files( 'actions_normalize.cpp', 'action.cpp', + 'context_helpers.cpp', 'option.cpp', 'keyboard.cpp', 'state.cpp', diff --git a/core/tests/unit/kmnkbd/test_actions_normalize.cpp b/core/tests/unit/kmnkbd/test_actions_normalize.cpp index dada129cfa..741b55a9c5 100644 --- a/core/tests/unit/kmnkbd/test_actions_normalize.cpp +++ b/core/tests/unit/kmnkbd/test_actions_normalize.cpp @@ -10,15 +10,11 @@ #include "path.hpp" #include "action.hpp" +#include "context.hpp" #include #include "../emscripten_filesystem.h" -// TODO: These helpers are in km_core_state_context_set_if_needed.cpp and -// should be refactored into a helper module -km_core_status set_context_from_string(km_core_context *context, km_core_cp const *new_context); -km_core_cp* get_context_as_string(km_core_context *context); - km_core_option_item test_env_opts[] = { KM_CORE_OPTIONS_END From 63574f85137896fe8d3dba8aab0155e503a56c7a Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Thu, 18 Jan 2024 09:31:13 +0700 Subject: [PATCH 2/2] chore(core): add assertions to context helpers --- core/src/context_helpers.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/src/context_helpers.cpp b/core/src/context_helpers.cpp index 8e6d2a1920..936f68be52 100644 --- a/core/src/context_helpers.cpp +++ b/core/src/context_helpers.cpp @@ -16,6 +16,11 @@ using namespace km::core; * Retrieves the context as a km_core_cp string, dropping markers */ km_core_cp* km::core::get_context_as_string(km_core_context *context) { + assert(context != nullptr); + if(context == nullptr) { + return nullptr; + } + size_t buf_size = 0; km_core_context_item* context_items = nullptr; @@ -44,6 +49,12 @@ km_core_cp* km::core::get_context_as_string(km_core_context *context) { * Updates the context from the new_context km_core_cp string */ km_core_status km::core::set_context_from_string(km_core_context *context, km_core_cp const *new_context) { + assert(context != nullptr); + assert(new_context != nullptr); + if(context == nullptr || new_context == nullptr) { + return KM_CORE_STATUS_INVALID_ARGUMENT; + } + km_core_context_item* new_context_items = nullptr; km_core_status status = km_core_context_items_from_utf16(new_context, &new_context_items);