option: rename to to_string_opt and to_string_view_opt

to_option_string -> to_string_opt
to_string_view   -> to_string_view_opt
This commit is contained in:
Dirk-Jan C. Binnema
2022-04-16 13:16:29 +03:00
parent 4f9c154d1a
commit 7c51bc68d4
9 changed files with 104 additions and 77 deletions

View File

@ -58,6 +58,7 @@ libmu_utils_la_SOURCES= \
mu-logger.cc \
mu-logger.hh \
mu-option.hh \
mu-option.cc \
mu-readline.cc \
mu-readline.hh \
mu-result.hh \

View File

@ -23,6 +23,7 @@ lib_mu_utils=static_library('mu-utils', [
'mu-logger.cc',
'mu-logger.hh',
'mu-option.hh',
'mu-option.cc',
'mu-readline.cc',
'mu-readline.hh',
'mu-result.hh',

View File

@ -15,6 +15,7 @@
#define MU_OPTION__
#include "optional.hpp"
#include <string>
namespace Mu {
@ -29,5 +30,31 @@ Some(T&& t)
}
constexpr auto Nothing = tl::nullopt; // 'None' is take already
/**
* Maybe create a string from a const char pointer.
*
* @param str a char pointer or NULL
*
* @return option with either the string or nothing if str was NULL.
*/
Option<std::string>
static inline to_string_opt(const char* str) {
if (str)
return std::string{str};
else
return Nothing;
}
/**
* Like maybe_string that takes a const char*, but additionally,
* g_free() the string.
*
* @param str char pointer or NULL (consumed)
*
* @return option with either the string or nothing if str was NULL.
*/
Option<std::string> to_string_opt_gchar(char*&& str);
} // namespace Mu
#endif /*MU_OPTION__*/

View File

@ -43,6 +43,7 @@
#include "mu-util.h"
#include "mu-str.h"
#include "mu-error.hh"
#include "mu-option.hh"
using namespace Mu;
@ -352,14 +353,12 @@ Mu::time_to_string(const std::string& frm, time_t t, bool utc)
return {};
}
char* str = g_date_time_format(dt, frm.c_str()); /* always utf8 */
auto datestr{to_string_opt_gchar(g_date_time_format(dt, frm.c_str()))};
g_date_time_unref(dt);
if (!str) {
if (!datestr)
g_warning("failed to format time with format '%s'", frm.c_str());
return {};
}
return from_gchars(std::move(str)/*consumed*/);
return datestr.value_or("");
}
static std::string

View File

@ -174,22 +174,22 @@ std::string date_to_time_t_string(int64_t t);
std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G_GNUC_CONST;
/**
* Create a std::string by consuming a gchar* array; this takes ownership
* of str which should no longer be used.
*
* @param str a gchar* or NULL (latter taken as "")
*
* @return a std::string
*/
static inline std::string
from_gchars(gchar*&& str)
{
std::string s{str ? str : ""};
g_free(str);
// /**
// * Create a std::string by consuming a gchar* array; this takes ownership
// * of str which should no longer be used.
// *
// * @param str a gchar* or NULL (latter taken as "")
// *
// * @return a std::string
// */
// static inline std::string
// from_gchars(gchar*&& str)
// {
// std::string s{str ? str : ""};
// g_free(str);
return s;
}
// return s;
// }
// https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member
template <auto fn>