From 9cc747b3e2f2566ef28204cc0806b28335f02d2b Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 24 Apr 2021 12:10:28 -0400 Subject: [PATCH] Resolves potential source of errors: specifying incorrect table size. (Having made exactly this mistake with the ZX Spectrum) --- Machines/AmstradCPC/Keyboard.cpp | 2 +- Machines/Commodore/Vic-20/Keyboard.cpp | 2 +- Machines/Electron/Keyboard.cpp | 2 +- Machines/Oric/Keyboard.cpp | 2 +- Machines/Sinclair/Keyboard/Keyboard.cpp | 6 +++--- Machines/Utility/Typer.cpp | 10 ---------- Machines/Utility/Typer.hpp | 12 ++++++++---- 7 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Machines/AmstradCPC/Keyboard.cpp b/Machines/AmstradCPC/Keyboard.cpp index 98b67e8ae..7473f942f 100644 --- a/Machines/AmstradCPC/Keyboard.cpp +++ b/Machines/AmstradCPC/Keyboard.cpp @@ -151,7 +151,7 @@ const uint16_t *CharacterMapper::sequence_for_character(char character) const { #undef SHIFT #undef X - return table_lookup_sequence_for_character(key_sequences, sizeof(key_sequences), character); + return table_lookup_sequence_for_character(key_sequences, character); } bool CharacterMapper::needs_pause_after_key(uint16_t key) const { diff --git a/Machines/Commodore/Vic-20/Keyboard.cpp b/Machines/Commodore/Vic-20/Keyboard.cpp index 1b79bd414..62493d0e1 100644 --- a/Machines/Commodore/Vic-20/Keyboard.cpp +++ b/Machines/Commodore/Vic-20/Keyboard.cpp @@ -151,5 +151,5 @@ const uint16_t *CharacterMapper::sequence_for_character(char character) const { #undef SHIFT #undef X - return table_lookup_sequence_for_character(key_sequences, sizeof(key_sequences), character); + return table_lookup_sequence_for_character(key_sequences, character); } diff --git a/Machines/Electron/Keyboard.cpp b/Machines/Electron/Keyboard.cpp index c554c2cbe..689e5707f 100644 --- a/Machines/Electron/Keyboard.cpp +++ b/Machines/Electron/Keyboard.cpp @@ -145,7 +145,7 @@ const uint16_t *CharacterMapper::sequence_for_character(char character) const { #undef SHIFT #undef X - return table_lookup_sequence_for_character(key_sequences, sizeof(key_sequences), character); + return table_lookup_sequence_for_character(key_sequences, character); } bool CharacterMapper::needs_pause_after_key(uint16_t key) const { diff --git a/Machines/Oric/Keyboard.cpp b/Machines/Oric/Keyboard.cpp index 2e373ad57..3b12bc7e2 100644 --- a/Machines/Oric/Keyboard.cpp +++ b/Machines/Oric/Keyboard.cpp @@ -127,5 +127,5 @@ const uint16_t *CharacterMapper::sequence_for_character(char character) const { #undef SHIFT #undef X - return table_lookup_sequence_for_character(key_sequences, sizeof(key_sequences), character); + return table_lookup_sequence_for_character(key_sequences, character); } diff --git a/Machines/Sinclair/Keyboard/Keyboard.cpp b/Machines/Sinclair/Keyboard/Keyboard.cpp index 1e9db2bf8..f0cd23b39 100644 --- a/Machines/Sinclair/Keyboard/Keyboard.cpp +++ b/Machines/Sinclair/Keyboard/Keyboard.cpp @@ -275,13 +275,13 @@ const uint16_t *CharacterMapper::sequence_for_character(char character) const { switch(machine_) { case Machine::ZX80: - return table_lookup_sequence_for_character(zx80_key_sequences, sizeof(zx80_key_sequences), character); + return table_lookup_sequence_for_character(zx80_key_sequences, character); case Machine::ZX81: - return table_lookup_sequence_for_character(zx81_key_sequences, sizeof(zx81_key_sequences), character); + return table_lookup_sequence_for_character(zx81_key_sequences, character); case Machine::ZXSpectrum: - return table_lookup_sequence_for_character(spectrum_key_sequences, sizeof(zx81_key_sequences), character); + return table_lookup_sequence_for_character(spectrum_key_sequences, character); } } diff --git a/Machines/Utility/Typer.cpp b/Machines/Utility/Typer.cpp index 9706d85bf..1e290bef5 100644 --- a/Machines/Utility/Typer.cpp +++ b/Machines/Utility/Typer.cpp @@ -131,13 +131,3 @@ bool Typer::type_next_character() { return true; } - -// MARK: - Character mapper - -const uint16_t *CharacterMapper::table_lookup_sequence_for_character(const KeySequence *sequences, std::size_t length, char character) const { - std::size_t ucharacter = size_t((unsigned char)character); - if(ucharacter >= (length / sizeof(KeySequence))) return nullptr; - if(sequences[ucharacter][0] == MachineTypes::MappedKeyboardMachine::KeyNotMapped) return nullptr; - return sequences[ucharacter]; -} - diff --git a/Machines/Utility/Typer.hpp b/Machines/Utility/Typer.hpp index 0e0a15b3a..79ac1b86a 100644 --- a/Machines/Utility/Typer.hpp +++ b/Machines/Utility/Typer.hpp @@ -44,11 +44,15 @@ class CharacterMapper { typedef uint16_t KeySequence[16]; /*! - Provided in the base class as a convenience: given the lookup table of key sequences @c sequences, - with @c length entries, returns the sequence for character @c character if it exists; otherwise - returns @c nullptr. + Provided in the base class as a convenience: given the C array of key sequences @c sequences, + returns the sequence for character @c character if it exists; otherwise returns @c nullptr. */ - const uint16_t *table_lookup_sequence_for_character(const KeySequence *sequences, std::size_t length, char character) const; + template const uint16_t *table_lookup_sequence_for_character(const Collection &sequences, char character) const { + std::size_t ucharacter = size_t((unsigned char)character); + if(ucharacter >= sizeof(sequences) / sizeof(KeySequence)) return nullptr; + if(sequences[ucharacter][0] == MachineTypes::MappedKeyboardMachine::KeyNotMapped) return nullptr; + return sequences[ucharacter]; + } }; /*!