quvi/util.lua: Remove unused choose_format
[libquvi-scripts.git] / share / common / quvi / util.lua
blobdd3f554f233e4f1dfb40c5fe433179edc0ffb2b9
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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
19 -- 02110-1301 USA
22 local M = {}
24 --[[
25 Check whether a string A ends with string B.
26 Parameters:
27 a .. String A
28 b .. String B
29 Returns:
30 true if string A ends with string B.
31 ]]--
32 function M.ends(a, b) -- http://lua-users.org/wiki/StringRecipes
33 return a:sub(-#b) == b
34 end
36 --[[
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).
39 Parameters:
40 a .. Media entity A
41 b .. Media entity B
42 Returns:
43 true if entity A is the higher quality, otherwise false.
44 ]]--
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
50 else
51 return true
52 end
53 end
54 end
55 return false
56 end
58 --[[
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).
61 Parameters:
62 a .. Media entity A
63 b .. Media entity B
64 Returns:
65 true if entity A is the lower quality, otherwise false.
66 ]]--
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
72 else
73 return true
74 end
75 end
76 end
77 return false
78 end
80 --[[
81 Try to match an array of patterns to a string.
82 Parameters:
83 t .. Array of patterns (if nil, simply return false)
84 s .. A string
85 Returns:
86 true if a pattern matched, otherwise false.
87 ]]--
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
92 end
93 return false
94 end
96 --[[
97 Tokenize a string.
98 Parameters:
99 s .. String to tokenize
100 p .. Pattern (e.g. "[%w-_]+")
101 Returns:
102 An array of tokens.
103 ]]--
104 function M.tokenize(s, p)
105 return s:gmatch(p)
108 --[[
109 Convert a string to a timestamp.
110 Parameters:
111 s .. String to convert
112 Returns:
113 Converted string.
114 ]]--
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}
124 local m = MON[m]
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
131 --[[
132 Decode a string.
133 Parameters:
134 s .. String to decode
135 Returns:
136 Decoded string.
137 ]]--
138 function M.decode(s) -- http://www.lua.org/pil/20.3.html
139 r = {}
140 for n,v in s:gmatch ("([^&=]+)=([^&=]+)") do
141 n = M.unescape(n)
142 r[n] = v
144 return r
147 --[[
148 Unescape a string.
149 Parameters:
150 s .. String to unescape
151 Returns:
152 Unescaped string
153 ]]--
154 function M.unescape(s) -- http://www.lua.org/pil/20.3.html
155 s = s:gsub('+', ' ')
156 return (s:gsub('%%(%x%x)',
157 function(h)
158 return string.char(tonumber(h, 16))
159 end))
162 --[[
163 Unescape slashed string.
164 Parameters:
165 s .. String to unescape
166 Returns:
167 Unescaped string
168 ]]--
169 function M.slash_unescape(s)
170 return (s:gsub('\\(.)', '%1'))
173 return M
175 -- vim: set ts=2 sw=2 tw=72 expandtab: