summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/utils/mu-command-handler.cc14
-rw-r--r--lib/utils/mu-html-to-text.cc2
-rw-r--r--lib/utils/mu-utils-file.cc8
-rw-r--r--lib/utils/mu-utils.cc15
-rw-r--r--lib/utils/mu-utils.hh22
5 files changed, 33 insertions, 28 deletions
diff --git a/lib/utils/mu-command-handler.cc b/lib/utils/mu-command-handler.cc
index 927df0b..6088280 100644
--- a/lib/utils/mu-command-handler.cc
+++ b/lib/utils/mu-command-handler.cc
@@ -1,5 +1,5 @@
/*
-** Copyright (C) 2020-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+** Copyright (C) 2020-2026 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
@@ -36,8 +36,8 @@ Command::string_vec_arg(const std::string& name) const
std::vector<std::string> vec;
for (const auto& item : val->list()) {
if (!item.stringp()) {
- // mu_warning("command: non-string in string-list for {}: {}",
- // name, to_string());
+ mu_warning("command: non-string in string-list for {}: {}",
+ name, to_string());
return Nothing;
} else
vec.emplace_back(item.string());
@@ -63,7 +63,6 @@ validate(const CommandHandler::CommandInfoMap& cmap,
//
// so, we're looking for the odd-numbered parameters.
const auto param_it = cmd.find_arg(argname);
- const auto&& param_val = std::next(param_it);
// it's an error when a required parameter is missing.
if (param_it == cmd.cend()) {
if (arginfo.required)
@@ -73,6 +72,13 @@ validate(const CommandHandler::CommandInfoMap& cmap,
continue; // not required
}
+ // the keyword is present; its value must follow it.
+ const auto param_val = std::next(param_it);
+ if (param_val == cmd.cend())
+ return Err(Error::Code::Command,
+ "missing value for parameter {} in command '{}'",
+ argname, cmd.to_string());
+
// the types must match, but the 'nil' symbol is acceptable as "no value"
if (param_val->type() != arginfo.type && !(param_val->nilp()))
return Err(Error::Code::Command,
diff --git a/lib/utils/mu-html-to-text.cc b/lib/utils/mu-html-to-text.cc
index 08f1f4d..69ff232 100644
--- a/lib/utils/mu-html-to-text.cc
+++ b/lib/utils/mu-html-to-text.cc
@@ -112,7 +112,7 @@ public:
* @return true or false
*/
bool looking_at(std::string_view str) const {
- if (pos_ >= html_.size() || pos_ + str.size() >= html_.size())
+ if (pos_ >= html_.size() || pos_ + str.size() > html_.size())
return false;
else
return matches({html_.data()+pos_, str.size()}, str);
diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc
index 56b1c6f..d1cb85b 100644
--- a/lib/utils/mu-utils-file.cc
+++ b/lib/utils/mu-utils-file.cc
@@ -87,9 +87,10 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t
path.c_str(),
relative_to.empty() ? nullptr : relative_to.c_str())).value()};
- // remove trailing '/'... is this needed?
- if (str[str.length()-1] == G_DIR_SEPARATOR)
- str.erase(str.length() - 1);
+ if (!str.empty()) { // remove trailing '/'... is this needed?
+ if (str[str.length()-1] == G_DIR_SEPARATOR)
+ str.erase(str.length() - 1);
+ }
return str;
}
@@ -232,7 +233,6 @@ Mu::read_from_stdin()
{
g_autoptr(GOutputStream) outmem = g_memory_output_stream_new_resizable();
g_autoptr(GInputStream) input = g_unix_input_stream_new(STDIN_FILENO, TRUE);
- //g_autoptr(GCancellable) cancel{maybe_cancellable_timeout(timeout)};
GError *err{};
auto bytes = g_output_stream_splice(outmem, input,
diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc
index f08e6ea..2af55d3 100644
--- a/lib/utils/mu-utils.cc
+++ b/lib/utils/mu-utils.cc
@@ -552,9 +552,8 @@ Mu::parse_size(const std::string& val, bool is_first)
minfo = NULL;
if (g_regex_match(rx, val.c_str(), (GRegexMatchFlags)0, &minfo)) {
- char* s;
- s = g_match_info_fetch(minfo, 1);
- size = atoll(s);
+ char* s = g_match_info_fetch(minfo, 1);
+ size = atoll(s); // check overflow?
g_free(s);
s = g_match_info_fetch(minfo, 2);
@@ -678,14 +677,10 @@ Mu::summarize(const std::string& str, size_t max_lines)
static bool
-locale_is_utf8 (void)
+locale_is_utf8 ()
{
- const gchar *dummy;
- static int is_utf8 = -1;
- if (G_UNLIKELY(is_utf8 == -1))
- is_utf8 = g_get_charset(&dummy) ? 1 : 0;
-
- return !!is_utf8;
+ static const bool is_utf8{g_get_charset({}) ? true : false};
+ return is_utf8;
}
bool
diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh
index 5758c31..ae953a3 100644
--- a/lib/utils/mu-utils.hh
+++ b/lib/utils/mu-utils.hh
@@ -217,7 +217,7 @@ utf8_flatten(const std::string& s) {
*
* @return a cleaned-up string.
*/
-std::string utf8_clean(const std::string& dirty);
+[[nodiscard]] std::string utf8_clean(const std::string& dirty);
/**
@@ -227,7 +227,7 @@ std::string utf8_clean(const std::string& dirty);
*
* @return string
*/
-std::string utf8_wordbreak(const std::string& txt);
+[[nodiscard]] std::string utf8_wordbreak(const std::string& txt);
/**
@@ -238,7 +238,7 @@ std::string utf8_wordbreak(const std::string& txt);
*
* @return the string without control characters
*/
-std::string remove_ctrl(const std::string& str);
+[[nodiscard]] std::string remove_ctrl(const std::string& str);
/**
* Split a string in parts. As a special case, splitting an empty string
@@ -249,7 +249,8 @@ std::string remove_ctrl(const std::string& str);
*
* @return the parts.
*/
-std::vector<std::string> split(const std::string& str, const std::string& sepa);
+[[nodiscard]] std::vector<std::string> split(const std::string& str,
+ const std::string& sepa);
/**
* Split a string in parts. As a special case, splitting an empty string
@@ -270,8 +271,10 @@ std::vector<std::string> split(const std::string& str, char sepa);
*
* @return string
*/
-std::string join(const std::vector<std::string>& svec, const std::string& sepa);
-static inline std::string join(const std::vector<std::string>& svec, char sepa) {
+[[nodiscard]] std::string join(const std::vector<std::string>& svec,
+ const std::string& sepa);
+[[nodiscard]] static inline std::string join(const std::vector<std::string>& svec,
+ char sepa) {
return join(svec, std::string(1, sepa));
}
@@ -313,7 +316,7 @@ static inline bool mu_print_encoded(fmt::format_string<T...> frm, T&&... args) n
*/
constexpr ::time_t time_t_min = 0;
constexpr ::time_t time_t_max = std::numeric_limits<::time_t>::max();
-constexpr ::time_t to_time_t(int64_t t) {
+[[nodiscard]] constexpr ::time_t to_time_t(int64_t t) {
return std::clamp(t,
static_cast<int64_t>(time_t_min),
static_cast<int64_t>(time_t_max));
@@ -332,7 +335,8 @@ constexpr ::time_t to_time_t(int64_t t) {
*
* @return the corresponding time_t or Nothing if parsing failed.
*/
-Option<::time_t> parse_date_time(const std::string& date, bool first, bool use_utc=false);
+[[nodiscard]] Option<::time_t> parse_date_time(const std::string& date,
+ bool first, bool use_utc=false);
/**
* Crudely convert HTML to plain text. This attempts to scrape the
@@ -342,7 +346,7 @@ Option<::time_t> parse_date_time(const std::string& date, bool first, bool use_u
*
* @return plain text
*/
-std::string html_to_text(const std::string& html);
+[[nodiscard]] std::string html_to_text(const std::string& html);
/**
* Hack to avoid locale crashes