quvi/util: Add the `tokenize' function
[libquvi-scripts.git] / share / common / quvi / util.lua
blob4601fded341db1f777c1f57f4e2cddabc5e74b12
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 Tokenize a string.
81 Parameters:
82 s .. String to tokenize
83 p .. Pattern (e.g. "[%w-_]+")
84 Returns:
85 An array of tokens.
86 ]]--
87 function M.tokenize(s, p)
88 return s:gmatch(p)
89 end
91 --[[
92 Decode a string.
93 Parameters:
94 s .. String to decode
95 Returns:
96 Decoded string.
97 ]]--
98 function M.decode(s) -- http://www.lua.org/pil/20.3.html
99 r = {}
100 for n,v in s:gmatch ("([^&=]+)=([^&=]+)") do
101 n = M.unescape(n)
102 r[n] = v
104 return r
107 --[[
108 Unescape a string.
109 Parameters:
110 s .. String to unescape
111 Returns:
112 Unescaped string
113 ]]--
114 function M.unescape(s) -- http://www.lua.org/pil/20.3.html
115 s = s:gsub('+', ' ')
116 return (s:gsub('%%(%x%x)',
117 function(h)
118 return string.char(tonumber(h, 16))
119 end))
122 --[[
123 Unescape slashed string.
124 Parameters:
125 s .. String to unescape
126 Returns:
127 Unescaped string
128 ]]--
129 function M.slash_unescape(s)
130 return (s:gsub('\\(.)', '%1'))
133 --[[
134 Trim a string removing leading and trailing whitespace.
135 Parameters:
136 s .. String to trim
137 Returns:
138 The trimmed string.
139 ]]--
140 function M.trim(s)
141 s = s:gsub('^%s+(.)', '%1')
142 return s:gsub('(.)%s+$', '%1')
145 --[[
146 Tokenize a string.
147 Parameters:
148 s .. String to tokenize
149 sep .. Separator
150 Returns:
151 Tokenized items in a table.
152 ]]--
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
157 end)
158 return fields
161 -- Uncomment to test
162 --[[
163 package.path = package.path .. ';../?.lua'
164 for _,v in pairs(M.tokenize('a,b,c,d,e',',')) do print(v) end
165 ]]--
167 return M
169 -- vim: set ts=2 sw=2 tw=72 expandtab: