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
82 s .. String to tokenize
83 p .. Pattern (e.g. "[%w-_]+")
87 function M
.tokenize(s
, p
)
98 function M
.decode(s
) -- http://www.lua.org/pil/20.3.html
100 for n
,v
in s
:gmatch ("([^&=]+)=([^&=]+)") do
110 s .. String to unescape
114 function M
.unescape(s
) -- http://www.lua.org/pil/20.3.html
116 return (s
:gsub('%%(%x%x)',
118 return string.char(tonumber(h
, 16))
123 Unescape slashed string.
125 s .. String to unescape
129 function M
.slash_unescape(s
)
130 return (s
:gsub('\\(.)', '%1'))
134 Trim a string removing leading and trailing whitespace.
141 s
= s
:gsub('^%s+(.)', '%1')
142 return s
:gsub('(.)%s+$', '%1')
148 s .. String to tokenize
151 Tokenized items in a table.
153 function M
.tokenize(s
,sep
) -- Based on http://lua-users.org/wiki/SplitJoin
154 local sep
, fields
= sep
or ':', {}
155 local pattern
= string.format('([^%s]+)', sep
)
156 s
:gsub(pattern
, function(c
) fields
[#fields
+1] = c
163 package.path = package.path .. ';../?.lua'
164 for _,v in pairs(M.tokenize('a,b,c,d,e',',')) do print(v) end
169 -- vim: set ts=2 sw=2 tw=72 expandtab: