From ac8af23fba3519beed0b1c4374556b901d2046da Mon Sep 17 00:00:00 2001 From: hashirama Date: Fri, 17 May 2024 15:22:38 -0400 Subject: [PATCH] constification --- src/main.cc | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main.cc b/src/main.cc index 2da26c3..677059a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,9 +12,9 @@ // Struct to hold the view and id of each entry struct Entry { - std::string_view decoded_view; - uint64_t id; - inline Entry(std::string_view decoded_view, uint64_t id) : decoded_view(decoded_view), id(id) {} + const std::string_view decoded_view; + const uint64_t id; + inline Entry(const std::string_view decoded_view, const uint64_t id) : decoded_view(decoded_view), id(id) {} }; // Function to remove one UTF-8 character and return the character along with the new string @@ -22,10 +22,10 @@ inline std::pair remove_one_utf8_char(const std::strin if (str.empty()) { return {"", str}; } - size_t len = str.size(); + const size_t len = str.size(); size_t i = 0; while (i < len) { - unsigned char c = str[i]; + const unsigned char c = str[i]; if (c < 0x80) { // 1-byte character return {str.substr(i, 1), str.substr(i + 1)}; } else if ((c >> 5) == 0x6) { // 2-byte character @@ -41,7 +41,7 @@ inline std::pair remove_one_utf8_char(const std::strin } // Function to check input from command line or piped input -inline std::string get_input(int argc, char* argv[], bool goldendict_mode) { +inline std::string get_input(const int argc, char* const argv[], const bool goldendict_mode) { std::string search_string; if (!isatty(fileno(stdin))) { // If input is piped @@ -81,21 +81,22 @@ inline std::string get_input(int argc, char* argv[], bool goldendict_mode) { return search_string; } -auto find_dic_file() -> std::filesystem::path { - static auto const locations = { +inline std::filesystem::path find_dic_file() { + static const std::vector locations = { std::filesystem::path("/usr/share/hakurei/"), std::filesystem::path(std::getenv("HOME")) / ".local/share/hakurei/", std::filesystem::current_path() }; - for (auto const& location : locations) { - if (std::filesystem::exists(location / "dict.bin") && std::filesystem::is_regular_file(location / "dict.bin")) { - return (location / "dict.bin"); + for (const auto& location : locations) { + const auto dict_path = location / "dict.bin"; + if (std::filesystem::exists(dict_path) && std::filesystem::is_regular_file(dict_path)) { + return dict_path; } } throw std::runtime_error("Couldn't find the word list."); } -int main(int argc, char* argv[]) { +int main(const int argc, char* argv[]) { bool debug_mode = false; bool goldendict_mode = false; std::string word, sentence; @@ -141,7 +142,7 @@ int main(int argc, char* argv[]) { // Add all substrings to the results if (!results.empty()) { for (const auto& entry : results) { - std::string substring(entry.decoded_view); + const std::string substring(entry.decoded_view); substrings.push_back(substring); derived_map[substring].push_back(search_string); if (!goldendict_mode) { @@ -151,7 +152,7 @@ int main(int argc, char* argv[]) { } // Remove one UTF-8 character from the search string and get the removed character - auto [removed_char, new_search_string] = remove_one_utf8_char(search_string); + const auto [removed_char, new_search_string] = remove_one_utf8_char(search_string); if (!removed_char.empty() && !goldendict_mode) { std::cout << removed_char << std::endl; @@ -181,7 +182,7 @@ int main(int argc, char* argv[]) { // Wrap the output in HTML format for GoldenDict if in GoldenDict mode if (goldendict_mode) { - std::cout << "
"; + std::cout << "
"; for (const auto& [key, values] : derived_map) { std::cout << "" << key << ""; if (!values.empty()) { @@ -197,4 +198,3 @@ int main(int argc, char* argv[]) { return 0; } -