Skip to content

Instantly share code, notes, and snippets.

@CandyMi
Created May 31, 2026 07:24
Show Gist options
  • Select an option

  • Save CandyMi/5ac3ab22a74425690a337b12df2f9941 to your computer and use it in GitHub Desktop.

Select an option

Save CandyMi/5ac3ab22a74425690a337b12df2f9941 to your computer and use it in GitHub Desktop.
yeast 算法 编码解码

yeast 算法 的 Lua 版 实现

local yeast = require "yeast"

local ts = 1780208984973

local ystr = yeast.encode(ts)
print(ystr)
print(yeast.decode(ystr) == ts, yeast.decode(ystr))

``bash PvykMED true 1780208984973

local ch2idx = {
['0']=0, ['1']=1, ['2']=2, ['3']=3, ['4']=4, ['5']=5, ['6']=6, ['7']=7, ['8']=8, ['9']=9,
['A']=10, ['B']=11, ['C']=12, ['D']=13, ['E']=14, ['F']=15, ['G']=16, ['H']=17, ['I']=18, ['J']=19,
['K']=20, ['L']=21, ['M']=22, ['N']=23, ['O']=24, ['P']=25, ['Q']=26, ['R']=27, ['S']=28, ['T']=29,
['U']=30, ['V']=31, ['W']=32, ['X']=33, ['Y']=34, ['Z']=35,
['a']=36, ['b']=37, ['c']=38, ['d']=39, ['e']=40, ['f']=41, ['g']=42, ['h']=43, ['i']=44, ['j']=45,
['k']=46, ['l']=47, ['m']=48, ['n']=49, ['o']=50, ['p']=51, ['q']=52, ['r']=53, ['s']=54, ['t']=55,
['u']=56, ['v']=57, ['w']=58, ['x']=59, ['y']=60, ['z']=61,
['-']=62, ['_']=63,
}
local idx2ch = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'-', '_'
}
local yeast = { }
---comment `yeast` 编码
---@param now number
---@return string
function yeast.encode(now)
local list = { }
local i = 7
while i > 0 do
local n = now & 63
list[i] = idx2ch[n+1]
now = (now - n) >> 6
i = i - 1
end
return table.concat(list)
end
---comment `yeast` 解码
---@param ystr string
---@return number?
function yeast.decode(ystr)
local result = 0
for i = 1, #ystr do
local v = ch2idx[ystr:sub(i, i)]
if not v then
return nil
end
result = result * 64 + v
end
return result
end
return yeast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment