2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
24 Check whether a string A ends with string B.
29 true if string A ends with string B.
31 function M
.ends_with(a
, b
) -- http://lua-users.org/wiki/StringRecipes
32 return a
:sub(-#b
) == b
36 Compare quality properties of two media entities. Compares the height, then
37 the width, followed by the bitrate property comparison (if it is set).
42 true if entity A is the higher quality, otherwise false.
44 function M
.is_higher_quality(a
, b
)
45 if a
.height
> b
.height
then
46 if a
.width
> b
.width
then
47 if a
['bitrate'] then -- Optional
48 if a
.bitrate
> b
.bitrate
then return true end
58 Compare quality properties of two media entities. Compares the height, then
59 the width, followed by the bitrate property comparison (if it is set).
64 true if entity A is the lower quality, otherwise false.
66 function M
.is_lower_quality(a
, b
)
67 if a
.height
< b
.height
then
68 if a
.width
< b
.width
then
69 if a
['bitrate'] then -- Optional
70 if a
.bitrate
< b
.bitrate
then return true end
86 function M
.decode(s
) -- http://www.lua.org/pil/20.3.html
88 for n
,v
in s
:gmatch("([^&=]+)=([^&=]+)") do
98 s .. String to unescape
102 function M
.unescape(s
) -- http://www.lua.org/pil/20.3.html
104 return (s
:gsub('%%(%x%x)',
106 return string.char(tonumber(h
, 16))
111 Unescape slashed string.
113 s .. String to unescape
117 function M
.slash_unescape(s
)
118 return (s
:gsub('\\(.)', '%1'))
122 Trim a string removing leading and trailing whitespace.
129 s
= s
:gsub('^%s+(.)', '%1')
130 return s
:gsub('(.)%s+$', '%1')
136 s .. String to tokenize
139 Tokenized items in a table.
141 function M
.tokenize(s
,sep
) -- Based on http://lua-users.org/wiki/SplitJoin
142 local sep
, fields
= sep
or ':', {}
143 local pattern
= string.format('([^%s]+)', sep
)
144 s
:gsub(pattern
, function(c
) fields
[#fields
+1] = c
150 Escape Lua magic characters.
152 s .. String to escape
156 function M
.escape_magic(s
)
157 return (s
:gsub('([()%%.%+%-*?[%]^$])', '%%%1'))
162 package.path = package.path .. ';../?.lua'
163 for _,v in pairs(M.tokenize('a,b,c,d,e',',')) do print(v) end
168 -- vim: set ts=2 sw=2 tw=72 expandtab: