模組:EncoderUtil
外观
![]() | 此模块被引用於約691,000個頁面。 為了避免造成大規模的影響,所有對此模块的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模块中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
使用方法
編碼
- 本模組._jsonEncode
能將Lua編碼為JSON的函數。與mw.text.jsonEncode不同在於,mw.text.jsonEncode遇到函數物件會出錯,而本模組的_jsonEncode會將函數物件以替代符號表示而不會出錯,能正常轉換各種Lua物件。
不支援模板直接呼叫。
解碼
JSON解碼僅需要使用mw.text.jsonDecode即可,本模組無特別提供。
解碼
- 本模組.yamlDecode
原始碼 | 輸出 |
---|---|
{{#invoke:EncoderUtil|yamlDecode|
---
receipt: Oz-Ware Purchase Invoice
date: 2012-08-06
customer:
given: Dorothy
family: Gale}}
|
<strong class="error"><span class="scribunto-error mw-scribunto-error-2cc7ff5b">脚本错误:函数“yamlDecode”不存在。</span></strong>
|
- 本模組.yaml2json
將YAML轉換為JSON。支援模板直接呼叫。
原始碼 | 輸出 |
---|---|
{{#invoke:EncoderUtil|yaml2json|
---
receipt: Oz-Ware Purchase Invoice
date: 2012-08-06
customer:
given: Dorothy
family: Gale}}
|
<strong class="error"><span class="scribunto-error mw-scribunto-error-139dbf57">脚本错误:函数“yaml2json”不存在。</span></strong>
|
二进制資料流
本函數的功能並非設計給數字進行进制轉換之用,而是資料流的編解碼。若需要將數字進行进制轉換,請參考Module:BaseConvert或Module:BigNumber提供的convertBase函數。
編碼
- 本模組._toBinary(x,digits)
能將一整數轉換成指定位數的二進制資料串。(供QR碼資料串編碼使用)
不支援模板直接呼叫。
- 本模組._binaryEncode(str)
能將一字串以UTF-8的格式編碼為二進制資料串。
有支援模板直接呼叫的函數binaryEncode
- 原始碼
{{#invoke:EncoderUtil|binaryEncode|維基百科}}
- 輸出→脚本错误:函数“binaryEncode”不存在。
- 本模組._hexadecimalEncode(str)
有支援模板直接呼叫的函數hexadecimalEncode
- 原始碼
{{#invoke:EncoderUtil|hexadecimalEncode|維基百科,自由的百科全書}}
- 輸出→脚本错误:函数“hexadecimalEncode”不存在。
解碼
- 本模組._binaryDecode(bin)
一個二進制資料串解碼為UTF-8格式編碼的字串。
有支援模板直接呼叫的函數binaryDecode
- 原始碼
{{#invoke:EncoderUtil|binaryDecode|111001111011011010101101111001011001111110111010111001111001100110111110111001111010011110010001}}
- 輸出→脚本错误:函数“binaryDecode”不存在。
- 本模組._hexadecimalDecode(hex)
有支援模板直接呼叫的函數hexadecimalDecode
- 原始碼
{{#invoke:EncoderUtil|hexadecimalDecode|e7b6ade59fbae799bee7a791efbc8ce887aae794b1e79a84e799bee7a791e585a8e69bb8}}
- 輸出→脚本错误:函数“hexadecimalDecode”不存在。
- 本模組._get_libqrcode()
初始化QR碼編碼程式庫。此處所有函數皆不支援模板直接呼叫。若要讓模板直接呼叫,請改用Module:QR和Module:RegularTiling提供的API(相依於本模組)。
初始化完的程式庫有以下成員:
- license:記錄函式庫三句版BSD授權條款的詳細資料。
- encode_string_numeric:將純數字字串編碼為QR碼格式的二进制資料流。
- encode_string_ascii:將純ASCII字串編碼為QR碼格式的二进制資料流。
- encode_string_binary:將UTF-8字串編碼為QR碼格式的二进制資料流。
- encode_data:根據QR碼編碼模式將輸入的資料編碼為QR碼格式的二进制資料流。
- qrcode:生成完整QR碼二維碼的二进制資料流。
- get_mode:判斷輸入字串適合的QR碼編碼模式。
- get_length:將資料長度資訊編碼為QR碼格式的二进制資料流。
- add_pad_data:生成QR碼的padding bits。
- get_generator_polynominal_adjusted:
- get_pixel_with_mask:取得QR碼特定位置的掩模。
- get_version_eclevel_mode_bistringlength:
- get_version_eclevel:
- remainder:各Ver的QR碼編碼時需要補餘的位元數量。
- arrange_codewords_and_calculate_ec:
- calculate_error_correction:計算糾錯碼
- convert_bitstring_to_bytes:將長度為的二进制字串轉換為長度為的數字陣列。
- get_matrix_and_penalty:
- get_matrix_with_lowest_penalty:
- bit_xor:
- 本模組._get_libbase64()
初始化Base64編碼程式庫。此處所有函數皆不支援模板直接呼叫。
初始化完的程式庫有以下成員:
- encode( str, encoder, usecaching ):將字串以指定編碼器編碼為Base64。
- decode( b64, decoder, usecaching ):將Base64資料流以指定解碼器進行解碼。
- makeencoder( s62, s63, spad ):以給定的62、63字元和指定的spad生成編碼器。
- makedecoder( s62, s63, spad ):以給定的62、63字元和指定的spad生成解碼器。
編碼
- 本模組.base64Encode(str)
將字串編碼為標準的Base64資料流。支援模板直接呼叫。
- 原始碼
{{#invoke:EncoderUtil|base64Encode|維基百科,自由的百科全書}}
- 輸出→脚本错误:函数“base64Encode”不存在。
解碼
- 本模組.base64Decode(b64)
將給定的Base64資料流依照標準的Base64格式進行解碼。支援模板直接呼叫。
- 原始碼
{{#invoke:EncoderUtil|base64Decode|57at5Z+655m+56eR77yM6Ieq55Sx55qE55m+56eR5YWo5pu4}}
- 輸出→脚本错误:函数“base64Decode”不存在。
local p={}
local function _safejson_number(obj, flat_out)
if type(obj) == type(0) then
if not not tostring(obj):lower():find('inf') then return tostring(obj) end
if not not tostring(obj):lower():find('nan') then return flat_out and tostring(obj) or '\127null\127'end
return flat_out and tostring(obj) or obj
else
return obj
end
end
local function _safejson_clone(obj)
if type(obj) == type({'table'}) then
local new_table = {}
for key,val in pairs(obj) do
if type(val) == type({'table'}) then
if xpcall(function()new_table[key] = mw.text.jsonDecode(mw.text.jsonEncode(val))end,function()end) then else new_table[key] = key end
elseif type(val) == type(function()end) then new_table[key] = key
elseif type(val) == type(nil) then
elseif type(val) == type(0) then new_table[key] = _safejson_number(val)
else new_table[key] = mw.clone(val) end
end
return new_table
else
return obj
end
end
local function _check_table(obj)
if type(obj) == type({'table'}) then
local new_table = {}
for key,val in pairs(obj) do
if type(val) == type({'table'}) then new_table[key] = _safejson_clone(val)
elseif type(val) == type(function()end) then new_table[key] = key
elseif type(val) == type(nil) then new_table[key] = '\127null\127'
elseif type(val) == type(0) then new_table[key] = _safejson_number(val)
else new_table[key] = mw.clone(val) end
end
return new_table
else
return obj
end
end
local function _jsonEncode(obj)
local result = ''
if type(obj) == type({'table'}) then
result = mw.text.jsonEncode(_check_table(obj))
elseif type(obj) == type(function()end) then result = 'function'
elseif type(obj) == type(nil) then result = 'null'
elseif type(obj) == type(0) then
local can_encode = false
can_encode,result = xpcall(function()return mw.text.jsonEncode(obj)end,function()end)
if can_encode==false then result=_safejson_number(obj,true)end
else result = mw.text.jsonEncode(obj) end
return result
end
function p.jsonEncode(obj)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {
parentFirst=true,
trim = false,
removeBlanks = false
})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
return _jsonEncode(args)
end
p._jsonEncode=_jsonEncode
return p