constification

This commit is contained in:
千住柱間 2024-05-17 15:22:38 -04:00
parent 28f8e8005a
commit ac8af23fba
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -12,9 +12,9 @@
// Struct to hold the view and id of each entry // Struct to hold the view and id of each entry
struct Entry { struct Entry {
std::string_view decoded_view; const std::string_view decoded_view;
uint64_t id; const uint64_t id;
inline Entry(std::string_view decoded_view, uint64_t id) : decoded_view(decoded_view), id(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 // Function to remove one UTF-8 character and return the character along with the new string
@ -22,10 +22,10 @@ inline std::pair<std::string, std::string> remove_one_utf8_char(const std::strin
if (str.empty()) { if (str.empty()) {
return {"", str}; return {"", str};
} }
size_t len = str.size(); const size_t len = str.size();
size_t i = 0; size_t i = 0;
while (i < len) { while (i < len) {
unsigned char c = str[i]; const unsigned char c = str[i];
if (c < 0x80) { // 1-byte character if (c < 0x80) { // 1-byte character
return {str.substr(i, 1), str.substr(i + 1)}; return {str.substr(i, 1), str.substr(i + 1)};
} else if ((c >> 5) == 0x6) { // 2-byte character } else if ((c >> 5) == 0x6) { // 2-byte character
@ -41,7 +41,7 @@ inline std::pair<std::string, std::string> remove_one_utf8_char(const std::strin
} }
// Function to check input from command line or piped input // 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; std::string search_string;
if (!isatty(fileno(stdin))) { // If input is piped 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; return search_string;
} }
auto find_dic_file() -> std::filesystem::path { inline std::filesystem::path find_dic_file() {
static auto const locations = { static const std::vector<std::filesystem::path> locations = {
std::filesystem::path("/usr/share/hakurei/"), std::filesystem::path("/usr/share/hakurei/"),
std::filesystem::path(std::getenv("HOME")) / ".local/share/hakurei/", std::filesystem::path(std::getenv("HOME")) / ".local/share/hakurei/",
std::filesystem::current_path() std::filesystem::current_path()
}; };
for (auto const& location : locations) { for (const auto& location : locations) {
if (std::filesystem::exists(location / "dict.bin") && std::filesystem::is_regular_file(location / "dict.bin")) { const auto dict_path = location / "dict.bin";
return (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."); 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 debug_mode = false;
bool goldendict_mode = false; bool goldendict_mode = false;
std::string word, sentence; std::string word, sentence;
@ -141,7 +142,7 @@ int main(int argc, char* argv[]) {
// Add all substrings to the results // Add all substrings to the results
if (!results.empty()) { if (!results.empty()) {
for (const auto& entry : results) { for (const auto& entry : results) {
std::string substring(entry.decoded_view); const std::string substring(entry.decoded_view);
substrings.push_back(substring); substrings.push_back(substring);
derived_map[substring].push_back(search_string); derived_map[substring].push_back(search_string);
if (!goldendict_mode) { 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 // 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) { if (!removed_char.empty() && !goldendict_mode) {
std::cout << removed_char << std::endl; 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 // Wrap the output in HTML format for GoldenDict if in GoldenDict mode
if (goldendict_mode) { if (goldendict_mode) {
std::cout << "<!DOCTYPE html><html><head><style>.gd-marisa { font-size: 2rem; margin-bottom: 0.05em; margin-top: -0.2em; color: #1268c3; font-weight: normal; } .gd-marisa a { display: inline-block; font-weight: normal; color: royalblue; text-decoration: none; border-bottom: dashed max(1px, calc(1em / 16)) currentColor; } .gd-marisa a.gd-headword { background-color: #ddeeff; border-radius: 0.2rem; font-weight: 500; } .gd-marisa > ul { --size: 1rem; font-size: var(--size); padding-inline-start: var(--size); margin-block: 2px; } .gd-marisa .alternatives { --size: 1rem; display: grid; font-size: var(--size); gap: calc( var(--size) / 4); max-width: 100%; margin: 0 auto; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); align-content: start; justify-content: space-around; text-align: left; padding: 5px 0px; } .gd-marisa .alternatives > ul { list-style-type: none; margin: 0; padding: calc( var(--size) / 4); background-color: hsl(150deg 30% 60% / 10%); } </style></head><body><div class=\"gd-marisa\">"; std::cout << "<!DOCTYPE html><html><head><style>.hakurei { font-size: 2rem; margin-bottom: 0.05em; margin-top: -0.2em; color: #1268c3; font-weight: normal; } .hakurei a { display: inline-block; font-weight: normal; color: royalblue; text-decoration: none; border-bottom: dashed max(1px, calc(1em / 16)) currentColor; } .hakurei a.hakurei-headword { background-color: #ddeeff; border-radius: 0.2rem; font-weight: 500; } .hakurei > ul { --size: 1rem; font-size: var(--size); padding-inline-start: var(--size); margin-block: 2px; } .hakurei .alternatives { --size: 1rem; display: grid; font-size: var(--size); gap: calc( var(--size) / 4); max-width: 100%; margin: 0 auto; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); align-content: start; justify-content: space-around; text-align: left; padding: 5px 0px; } .hakurei .alternatives > ul { list-style-type: none; margin: 0; padding: calc( var(--size) / 4); background-color: hsl(150deg 30% 60% / 10%); } </style></head><body><div class=\"hakurei\">";
for (const auto& [key, values] : derived_map) { for (const auto& [key, values] : derived_map) {
std::cout << "<a href=\"bword:" << key << "\">" << key << "</a>"; std::cout << "<a href=\"bword:" << key << "\">" << key << "</a>";
if (!values.empty()) { if (!values.empty()) {
@ -197,4 +198,3 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }