5 месяцев назад История
    README.md

    lua-percented

    Модуль для Lua для процентного кодирования и декодирования по умолчанию кодирует или декодирует весь заданный ввод, но имеется возможность задать свои правилам, такие как:

    • какие байты ввода не кодировать, а выводить как есть;
    • какие байты ввода не кодировать, а заменять на другие;

    Это позволяет например кодировать компоненты URL адресов по заданным правилам, и/или декодировать их (или адреса целиком).

    Подходит для любой версии Lua из:

    • Luajit
    • Lua5.1
    • Lua5.2
    • Lua5.3
    • Lua5.3

    Зависимости

    sudo apt install lua
    

    Установка и удаление

    Глобально

    sudo make install
    sudo make uninstall
    

    Локально

    make PREFIX=$HOME/.local install
    make PREFIX=$HOME/.local uninstall
    

    Независимо

    make DESTDIR=$HOME/some_you_dir install
    make DESTDIR=$HOME/some_you_dir uninstall
    

    Использование

    Минимальный пример

    local percented = require('percented')
    local enc, err = percented.encode('Привет World')
    if err then
       error(err)
    end
    print(enc)
    ----------
    local dec,err = percented.decode(enc)
    if err then
       error(err)
    end
    print(dec)
    ----------
    
    lua example.lua
    %D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%57%6F%72%6C%64
    Привет World
    

    Пример использования кодирования по своим правилам

    local percented = require('percented')
    
    local ignore = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
    local replace_from = " ";
    local replace_to   = "+";
    local stat,err = percented.register(ignore,replace_from,replace_to,'my_rule')
    if err then
       error(err)
    end
    ----------
    local enc, err = percented.encode('Привет World','my_rule')
    if err then
       error(err)
    end
    print(enc)
    ----------
    local dec,err = percented.decode(enc,'my_rule')
    if err then
       error(err)
    end
    print(dec)
    ----------
    
    lua example.lua
    %D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82+World
    Привет World
    

    Описание API

    ----------------------------------------------------------
    -- optional string : ignore - no encode this bytes
    -- optional string : replace_from - change this bytes
    -- optional string : replace_to   - change on this bytes
    ----------------------------------------------------------
    optional for replace_to and replace_from only if all this
    is nil, need corrent replace pair, in replace pair not
    allow use reserved '%' symbol, in replace pair must be only
    unique bytes, repeat bytes in replace_from and replace_to
    not  allowed. Optional from ignore if you need encode
    all, but some bytes need only replace and not encode
    -----------------------------------------------------------
    -- return boolean  : true,nil or false,error_string
    -----------------------------------------------------------
    
    percented.register(ignore,replace_from,replace_to,rule_name)
    
    -----------------------------------------------------------
    -- required string : input_data : string with any bytedata
    -- optional string : rule_name  : you name for you prepared rule
    -----------------------------------------------------------
    -- return string   : encoded_string or nil,error_string
    -----------------------------------------------------------
    
    percented.encode(input_data,rule_name)
    
    -----------------------------------------------------------
    -- required string : input_data : percented encoded data
    -- optional string : rule_name  : you name for you prepared rule
    -----------------------------------------------------------
    -- return string   : decoced_data or nil,error_string
    -----------------------------------------------------------
    
    percented.decode(input_data,basename)
    

    На заметку

    Кажется всё работает правильно. Потоковый механизм кодирования и декодирования в планах.

    Описание

    Процентное кодирование и декодирование на Lua

    Конвейеры
    0 успешных
    0 с ошибкой