本文介绍了Lua-解码URI(luvit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Lua(Luvit)项目中的JavaScript中使用decodeURIdecodeURIComponent.

I would like to use decodeURI or decodeURIComponent as in JavaScript in my Lua (Luvit) project.

JavaScript:

JavaScript:

decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82')
// result: привет

Luvit:

require('querystring').urldecode('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82')
-- result: '%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'

推荐答案

如果您了解 URI百分比编码格式.每个%XX子字符串代表使用%前缀和十六进制八位字节编码的UTF-8数据.

This is trivial to do yourself in Lua if you understand the URI percent-encoded format. Each %XX substring represents UTF-8 data encoded with a % prefix and a hexadecimal octet.

local decodeURI
do
    local char, gsub, tonumber = string.char, string.gsub, tonumber
    local function _(hex) return char(tonumber(hex, 16)) end

    function decodeURI(s)
        s = gsub(s, '%%(%x%x)', _)
        return s
    end
end

print(decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'))

这篇关于Lua-解码URI(luvit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 07:45