<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FErutuon%2FUTF-8</id>
	<title>Module:Sandbox/Erutuon/UTF-8 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FErutuon%2FUTF-8"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Erutuon/UTF-8&amp;action=history"/>
	<updated>2026-05-27T10:36:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/Erutuon/UTF-8&amp;diff=145388&amp;oldid=prev</id>
		<title>imported&gt;Erutuon: Erutuon moved page Module:Sandbox/Erutuon/Unicode to Module:Sandbox/Erutuon/UTF-8 without leaving a redirect</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Erutuon/UTF-8&amp;diff=145388&amp;oldid=prev"/>
		<updated>2018-07-06T18:58:43Z</updated>

		<summary type="html">&lt;p&gt;Erutuon moved page &lt;a href=&quot;/research/Module:Sandbox/Erutuon/Unicode&quot; title=&quot;Module:Sandbox/Erutuon/Unicode&quot;&gt;Module:Sandbox/Erutuon/Unicode&lt;/a&gt; to &lt;a href=&quot;/research/Module:Sandbox/Erutuon/UTF-8&quot; title=&quot;Module:Sandbox/Erutuon/UTF-8&quot;&gt;Module:Sandbox/Erutuon/UTF-8&lt;/a&gt; without leaving a redirect&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
local bit = require(&amp;quot;bit32&amp;quot;)&lt;br /&gt;
local band = bit.band&lt;br /&gt;
local rshift = bit.rshift&lt;br /&gt;
&lt;br /&gt;
function table.forEach(t, func)&lt;br /&gt;
	for i, item in ipairs(t) do&lt;br /&gt;
		func(item)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function setMt(arr)&lt;br /&gt;
	return setmetatable(arr, { __index =  table })&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Converts a string representing a number in binary base to a Lua number.&lt;br /&gt;
local function binary(stringBinary)&lt;br /&gt;
	return tonumber(stringBinary, 2)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find the digit at a certain position in a byte.&lt;br /&gt;
local function digitAt(number, index)&lt;br /&gt;
	if type(number) == &amp;quot;string&amp;quot; then&lt;br /&gt;
		number = binary(number)&lt;br /&gt;
	end&lt;br /&gt;
	return band(rshift(number, 8 - index), 1)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Returns a table containing bits in a byte, from highest to lowest.&lt;br /&gt;
local function getBits(byte)&lt;br /&gt;
	local t = {}&lt;br /&gt;
	for bit = 8, 1, -1 do&lt;br /&gt;
		t[bit] = band(byte, 1)&lt;br /&gt;
		byte = rshift(byte, 1)&lt;br /&gt;
	end&lt;br /&gt;
	return t&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- mw.log(table.concat(getBits(rshift(binary(&amp;quot;11100001&amp;quot;), 8 - 3))))&lt;br /&gt;
&lt;br /&gt;
-- Do something to each byte in a string; put the result in a table.&lt;br /&gt;
local function iterBytes(str, func)&lt;br /&gt;
	local out = {}&lt;br /&gt;
	for i = 1, #str do&lt;br /&gt;
		table.insert(out, func(string.byte(str, i)))&lt;br /&gt;
	end&lt;br /&gt;
	return out&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function makeTag(color)&lt;br /&gt;
	return { &amp;#039;&amp;lt;span style=&amp;quot;color: &amp;#039; .. color .. &amp;#039;;&amp;quot;&amp;gt;&amp;#039;, &amp;#039;&amp;lt;/span&amp;gt;&amp;#039; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find leading digits marking ASCII, leading bytes, or continuation bytes,&lt;br /&gt;
-- else tag byte as red.&lt;br /&gt;
local function markDigits(byteTable)&lt;br /&gt;
	local onesCount = 0&lt;br /&gt;
	setMt(byteTable)&lt;br /&gt;
	for i, digit in ipairs(byteTable) do&lt;br /&gt;
		if digit == 1 then&lt;br /&gt;
			onesCount = onesCount + 1&lt;br /&gt;
			if onesCount &amp;gt; 4 then&lt;br /&gt;
				local tag = makeTag(&amp;quot;red&amp;quot;)&lt;br /&gt;
				byteTable:insert(#byteTable, tag[2])&lt;br /&gt;
				byteTable:insert(1, tag[1])&lt;br /&gt;
			end&lt;br /&gt;
		else&lt;br /&gt;
			local tag&lt;br /&gt;
			-- ASCII (0x00 - 0x7F)&lt;br /&gt;
			if onesCount == 0 then&lt;br /&gt;
				tag = makeTag(&amp;quot;darkgray&amp;quot;)&lt;br /&gt;
			&lt;br /&gt;
			-- continuation bytes&lt;br /&gt;
			elseif onesCount == 1 then&lt;br /&gt;
				tag = makeTag(&amp;quot;chocolate&amp;quot;)&lt;br /&gt;
			&lt;br /&gt;
			-- leading bytes&lt;br /&gt;
			else&lt;br /&gt;
				tag = makeTag(&amp;quot;deeppink&amp;quot;)&lt;br /&gt;
			end&lt;br /&gt;
			byteTable:insert(i + 1, tag[2])&lt;br /&gt;
			byteTable:insert(1, tag[1])&lt;br /&gt;
			return byteTable&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return byteTable&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function printBytes(str)&lt;br /&gt;
	return table.concat(&lt;br /&gt;
		iterBytes(&lt;br /&gt;
			str,&lt;br /&gt;
			function(byte)&lt;br /&gt;
				return table.concat(markDigits(getBits(byte)))&lt;br /&gt;
			end&lt;br /&gt;
		),&lt;br /&gt;
		&amp;quot; &amp;quot;&lt;br /&gt;
	)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function makeCharByteTables(str)&lt;br /&gt;
	local chars = setMt {}&lt;br /&gt;
	local bytes = setMt {}&lt;br /&gt;
	for char in mw.ustring.gmatch(str, &amp;quot;.&amp;quot;) do&lt;br /&gt;
		chars:insert(char)&lt;br /&gt;
		bytes:insert(printBytes(char))&lt;br /&gt;
	end&lt;br /&gt;
	return chars, bytes&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function print(chars, bytes)&lt;br /&gt;
	setMt(chars)&lt;br /&gt;
	setMt(bytes)&lt;br /&gt;
	local output = setMt { &amp;#039;{| class=&amp;quot;wikitable&amp;quot;&amp;#039; }&lt;br /&gt;
	chars:forEach(&lt;br /&gt;
		function(char)&lt;br /&gt;
			output:insert(&amp;quot;| &amp;quot; .. char)&lt;br /&gt;
		end&lt;br /&gt;
	)&lt;br /&gt;
	output:insert(&amp;quot;|-&amp;quot;)&lt;br /&gt;
	bytes:forEach(&lt;br /&gt;
		function(byteString)&lt;br /&gt;
			output:insert(&amp;quot;| &amp;lt;code&amp;gt;&amp;quot; .. byteString .. &amp;quot;&amp;lt;/code&amp;gt;&amp;quot;)&lt;br /&gt;
		end&lt;br /&gt;
	)&lt;br /&gt;
	output:insert(&amp;quot;|}&amp;quot;)&lt;br /&gt;
	return output:concat(&amp;quot;\n&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.show(frame)&lt;br /&gt;
	local str = frame.args[1] or &amp;quot;abc πρᾶγμᾰ&amp;quot;&lt;br /&gt;
	return print(makeCharByteTables(str))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Erutuon</name></author>
	</entry>
</feed>