Lua: bit.rflagdecode

This commit is contained in:
Ilari Liusvaara 2013-01-23 08:48:27 +02:00
parent 0763a00bef
commit 242ce42bb7
3 changed files with 28 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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<uint64_t>(1, fname.c_str());
uint64_t b = L.get_numeric_argument<uint64_t>(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<combine_none, BITWISE_MASK> bit_none("bit.none");