Modul:Expr: Unterschied zwischen den Versionen
+ base62
K (Änderte den Schutz von „Modul:Expr“: Entsperrwunsch: Spezial:Permalink/135673840#MediaWiki et al. ([Bearbeiten=Nur angemeldete, nicht neue Benutzer] (unbeschränkt) [Verschieben=Nur Administratoren] (unbeschränkt))) |
(+ base62) |
||
| Zeile 1: | Zeile 1: | ||
--[=[ | --[=[ 2014-09-24 | ||
Expr | Expr | ||
* max | * max | ||
| Zeile 45: | Zeile 45: | ||
end | end | ||
if not r then | if not r then | ||
r = "(((".. say .. ")))" | r = "(((" .. say .. ")))" | ||
end | end | ||
return r | return r | ||
| Zeile 84: | Zeile 84: | ||
return r | return r | ||
end -- expr() | end -- expr() | ||
local function base62( value ) | |||
-- Convert number from and to base62 encoding | |||
-- Precondition: | |||
-- value -- number or string to be converted | |||
-- number: to base62 | |||
-- string: base62 to number | |||
-- Lua limitation at 10^53; larger numbers are less precise | |||
-- Postcondition: | |||
-- returns string, or number, or false | |||
local r = false | |||
local state = type( value ) | |||
if state == "number" then | |||
local k = math.floor( value ) | |||
if k == value and value > 0 then | |||
local m | |||
r = "" | |||
while k > 0 do | |||
m = k % 62 | |||
k = ( k - m ) / 62 | |||
if m >= 36 then | |||
m = m + 61 | |||
elseif m >= 11 then | |||
m = m + 55 | |||
else | |||
m = m + 48 | |||
end | |||
r = string.char( m ) .. r | |||
end | |||
elseif value == 0 then | |||
r = "0" | |||
end | |||
elseif state == "string" then | |||
if value:match( "^%w+$" ) then | |||
local n = #value | |||
local k = 1 | |||
local c | |||
r = 0 | |||
for i = n, 1, -1 do | |||
c = value:byte( i, i ) | |||
if c >= 48 and c <= 57 then | |||
c = c - 48 | |||
elseif c >= 65 and c <= 90 then | |||
c = c - 55 | |||
elseif c >= 97 and c <= 122 then | |||
c = c - 61 | |||
else -- How comes? | |||
r = nil | |||
break -- for i | |||
end | |||
r = r + c * k | |||
k = k * 62 | |||
end -- for i | |||
end | |||
end | |||
return r | |||
end -- base62() | |||
| Zeile 133: | Zeile 192: | ||
elseif scope == "number" then | elseif scope == "number" then | ||
n = v | n = v | ||
else | |||
n = false | |||
end | end | ||
if n then | if n then | ||
| Zeile 173: | Zeile 234: | ||
-- Export | -- Export | ||
local p = {} | local p = {} | ||
function p.base62( frame ) | |||
local r | |||
local s = frame.args[ 1 ] | |||
if s then | |||
local s2 = frame.args[ 2 ] | |||
if s2 then | |||
s2 = mw.text.trim( s2 ) | |||
end | |||
if s2 == "D2B" then | |||
s = tonumber( s ) | |||
else | |||
s = mw.text.trim( s ) | |||
s2 = false | |||
end | |||
r = base62( s ) | |||
if r and not s2 then | |||
r = string.format( "%17d", r ) | |||
end | |||
end | |||
return r or "" | |||
end | |||
function p.max( frame ) | function p.max( frame ) | ||
| Zeile 193: | Zeile 276: | ||
function p.Expr( f, a ) | function p.Expr( f, a ) | ||
local r = false | local r = false | ||
if f == "min" or f == "max" then | if f == "min" or f == "max" then | ||
local frame = mw.getCurrentFrame() | |||
local low = ( f == "min" ) | |||
local lucky | local lucky | ||
lucky, r = pcall( minmax, a, frame, low, true ) | lucky, r = pcall( minmax, a, frame, low, true ) | ||
elseif f == "base62" then | |||
r = base62( a ) | |||
end | end | ||
return r | return r | ||