Last active
December 8, 2024 14:45
-
-
Save AMD-NICK/7651b17de852301bbfb68cbab2e21342 to your computer and use it in GitHub Desktop.
Попытка сделать в гмоде корректный срез UTF16. Вогель говорит, что работает, но для энтитей телеги, куда и предназначался, не пашет
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function utf8to16(sData) | |
| local ret = "" | |
| for utf8char in string.gmatch(sData, "[%z\x01-\x7F\xC2-\xF4][\x80-\xBF]*") do | |
| local codepoint = utf8.codepoint(utf8char) | |
| ret = ret .. string.char( | |
| bit.band(codepoint, 0xff), | |
| bit.band(bit.rshift(codepoint, 8), 0xff) | |
| ) | |
| end | |
| return ret | |
| end | |
| local function utf16to8(sData) | |
| local ret = "" | |
| if #sData % 2 ~= 0 then error("Not pow. of 2") end | |
| for index = 1, #sData, 2 do | |
| local byte1, byte2 = string.byte(sData, index, index + 1) | |
| local codepoint = bit.bor(byte1, bit.lshift(byte2, 8)) | |
| ret = ret .. utf8.char(codepoint) | |
| end | |
| return ret | |
| end | |
| local function sub(sData, nStart, nEnd) | |
| local str = utf8to16(sData) | |
| local len = #str | |
| return utf16to8(str:sub(nStart and nStart * 2 - 1 or 1, nEnd and nEnd * 2 or len)) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Портировал JS код, но потом узнал, что в ЖС с ютф16 и так порядок. Код оказался непригоден | |
| -- utf8To16: function (input) { | |
| -- var _escape = function(s) { | |
| -- function q(c) { | |
| -- c = c.charCodeAt(); | |
| -- return '%' + (c<16 ? '0' : '') + c.toString(16).toUpperCase(); | |
| -- } | |
| -- return s.replace(/[\x00-),:-?[-^`{-\xFF]/g, q); | |
| -- }; | |
| -- try{ | |
| -- return decodeURIComponent(_escape(input)); | |
| -- }catch (URIError) { | |
| -- //include invalid character, cannot convert | |
| -- return input; | |
| -- } | |
| -- }, | |
| -- utf16To8: function (input) { | |
| -- var _unescape = function(s) { | |
| -- function d(x, n) { | |
| -- return String.fromCharCode(parseInt(n, 16)); | |
| -- } | |
| -- return s.replace(/%([0-9A-F]{2})/ig, d); | |
| -- }; | |
| -- try{ | |
| -- return _unescape(encodeURIComponent(input)); | |
| -- }catch (URIError) { | |
| -- //include invalid character, cannot convert | |
| -- return input; | |
| -- } | |
| -- }, | |
| local function utf8to16(input) | |
| -- [\x00-),:-?[-^`{-\xFF] | |
| return input:gsub("[%z\x01-%),:-%?%[-%^`{-\xFF]", function(c) | |
| local b = string.byte(c) | |
| local cc = "%" .. (b < 16 and "0" or "") .. string.format("%X",b) | |
| print(b, cc) | |
| return сс | |
| end)--:URLDecode() | |
| end | |
| local function utf16to8(input) -- а оно чет делает вообще? | |
| local encoded = input:URLEncode() | |
| return encoded:gsub("%%(%x%x)", function(hh) | |
| -- print("hh", hh, tonumber(hh,16)) | |
| return string.char(tonumber(hh,16)) | |
| -- return tonumber(hh,16) .. " " | |
| end) --:URLEncode() | |
| end | |
| local input = "🕹 New Item!" | |
| local utf16 = utf8to16(input) | |
| -- local utf8 = utf16to8(utf16) | |
| print( #input, #utf16 ) | |
| -- print( utf8, input:URLEncode() ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function pushika(...) | |
| PRINT(...) | |
| end | |
| function string.utf16sub(str, a,b, PUSH) | |
| if PUSH then | |
| end | |
| end | |
| local f = vgui.Create('DHTML') | |
| f:SetSize(100, 100) | |
| f:SetPaintedManually(true) | |
| f:SetMouseInputEnabled(false) | |
| f:SetAllowLua(true) | |
| f:SetHTML([[ | |
| <script type='text/JavaScript'> | |
| function sub(input, startl, endl) { | |
| out = input.substring(startl, endl); | |
| console.log('RUNLUA:pushika("'+out+'")'); | |
| } | |
| </script> | |
| ]]) | |
| local text = "🕹 New DMarket Item!"; | |
| local offset = 3; | |
| local length = 17; | |
| local startl = offset; | |
| local endl = startl + length; | |
| f:RunJavascript('sub("' .. text .. '", ' .. startl .. ', ' .. endl .. ');') | |
| timer.Simple(5,function() | |
| if IsValid(f) then | |
| f:Remove() | |
| end | |
| end) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Попытка корректно делать sub для utf16 телеграма