Differences From
Artifact [9b28e1fde4]:
17 17 end
18 18 return pkg
19 19 end
20 20 ss = namespace 'sirsem'
21 21 ss.namespace = namespace
22 22 end
23 23
24 +local native = _G.native
25 +
24 26 function ss.map(fn, lst)
25 27 local new = {}
26 28 for k,v in pairs(lst) do
27 29 table.insert(new, fn(v,k))
28 30 end
29 31 return new
30 32 end
................................................................................
112 114 -- tbl (lightweight alternative to shallow copies)
113 115 tpl = tpl or {}
114 116 return setmetatable({}, {__index=tbl})
115 117 end
116 118
117 119 ss.str = {}
118 120
119 -function ss.str.begins(str, pfx)
120 - -- appallingly, this is actually ~2/5ths faster than either
121 - -- of the below. i hate scripting languages so much
122 - return string.find(str, pfx, 1, true) == 1
123 - -- to my shock, disgust, and horror, even writing my own
124 - -- string scanning library for lua IN C only sped this up by
125 - -- a tiny fraction. i am just speechless.
126 --- return string.sub(str, 1, #pfx) == pfx
127 -
128 --- local pl = string.len(pfx)
129 --- local sl = string.len(str)
130 --- if sl < pl then return false end
131 --- for i=1,pl do
132 --- if string.byte(str,i) ~= string.byte(pfx,i) then
133 --- return false
134 --- end
135 --- end
136 --- return true
121 +if native then
122 + function ss.str.begins(str, pfx)
123 + return native.strutils.rangematch(str,1,pfx)
124 + end
125 +else
126 + function ss.str.begins(str, pfx)
127 + -- appallingly, this is actually ~2/5ths faster than either
128 + -- of the below. i hate scripting languages so much
129 + return string.find(str, pfx, 1, true) == 1
130 + -- to my shock, disgust, and horror, even writing my own
131 + -- string scanning library for lua IN C only sped this up by
132 + -- a tiny fraction. i am just speechless.
133 + -- return string.sub(str, 1, #pfx) == pfx
134 +
135 + -- local pl = string.len(pfx)
136 + -- local sl = string.len(str)
137 + -- if sl < pl then return false end
138 + -- for i=1,pl do
139 + -- if string.byte(str,i) ~= string.byte(pfx,i) then
140 + -- return false
141 + -- end
142 + -- end
143 + -- return true
144 + end
137 145 end
138 146
139 147 function ss.enum(syms)
140 148 local e = {}
141 149 for i,v in pairs(syms) do
142 150 e[v] = i
143 151 e[i] = v
................................................................................
1170 1178 local fetchableProtocols = {
1171 1179 http = {
1172 1180 proto = {
1173 1181 {'http'};
1174 1182 {'https'};
1175 1183 {'http', 'tls'};
1176 1184 };
1177 - fetch = function(uri)
1185 + fetch = native and native.net and function(uri)
1186 + -- translate to a curl-compatible URI
1187 + if uri.path and uri.path ~= '' and uri.path:sub(1,1) ~= '/' then
1188 + fetchexn('relative HTTP URIs like ā%sā are not fetchable', uri):throw()
1189 + end
1190 + uri = uri:clone()
1191 + if uri.class[2] == 'tls' then
1192 + uri.class = {'https'}
1193 + end
1194 + if not uri.namespace then
1195 + uri.namespace = 'localhost'
1196 + end
1197 + local body, e = native.net.fetchURI(tostring(uri))
1198 + if e then
1199 + fetchexn('could not fetch URI ā%sā: %s',uri,e):throw()
1200 + end
1201 + return body
1202 + end or function(uri)
1178 1203 fetchexn('cortav must be compiled with the C shim and libcurl support to use http fetch'):throw()
1179 1204 end;
1180 1205 };
1181 1206 file = {
1182 1207 proto = {
1183 1208 {'file'};
1184 1209 {'file', 'txt'};
................................................................................
1336 1361 class = nil;
1337 1362 namespace = nil;
1338 1363 path = nil;
1339 1364 query = nil;
1340 1365 frag = nil;
1341 1366 auth = nil;
1342 1367 } end;
1368 + clonesetup = function(me)
1369 + me.class = ss.clone(me.class)
1370 + end;
1343 1371 construct = function(me, str)
1344 1372 local enc = ss.str.enc.utf8
1345 1373 -- URIs must be either ASCII or utf8, so we read and
1346 1374 -- store as UTF8. to use a URI in another encoding, it
1347 1375 -- must be manually converted to and fro using the
1348 1376 -- appropriate functions, such as encodeUCS
1349 1377 if not str then return end