Code revisions.

This commit is contained in:
J-D-K 2025-11-29 16:37:32 -05:00
parent fee615c300
commit 0fce729cb6
5 changed files with 40 additions and 47 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <initializer_list>
#include <span>
#include <string>
#include <switch.h>
#include <vector>
@ -29,8 +30,11 @@ namespace keyboard
/// @brief Constructs a new dictionary using the list passed.
Dictionary(std::initializer_list<std::string_view> 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<std::string_view> wordList);
void add_list_to_list(std::initializer_list<std::string_view> wordList);
/// @brief Returns the number of words in the internal list.
size_t get_count() const noexcept;

View File

@ -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

View File

@ -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<std::string, 8> 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);

View File

@ -4,34 +4,37 @@
// ---- Construction ----
keyboard::Dictionary::Dictionary(std::initializer_list<std::string_view> wordList) { Dictionary::add_list(wordList); }
keyboard::Dictionary::Dictionary(std::initializer_list<std::string_view> wordList) { Dictionary::add_list_to_list(wordList); }
// ---- Public functions ----
void keyboard::Dictionary::add_list(std::initializer_list<std::string_view> 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<uint16_t *>(newWord.predict);
uint16_t *dictWord = reinterpret_cast<uint16_t *>(newWord.word);
// This makes the following easier to read and less repetitive.
const uint8_t *inData = reinterpret_cast<const uint8_t *>(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<std::string_view> 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<uint16_t *>(newWord.predict);
uint16_t *dictWord = reinterpret_cast<uint16_t *>(newWord.word);
// Same here.
const uint8_t *inData = reinterpret_cast<const uint8_t *>(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(); }
const keyboard::Dictionary::Word *keyboard::Dictionary::get_words() const noexcept { return m_words.data(); }
// ---- Private functions ----

View File

@ -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<uint32_t, std::string_view> &get_replacement_table()
{
static const std::unordered_map<uint32_t, std::string_view> replacementTable = {