From 30c302d13d32569efd73be5274ce15be83f8b8f2 Mon Sep 17 00:00:00 2001 From: hashirama Date: Sun, 19 May 2024 01:56:16 -0400 Subject: [PATCH] trim white spaces --- src/hakurei.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/hakurei.cpp b/src/hakurei.cpp index 99fbc85..488f8d9 100644 --- a/src/hakurei.cpp +++ b/src/hakurei.cpp @@ -3,6 +3,7 @@ // $ patchelf --set-rpath /usr/local/lib/gcc13 hakurei + #include #include #include @@ -18,6 +19,8 @@ #include #include + + struct Entry { const std::string_view decoded_view; const uint64_t id; @@ -46,6 +49,19 @@ inline std::pair remove_one_utf8_char(const std::strin return {"", ""}; } + +std::string trim(const std::string& str) { + size_t start = str.find_first_not_of(" \t\n\r\f\v"); + size_t end = str.find_last_not_of(" \t\n\r\f\v"); + + if (start == std::string::npos || end == std::string::npos) + return ""; // String contains only whitespace + + return str.substr(start, end - start + 1); +} + + + inline std::string get_input(const int argc, char* const argv[], const bool goldendict_mode) { std::string search_string; if (!isatty(fileno(stdin))) { @@ -79,7 +95,8 @@ inline std::string get_input(const int argc, char* const argv[], const bool gold std::cerr << "Search string not provided." << std::endl; exit(1); } - return search_string; + + return trim(search_string); } inline std::filesystem::path find_dic_file() {