[LUA] Useful functions

Discuss your Lua playlist, album art and interface scripts.
ale5000
Cone that earned his stripes
Cone that earned his stripes
Posts: 125
Joined: 03 Mar 2004 16:12
Operating System: Windows
Contact:

[LUA] Useful functions

Postby ale5000 » 27 May 2011 14:50

I have collected some functions to simplify the work, some made by me and some made by others.
If someone has better alternatives, please post them.

Code: Select all

function string.substr(text, start, length) -- by ale5000 return text:sub(start, start - 1 + length) end

Code: Select all

function string.charAt(text, pos) -- by ale5000 return text:sub(pos, pos) end

Code: Select all

function string.setAt(text, pos, new_value) -- by ale5000 return text:sub(1, pos - 1)..new_value..text:sub(pos + 1) end

Code: Select all

function string.lastIndexOf(text, search) -- by ale5000 local start, last_pos = 0 while start ~= nil do last_pos = start start = text:find(search, start + 1, true) end if last_pos == 0 then return nil end return last_pos end

Code: Select all

function string.trim_spaces(text) -- by ale5000 return text:gsub("^%s*(.-)%s*$", "%1") end

Code: Select all

function string.skip_bytes(data, bytes) -- by ale5000 return data:sub(bytes + 1) end

Code: Select all

-- Returns a string that has the internal numerical code equal of the number passed function math.number_to_binary_code(in_number) -- by ale5000 if in_number == 0 then return "\0" end local out_string = "" while in_number > 0 do out_string = string.char(in_number % 256)..out_string in_number = math.floor(in_number / 256) end return out_string end

Code: Select all

-- Returns a number with the internal numerical code of the string passed function string.binary_code_to_number(in_string) -- by ale5000 local len, i, out_number = in_string:len() + 1, 0, 0 while len > 1 do len = len - 1 out_number = in_string:sub(len, len):byte() * (256^i) + out_number i = i + 1 end return out_number end

Code: Select all

function string.hex_string_to_binary_code(in_string) -- by ale5000 local len, out_string = in_string:len() + 1, "" while len > 2 do len = len - 2 out_string = string.char( "0x"..in_string:substr(len, 2) )..out_string end return out_string end

Code: Select all

function string.size_should_be(binary, required_length) -- by ale5000 local len_to_add = required_length - binary:len() if len_to_add < 0 then vlc.msg.err("size should be: The string is too long => "..binary) binary = "" len_to_add = required_length end while len_to_add > 0 do binary = "\0"..binary len_to_add = len_to_add - 1 end return binary end

Code: Select all

function string.size_should_be__add_to_right(binary, required_length) -- by ale5000 local len_to_add = required_length - binary:len() if len_to_add < 0 then vlc.msg.err("size should be, add to right: The string is too long => "..binary) binary = "" len_to_add = required_length end while len_to_add > 0 do binary = binary.."\0" len_to_add = len_to_add - 1 end return binary end

Code: Select all

function string.add_length(in_string) -- by ale5000 if in_string == "" then return "" end return math.number_to_binary_code(in_string:len() + 4):size_should_be(4)..in_string -- The length of the length itself is included end

Code: Select all

function string.add_length_x_byte(in_string, size_of_length) -- by ale5000 return math.number_to_binary_code(in_string:len()):size_should_be(size_of_length)..in_string end

Code: Select all

function string.base64_decode(data) -- by Alex Kloss <alexthkloss AT web.de> local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" data = string.gsub(data, '[^'..b..'=]', '') return (data:gsub('.', function(x) if x == '=' then return '' end local r,f='',(b:find(x)-1) for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end return r; end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) if #x ~= 8 then return '' end local c=0 for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end return string.char(c) end)) end

Code: Select all

function bitwise_xor(x, y) -- by Arno Wagner <arno AT wagner.name> if x == nil or y == nil then vlc.msg.err("Bitwise XOR: You cannot pass a nil value") return nil end local z = 0 for i = 0, 31 do if x % 2 == 0 then -- x had a '0' in bit i if y % 2 == 1 then -- y had a '1' in bit i y = y - 1 z = z + 2 ^ i -- set bit i of z to '1' end else -- x had a '1' in bit i x = x - 1 if y % 2 == 0 then -- y had a '0' in bit i z = z + 2 ^ i -- set bit i of z to '1' else y = y - 1 end end y = y / 2 x = x / 2 end return z end
Last edited by ale5000 on 05 Jun 2011 05:10, edited 12 times in total.

batrick
New Cone
New Cone
Posts: 2
Joined: 28 May 2011 10:47

Re: [] Useful LUA functions

Postby batrick » 28 May 2011 10:49

Code: Select all

do local t = { ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011", ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111", ["8"] = "1000", ["9"] = "1001", a = "1010", b = "1011", c = "1100", d = "1101", e = "1110", f = "1111" }; --- Converts the given number, n, to a string in a binary number format (12 -- becomes "1100"). -- @param n Number to convert. -- @return String in binary format. function tobinary(n) assert(tonumber(n), "number expected"); return (("%x"):format(n):gsub("%w", t):gsub("^0*", "")); end end
Your hexstring_to_binary function can be derived from the above.

franc
Blank Cone
Blank Cone
Posts: 53
Joined: 12 May 2007 10:06

Re: [LUA] Useful functions

Postby franc » 10 Jan 2021 21:01

Timer

Code: Select all

-- sleep function, from: -- http://lua-users.org/wiki/SleepFunction function sleep(s) local ntime = os.time() + s repeat until os.time() > ntime end


Return to “Scripting VLC in lua”

Who is online

Users browsing this forum: No registered users and 17 guests