constification
This commit is contained in:
parent
28f8e8005a
commit
ac8af23fba
32
src/main.cc
32
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<std::string, std::string> 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<std::string, std::string> 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<std::filesystem::path> 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 << "<!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) {
|
||||
std::cout << "<a href=\"bword:" << key << "\">" << key << "</a>";
|
||||
if (!values.empty()) {
|
||||
|
@ -197,4 +198,3 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue