spiegel-keyman/core/src/ldml/ldml_vkeys.cpp
Steven R. Loomis 812fd1713a spec(core): ldml tests for either shift/ctrl/alt 🙀
- lookup needs to handle either/neither/both cases for alt and ctrl
- and a bug in the test runner: would 'pass' if failed on exactly line 256 (0x100 & 0xFF == 0)

#7533
2023-03-02 17:13:47 -06:00

82 lines
1.9 KiB
C++

/*
Copyright: © SIL International.
Description: Implementation for LDML Vkeys
Create Date: 6 Oct 2022
Authors: Steven R. Loomis
*/
#include "ldml_vkeys.hpp"
#include "kmx_file.h"
namespace km {
namespace kbp {
namespace ldml {
vkeys::vkeys() : vkey_to_string() {
}
void
vkeys::add(km_kbp_virtual_key vk, uint16_t modifier_state, std::u16string output) {
// construct key
const vkey_id id(vk, modifier_state);
// assign the string
vkey_to_string[id] = output;
}
static const uint16_t BOTH_ALT = LALTFLAG | RALTFLAG;
static const uint16_t BOTH_CTRL = LCTRLFLAG | RCTRLFLAG;
std::u16string
vkeys::lookup(km_kbp_virtual_key vk, uint16_t modifier_state) const {
const vkey_id id(vk, modifier_state);
// try exact match first
std::u16string ret = lookup(id);
if (!ret.empty()) {
return ret;
}
const bool have_ctrl = modifier_state & BOTH_CTRL;
const bool have_alt = modifier_state & BOTH_ALT;
// look for a layer with "alt" (either)
if (have_alt) {
const vkey_id id_alt(vk, (modifier_state & ~(BOTH_ALT)) | K_ALTFLAG);
ret = lookup(id_alt);
if (!ret.empty()) {
return ret;
}
}
// look for a layer with "ctrl" (either)
if (have_ctrl) {
const vkey_id id_ctrl(vk, (modifier_state & ~(BOTH_CTRL)) | K_CTRLFLAG);
ret = lookup(id_ctrl);
if (!ret.empty()) {
return ret;
}
}
// look for a layer with "alt ctrl" (either)
if (have_ctrl && have_alt) {
const vkey_id id_ctrl_alt(vk, (modifier_state & ~(BOTH_ALT | BOTH_CTRL)) | K_CTRLFLAG | K_ALTFLAG);
ret = lookup(id_ctrl_alt);
if (!ret.empty()) {
return ret;
}
}
// default: return failure.
return ret;
}
std::u16string
vkeys::lookup(const vkey_id& id) const {
const auto key = vkey_to_string.find(id);
if (key == vkey_to_string.end()) {
return std::u16string(); // TODO-LDML: optimize object construction?
}
return key->second;
}
} // namespace ldml
} // namespace kbp
} // namespace km