5641c4307e89db03c1301d40624a26b199dc6bc2
[libquvi-scripts.git] / share / common / quvi / util.lua
blob5641c4307e89db03c1301d40624a26b199dc6bc2
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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/>.
21 local M = {}
23 --[[
24 Check whether a string A ends with string B.
25 Parameters:
26 a .. String A
27 b .. String B
28 Returns:
29 true if string A ends with string B.
30 ]]--
31 function M.ends_with(a, b) -- http://lua-users.org/wiki/StringRecipes
32 return a:sub(-#b) == b
33 end
35 --[[
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).
38 Parameters:
39 a .. Media entity A
40 b .. Media entity B
41 Returns:
42 true if entity A is the higher quality, otherwise false.
43 ]]--
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
49 else
50 return true
51 end
52 end
53 end
54 return false
55 end
57 --[[
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).
60 Parameters:
61 a .. Media entity A
62 b .. Media entity B
63 Returns:
64 true if entity A is the lower quality, otherwise false.
65 ]]--
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
71 else
72 return true
73 end
74 end
75 end
76 return false
77 end
79 --[[
80 Decode a string.
81 Parameters:
82 s .. String to decode
83 Returns:
84 Decoded string.
85 ]]--
86 function M.decode(s) -- http://www.lua.org/pil/20.3.html
87 r = {}
88 for n,v in s:gmatch("([^&=]+)=([^&=]+)") do
89 n = M.unescape(n)
90 r[n] = v
91 end
92 return r
93 end
95 --[[
96 Unescape a string.
97 Parameters:
98 s .. String to unescape
99 Returns:
100 Unescaped string
101 ]]--
102 function M.unescape(s) -- http://www.lua.org/pil/20.3.html
103 s = s:gsub('+', ' ')
104 return (s:gsub('%%(%x%x)',
105 function(h)
106 return string.char(tonumber(h, 16))
107 end))
110 --[[
111 Unescape slashed string.
112 Parameters:
113 s .. String to unescape
114 Returns:
115 Unescaped string
116 ]]--
117 function M.slash_unescape(s)
118 return (s:gsub('\\(.)', '%1'))
121 --[[
122 Trim a string removing leading and trailing whitespace.
123 Parameters:
124 s .. String to trim
125 Returns:
126 The trimmed string.
127 ]]--
128 function M.trim(s)
129 s = s:gsub('^%s+(.)', '%1')
130 return s:gsub('(.)%s+$', '%1')
133 --[[
134 Tokenize a string.
135 Parameters:
136 s .. String to tokenize
137 sep .. Separator
138 Returns:
139 Tokenized items in a table.
140 ]]--
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
145 end)
146 return fields
149 -- Uncomment to test
150 --[[
151 package.path = package.path .. ';../?.lua'
152 for _,v in pairs(M.tokenize('a,b,c,d,e',',')) do print(v) end
153 ]]--
155 return M
157 -- vim: set ts=2 sw=2 tw=72 expandtab: