function toUTF8(cp1251_str)
local utf8 = {}
for c in cp1251_str:gmatch('.') do
local byte = string.byte(c)
if byte < 128 then
table.insert(utf8, c)
elseif byte >= 192 then
local cp1251_to_utf8 = {
[192] = {208, 144}, -- А
[193] = {208, 145}, -- Б
[194] = {208, 146}, -- В
[195] = {208, 147}, -- Г
[196] = {208, 148}, -- Д
[197] = {208, 149}, -- Е
[198] = {208, 150}, -- Ж
[199] = {208, 151}, -- З
[200] = {208, 152}, -- И
[201] = {208, 153}, -- Й
[202] = {208, 154}, -- К
[203] = {208, 155}, -- Л
[204] = {208, 156}, -- М
[205] = {208, 157}, -- Н
[206] = {208, 158}, -- О
[207] = {208, 159}, -- П
[208] = {208, 160}, -- Р
[209] = {208, 161}, -- С
[210] = {208, 162}, -- Т
[211] = {208, 163}, -- У
[212] = {208, 164}, -- Ф
[213] = {208, 165}, -- Х
[214] = {208, 166}, -- Ц
[215] = {208, 167}, -- Ч
[216] = {208, 168}, -- Ш
[217] = {208, 169}, -- Щ
[218] = {208, 170}, -- Ъ
[219] = {208, 171}, -- Ы
[220] = {208, 172}, -- Ь
[221] = {208, 173}, -- Э
[222] = {208, 174}, -- Ю
[223] = {208, 175}, -- Я
[224] = {208, 176}, -- а
[225] = {208, 177}, -- б
[226] = {208, 178}, -- в
[227] = {208, 179}, -- г
[228] = {208, 180}, -- д
[229] = {208, 181}, -- е
[230] = {208, 182}, -- ж
[231] = {208, 183}, -- з
[232] = {208, 184}, -- и
[233] = {208, 185}, -- й
[234] = {208, 186}, -- к
[235] = {208, 187}, -- л
[236] = {208, 188}, -- м
[237] = {208, 189}, -- н
[238] = {208, 190}, -- о
[239] = {208, 191}, -- п
[240] = {209, 128}, -- р
[241] = {209, 129}, -- с
[242] = {209, 130}, -- т
[243] = {209, 131}, -- у
[244] = {209, 132}, -- ф
[245] = {209, 133}, -- х
[246] = {209, 134}, -- ц
[247] = {209, 135}, -- ч
[248] = {209, 136}, -- ш
[249] = {209, 137}, -- щ
[250] = {209, 138}, -- ъ
[251] = {209, 139}, -- ы
[252] = {209, 140}, -- ь
[253] = {209, 141}, -- э
[254] = {209, 142}, -- ю
[255] = {209, 143}, -- я
}
local utf8_bytes = cp1251_to_utf8[byte]
if utf8_bytes then
table.insert(utf8, string.char(utf8_bytes[1], utf8_bytes[2]))
end
end
end
return table.concat(utf8)
end
function encodeUrl(str)
local utf8_str = toUTF8(str)
return (utf8_str:gsub('([^%w%-%.%_%~])', function(c)
local bytes = {}
for i = 1, #c do
table.insert(bytes, string.format('%%%02X', string.byte(c, i)))
end
return table.concat(bytes)
end))
end