Lua - decodeURI (luvit)
Asked Answered
K

3

6

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

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'
K answered 5/12, 2013 at 16:55 Comment(1)
See https://mcmap.net/q/958264/-how-to-urldecode-a-request_uri-string-in-lua for some code.Innervate
A
13

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'))
Adjudge answered 5/12, 2013 at 17:42 Comment(1)
You can use %%(%x%x) as the pattern.Innervate
I
3

Here is another take. This code will save you lots of function calls if you have to decode many strings.

local hex = {}
for i = 0, 255 do
    hex[string.format("%02x", i)] = string.char(i)
    hex[string.format("%02X", i)] = string.char(i)
end

local function decodeURI(s)
    return (s:gsub('%%(%x%x)', hex))
end

print(decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'))
Innervate answered 5/12, 2013 at 17:49 Comment(2)
Interesting. You're caching all the possible percent-encoded hex octets, upper and lowercase, into a lookup table for use by gsub. The only thing I notice about this is the corner case where you might get a mixed-case octet such as Fe, but I doubt that would happen in practice.Adjudge
code doesn't work for the first 16 characters hex[string.format("%02x",i)]=string.char(i) add a 2 before the xBadminton
B
0

URIs represent ' ' with '+' And other special characters are represented as a percent followed by the 2 digit hex character code '%0A' for '\n' for example

local function decodeCharacter(code)
    -- get the number for the hex code 
    --   then get the character for that number
    return string.char(tonumber(code, 16))
end

function decodeURI(s)
    -- first replace '+' with ' '
    --   then, on the resulting string, decode % encoding
    local str = s:gsub("+", " ")
        :gsub('%%(%x%x)', decodeCharacter)
    return str 
    -- assignment to str removes the second return value of gsub
end

print(decodeURI('he%79+there%21')) -- prints "hey there!"
Badminton answered 27/11, 2019 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.