clang-format: update c/cc coding style

Update all cc code using .clang-format; please do so as well for future PRs
etc.; emacs has a handy 'clang-format' mode to make this automatic.

For comparing old changes with git blame, we can disregard this one using
--ignore-rev

(see https://www.moxio.com/blog/43/ignoring-bulk-change-commits-with-git-blame )
This commit is contained in:
Dirk-Jan C. Binnema
2021-10-20 12:18:15 +03:00
parent 09935cc4b3
commit 3dd721d5a3
111 changed files with 13851 additions and 14579 deletions

View File

@ -42,8 +42,12 @@ using StringVec = std::vector<std::string>;
*
* @return a flattened string
*/
std::string utf8_flatten (const char *str);
inline std::string utf8_flatten (const std::string& s) { return utf8_flatten(s.c_str()); }
std::string utf8_flatten(const char* str);
inline std::string
utf8_flatten(const std::string& s)
{
return utf8_flatten(s.c_str());
}
/**
* Replace all control characters with spaces, and remove leading and trailing space.
@ -52,8 +56,7 @@ inline std::string utf8_flatten (const std::string& s) { return utf8_flatten(s.c
*
* @return a cleaned-up string.
*/
std::string utf8_clean (const std::string& dirty);
std::string utf8_clean(const std::string& dirty);
/**
* Remove ctrl characters, replacing them with ' '; subsequent
@ -63,8 +66,7 @@ std::string utf8_clean (const std::string& dirty);
*
* @return the string without control characters
*/
std::string remove_ctrl (const std::string& str);
std::string remove_ctrl(const std::string& str);
/**
* Split a string in parts
@ -74,8 +76,7 @@ std::string remove_ctrl (const std::string& str);
*
* @return the parts.
*/
std::vector<std::string> split (const std::string& str,
const std::string& sepa);
std::vector<std::string> split(const std::string& str, const std::string& sepa);
/**
* Quote & escape a string for " and \
@ -84,7 +85,7 @@ std::vector<std::string> split (const std::string& str,
*
* @return quoted string
*/
std::string quote (const std::string& str);
std::string quote(const std::string& str);
/**
* Format a string, printf style
@ -94,7 +95,7 @@ std::string quote (const std::string& str);
*
* @return a formatted string
*/
std::string format (const char *frm, ...) __attribute__((format(printf, 1, 2)));
std::string format(const char* frm, ...) __attribute__((format(printf, 1, 2)));
/**
* Format a string, printf style
@ -104,8 +105,7 @@ std::string format (const char *frm, ...) __attribute__((format(printf, 1, 2)));
*
* @return a formatted string
*/
std::string vformat (const char *frm, va_list args) __attribute__((format(printf, 1, 0)));
std::string vformat(const char* frm, va_list args) __attribute__((format(printf, 1, 0)));
/**
* Convert an date to the corresponding time expressed as a string with a
@ -118,7 +118,7 @@ std::string vformat (const char *frm, va_list args) __attribute__((format(printf
*
* @return the corresponding time_t expressed as a string
*/
std::string date_to_time_t_string (const std::string& date, bool first);
std::string date_to_time_t_string(const std::string& date, bool first);
/**
* 64-bit incarnation of time_t expressed as a 10-digit string. Uses 64-bit for the time-value,
@ -128,37 +128,52 @@ std::string date_to_time_t_string (const std::string& date, bool first);
*
* @return
*/
std::string date_to_time_t_string (int64_t t);
std::string date_to_time_t_string(int64_t t);
using Clock = std::chrono::steady_clock;
using Duration = Clock::duration;
template <typename Unit> constexpr int64_t to_unit (Duration d) {
using namespace std::chrono;
return duration_cast<Unit>(d).count();
template <typename Unit>
constexpr int64_t
to_unit(Duration d)
{
using namespace std::chrono;
return duration_cast<Unit>(d).count();
}
constexpr int64_t to_s (Duration d) { return to_unit<std::chrono::seconds>(d); }
constexpr int64_t to_ms (Duration d) { return to_unit<std::chrono::milliseconds>(d); }
constexpr int64_t to_us (Duration d) { return to_unit<std::chrono::microseconds>(d); }
constexpr int64_t
to_s(Duration d)
{
return to_unit<std::chrono::seconds>(d);
}
constexpr int64_t
to_ms(Duration d)
{
return to_unit<std::chrono::milliseconds>(d);
}
constexpr int64_t
to_us(Duration d)
{
return to_unit<std::chrono::microseconds>(d);
}
struct StopWatch {
using Clock = std::chrono::steady_clock;
StopWatch (const std::string name):
start_{Clock::now()},
name_{name} {}
~StopWatch() {
const auto us{to_us(Clock::now()-start_)};
if (us > 2000000)
g_debug ("%s: finished after %0.1f s", name_.c_str(), us/1000000.0);
else if (us > 2000)
g_debug ("%s: finished after %0.1f ms", name_.c_str(), us/1000.0);
else
g_debug ("%s: finished after %" G_GINT64_FORMAT " us", name_.c_str(), us);
}
private:
Clock::time_point start_;
std::string name_;
using Clock = std::chrono::steady_clock;
StopWatch(const std::string name) : start_{Clock::now()}, name_{name} {}
~StopWatch()
{
const auto us{to_us(Clock::now() - start_)};
if (us > 2000000)
g_debug("%s: finished after %0.1f s", name_.c_str(), us / 1000000.0);
else if (us > 2000)
g_debug("%s: finished after %0.1f ms", name_.c_str(), us / 1000.0);
else
g_debug("%s: finished after %" G_GINT64_FORMAT " us", name_.c_str(), us);
}
private:
Clock::time_point start_;
std::string name_;
};
/**
@ -179,7 +194,7 @@ std::string canonicalize_filename(const std::string& path, const std::string& re
*
* @return the size expressed as a string with the decimal number of bytes
*/
std::string size_to_string (const std::string& sizestr, bool first);
std::string size_to_string(const std::string& sizestr, bool first);
/**
* Convert a size into a size in bytes string
@ -189,8 +204,7 @@ std::string size_to_string (const std::string& sizestr, bool first);
*
* @return the size expressed as a string with the decimal number of bytes
*/
std::string size_to_string (int64_t size);
std::string size_to_string(int64_t size);
/**
* Convert any ostreamable<< value to a string
@ -200,90 +214,98 @@ std::string size_to_string (int64_t size);
* @return a std::string
*/
template <typename T>
static inline std::string to_string (const T& val)
static inline std::string
to_string(const T& val)
{
std::stringstream sstr;
sstr << val;
std::stringstream sstr;
sstr << val;
return sstr.str();
return sstr.str();
}
struct MaybeAnsi {
explicit MaybeAnsi(bool use_color): color_{use_color} {}
explicit MaybeAnsi(bool use_color) : color_{use_color} {}
enum struct Color {
Black = 30,
Red = 31,
Green = 32,
Yellow = 33,
Blue = 34,
Magenta = 35,
Cyan = 36,
White = 37,
enum struct Color {
Black = 30,
Red = 31,
Green = 32,
Yellow = 33,
Blue = 34,
Magenta = 35,
Cyan = 36,
White = 37,
BrightBlack = 90,
BrightRed = 91,
BrightGreen = 92,
BrightYellow = 93,
BrightBlue = 94,
BrightMagenta = 95,
BrightCyan = 96,
BrightWhite = 97,
};
BrightBlack = 90,
BrightRed = 91,
BrightGreen = 92,
BrightYellow = 93,
BrightBlue = 94,
BrightMagenta = 95,
BrightCyan = 96,
BrightWhite = 97,
};
std::string fg(Color c) const { return ansi(c, true); }
std::string bg(Color c) const { return ansi(c, false); }
std::string fg(Color c) const { return ansi(c, true); }
std::string bg(Color c) const { return ansi(c, false); }
std::string reset() const { return color_ ? "\x1b[0m" : ""; }
std::string reset() const { return color_ ? "\x1b[0m" : ""; }
private:
std::string ansi(Color c, bool fg=true) const {
return color_ ? format("\x1b[%dm", static_cast<int>(c) + (fg ? 0 : 10)) : "";
}
private:
std::string ansi(Color c, bool fg = true) const
{
return color_ ? format("\x1b[%dm", static_cast<int>(c) + (fg ? 0 : 10)) : "";
}
const bool color_;
const bool color_;
};
template <typename Func> void xapian_try(Func&& func) noexcept try {
func();
} catch (const Xapian::Error &xerr) {
g_critical ("%s: xapian error '%s'",
__func__, xerr.get_msg().c_str());
template <typename Func>
void
xapian_try(Func&& func) noexcept
try {
func();
} catch (const Xapian::Error& xerr) {
g_critical("%s: xapian error '%s'", __func__, xerr.get_msg().c_str());
} catch (const std::runtime_error& re) {
g_critical ("%s: error: %s", __func__, re.what());
g_critical("%s: error: %s", __func__, re.what());
} catch (...) {
g_critical ("%s: caught exception", __func__);
g_critical("%s: caught exception", __func__);
}
template <typename Func, typename Default=std::invoke_result<Func>>
auto xapian_try(Func&& func, Default&& def) noexcept -> std::decay_t<decltype(func())> try {
return func();
} catch (const Xapian::Error &xerr) {
g_critical ("%s: xapian error '%s'",
__func__, xerr.get_msg().c_str());
template <typename Func, typename Default = std::invoke_result<Func>>
auto
xapian_try(Func&& func, Default&& def) noexcept -> std::decay_t<decltype(func())>
try {
return func();
} catch (const Xapian::Error& xerr) {
g_critical("%s: xapian error '%s'", __func__, xerr.get_msg().c_str());
return static_cast<Default>(def);
} catch (const std::runtime_error& re) {
g_critical ("%s: error: %s", __func__, re.what());
g_critical("%s: error: %s", __func__, re.what());
return static_cast<Default>(def);
} catch (...) {
g_critical ("%s: caught exception", __func__);
g_critical("%s: caught exception", __func__);
return static_cast<Default>(def);
}
/// Allow using enum structs as bitflags
#define MU_TO_NUM(ET,ELM) std::underlying_type_t<ET>(ELM)
#define MU_TO_ENUM(ET,NUM) static_cast<ET>(NUM)
#define MU_ENABLE_BITOPS(ET) \
constexpr ET operator& (ET e1, ET e2) { return MU_TO_ENUM(ET,MU_TO_NUM(ET,e1)&MU_TO_NUM(ET,e2)); } \
constexpr ET operator| (ET e1, ET e2) { return MU_TO_ENUM(ET,MU_TO_NUM(ET,e1)|MU_TO_NUM(ET,e2)); } \
constexpr ET operator~ (ET e) { return MU_TO_ENUM(ET,~(MU_TO_NUM(ET, e))); } \
constexpr bool any_of(ET e) { return MU_TO_NUM(ET,e) != 0; } \
constexpr bool none_of(ET e) { return MU_TO_NUM(ET,e) == 0; } \
static inline ET& operator&=(ET& e1, ET e2) { return e1 = e1 & e2;} \
static inline ET& operator|=(ET& e1, ET e2) { return e1 = e1 | e2;}
#define MU_TO_NUM(ET, ELM) std::underlying_type_t<ET>(ELM)
#define MU_TO_ENUM(ET, NUM) static_cast<ET>(NUM)
#define MU_ENABLE_BITOPS(ET) \
constexpr ET operator&(ET e1, ET e2) \
{ \
return MU_TO_ENUM(ET, MU_TO_NUM(ET, e1) & MU_TO_NUM(ET, e2)); \
} \
constexpr ET operator|(ET e1, ET e2) \
{ \
return MU_TO_ENUM(ET, MU_TO_NUM(ET, e1) | MU_TO_NUM(ET, e2)); \
} \
constexpr ET operator~(ET e) { return MU_TO_ENUM(ET, ~(MU_TO_NUM(ET, e))); } \
constexpr bool any_of(ET e) { return MU_TO_NUM(ET, e) != 0; } \
constexpr bool none_of(ET e) { return MU_TO_NUM(ET, e) == 0; } \
static inline ET& operator&=(ET& e1, ET e2) { return e1 = e1 & e2; } \
static inline ET& operator|=(ET& e1, ET e2) { return e1 = e1 | e2; }
/**
* For unit tests, assert two std::string's are equal.
@ -298,7 +320,7 @@ void assert_equal(const std::string& s1, const std::string& s2);
* @param c1 container1
* @param c2 container2
*/
void assert_equal (const StringVec& v1, const StringVec& v2);
void assert_equal(const StringVec& v1, const StringVec& v2);
/**
* For unit-tests, allow warnings in the current function.
@ -308,5 +330,4 @@ void allow_warnings();
} // namespace Mu
#endif /* __MU_UTILS_HH__ */