clboss/Util/Str.hpp
Ken Sedgwick 10ffad5099
Util::Str::group_digits -- readable msat/sat amounts in the xrebalance logs
Large amounts in the XRebalancer / XMoveFunds log lines were
unbroken digit runs (delivered 18851040 msat); grep-heavy log
sessions kept miscounting them.  Add Util::Str::group_digits,
which renders an integer with an underscore every three digit
places (18_851_040), matching the digit grouping
clboss-xrebalance-view and the other contrib tools already
print.

Applied to the amounts that get large:
- XRebalancer cycle line (derived N, request)
- XRebalancer transfer-done line (delivered, fee)
- XMoveFunds planning line (amount, maxfee)
- XMoveFunds 204 summary and fee-picture diagnostics
  (alloc_fee, required_out)
- XMoveFunds per-part budget refusals (would deliver)
- format_route_fees per-hop table (in/out/fee)

Counts, ppm values, and cltv deltas stay plain.  Signed
overload handles the int64 call sites (INT64_MIN-safe).

New tests/util/test_str covers both overloads and the
boundary cases.
2026-08-04 11:02:00 -07:00

54 lines
1.4 KiB
C++

#ifndef UTIL_STR_HPP
#define UTIL_STR_HPP
/*
* Minor string utilities.
*/
#include"Util/BacktraceException.hpp"
#include<cstdint>
#include<stdarg.h>
#include<stdexcept>
#include<string>
#include<vector>
namespace Util {
namespace Str {
/* Outputs a two-digit hex string of the given byte. */
std::string hexbyte(std::uint8_t);
/* Outputs a string of the given data. */
std::string hexdump(void const* p, std::size_t s);
/* Creates a buffer from the given hex string. */
struct HexParseFailure : public Util::BacktraceException<std::runtime_error> {
HexParseFailure(std::string msg)
: Util::BacktraceException<std::runtime_error>("hexread: " + msg) { }
};
std::vector<std::uint8_t> hexread(std::string const&);
/* Checks that the given string is a hex string with an
* even number of digits.
*/
bool ishex(std::string const&);
std::string trim(std::string const& s);
/* Renders an integer with '_' between every three digit places
* (18851040 -> "18_851_040"), for log lines carrying large msat/sat
* amounts. Matches the digit grouping clboss-xrebalance-view and
* the other contrib tools print. */
std::string group_digits(std::uint64_t);
std::string group_digits(std::int64_t);
/* Like `sprintf`. */
std::string fmt(char const *tpl, ...)
#if HAVE_ATTRIBUTE_FORMAT
__attribute__ ((format (printf, 1, 2)))
#endif
;
std::string vfmt(char const *tpl, va_list ap);
}}
#endif /* !defined(UTIL_STR_HPP) */