2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library 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 GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Check whether a string A ends with string B.
30 true if string A ends with string B.
32 function M
.ends(a
, b
) -- http://lua-users.org/wiki/StringRecipes
33 return a
:sub(-#b
) == b
37 Compare quality properties of two media entities. Compares the height, then
38 the width, followed by the bitrate property comparison (if it is set).
43 true if entity A is the higher quality, otherwise false.
45 function M
.is_higher_quality(a
, b
)
46 if a
.height
> b
.height
then
47 if a
.width
> b
.width
then
48 if a
['bitrate'] then -- Optional
49 if a
.bitrate
> b
.bitrate
then return true end
59 Compare quality properties of two media entities. Compares the height, then
60 the width, followed by the bitrate property comparison (if it is set).
65 true if entity A is the lower quality, otherwise false.
67 function M
.is_lower_quality(a
, b
)
68 if a
.height
< b
.height
then
69 if a
.width
< b
.width
then
70 if a
['bitrate'] then -- Optional
71 if a
.bitrate
< b
.bitrate
then return true end
81 Try to match an array of patterns to a string.
83 t .. Array of patterns (if nil, simply return false)
86 true if a pattern matched, otherwise false.
88 function M
.match_any(t
, s
)
89 if not s
then return false end
90 for _
,p
in pairs(t
) do
91 if s
:match(p
) then return true end
99 s .. String to tokenize
100 p .. Pattern (e.g. "[%w-_]+")
104 function M
.tokenize(s
, p
)
109 Convert a string to a timestamp.
111 s .. String to convert
115 function M
.to_timestamp(s
) -- Based on <http://is.gd/ee9ZTD>
116 local p
= "%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+)"
118 local d
,m
,y
,hh
,mm
,ss
= s
:match(p
)
119 if not d
then error('no match: date') end
121 local MON
= {Jan
=1, Feb
=2, Mar
=3, Apr
=4, May
=5, Jun
=6, Jul
=7, Aug
=8,
122 Sep
=9, Oct
=10, Nov
=11, Dec
=12}
125 local offset
= os
.time() - os
.time(os
.date("!*t"))
127 return os
.time({day
=d
,month
=m
,year
=y
,
128 hour
=hh
,min=mm
,sec
=ss
}) + offset
134 s .. String to decode
138 function M
.decode(s
) -- http://www.lua.org/pil/20.3.html
140 for n
,v
in s
:gmatch ("([^&=]+)=([^&=]+)") do
150 s .. String to unescape
154 function M
.unescape(s
) -- http://www.lua.org/pil/20.3.html
156 return (s
:gsub('%%(%x%x)',
158 return string.char(tonumber(h
, 16))
163 Unescape slashed string.
165 s .. String to unescape
169 function M
.slash_unescape(s
)
170 return (s
:gsub('\\(.)', '%1'))
175 -- vim: set ts=2 sw=2 tw=72 expandtab: