diff --git a/include/keyboard/Dictionary.hpp b/include/keyboard/Dictionary.hpp index 715e36f..68e3b00 100644 --- a/include/keyboard/Dictionary.hpp +++ b/include/keyboard/Dictionary.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -29,8 +30,11 @@ namespace keyboard /// @brief Constructs a new dictionary using the list passed. Dictionary(std::initializer_list wordList); + /// @brief Helper function to add word to the internal list to cut down on repetition. + void add_word_to_list(std::string_view word); + /// @brief Adds the list to the internal list. - void add_list(std::initializer_list wordList); + void add_list_to_list(std::initializer_list wordList); /// @brief Returns the number of words in the internal list. size_t get_count() const noexcept; diff --git a/include/stringutil.hpp b/include/stringutil.hpp index 1c81e0a..1bb9b52 100644 --- a/include/stringutil.hpp +++ b/include/stringutil.hpp @@ -42,9 +42,4 @@ namespace stringutil /// @param format Optional. Format to use. Default is Year_Month_Day-Time /// @return Date string. std::string get_date_string(stringutil::DateFormat format = stringutil::DateFormat::Year_Month_Day); - - /// @brief Attempts to generate an abbreviated version of the title passed by detecting spaces. - /// @param title Title string to generate abbreviation from. - /// @return String containing abbreviation. - std::string generate_abbreviated_title(std::string_view title); } // namespace stringutil diff --git a/source/appstates/BackupMenuState.cpp b/source/appstates/BackupMenuState.cpp index 674555d..c94d3a2 100644 --- a/source/appstates/BackupMenuState.cpp +++ b/source/appstates/BackupMenuState.cpp @@ -277,18 +277,18 @@ void BackupMenuState::name_and_create_backup() // Doing this like this so the strings don't linger. keyboard::Dictionary dictionary{}; { - // Start with all available date formats. - const std::string dateA = stringutil::get_date_string(stringutil::DateFormat::Year_Month_Day); - const std::string dateB = stringutil::get_date_string(stringutil::DateFormat::Year_Day_Month); - const std::string dateC = stringutil::get_date_string(stringutil::DateFormat::YearMonthDay); - const std::string dateD = stringutil::get_date_string(stringutil::DateFormat::YearDayMonth); - const std::string dateE = stringutil::get_date_string(stringutil::DateFormat::AscTime); - - // Path safe nickname. - const std::string user = m_user->get_path_safe_nickname(); + // Array of dictionary strings. + const std::array dictionaryStrings = { + stringutil::get_date_string(stringutil::DateFormat::Year_Month_Day), + stringutil::get_date_string(stringutil::DateFormat::Year_Day_Month), + stringutil::get_date_string(stringutil::DateFormat::YearMonthDay), + stringutil::get_date_string(stringutil::DateFormat::YearDayMonth), + stringutil::get_date_string(stringutil::DateFormat::AscTime), + m_user->get_path_safe_nickname(), + STRING_ZIP_EXT}; // Add them before continuing. - dictionary.add_list({dateA, dateB, dateC, dateD, dateE, user, STRING_ZIP_EXT}); + for (const std::string_view word : dictionaryStrings) { dictionary.add_word_to_list(word); } } const char *keyboardHeader = strings::get_by_name(strings::names::KEYBOARD, 0); diff --git a/source/keyboard/Dictionary.cpp b/source/keyboard/Dictionary.cpp index 05a709a..bbcaa70 100644 --- a/source/keyboard/Dictionary.cpp +++ b/source/keyboard/Dictionary.cpp @@ -4,34 +4,37 @@ // ---- Construction ---- -keyboard::Dictionary::Dictionary(std::initializer_list wordList) { Dictionary::add_list(wordList); } +keyboard::Dictionary::Dictionary(std::initializer_list wordList) { Dictionary::add_list_to_list(wordList); } // ---- Public functions ---- -void keyboard::Dictionary::add_list(std::initializer_list wordList) +void keyboard::Dictionary::add_word_to_list(std::string_view word) +{ + // New word. + Dictionary::Word newWord{}; + + // LibNX's functions for this expect uint16 instead of char16... If there's even much of a difference? + uint16_t *predict = reinterpret_cast(newWord.predict); + uint16_t *dictWord = reinterpret_cast(newWord.word); + + // This makes the following easier to read and less repetitive. + const uint8_t *inData = reinterpret_cast(word.data()); + + // The dictionary suggestions are UTF-16. + utf8_to_utf16(predict, inData, Dictionary::WORD_LENGTH); + utf8_to_utf16(dictWord, inData, Dictionary::WORD_LENGTH); + + m_words.push_back(newWord); +} + +void keyboard::Dictionary::add_list_to_list(std::initializer_list wordList) { // Loop through the list. - for (const std::string_view word : wordList) - { - // New word - Dictionary::Word newWord{}; - - // This is cleaner and easier to read. libnx expects uint16 instead of char16 - uint16_t *predict = reinterpret_cast(newWord.predict); - uint16_t *dictWord = reinterpret_cast(newWord.word); - - // Same here. - const uint8_t *inData = reinterpret_cast(word.data()); - - // The words are UTF-16. We need to convert them. - utf8_to_utf16(predict, inData, Dictionary::WORD_LENGTH); - utf8_to_utf16(dictWord, inData, Dictionary::WORD_LENGTH); - - // Push the word to the vector. - m_words.push_back(newWord); - } + for (const std::string_view word : wordList) { Dictionary::add_word_to_list(word); } } size_t keyboard::Dictionary::get_count() const noexcept { return m_words.size(); } -const keyboard::Dictionary::Word *keyboard::Dictionary::get_words() const noexcept { return m_words.data(); } \ No newline at end of file +const keyboard::Dictionary::Word *keyboard::Dictionary::get_words() const noexcept { return m_words.data(); } + +// ---- Private functions ---- diff --git a/source/stringutil.cpp b/source/stringutil.cpp index d68d84e..13d8a3a 100644 --- a/source/stringutil.cpp +++ b/source/stringutil.cpp @@ -170,15 +170,6 @@ std::string stringutil::get_date_string(stringutil::DateFormat format) return returnString; } -std::string stringutil::generate_abbreviated_title(std::string_view title) -{ - size_t space = title.find_first_of(' '); - // Don't bother. - if (title.empty() || space == title.npos) { return {}; } - - // -} - static const std::unordered_map &get_replacement_table() { static const std::unordered_map replacementTable = {