diff --git a/manual.lyx b/manual.lyx index 891e5591..4c018539 100644 --- a/manual.lyx +++ b/manual.lyx @@ -1458,6 +1458,15 @@ Return string of length bits where ith character is ith character of on Out of range reads give last character, or '*'/'-' if empty. \end_layout +\begin_layout Subsubsection +bit.rflagdecode(number a, number bits, [string on, [string off]]) +\end_layout + +\begin_layout Standard +Like bit.flagdecode, but outputs the string in the opposite order (most significa +nt bit first). +\end_layout + \begin_layout Subsection Table gui: \end_layout diff --git a/manual.txt b/manual.txt index 33cf73de..70a75f9e 100644 --- a/manual.txt +++ b/manual.txt @@ -714,6 +714,12 @@ Return string of length bits where ith character is ith character of on if bit i is on, otherwise ith character of off. Out of range reads give last character, or '*'/'-' if empty. +8.2.18 bit.rflagdecode(number a, number bits, [string on, [string + off]]) + +Like bit.flagdecode, but outputs the string in the opposite order +(most significant bit first). + 8.3 Table gui: Most of these functions can only be called in on_paint and diff --git a/src/lua/bit.cpp b/src/lua/bit.cpp index 1d2bfdf7..2cf17486 100644 --- a/src/lua/bit.cpp +++ b/src/lua/bit.cpp @@ -1,4 +1,5 @@ #include "lua/internal.hpp" +#include "library/minmax.hpp" #define BITWISE_BITS 48 #define BITWISE_MASK ((1ULL << (BITWISE_BITS)) - 1) @@ -204,7 +205,8 @@ namespace return 2; }); - function_ptr_luafun lua_flagdecode(LS, "bit.flagdecode", [](lua_state& L, const std::string& fname) -> int { + int flagdecode_core(lua_state& L, const std::string& fname, bool reverse) + { uint64_t a = L.get_numeric_argument(1, fname.c_str()); uint64_t b = L.get_numeric_argument(2, fname.c_str()); std::string on, off; @@ -218,14 +220,23 @@ namespace char offc = offl ? off[offl - 1] : '-'; char buffer[65]; unsigned i; + size_t bias = min(b, (uint64_t)64) - 1; for(i = 0; i < 64 && i < b; i++) { char onc2 = (i < onl) ? on[i] : onc; char offc2 = (i < offl) ? off[i] : offc; - buffer[i] = ((a >> i) & 1) ? onc2 : offc2; + buffer[reverse ? (bias - i) : i] = ((a >> i) & 1) ? onc2 : offc2; } buffer[i] = '\0'; L.pushstring(buffer); return 1; + } + + function_ptr_luafun lua_flagdecode(LS, "bit.flagdecode", [](lua_state& L, const std::string& fname) -> int { + return flagdecode_core(L, fname, false); + }); + + function_ptr_luafun lua_rflagdecode(LS, "bit.rflagdecode", [](lua_state& L, const std::string& fname) -> int { + return flagdecode_core(L, fname, true); }); lua_symmetric_bitwise bit_none("bit.none");