Module:Sandbox/Erutuon/UTF-8

From blackwiki
< Module:Sandbox‎ | Erutuon
Revision as of 05:07, 17 September 2017 by blackwiki>Erutuon (printing binary digits for characters)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

An experiment with printing binary digit representations of characters.

Usage

Script error: The module returned a nil value. It is supposed to return an export table.



local p = {}

local bit = require("bit32")
local band = bit.band
local rshift = bit.rshift

local function getBits(byte)
    local t = {}
    for bit = 8, 1, -1 do
        t[bit] = band(byte, 1)
        byte = rshift(byte, 1)
    end
    return t
end

local function iterBytes(str, func)
    local out = {}
    for i = 1, #str do
        table.insert(out, func(string.byte(str, i)))
    end
    return out
end

function p.show(frame)
	local str = frame.args[1] or "ᾰ̓τῑμᾰ́ζω"
    return table.concat(
        iterBytes(
            str,
            function(byte)
                return table.concat(getBits(byte))
            end
        ),
        " "
    )
end