| 1 |
-- merged file : lualibs-basic-merged.lua |
| 2 |
-- parent file : lualibs-basic.lua |
| 3 |
-- merge date : 2023-07-13 12:55 |
| 4 |
|
| 5 |
do -- begin closure to overcome local limits and interference |
| 6 |
|
| 7 |
if not modules then modules={} end modules ['l-lua']={ |
| 8 |
version=1.001, |
| 9 |
comment="companion to luat-lib.mkiv", |
| 10 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 11 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 12 |
license="see context related readme files" |
| 13 |
} |
| 14 |
local next,type,tonumber=next,type,tonumber |
| 15 |
LUAMAJORVERSION,LUAMINORVERSION=string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$") |
| 16 |
LUAMAJORVERSION=tonumber(LUAMAJORVERSION) or 5 |
| 17 |
LUAMINORVERSION=tonumber(LUAMINORVERSION) or 1 |
| 18 |
LUAVERSION=LUAMAJORVERSION+LUAMINORVERSION/10 |
| 19 |
if LUAVERSION<5.2 and jit then |
| 20 |
MINORVERSION=2 |
| 21 |
LUAVERSION=5.2 |
| 22 |
end |
| 23 |
if not lpeg then |
| 24 |
lpeg=require("lpeg") |
| 25 |
end |
| 26 |
if loadstring then |
| 27 |
local loadnormal=load |
| 28 |
function load(first,...) |
| 29 |
if type(first)=="string" then |
| 30 |
return loadstring(first,...) |
| 31 |
else |
| 32 |
return loadnormal(first,...) |
| 33 |
end |
| 34 |
end |
| 35 |
else |
| 36 |
loadstring=load |
| 37 |
end |
| 38 |
if not ipairs then |
| 39 |
local function iterate(a,i) |
| 40 |
i=i+1 |
| 41 |
local v=a[i] |
| 42 |
if v~=nil then |
| 43 |
return i,v |
| 44 |
end |
| 45 |
end |
| 46 |
function ipairs(a) |
| 47 |
return iterate,a,0 |
| 48 |
end |
| 49 |
end |
| 50 |
if not pairs then |
| 51 |
function pairs(t) |
| 52 |
return next,t |
| 53 |
end |
| 54 |
end |
| 55 |
if not table.unpack then |
| 56 |
table.unpack=_G.unpack |
| 57 |
elseif not unpack then |
| 58 |
_G.unpack=table.unpack |
| 59 |
end |
| 60 |
if not package.loaders then |
| 61 |
package.loaders=package.searchers |
| 62 |
end |
| 63 |
local print,select,tostring=print,select,tostring |
| 64 |
local inspectors={} |
| 65 |
function setinspector(kind,inspector) |
| 66 |
inspectors[kind]=inspector |
| 67 |
end |
| 68 |
function inspect(...) |
| 69 |
for s=1,select("#",...) do |
| 70 |
local value=select(s,...) |
| 71 |
if value==nil then |
| 72 |
print("nil") |
| 73 |
else |
| 74 |
local done=false |
| 75 |
local kind=type(value) |
| 76 |
local inspector=inspectors[kind] |
| 77 |
if inspector then |
| 78 |
done=inspector(value) |
| 79 |
if done then |
| 80 |
break |
| 81 |
end |
| 82 |
end |
| 83 |
for kind,inspector in next,inspectors do |
| 84 |
done=inspector(value) |
| 85 |
if done then |
| 86 |
break |
| 87 |
end |
| 88 |
end |
| 89 |
if not done then |
| 90 |
print(tostring(value)) |
| 91 |
end |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
local dummy=function() end |
| 96 |
function optionalrequire(...) |
| 97 |
local ok,result=xpcall(require,dummy,...) |
| 98 |
if ok then |
| 99 |
return result |
| 100 |
end |
| 101 |
end |
| 102 |
local flush=io.flush |
| 103 |
if flush then |
| 104 |
local execute=os.execute if execute then function os.execute(...) flush() return execute(...) end end |
| 105 |
local exec=os.exec if exec then function os.exec (...) flush() return exec (...) end end |
| 106 |
local spawn=os.spawn if spawn then function os.spawn (...) flush() return spawn (...) end end |
| 107 |
local popen=io.popen if popen then function io.popen (...) flush() return popen (...) end end |
| 108 |
end |
| 109 |
FFISUPPORTED=type(ffi)=="table" and ffi.os~="" and ffi.arch~="" and ffi.load |
| 110 |
if not FFISUPPORTED then |
| 111 |
local okay;okay,ffi=pcall(require,"ffi") |
| 112 |
FFISUPPORTED=type(ffi)=="table" and ffi.os~="" and ffi.arch~="" and ffi.load |
| 113 |
end |
| 114 |
if not FFISUPPORTED then |
| 115 |
ffi=nil |
| 116 |
elseif not ffi.number then |
| 117 |
ffi.number=tonumber |
| 118 |
end |
| 119 |
if LUAVERSION>5.3 then |
| 120 |
end |
| 121 |
if status and os.setenv then |
| 122 |
os.setenv("engine",string.lower(status.luatex_engine or "unknown")) |
| 123 |
end |
| 124 |
|
| 125 |
end -- closure |
| 126 |
|
| 127 |
do -- begin closure to overcome local limits and interference |
| 128 |
|
| 129 |
if not modules then modules={} end modules ['l-package']={ |
| 130 |
version=1.001, |
| 131 |
comment="companion to luat-lib.mkiv", |
| 132 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 133 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 134 |
license="see context related readme files" |
| 135 |
} |
| 136 |
local type,unpack=type,unpack |
| 137 |
local gsub,format,find=string.gsub,string.format,string.find |
| 138 |
local insert,remove=table.insert,table.remove |
| 139 |
local P,S,Cs,lpegmatch=lpeg.P,lpeg.S,lpeg.Cs,lpeg.match |
| 140 |
local package=package |
| 141 |
local searchers=package.searchers or package.loaders |
| 142 |
local filejoin=file and file.join or function(path,name) return path.."/"..name end |
| 143 |
local isreadable=file and file.is_readable or function(name) local f=io.open(name) if f then f:close() return true end end |
| 144 |
local addsuffix=file and file.addsuffix or function(name,suffix) return name.."."..suffix end |
| 145 |
local function cleanpath(path) |
| 146 |
return path |
| 147 |
end |
| 148 |
local pattern=Cs((((1-S("\\/"))^0*(S("\\/")^1/"/"))^0*(P(".")^1/"/"+P(1))^1)*-1) |
| 149 |
local function lualibfile(name) |
| 150 |
return lpegmatch(pattern,name) or name |
| 151 |
end |
| 152 |
local offset=luarocks and 1 or 0 |
| 153 |
local helpers=package.helpers or { |
| 154 |
cleanpath=cleanpath, |
| 155 |
lualibfile=lualibfile, |
| 156 |
trace=false, |
| 157 |
report=function(...) print(format(...)) end, |
| 158 |
builtin={ |
| 159 |
["preload table"]=searchers[1+offset], |
| 160 |
["path specification"]=searchers[2+offset], |
| 161 |
["cpath specification"]=searchers[3+offset], |
| 162 |
["all in one fallback"]=searchers[4+offset], |
| 163 |
}, |
| 164 |
methods={}, |
| 165 |
sequence={ |
| 166 |
"reset loaded", |
| 167 |
"already loaded", |
| 168 |
"preload table", |
| 169 |
"qualified path", |
| 170 |
"lua extra list", |
| 171 |
"lib extra list", |
| 172 |
"path specification", |
| 173 |
"cpath specification", |
| 174 |
"all in one fallback", |
| 175 |
"not loaded", |
| 176 |
} |
| 177 |
} |
| 178 |
package.helpers=helpers |
| 179 |
local methods=helpers.methods |
| 180 |
local builtin=helpers.builtin |
| 181 |
local extraluapaths={} |
| 182 |
local extralibpaths={} |
| 183 |
local checkedfiles={} |
| 184 |
local luapaths=nil |
| 185 |
local libpaths=nil |
| 186 |
local oldluapath=nil |
| 187 |
local oldlibpath=nil |
| 188 |
local nofextralua=-1 |
| 189 |
local nofextralib=-1 |
| 190 |
local nofpathlua=-1 |
| 191 |
local nofpathlib=-1 |
| 192 |
local function listpaths(what,paths) |
| 193 |
local nofpaths=#paths |
| 194 |
if nofpaths>0 then |
| 195 |
for i=1,nofpaths do |
| 196 |
helpers.report("using %s path %i: %s",what,i,paths[i]) |
| 197 |
end |
| 198 |
else |
| 199 |
helpers.report("no %s paths defined",what) |
| 200 |
end |
| 201 |
return nofpaths |
| 202 |
end |
| 203 |
local function getextraluapaths() |
| 204 |
if helpers.trace and #extraluapaths~=nofextralua then |
| 205 |
nofextralua=listpaths("extra lua",extraluapaths) |
| 206 |
end |
| 207 |
return extraluapaths |
| 208 |
end |
| 209 |
local function getextralibpaths() |
| 210 |
if helpers.trace and #extralibpaths~=nofextralib then |
| 211 |
nofextralib=listpaths("extra lib",extralibpaths) |
| 212 |
end |
| 213 |
return extralibpaths |
| 214 |
end |
| 215 |
local function getluapaths() |
| 216 |
local luapath=package.path or "" |
| 217 |
if oldluapath~=luapath then |
| 218 |
luapaths=file.splitpath(luapath,";") |
| 219 |
oldluapath=luapath |
| 220 |
nofpathlua=-1 |
| 221 |
end |
| 222 |
if helpers.trace and #luapaths~=nofpathlua then |
| 223 |
nofpathlua=listpaths("builtin lua",luapaths) |
| 224 |
end |
| 225 |
return luapaths |
| 226 |
end |
| 227 |
local function getlibpaths() |
| 228 |
local libpath=package.cpath or "" |
| 229 |
if oldlibpath~=libpath then |
| 230 |
libpaths=file.splitpath(libpath,";") |
| 231 |
oldlibpath=libpath |
| 232 |
nofpathlib=-1 |
| 233 |
end |
| 234 |
if helpers.trace and #libpaths~=nofpathlib then |
| 235 |
nofpathlib=listpaths("builtin lib",libpaths) |
| 236 |
end |
| 237 |
return libpaths |
| 238 |
end |
| 239 |
package.luapaths=getluapaths |
| 240 |
package.libpaths=getlibpaths |
| 241 |
package.extraluapaths=getextraluapaths |
| 242 |
package.extralibpaths=getextralibpaths |
| 243 |
local hashes={ |
| 244 |
lua={}, |
| 245 |
lib={}, |
| 246 |
} |
| 247 |
local function registerpath(tag,what,target,...) |
| 248 |
local pathlist={... } |
| 249 |
local cleanpath=helpers.cleanpath |
| 250 |
local trace=helpers.trace |
| 251 |
local report=helpers.report |
| 252 |
local hash=hashes[what] |
| 253 |
local function add(path) |
| 254 |
local path=cleanpath(path) |
| 255 |
if not hash[path] then |
| 256 |
target[#target+1]=path |
| 257 |
hash[path]=true |
| 258 |
if trace then |
| 259 |
report("registered %s path %s: %s",tag,#target,path) |
| 260 |
end |
| 261 |
else |
| 262 |
if trace then |
| 263 |
report("duplicate %s path: %s",tag,path) |
| 264 |
end |
| 265 |
end |
| 266 |
end |
| 267 |
for p=1,#pathlist do |
| 268 |
local path=pathlist[p] |
| 269 |
if type(path)=="table" then |
| 270 |
for i=1,#path do |
| 271 |
add(path[i]) |
| 272 |
end |
| 273 |
else |
| 274 |
add(path) |
| 275 |
end |
| 276 |
end |
| 277 |
end |
| 278 |
local function pushpath(tag,what,target,path) |
| 279 |
local path=helpers.cleanpath(path) |
| 280 |
insert(target,1,path) |
| 281 |
if helpers.trace then |
| 282 |
helpers.report("pushing %s path in front: %s",tag,path) |
| 283 |
end |
| 284 |
end |
| 285 |
local function poppath(tag,what,target) |
| 286 |
local path=remove(target,1) |
| 287 |
if helpers.trace then |
| 288 |
if path then |
| 289 |
helpers.report("popping %s path from front: %s",tag,path) |
| 290 |
else |
| 291 |
helpers.report("no %s path to pop",tag) |
| 292 |
end |
| 293 |
end |
| 294 |
end |
| 295 |
helpers.registerpath=registerpath |
| 296 |
function package.extraluapath(...) |
| 297 |
registerpath("extra lua","lua",extraluapaths,...) |
| 298 |
end |
| 299 |
function package.pushluapath(path) |
| 300 |
pushpath("extra lua","lua",extraluapaths,path) |
| 301 |
end |
| 302 |
function package.popluapath() |
| 303 |
poppath("extra lua","lua",extraluapaths) |
| 304 |
end |
| 305 |
function package.extralibpath(...) |
| 306 |
registerpath("extra lib","lib",extralibpaths,...) |
| 307 |
end |
| 308 |
function package.pushlibpath(path) |
| 309 |
pushpath("extra lib","lib",extralibpaths,path) |
| 310 |
end |
| 311 |
function package.poplibpath() |
| 312 |
poppath("extra lib","lua",extralibpaths) |
| 313 |
end |
| 314 |
local function loadedaslib(resolved,rawname) |
| 315 |
local base=gsub(rawname,"%.","_") |
| 316 |
local init="luaopen_"..gsub(base,"%.","_") |
| 317 |
local data={ resolved,init,"" } |
| 318 |
checkedfiles[#checkedfiles+1]=data |
| 319 |
if helpers.trace then |
| 320 |
helpers.report("calling loadlib with '%s' with init '%s'",resolved,init) |
| 321 |
end |
| 322 |
local a,b,c=package.loadlib(resolved,init) |
| 323 |
if not a and type(b)=="string" then |
| 324 |
data[3]=string.fullstrip(b or "unknown error") |
| 325 |
end |
| 326 |
return a,b,c |
| 327 |
end |
| 328 |
helpers.loadedaslib=loadedaslib |
| 329 |
local function loadedbypath(name,rawname,paths,islib,what) |
| 330 |
local trace=helpers.trace |
| 331 |
for p=1,#paths do |
| 332 |
local path=paths[p] |
| 333 |
local resolved=filejoin(path,name) |
| 334 |
if trace then |
| 335 |
helpers.report("%s path, identifying '%s' on '%s'",what,name,path) |
| 336 |
end |
| 337 |
if isreadable(resolved) then |
| 338 |
if trace then |
| 339 |
helpers.report("%s path, '%s' found on '%s'",what,name,resolved) |
| 340 |
end |
| 341 |
if islib then |
| 342 |
return loadedaslib(resolved,rawname) |
| 343 |
else |
| 344 |
return loadfile(resolved) |
| 345 |
end |
| 346 |
end |
| 347 |
end |
| 348 |
end |
| 349 |
helpers.loadedbypath=loadedbypath |
| 350 |
local function loadedbyname(name,rawname) |
| 351 |
if find(name,"^/") or find(name,"^[a-zA-Z]:/") then |
| 352 |
local trace=helpers.trace |
| 353 |
if trace then |
| 354 |
helpers.report("qualified name, identifying '%s'",what,name) |
| 355 |
end |
| 356 |
if isreadable(name) then |
| 357 |
if trace then |
| 358 |
helpers.report("qualified name, '%s' found",what,name) |
| 359 |
end |
| 360 |
return loadfile(name) |
| 361 |
end |
| 362 |
end |
| 363 |
end |
| 364 |
helpers.loadedbyname=loadedbyname |
| 365 |
methods["reset loaded"]=function(name) |
| 366 |
checkedfiles={} |
| 367 |
return false |
| 368 |
end |
| 369 |
methods["already loaded"]=function(name) |
| 370 |
return package.loaded[name] |
| 371 |
end |
| 372 |
methods["preload table"]=function(name) |
| 373 |
local f=builtin["preload table"] |
| 374 |
if f then |
| 375 |
return f(name) |
| 376 |
end |
| 377 |
end |
| 378 |
methods["qualified path"]=function(name) |
| 379 |
return loadedbyname(addsuffix(lualibfile(name),"lua"),name) |
| 380 |
end |
| 381 |
methods["lua extra list"]=function(name) |
| 382 |
return loadedbypath(addsuffix(lualibfile(name),"lua"),name,getextraluapaths(),false,"lua") |
| 383 |
end |
| 384 |
methods["lib extra list"]=function(name) |
| 385 |
return loadedbypath(addsuffix(lualibfile(name),os.libsuffix),name,getextralibpaths(),true,"lib") |
| 386 |
end |
| 387 |
methods["path specification"]=function(name) |
| 388 |
local f=builtin["path specification"] |
| 389 |
if f then |
| 390 |
getluapaths() |
| 391 |
return f(name) |
| 392 |
end |
| 393 |
end |
| 394 |
methods["cpath specification"]=function(name) |
| 395 |
local f=builtin["cpath specification"] |
| 396 |
if f then |
| 397 |
getlibpaths() |
| 398 |
return f(name) |
| 399 |
end |
| 400 |
end |
| 401 |
methods["all in one fallback"]=function(name) |
| 402 |
local f=builtin["all in one fallback"] |
| 403 |
if f then |
| 404 |
return f(name) |
| 405 |
end |
| 406 |
end |
| 407 |
methods["not loaded"]=function(name) |
| 408 |
if helpers.trace then |
| 409 |
helpers.report("unable to locate '%s'",name or "?") |
| 410 |
for i=1,#checkedfiles do |
| 411 |
helpers.report("checked file '%s', initializer '%s', message '%s'",unpack(checkedfiles[i])) |
| 412 |
end |
| 413 |
end |
| 414 |
return nil |
| 415 |
end |
| 416 |
local level=0 |
| 417 |
local used={} |
| 418 |
helpers.traceused=false |
| 419 |
function helpers.loaded(name) |
| 420 |
local sequence=helpers.sequence |
| 421 |
level=level+1 |
| 422 |
for i=1,#sequence do |
| 423 |
local method=sequence[i] |
| 424 |
local lookup=method and methods[method] |
| 425 |
if type(lookup)=="function" then |
| 426 |
if helpers.trace then |
| 427 |
helpers.report("%s, level '%s', method '%s', name '%s'","locating",level,method,name) |
| 428 |
end |
| 429 |
local result,rest=lookup(name) |
| 430 |
if type(result)=="function" then |
| 431 |
if helpers.trace then |
| 432 |
helpers.report("%s, level '%s', method '%s', name '%s'","found",level,method,name) |
| 433 |
end |
| 434 |
if helpers.traceused then |
| 435 |
used[#used+1]={ level=level,name=name } |
| 436 |
end |
| 437 |
level=level-1 |
| 438 |
return result,rest |
| 439 |
end |
| 440 |
end |
| 441 |
end |
| 442 |
level=level-1 |
| 443 |
return nil |
| 444 |
end |
| 445 |
function helpers.showused() |
| 446 |
local n=#used |
| 447 |
if n>0 then |
| 448 |
helpers.report("%s libraries loaded:",n) |
| 449 |
helpers.report() |
| 450 |
for i=1,n do |
| 451 |
local u=used[i] |
| 452 |
helpers.report("%i %a",u.level,u.name) |
| 453 |
end |
| 454 |
helpers.report() |
| 455 |
end |
| 456 |
end |
| 457 |
function helpers.unload(name) |
| 458 |
if helpers.trace then |
| 459 |
if package.loaded[name] then |
| 460 |
helpers.report("unloading, name '%s', %s",name,"done") |
| 461 |
else |
| 462 |
helpers.report("unloading, name '%s', %s",name,"not loaded") |
| 463 |
end |
| 464 |
end |
| 465 |
package.loaded[name]=nil |
| 466 |
end |
| 467 |
table.insert(searchers,1,helpers.loaded) |
| 468 |
if context then |
| 469 |
package.path="" |
| 470 |
end |
| 471 |
|
| 472 |
end -- closure |
| 473 |
|
| 474 |
do -- begin closure to overcome local limits and interference |
| 475 |
|
| 476 |
if not modules then modules={} end modules ['l-lpeg']={ |
| 477 |
version=1.001, |
| 478 |
comment="companion to luat-lib.mkiv", |
| 479 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 480 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 481 |
license="see context related readme files" |
| 482 |
} |
| 483 |
lpeg=require("lpeg") |
| 484 |
local lpeg=lpeg |
| 485 |
if not lpeg.print then function lpeg.print(...) print(lpeg.pcode(...)) end end |
| 486 |
local type,next,tostring=type,next,tostring |
| 487 |
local byte,char,gmatch,format=string.byte,string.char,string.gmatch,string.format |
| 488 |
local floor=math.floor |
| 489 |
local P,R,S,V,Ct,C,Cs,Cc,Cp,Cmt=lpeg.P,lpeg.R,lpeg.S,lpeg.V,lpeg.Ct,lpeg.C,lpeg.Cs,lpeg.Cc,lpeg.Cp,lpeg.Cmt |
| 490 |
local lpegtype,lpegmatch,lpegprint=lpeg.type,lpeg.match,lpeg.print |
| 491 |
if setinspector then |
| 492 |
setinspector("lpeg",function(v) if lpegtype(v) then lpegprint(v) return true end end) |
| 493 |
end |
| 494 |
lpeg.patterns=lpeg.patterns or {} |
| 495 |
local patterns=lpeg.patterns |
| 496 |
local anything=P(1) |
| 497 |
local endofstring=P(-1) |
| 498 |
local alwaysmatched=P(true) |
| 499 |
patterns.anything=anything |
| 500 |
patterns.endofstring=endofstring |
| 501 |
patterns.beginofstring=alwaysmatched |
| 502 |
patterns.alwaysmatched=alwaysmatched |
| 503 |
local sign=S('+-') |
| 504 |
local zero=P('0') |
| 505 |
local digit=R('09') |
| 506 |
local digits=digit^1 |
| 507 |
local octdigit=R("07") |
| 508 |
local octdigits=octdigit^1 |
| 509 |
local lowercase=R("az") |
| 510 |
local uppercase=R("AZ") |
| 511 |
local underscore=P("_") |
| 512 |
local hexdigit=digit+lowercase+uppercase |
| 513 |
local hexdigits=hexdigit^1 |
| 514 |
local cr,lf,crlf=P("\r"),P("\n"),P("\r\n") |
| 515 |
local newline=P("\r")*(P("\n")+P(true))+P("\n") |
| 516 |
local escaped=P("\\")*anything |
| 517 |
local squote=P("'") |
| 518 |
local dquote=P('"') |
| 519 |
local space=P(" ") |
| 520 |
local period=P(".") |
| 521 |
local comma=P(",") |
| 522 |
local utfbom_32_be=P('\000\000\254\255') |
| 523 |
local utfbom_32_le=P('\255\254\000\000') |
| 524 |
local utfbom_16_be=P('\254\255') |
| 525 |
local utfbom_16_le=P('\255\254') |
| 526 |
local utfbom_8=P('\239\187\191') |
| 527 |
local utfbom=utfbom_32_be+utfbom_32_le+utfbom_16_be+utfbom_16_le+utfbom_8 |
| 528 |
local utftype=utfbom_32_be*Cc("utf-32-be")+utfbom_32_le*Cc("utf-32-le")+utfbom_16_be*Cc("utf-16-be")+utfbom_16_le*Cc("utf-16-le")+utfbom_8*Cc("utf-8")+alwaysmatched*Cc("utf-8") |
| 529 |
local utfstricttype=utfbom_32_be*Cc("utf-32-be")+utfbom_32_le*Cc("utf-32-le")+utfbom_16_be*Cc("utf-16-be")+utfbom_16_le*Cc("utf-16-le")+utfbom_8*Cc("utf-8") |
| 530 |
local utfoffset=utfbom_32_be*Cc(4)+utfbom_32_le*Cc(4)+utfbom_16_be*Cc(2)+utfbom_16_le*Cc(2)+utfbom_8*Cc(3)+Cc(0) |
| 531 |
local utf8next=R("\128\191") |
| 532 |
patterns.utfbom_32_be=utfbom_32_be |
| 533 |
patterns.utfbom_32_le=utfbom_32_le |
| 534 |
patterns.utfbom_16_be=utfbom_16_be |
| 535 |
patterns.utfbom_16_le=utfbom_16_le |
| 536 |
patterns.utfbom_8=utfbom_8 |
| 537 |
patterns.utf_16_be_nl=P("\000\r\000\n")+P("\000\r")+P("\000\n") |
| 538 |
patterns.utf_16_le_nl=P("\r\000\n\000")+P("\r\000")+P("\n\000") |
| 539 |
patterns.utf_32_be_nl=P("\000\000\000\r\000\000\000\n")+P("\000\000\000\r")+P("\000\000\000\n") |
| 540 |
patterns.utf_32_le_nl=P("\r\000\000\000\n\000\000\000")+P("\r\000\000\000")+P("\n\000\000\000") |
| 541 |
patterns.utf8one=R("\000\127") |
| 542 |
patterns.utf8two=R("\194\223")*utf8next |
| 543 |
patterns.utf8three=R("\224\239")*utf8next*utf8next |
| 544 |
patterns.utf8four=R("\240\244")*utf8next*utf8next*utf8next |
| 545 |
patterns.utfbom=utfbom |
| 546 |
patterns.utftype=utftype |
| 547 |
patterns.utfstricttype=utfstricttype |
| 548 |
patterns.utfoffset=utfoffset |
| 549 |
local utf8char=patterns.utf8one+patterns.utf8two+patterns.utf8three+patterns.utf8four |
| 550 |
local validutf8char=utf8char^0*endofstring*Cc(true)+Cc(false) |
| 551 |
local utf8character=P(1)*R("\128\191")^0 |
| 552 |
patterns.utf8=utf8char |
| 553 |
patterns.utf8char=utf8char |
| 554 |
patterns.utf8character=utf8character |
| 555 |
patterns.validutf8=validutf8char |
| 556 |
patterns.validutf8char=validutf8char |
| 557 |
local eol=S("\n\r") |
| 558 |
local spacer=S(" \t\f\v") |
| 559 |
local whitespace=eol+spacer |
| 560 |
local nonspacer=1-spacer |
| 561 |
local nonwhitespace=1-whitespace |
| 562 |
patterns.eol=eol |
| 563 |
patterns.spacer=spacer |
| 564 |
patterns.whitespace=whitespace |
| 565 |
patterns.nonspacer=nonspacer |
| 566 |
patterns.nonwhitespace=nonwhitespace |
| 567 |
local stripper=spacer^0*C((spacer^0*nonspacer^1)^0) |
| 568 |
local fullstripper=whitespace^0*C((whitespace^0*nonwhitespace^1)^0) |
| 569 |
local collapser=Cs(spacer^0/""*nonspacer^0*((spacer^0/" "*nonspacer^1)^0)) |
| 570 |
local nospacer=Cs((whitespace^1/""+nonwhitespace^1)^0) |
| 571 |
local b_collapser=Cs(whitespace^0/""*(nonwhitespace^1+whitespace^1/" ")^0) |
| 572 |
local m_collapser=Cs((nonwhitespace^1+whitespace^1/" ")^0) |
| 573 |
local e_collapser=Cs((whitespace^1*endofstring/""+nonwhitespace^1+whitespace^1/" ")^0) |
| 574 |
local x_collapser=Cs((nonwhitespace^1+whitespace^1/"" )^0) |
| 575 |
local b_stripper=Cs(spacer^0/""*(nonspacer^1+spacer^1/" ")^0) |
| 576 |
local m_stripper=Cs((nonspacer^1+spacer^1/" ")^0) |
| 577 |
local e_stripper=Cs((spacer^1*endofstring/""+nonspacer^1+spacer^1/" ")^0) |
| 578 |
local x_stripper=Cs((nonspacer^1+spacer^1/"" )^0) |
| 579 |
patterns.stripper=stripper |
| 580 |
patterns.fullstripper=fullstripper |
| 581 |
patterns.collapser=collapser |
| 582 |
patterns.nospacer=nospacer |
| 583 |
patterns.b_collapser=b_collapser |
| 584 |
patterns.m_collapser=m_collapser |
| 585 |
patterns.e_collapser=e_collapser |
| 586 |
patterns.x_collapser=x_collapser |
| 587 |
patterns.b_stripper=b_stripper |
| 588 |
patterns.m_stripper=m_stripper |
| 589 |
patterns.e_stripper=e_stripper |
| 590 |
patterns.x_stripper=x_stripper |
| 591 |
patterns.lowercase=lowercase |
| 592 |
patterns.uppercase=uppercase |
| 593 |
patterns.letter=patterns.lowercase+patterns.uppercase |
| 594 |
patterns.space=space |
| 595 |
patterns.tab=P("\t") |
| 596 |
patterns.spaceortab=patterns.space+patterns.tab |
| 597 |
patterns.newline=newline |
| 598 |
patterns.emptyline=newline^1 |
| 599 |
patterns.equal=P("=") |
| 600 |
patterns.comma=comma |
| 601 |
patterns.commaspacer=comma*spacer^0 |
| 602 |
patterns.period=period |
| 603 |
patterns.colon=P(":") |
| 604 |
patterns.semicolon=P(";") |
| 605 |
patterns.underscore=underscore |
| 606 |
patterns.escaped=escaped |
| 607 |
patterns.squote=squote |
| 608 |
patterns.dquote=dquote |
| 609 |
patterns.nosquote=(escaped+(1-squote))^0 |
| 610 |
patterns.nodquote=(escaped+(1-dquote))^0 |
| 611 |
patterns.unsingle=(squote/"")*patterns.nosquote*(squote/"") |
| 612 |
patterns.undouble=(dquote/"")*patterns.nodquote*(dquote/"") |
| 613 |
patterns.unquoted=patterns.undouble+patterns.unsingle |
| 614 |
patterns.unspacer=((patterns.spacer^1)/"")^0 |
| 615 |
patterns.singlequoted=squote*patterns.nosquote*squote |
| 616 |
patterns.doublequoted=dquote*patterns.nodquote*dquote |
| 617 |
patterns.quoted=patterns.doublequoted+patterns.singlequoted |
| 618 |
patterns.digit=digit |
| 619 |
patterns.digits=digits |
| 620 |
patterns.octdigit=octdigit |
| 621 |
patterns.octdigits=octdigits |
| 622 |
patterns.hexdigit=hexdigit |
| 623 |
patterns.hexdigits=hexdigits |
| 624 |
patterns.sign=sign |
| 625 |
patterns.cardinal=digits |
| 626 |
patterns.integer=sign^-1*digits |
| 627 |
patterns.unsigned=digit^0*period*digits |
| 628 |
patterns.float=sign^-1*patterns.unsigned |
| 629 |
patterns.cunsigned=digit^0*comma*digits |
| 630 |
patterns.cpunsigned=digit^0*(period+comma)*digits |
| 631 |
patterns.cfloat=sign^-1*patterns.cunsigned |
| 632 |
patterns.cpfloat=sign^-1*patterns.cpunsigned |
| 633 |
patterns.number=patterns.float+patterns.integer |
| 634 |
patterns.cnumber=patterns.cfloat+patterns.integer |
| 635 |
patterns.cpnumber=patterns.cpfloat+patterns.integer |
| 636 |
patterns.oct=zero*octdigits |
| 637 |
patterns.octal=patterns.oct |
| 638 |
patterns.HEX=zero*P("X")*(digit+uppercase)^1 |
| 639 |
patterns.hex=zero*P("x")*(digit+lowercase)^1 |
| 640 |
patterns.hexadecimal=zero*S("xX")*hexdigits |
| 641 |
patterns.hexafloat=sign^-1*zero*S("xX")*(hexdigit^0*period*hexdigits+hexdigits*period*hexdigit^0+hexdigits)*(S("pP")*sign^-1*hexdigits)^-1 |
| 642 |
patterns.decafloat=sign^-1*(digit^0*period*digits+digits*period*digit^0+digits)*S("eE")*sign^-1*digits |
| 643 |
patterns.propername=(uppercase+lowercase+underscore)*(uppercase+lowercase+underscore+digit)^0*endofstring |
| 644 |
patterns.somecontent=(anything-newline-space)^1 |
| 645 |
patterns.beginline=#(1-newline) |
| 646 |
patterns.longtostring=Cs(whitespace^0/""*((patterns.quoted+nonwhitespace^1+whitespace^1/""*(endofstring+Cc(" ")))^0)) |
| 647 |
local function anywhere(pattern) |
| 648 |
return (1-P(pattern))^0*P(pattern) |
| 649 |
end |
| 650 |
lpeg.anywhere=anywhere |
| 651 |
function lpeg.instringchecker(p) |
| 652 |
p=anywhere(p) |
| 653 |
return function(str) |
| 654 |
return lpegmatch(p,str) and true or false |
| 655 |
end |
| 656 |
end |
| 657 |
function lpeg.splitter(pattern,action) |
| 658 |
if action then |
| 659 |
return (((1-P(pattern))^1)/action+1)^0 |
| 660 |
else |
| 661 |
return (Cs((1-P(pattern))^1)+1)^0 |
| 662 |
end |
| 663 |
end |
| 664 |
function lpeg.tsplitter(pattern,action) |
| 665 |
if action then |
| 666 |
return Ct((((1-P(pattern))^1)/action+1)^0) |
| 667 |
else |
| 668 |
return Ct((Cs((1-P(pattern))^1)+1)^0) |
| 669 |
end |
| 670 |
end |
| 671 |
local splitters_s,splitters_m,splitters_t={},{},{} |
| 672 |
local function splitat(separator,single) |
| 673 |
local splitter=(single and splitters_s[separator]) or splitters_m[separator] |
| 674 |
if not splitter then |
| 675 |
separator=P(separator) |
| 676 |
local other=C((1-separator)^0) |
| 677 |
if single then |
| 678 |
local any=anything |
| 679 |
splitter=other*(separator*C(any^0)+"") |
| 680 |
splitters_s[separator]=splitter |
| 681 |
else |
| 682 |
splitter=other*(separator*other)^0 |
| 683 |
splitters_m[separator]=splitter |
| 684 |
end |
| 685 |
end |
| 686 |
return splitter |
| 687 |
end |
| 688 |
local function tsplitat(separator) |
| 689 |
local splitter=splitters_t[separator] |
| 690 |
if not splitter then |
| 691 |
splitter=Ct(splitat(separator)) |
| 692 |
splitters_t[separator]=splitter |
| 693 |
end |
| 694 |
return splitter |
| 695 |
end |
| 696 |
lpeg.splitat=splitat |
| 697 |
lpeg.tsplitat=tsplitat |
| 698 |
function string.splitup(str,separator) |
| 699 |
if not separator then |
| 700 |
separator="," |
| 701 |
end |
| 702 |
return lpegmatch(splitters_m[separator] or splitat(separator),str) |
| 703 |
end |
| 704 |
local cache={} |
| 705 |
function lpeg.split(separator,str) |
| 706 |
local c=cache[separator] |
| 707 |
if not c then |
| 708 |
c=tsplitat(separator) |
| 709 |
cache[separator]=c |
| 710 |
end |
| 711 |
return lpegmatch(c,str) |
| 712 |
end |
| 713 |
function string.split(str,separator) |
| 714 |
if separator then |
| 715 |
local c=cache[separator] |
| 716 |
if not c then |
| 717 |
c=tsplitat(separator) |
| 718 |
cache[separator]=c |
| 719 |
end |
| 720 |
return lpegmatch(c,str) |
| 721 |
else |
| 722 |
return { str } |
| 723 |
end |
| 724 |
end |
| 725 |
local spacing=patterns.spacer^0*newline |
| 726 |
local empty=spacing*Cc("") |
| 727 |
local nonempty=Cs((1-spacing)^1)*spacing^-1 |
| 728 |
local content=(empty+nonempty)^1 |
| 729 |
patterns.textline=content |
| 730 |
local linesplitter=tsplitat(newline) |
| 731 |
patterns.linesplitter=linesplitter |
| 732 |
function string.splitlines(str) |
| 733 |
return lpegmatch(linesplitter,str) |
| 734 |
end |
| 735 |
local cache={} |
| 736 |
function lpeg.checkedsplit(separator,str) |
| 737 |
local c=cache[separator] |
| 738 |
if not c then |
| 739 |
separator=P(separator) |
| 740 |
local other=C((1-separator)^1) |
| 741 |
c=Ct(separator^0*other*(separator^1*other)^0) |
| 742 |
cache[separator]=c |
| 743 |
end |
| 744 |
return lpegmatch(c,str) |
| 745 |
end |
| 746 |
function string.checkedsplit(str,separator) |
| 747 |
local c=cache[separator] |
| 748 |
if not c then |
| 749 |
separator=P(separator) |
| 750 |
local other=C((1-separator)^1) |
| 751 |
c=Ct(separator^0*other*(separator^1*other)^0) |
| 752 |
cache[separator]=c |
| 753 |
end |
| 754 |
return lpegmatch(c,str) |
| 755 |
end |
| 756 |
local function f2(s) local c1,c2=byte(s,1,2) return c1*64+c2-12416 end |
| 757 |
local function f3(s) local c1,c2,c3=byte(s,1,3) return (c1*64+c2)*64+c3-925824 end |
| 758 |
local function f4(s) local c1,c2,c3,c4=byte(s,1,4) return ((c1*64+c2)*64+c3)*64+c4-63447168 end |
| 759 |
local utf8byte=patterns.utf8one/byte+patterns.utf8two/f2+patterns.utf8three/f3+patterns.utf8four/f4 |
| 760 |
patterns.utf8byte=utf8byte |
| 761 |
local cache={} |
| 762 |
function lpeg.stripper(str) |
| 763 |
if type(str)=="string" then |
| 764 |
local s=cache[str] |
| 765 |
if not s then |
| 766 |
s=Cs(((S(str)^1)/""+1)^0) |
| 767 |
cache[str]=s |
| 768 |
end |
| 769 |
return s |
| 770 |
else |
| 771 |
return Cs(((str^1)/""+1)^0) |
| 772 |
end |
| 773 |
end |
| 774 |
local cache={} |
| 775 |
function lpeg.keeper(str) |
| 776 |
if type(str)=="string" then |
| 777 |
local s=cache[str] |
| 778 |
if not s then |
| 779 |
s=Cs((((1-S(str))^1)/""+1)^0) |
| 780 |
cache[str]=s |
| 781 |
end |
| 782 |
return s |
| 783 |
else |
| 784 |
return Cs((((1-str)^1)/""+1)^0) |
| 785 |
end |
| 786 |
end |
| 787 |
function lpeg.frontstripper(str) |
| 788 |
return (P(str)+P(true))*Cs(anything^0) |
| 789 |
end |
| 790 |
function lpeg.endstripper(str) |
| 791 |
return Cs((1-P(str)*endofstring)^0) |
| 792 |
end |
| 793 |
function lpeg.replacer(one,two,makefunction,isutf) |
| 794 |
local pattern |
| 795 |
local u=isutf and utf8char or 1 |
| 796 |
if type(one)=="table" then |
| 797 |
local no=#one |
| 798 |
local p=P(false) |
| 799 |
if no==0 then |
| 800 |
for k,v in next,one do |
| 801 |
p=p+P(k)/v |
| 802 |
end |
| 803 |
pattern=Cs((p+u)^0) |
| 804 |
elseif no==1 then |
| 805 |
local o=one[1] |
| 806 |
one,two=P(o[1]),o[2] |
| 807 |
pattern=Cs((one/two+u)^0) |
| 808 |
else |
| 809 |
for i=1,no do |
| 810 |
local o=one[i] |
| 811 |
p=p+P(o[1])/o[2] |
| 812 |
end |
| 813 |
pattern=Cs((p+u)^0) |
| 814 |
end |
| 815 |
else |
| 816 |
pattern=Cs((P(one)/(two or "")+u)^0) |
| 817 |
end |
| 818 |
if makefunction then |
| 819 |
return function(str) |
| 820 |
return lpegmatch(pattern,str) |
| 821 |
end |
| 822 |
else |
| 823 |
return pattern |
| 824 |
end |
| 825 |
end |
| 826 |
function lpeg.finder(lst,makefunction,isutf) |
| 827 |
local pattern |
| 828 |
if type(lst)=="table" then |
| 829 |
pattern=P(false) |
| 830 |
if #lst==0 then |
| 831 |
for k,v in next,lst do |
| 832 |
pattern=pattern+P(k) |
| 833 |
end |
| 834 |
else |
| 835 |
for i=1,#lst do |
| 836 |
pattern=pattern+P(lst[i]) |
| 837 |
end |
| 838 |
end |
| 839 |
else |
| 840 |
pattern=P(lst) |
| 841 |
end |
| 842 |
if isutf then |
| 843 |
pattern=((utf8char or 1)-pattern)^0*pattern |
| 844 |
else |
| 845 |
pattern=(1-pattern)^0*pattern |
| 846 |
end |
| 847 |
if makefunction then |
| 848 |
return function(str) |
| 849 |
return lpegmatch(pattern,str) |
| 850 |
end |
| 851 |
else |
| 852 |
return pattern |
| 853 |
end |
| 854 |
end |
| 855 |
local splitters_f,splitters_s={},{} |
| 856 |
function lpeg.firstofsplit(separator) |
| 857 |
local splitter=splitters_f[separator] |
| 858 |
if not splitter then |
| 859 |
local pattern=P(separator) |
| 860 |
splitter=C((1-pattern)^0) |
| 861 |
splitters_f[separator]=splitter |
| 862 |
end |
| 863 |
return splitter |
| 864 |
end |
| 865 |
function lpeg.secondofsplit(separator) |
| 866 |
local splitter=splitters_s[separator] |
| 867 |
if not splitter then |
| 868 |
local pattern=P(separator) |
| 869 |
splitter=(1-pattern)^0*pattern*C(anything^0) |
| 870 |
splitters_s[separator]=splitter |
| 871 |
end |
| 872 |
return splitter |
| 873 |
end |
| 874 |
local splitters_s,splitters_p={},{} |
| 875 |
function lpeg.beforesuffix(separator) |
| 876 |
local splitter=splitters_s[separator] |
| 877 |
if not splitter then |
| 878 |
local pattern=P(separator) |
| 879 |
splitter=C((1-pattern)^0)*pattern*endofstring |
| 880 |
splitters_s[separator]=splitter |
| 881 |
end |
| 882 |
return splitter |
| 883 |
end |
| 884 |
function lpeg.afterprefix(separator) |
| 885 |
local splitter=splitters_p[separator] |
| 886 |
if not splitter then |
| 887 |
local pattern=P(separator) |
| 888 |
splitter=pattern*C(anything^0) |
| 889 |
splitters_p[separator]=splitter |
| 890 |
end |
| 891 |
return splitter |
| 892 |
end |
| 893 |
function lpeg.balancer(left,right) |
| 894 |
left,right=P(left),P(right) |
| 895 |
return P { left*((1-left-right)+V(1))^0*right } |
| 896 |
end |
| 897 |
function lpeg.counter(pattern,action) |
| 898 |
local n=0 |
| 899 |
local pattern=(P(pattern)/function() n=n+1 end+anything)^0 |
| 900 |
if action then |
| 901 |
return function(str) n=0;lpegmatch(pattern,str);action(n) end |
| 902 |
else |
| 903 |
return function(str) n=0;lpegmatch(pattern,str);return n end |
| 904 |
end |
| 905 |
end |
| 906 |
function lpeg.is_lpeg(p) |
| 907 |
return p and lpegtype(p)=="pattern" |
| 908 |
end |
| 909 |
function lpeg.oneof(list,...) |
| 910 |
if type(list)~="table" then |
| 911 |
list={ list,... } |
| 912 |
end |
| 913 |
local p=P(list[1]) |
| 914 |
for l=2,#list do |
| 915 |
p=p+P(list[l]) |
| 916 |
end |
| 917 |
return p |
| 918 |
end |
| 919 |
local sort=table.sort |
| 920 |
local function copyindexed(old) |
| 921 |
local new={} |
| 922 |
for i=1,#old do |
| 923 |
new[i]=old |
| 924 |
end |
| 925 |
return new |
| 926 |
end |
| 927 |
local function sortedkeys(tab) |
| 928 |
local keys,s={},0 |
| 929 |
for key,_ in next,tab do |
| 930 |
s=s+1 |
| 931 |
keys[s]=key |
| 932 |
end |
| 933 |
sort(keys) |
| 934 |
return keys |
| 935 |
end |
| 936 |
function lpeg.append(list,pp,delayed,checked) |
| 937 |
local p=pp |
| 938 |
if #list>0 then |
| 939 |
local keys=copyindexed(list) |
| 940 |
sort(keys) |
| 941 |
for i=#keys,1,-1 do |
| 942 |
local k=keys[i] |
| 943 |
if p then |
| 944 |
p=P(k)+p |
| 945 |
else |
| 946 |
p=P(k) |
| 947 |
end |
| 948 |
end |
| 949 |
elseif delayed then |
| 950 |
local keys=sortedkeys(list) |
| 951 |
if p then |
| 952 |
for i=1,#keys,1 do |
| 953 |
local k=keys[i] |
| 954 |
local v=list[k] |
| 955 |
p=P(k)/list+p |
| 956 |
end |
| 957 |
else |
| 958 |
for i=1,#keys do |
| 959 |
local k=keys[i] |
| 960 |
local v=list[k] |
| 961 |
if p then |
| 962 |
p=P(k)+p |
| 963 |
else |
| 964 |
p=P(k) |
| 965 |
end |
| 966 |
end |
| 967 |
if p then |
| 968 |
p=p/list |
| 969 |
end |
| 970 |
end |
| 971 |
elseif checked then |
| 972 |
local keys=sortedkeys(list) |
| 973 |
for i=1,#keys do |
| 974 |
local k=keys[i] |
| 975 |
local v=list[k] |
| 976 |
if p then |
| 977 |
if k==v then |
| 978 |
p=P(k)+p |
| 979 |
else |
| 980 |
p=P(k)/v+p |
| 981 |
end |
| 982 |
else |
| 983 |
if k==v then |
| 984 |
p=P(k) |
| 985 |
else |
| 986 |
p=P(k)/v |
| 987 |
end |
| 988 |
end |
| 989 |
end |
| 990 |
else |
| 991 |
local keys=sortedkeys(list) |
| 992 |
for i=1,#keys do |
| 993 |
local k=keys[i] |
| 994 |
local v=list[k] |
| 995 |
if p then |
| 996 |
p=P(k)/v+p |
| 997 |
else |
| 998 |
p=P(k)/v |
| 999 |
end |
| 1000 |
end |
| 1001 |
end |
| 1002 |
return p |
| 1003 |
end |
| 1004 |
local p_false=P(false) |
| 1005 |
local p_true=P(true) |
| 1006 |
local lower=utf and utf.lower or string.lower |
| 1007 |
local upper=utf and utf.upper or string.upper |
| 1008 |
function lpeg.setutfcasers(l,u) |
| 1009 |
lower=l or lower |
| 1010 |
upper=u or upper |
| 1011 |
end |
| 1012 |
local function make1(t,rest) |
| 1013 |
local p=p_false |
| 1014 |
local keys=sortedkeys(t) |
| 1015 |
for i=1,#keys do |
| 1016 |
local k=keys[i] |
| 1017 |
if k~="" then |
| 1018 |
local v=t[k] |
| 1019 |
if v==true then |
| 1020 |
p=p+P(k)*p_true |
| 1021 |
elseif v==false then |
| 1022 |
else |
| 1023 |
p=p+P(k)*make1(v,v[""]) |
| 1024 |
end |
| 1025 |
end |
| 1026 |
end |
| 1027 |
if rest then |
| 1028 |
p=p+p_true |
| 1029 |
end |
| 1030 |
return p |
| 1031 |
end |
| 1032 |
local function make2(t,rest) |
| 1033 |
local p=p_false |
| 1034 |
local keys=sortedkeys(t) |
| 1035 |
for i=1,#keys do |
| 1036 |
local k=keys[i] |
| 1037 |
if k~="" then |
| 1038 |
local v=t[k] |
| 1039 |
if v==true then |
| 1040 |
p=p+(P(lower(k))+P(upper(k)))*p_true |
| 1041 |
elseif v==false then |
| 1042 |
else |
| 1043 |
p=p+(P(lower(k))+P(upper(k)))*make2(v,v[""]) |
| 1044 |
end |
| 1045 |
end |
| 1046 |
end |
| 1047 |
if rest then |
| 1048 |
p=p+p_true |
| 1049 |
end |
| 1050 |
return p |
| 1051 |
end |
| 1052 |
local function utfchartabletopattern(list,insensitive) |
| 1053 |
local tree={} |
| 1054 |
local n=#list |
| 1055 |
if n==0 then |
| 1056 |
for s in next,list do |
| 1057 |
local t=tree |
| 1058 |
local p,pk |
| 1059 |
for c in gmatch(s,".") do |
| 1060 |
if t==true then |
| 1061 |
t={ [c]=true,[""]=true } |
| 1062 |
p[pk]=t |
| 1063 |
p=t |
| 1064 |
t=false |
| 1065 |
elseif t==false then |
| 1066 |
t={ [c]=false } |
| 1067 |
p[pk]=t |
| 1068 |
p=t |
| 1069 |
t=false |
| 1070 |
else |
| 1071 |
local tc=t[c] |
| 1072 |
if not tc then |
| 1073 |
tc=false |
| 1074 |
t[c]=false |
| 1075 |
end |
| 1076 |
p=t |
| 1077 |
t=tc |
| 1078 |
end |
| 1079 |
pk=c |
| 1080 |
end |
| 1081 |
if t==false then |
| 1082 |
p[pk]=true |
| 1083 |
elseif t==true then |
| 1084 |
else |
| 1085 |
t[""]=true |
| 1086 |
end |
| 1087 |
end |
| 1088 |
else |
| 1089 |
for i=1,n do |
| 1090 |
local s=list[i] |
| 1091 |
local t=tree |
| 1092 |
local p,pk |
| 1093 |
for c in gmatch(s,".") do |
| 1094 |
if t==true then |
| 1095 |
t={ [c]=true,[""]=true } |
| 1096 |
p[pk]=t |
| 1097 |
p=t |
| 1098 |
t=false |
| 1099 |
elseif t==false then |
| 1100 |
t={ [c]=false } |
| 1101 |
p[pk]=t |
| 1102 |
p=t |
| 1103 |
t=false |
| 1104 |
else |
| 1105 |
local tc=t[c] |
| 1106 |
if not tc then |
| 1107 |
tc=false |
| 1108 |
t[c]=false |
| 1109 |
end |
| 1110 |
p=t |
| 1111 |
t=tc |
| 1112 |
end |
| 1113 |
pk=c |
| 1114 |
end |
| 1115 |
if t==false then |
| 1116 |
p[pk]=true |
| 1117 |
elseif t==true then |
| 1118 |
else |
| 1119 |
t[""]=true |
| 1120 |
end |
| 1121 |
end |
| 1122 |
end |
| 1123 |
return (insensitive and make2 or make1)(tree) |
| 1124 |
end |
| 1125 |
lpeg.utfchartabletopattern=utfchartabletopattern |
| 1126 |
function lpeg.utfreplacer(list,insensitive) |
| 1127 |
local pattern=Cs((utfchartabletopattern(list,insensitive)/list+utf8character)^0) |
| 1128 |
return function(str) |
| 1129 |
return lpegmatch(pattern,str) or str |
| 1130 |
end |
| 1131 |
end |
| 1132 |
patterns.containseol=lpeg.finder(eol) |
| 1133 |
local function nextstep(n,step,result) |
| 1134 |
local m=n%step |
| 1135 |
local d=floor(n/step) |
| 1136 |
if d>0 then |
| 1137 |
local v=V(tostring(step)) |
| 1138 |
local s=result.start |
| 1139 |
for i=1,d do |
| 1140 |
if s then |
| 1141 |
s=v*s |
| 1142 |
else |
| 1143 |
s=v |
| 1144 |
end |
| 1145 |
end |
| 1146 |
result.start=s |
| 1147 |
end |
| 1148 |
if step>1 and result.start then |
| 1149 |
local v=V(tostring(step/2)) |
| 1150 |
result[tostring(step)]=v*v |
| 1151 |
end |
| 1152 |
if step>0 then |
| 1153 |
return nextstep(m,step/2,result) |
| 1154 |
else |
| 1155 |
return result |
| 1156 |
end |
| 1157 |
end |
| 1158 |
function lpeg.times(pattern,n) |
| 1159 |
return P(nextstep(n,2^16,{ "start",["1"]=pattern })) |
| 1160 |
end |
| 1161 |
do |
| 1162 |
local trailingzeros=zero^0*-digit |
| 1163 |
local stripper=Cs(( |
| 1164 |
digits*( |
| 1165 |
period*trailingzeros/""+period*(digit-trailingzeros)^1*(trailingzeros/"") |
| 1166 |
)+1 |
| 1167 |
)^0) |
| 1168 |
lpeg.patterns.stripzeros=stripper |
| 1169 |
local nonzero=digit-zero |
| 1170 |
local trailingzeros=zero^1*endofstring |
| 1171 |
local stripper=Cs((1-period)^0*( |
| 1172 |
period*trailingzeros/""+period*(nonzero^1+(trailingzeros/"")+zero^1)^0+endofstring |
| 1173 |
)) |
| 1174 |
lpeg.patterns.stripzero=stripper |
| 1175 |
end |
| 1176 |
local byte_to_HEX={} |
| 1177 |
local byte_to_hex={} |
| 1178 |
local byte_to_dec={} |
| 1179 |
local hex_to_byte={} |
| 1180 |
for i=0,255 do |
| 1181 |
local H=format("%02X",i) |
| 1182 |
local h=format("%02x",i) |
| 1183 |
local d=format("%03i",i) |
| 1184 |
local c=char(i) |
| 1185 |
byte_to_HEX[c]=H |
| 1186 |
byte_to_hex[c]=h |
| 1187 |
byte_to_dec[c]=d |
| 1188 |
hex_to_byte[h]=c |
| 1189 |
hex_to_byte[H]=c |
| 1190 |
end |
| 1191 |
local hextobyte=P(2)/hex_to_byte |
| 1192 |
local bytetoHEX=P(1)/byte_to_HEX |
| 1193 |
local bytetohex=P(1)/byte_to_hex |
| 1194 |
local bytetodec=P(1)/byte_to_dec |
| 1195 |
local hextobytes=Cs(hextobyte^0) |
| 1196 |
local bytestoHEX=Cs(bytetoHEX^0) |
| 1197 |
local bytestohex=Cs(bytetohex^0) |
| 1198 |
local bytestodec=Cs(bytetodec^0) |
| 1199 |
patterns.hextobyte=hextobyte |
| 1200 |
patterns.bytetoHEX=bytetoHEX |
| 1201 |
patterns.bytetohex=bytetohex |
| 1202 |
patterns.bytetodec=bytetodec |
| 1203 |
patterns.hextobytes=hextobytes |
| 1204 |
patterns.bytestoHEX=bytestoHEX |
| 1205 |
patterns.bytestohex=bytestohex |
| 1206 |
patterns.bytestodec=bytestodec |
| 1207 |
function string.toHEX(s) |
| 1208 |
if not s or s=="" then |
| 1209 |
return s |
| 1210 |
else |
| 1211 |
return lpegmatch(bytestoHEX,s) |
| 1212 |
end |
| 1213 |
end |
| 1214 |
function string.tohex(s) |
| 1215 |
if not s or s=="" then |
| 1216 |
return s |
| 1217 |
else |
| 1218 |
return lpegmatch(bytestohex,s) |
| 1219 |
end |
| 1220 |
end |
| 1221 |
function string.todec(s) |
| 1222 |
if not s or s=="" then |
| 1223 |
return s |
| 1224 |
else |
| 1225 |
return lpegmatch(bytestodec,s) |
| 1226 |
end |
| 1227 |
end |
| 1228 |
function string.tobytes(s) |
| 1229 |
if not s or s=="" then |
| 1230 |
return s |
| 1231 |
else |
| 1232 |
return lpegmatch(hextobytes,s) |
| 1233 |
end |
| 1234 |
end |
| 1235 |
local patterns={} |
| 1236 |
local function containsws(what) |
| 1237 |
local p=patterns[what] |
| 1238 |
if not p then |
| 1239 |
local p1=P(what)*(whitespace+endofstring)*Cc(true) |
| 1240 |
local p2=whitespace*P(p1) |
| 1241 |
p=P(p1)+P(1-p2)^0*p2+Cc(false) |
| 1242 |
patterns[what]=p |
| 1243 |
end |
| 1244 |
return p |
| 1245 |
end |
| 1246 |
lpeg.containsws=containsws |
| 1247 |
function string.containsws(str,what) |
| 1248 |
return lpegmatch(patterns[what] or containsws(what),str) |
| 1249 |
end |
| 1250 |
|
| 1251 |
end -- closure |
| 1252 |
|
| 1253 |
do -- begin closure to overcome local limits and interference |
| 1254 |
|
| 1255 |
if not modules then modules={} end modules ['l-functions']={ |
| 1256 |
version=1.001, |
| 1257 |
comment="companion to luat-lib.mkiv", |
| 1258 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 1259 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 1260 |
license="see context related readme files" |
| 1261 |
} |
| 1262 |
functions=functions or {} |
| 1263 |
function functions.dummy() end |
| 1264 |
|
| 1265 |
end -- closure |
| 1266 |
|
| 1267 |
do -- begin closure to overcome local limits and interference |
| 1268 |
|
| 1269 |
if not modules then modules={} end modules ['l-string']={ |
| 1270 |
version=1.001, |
| 1271 |
comment="companion to luat-lib.mkiv", |
| 1272 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 1273 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 1274 |
license="see context related readme files" |
| 1275 |
} |
| 1276 |
local string=string |
| 1277 |
local sub,gmatch,format,char,byte,rep,lower,find=string.sub,string.gmatch,string.format,string.char,string.byte,string.rep,string.lower,string.find |
| 1278 |
local lpegmatch,patterns=lpeg.match,lpeg.patterns |
| 1279 |
local P,S,C,Ct,Cc,Cs=lpeg.P,lpeg.S,lpeg.C,lpeg.Ct,lpeg.Cc,lpeg.Cs |
| 1280 |
local unquoted=patterns.squote*C(patterns.nosquote)*patterns.squote+patterns.dquote*C(patterns.nodquote)*patterns.dquote |
| 1281 |
function string.unquoted(str) |
| 1282 |
return lpegmatch(unquoted,str) or str |
| 1283 |
end |
| 1284 |
function string.quoted(str) |
| 1285 |
return format("%q",str) |
| 1286 |
end |
| 1287 |
function string.count(str,pattern) |
| 1288 |
local n=0 |
| 1289 |
local i=1 |
| 1290 |
local l=#pattern |
| 1291 |
while true do |
| 1292 |
i=find(str,pattern,i) |
| 1293 |
if i then |
| 1294 |
n=n+1 |
| 1295 |
i=i+l |
| 1296 |
else |
| 1297 |
break |
| 1298 |
end |
| 1299 |
end |
| 1300 |
return n |
| 1301 |
end |
| 1302 |
function string.limit(str,n,sentinel) |
| 1303 |
if #str>n then |
| 1304 |
sentinel=sentinel or "..." |
| 1305 |
return sub(str,1,(n-#sentinel))..sentinel |
| 1306 |
else |
| 1307 |
return str |
| 1308 |
end |
| 1309 |
end |
| 1310 |
local stripper=patterns.stripper |
| 1311 |
local fullstripper=patterns.fullstripper |
| 1312 |
local collapser=patterns.collapser |
| 1313 |
local nospacer=patterns.nospacer |
| 1314 |
local longtostring=patterns.longtostring |
| 1315 |
function string.strip(str) |
| 1316 |
return str and lpegmatch(stripper,str) or "" |
| 1317 |
end |
| 1318 |
function string.fullstrip(str) |
| 1319 |
return str and lpegmatch(fullstripper,str) or "" |
| 1320 |
end |
| 1321 |
function string.collapsespaces(str) |
| 1322 |
return str and lpegmatch(collapser,str) or "" |
| 1323 |
end |
| 1324 |
function string.nospaces(str) |
| 1325 |
return str and lpegmatch(nospacer,str) or "" |
| 1326 |
end |
| 1327 |
function string.longtostring(str) |
| 1328 |
return str and lpegmatch(longtostring,str) or "" |
| 1329 |
end |
| 1330 |
local pattern=P(" ")^0*P(-1) |
| 1331 |
function string.is_empty(str) |
| 1332 |
if not str or str=="" then |
| 1333 |
return true |
| 1334 |
else |
| 1335 |
return lpegmatch(pattern,str) and true or false |
| 1336 |
end |
| 1337 |
end |
| 1338 |
local anything=patterns.anything |
| 1339 |
local moreescapes=Cc("%")*S(".-+%?()[]*$^{}") |
| 1340 |
local allescapes=Cc("%")*S(".-+%?()[]*") |
| 1341 |
local someescapes=Cc("%")*S(".-+%()[]") |
| 1342 |
local matchescapes=Cc(".")*S("*?") |
| 1343 |
local pattern_m=Cs ((moreescapes+anything )^0 ) |
| 1344 |
local pattern_a=Cs ((allescapes+anything )^0 ) |
| 1345 |
local pattern_b=Cs ((someescapes+matchescapes+anything )^0 ) |
| 1346 |
local pattern_c=Cs (Cc("^")*(someescapes+matchescapes+anything )^0*Cc("$") ) |
| 1347 |
function string.escapedpattern(str,simple) |
| 1348 |
return lpegmatch(simple and pattern_b or pattern_a,str) |
| 1349 |
end |
| 1350 |
function string.topattern(str,lowercase,strict) |
| 1351 |
if str=="" or type(str)~="string" then |
| 1352 |
return ".*" |
| 1353 |
elseif strict=="all" then |
| 1354 |
str=lpegmatch(pattern_m,str) |
| 1355 |
elseif strict then |
| 1356 |
str=lpegmatch(pattern_c,str) |
| 1357 |
else |
| 1358 |
str=lpegmatch(pattern_b,str) |
| 1359 |
end |
| 1360 |
if lowercase then |
| 1361 |
return lower(str) |
| 1362 |
else |
| 1363 |
return str |
| 1364 |
end |
| 1365 |
end |
| 1366 |
function string.valid(str,default) |
| 1367 |
return (type(str)=="string" and str~="" and str) or default or nil |
| 1368 |
end |
| 1369 |
string.itself=function(s) return s end |
| 1370 |
local pattern_c=Ct(C(1)^0) |
| 1371 |
local pattern_b=Ct((C(1)/byte)^0) |
| 1372 |
function string.totable(str,bytes) |
| 1373 |
return lpegmatch(bytes and pattern_b or pattern_c,str) |
| 1374 |
end |
| 1375 |
local replacer=lpeg.replacer("@","%%") |
| 1376 |
function string.tformat(fmt,...) |
| 1377 |
return format(lpegmatch(replacer,fmt),...) |
| 1378 |
end |
| 1379 |
string.quote=string.quoted |
| 1380 |
string.unquote=string.unquoted |
| 1381 |
if not string.bytetable then |
| 1382 |
local limit=5000 |
| 1383 |
function string.bytetable(str) |
| 1384 |
local n=#str |
| 1385 |
if n>limit then |
| 1386 |
local t={ byte(str,1,limit) } |
| 1387 |
for i=limit+1,n do |
| 1388 |
t[i]=byte(str,i) |
| 1389 |
end |
| 1390 |
return t |
| 1391 |
else |
| 1392 |
return { byte(str,1,n) } |
| 1393 |
end |
| 1394 |
end |
| 1395 |
end |
| 1396 |
|
| 1397 |
end -- closure |
| 1398 |
|
| 1399 |
do -- begin closure to overcome local limits and interference |
| 1400 |
|
| 1401 |
if not modules then modules={} end modules ['l-table']={ |
| 1402 |
version=1.001, |
| 1403 |
comment="companion to luat-lib.mkiv", |
| 1404 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 1405 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 1406 |
license="see context related readme files" |
| 1407 |
} |
| 1408 |
local type,next,tostring,tonumber,select,rawget=type,next,tostring,tonumber,select,rawget |
| 1409 |
local table,string=table,string |
| 1410 |
local concat,sort=table.concat,table.sort |
| 1411 |
local format,lower,dump=string.format,string.lower,string.dump |
| 1412 |
local getmetatable,setmetatable=getmetatable,setmetatable |
| 1413 |
local lpegmatch,patterns=lpeg.match,lpeg.patterns |
| 1414 |
local floor=math.floor |
| 1415 |
local stripper=patterns.stripper |
| 1416 |
function table.getn(t) |
| 1417 |
return t and #t |
| 1418 |
end |
| 1419 |
function table.strip(tab) |
| 1420 |
local lst={} |
| 1421 |
local l=0 |
| 1422 |
for i=1,#tab do |
| 1423 |
local s=lpegmatch(stripper,tab[i]) or "" |
| 1424 |
if s=="" then |
| 1425 |
else |
| 1426 |
l=l+1 |
| 1427 |
lst[l]=s |
| 1428 |
end |
| 1429 |
end |
| 1430 |
return lst |
| 1431 |
end |
| 1432 |
function table.keys(t) |
| 1433 |
if t then |
| 1434 |
local keys={} |
| 1435 |
local k=0 |
| 1436 |
for key in next,t do |
| 1437 |
k=k+1 |
| 1438 |
keys[k]=key |
| 1439 |
end |
| 1440 |
return keys |
| 1441 |
else |
| 1442 |
return {} |
| 1443 |
end |
| 1444 |
end |
| 1445 |
local function compare(a,b) |
| 1446 |
local ta=type(a) |
| 1447 |
if ta=="number" then |
| 1448 |
local tb=type(b) |
| 1449 |
if ta==tb then |
| 1450 |
return a<b |
| 1451 |
elseif tb=="string" then |
| 1452 |
return tostring(a)<b |
| 1453 |
end |
| 1454 |
elseif ta=="string" then |
| 1455 |
local tb=type(b) |
| 1456 |
if ta==tb then |
| 1457 |
return a<b |
| 1458 |
else |
| 1459 |
return a<tostring(b) |
| 1460 |
end |
| 1461 |
end |
| 1462 |
return tostring(a)<tostring(b) |
| 1463 |
end |
| 1464 |
local function sortedkeys(tab) |
| 1465 |
if tab then |
| 1466 |
local srt={} |
| 1467 |
local category=0 |
| 1468 |
local s=0 |
| 1469 |
for key in next,tab do |
| 1470 |
s=s+1 |
| 1471 |
srt[s]=key |
| 1472 |
if category~=3 then |
| 1473 |
local tkey=type(key) |
| 1474 |
if category==1 then |
| 1475 |
if tkey~="string" then |
| 1476 |
category=3 |
| 1477 |
end |
| 1478 |
elseif category==2 then |
| 1479 |
if tkey~="number" then |
| 1480 |
category=3 |
| 1481 |
end |
| 1482 |
else |
| 1483 |
if tkey=="string" then |
| 1484 |
category=1 |
| 1485 |
elseif tkey=="number" then |
| 1486 |
category=2 |
| 1487 |
else |
| 1488 |
category=3 |
| 1489 |
end |
| 1490 |
end |
| 1491 |
end |
| 1492 |
end |
| 1493 |
if s<2 then |
| 1494 |
elseif category==3 then |
| 1495 |
sort(srt,compare) |
| 1496 |
else |
| 1497 |
sort(srt) |
| 1498 |
end |
| 1499 |
return srt |
| 1500 |
else |
| 1501 |
return {} |
| 1502 |
end |
| 1503 |
end |
| 1504 |
local function sortedhashonly(tab) |
| 1505 |
if tab then |
| 1506 |
local srt={} |
| 1507 |
local s=0 |
| 1508 |
for key in next,tab do |
| 1509 |
if type(key)=="string" then |
| 1510 |
s=s+1 |
| 1511 |
srt[s]=key |
| 1512 |
end |
| 1513 |
end |
| 1514 |
if s>1 then |
| 1515 |
sort(srt) |
| 1516 |
end |
| 1517 |
return srt |
| 1518 |
else |
| 1519 |
return {} |
| 1520 |
end |
| 1521 |
end |
| 1522 |
local function sortedindexonly(tab) |
| 1523 |
if tab then |
| 1524 |
local srt={} |
| 1525 |
local s=0 |
| 1526 |
for key in next,tab do |
| 1527 |
if type(key)=="number" then |
| 1528 |
s=s+1 |
| 1529 |
srt[s]=key |
| 1530 |
end |
| 1531 |
end |
| 1532 |
if s>1 then |
| 1533 |
sort(srt) |
| 1534 |
end |
| 1535 |
return srt |
| 1536 |
else |
| 1537 |
return {} |
| 1538 |
end |
| 1539 |
end |
| 1540 |
local function sortedhashkeys(tab,cmp) |
| 1541 |
if tab then |
| 1542 |
local srt={} |
| 1543 |
local s=0 |
| 1544 |
for key in next,tab do |
| 1545 |
if key then |
| 1546 |
s=s+1 |
| 1547 |
srt[s]=key |
| 1548 |
end |
| 1549 |
end |
| 1550 |
if s>1 then |
| 1551 |
sort(srt,cmp) |
| 1552 |
end |
| 1553 |
return srt |
| 1554 |
else |
| 1555 |
return {} |
| 1556 |
end |
| 1557 |
end |
| 1558 |
function table.allkeys(t) |
| 1559 |
local keys={} |
| 1560 |
for k,v in next,t do |
| 1561 |
for k in next,v do |
| 1562 |
keys[k]=true |
| 1563 |
end |
| 1564 |
end |
| 1565 |
return sortedkeys(keys) |
| 1566 |
end |
| 1567 |
table.sortedkeys=sortedkeys |
| 1568 |
table.sortedhashonly=sortedhashonly |
| 1569 |
table.sortedindexonly=sortedindexonly |
| 1570 |
table.sortedhashkeys=sortedhashkeys |
| 1571 |
local function nothing() end |
| 1572 |
local function sortedhash(t,cmp) |
| 1573 |
if t then |
| 1574 |
local s |
| 1575 |
if cmp then |
| 1576 |
s=sortedhashkeys(t,function(a,b) return cmp(t,a,b) end) |
| 1577 |
else |
| 1578 |
s=sortedkeys(t) |
| 1579 |
end |
| 1580 |
local m=#s |
| 1581 |
if m==1 then |
| 1582 |
return next,t |
| 1583 |
elseif m>0 then |
| 1584 |
local n=0 |
| 1585 |
return function() |
| 1586 |
if n<m then |
| 1587 |
n=n+1 |
| 1588 |
local k=s[n] |
| 1589 |
return k,t[k] |
| 1590 |
end |
| 1591 |
end |
| 1592 |
end |
| 1593 |
end |
| 1594 |
return nothing |
| 1595 |
end |
| 1596 |
table.sortedhash=sortedhash |
| 1597 |
table.sortedpairs=sortedhash |
| 1598 |
function table.append(t,list) |
| 1599 |
local n=#t |
| 1600 |
for i=1,#list do |
| 1601 |
n=n+1 |
| 1602 |
t[n]=list[i] |
| 1603 |
end |
| 1604 |
return t |
| 1605 |
end |
| 1606 |
function table.prepend(t,list) |
| 1607 |
local nl=#list |
| 1608 |
local nt=nl+#t |
| 1609 |
for i=#t,1,-1 do |
| 1610 |
t[nt]=t[i] |
| 1611 |
nt=nt-1 |
| 1612 |
end |
| 1613 |
for i=1,#list do |
| 1614 |
t[i]=list[i] |
| 1615 |
end |
| 1616 |
return t |
| 1617 |
end |
| 1618 |
function table.merge(t,...) |
| 1619 |
if not t then |
| 1620 |
t={} |
| 1621 |
end |
| 1622 |
for i=1,select("#",...) do |
| 1623 |
for k,v in next,(select(i,...)) do |
| 1624 |
t[k]=v |
| 1625 |
end |
| 1626 |
end |
| 1627 |
return t |
| 1628 |
end |
| 1629 |
function table.merged(...) |
| 1630 |
local t={} |
| 1631 |
for i=1,select("#",...) do |
| 1632 |
for k,v in next,(select(i,...)) do |
| 1633 |
t[k]=v |
| 1634 |
end |
| 1635 |
end |
| 1636 |
return t |
| 1637 |
end |
| 1638 |
function table.imerge(t,...) |
| 1639 |
local nt=#t |
| 1640 |
for i=1,select("#",...) do |
| 1641 |
local nst=select(i,...) |
| 1642 |
for j=1,#nst do |
| 1643 |
nt=nt+1 |
| 1644 |
t[nt]=nst[j] |
| 1645 |
end |
| 1646 |
end |
| 1647 |
return t |
| 1648 |
end |
| 1649 |
function table.imerged(...) |
| 1650 |
local tmp={} |
| 1651 |
local ntmp=0 |
| 1652 |
for i=1,select("#",...) do |
| 1653 |
local nst=select(i,...) |
| 1654 |
for j=1,#nst do |
| 1655 |
ntmp=ntmp+1 |
| 1656 |
tmp[ntmp]=nst[j] |
| 1657 |
end |
| 1658 |
end |
| 1659 |
return tmp |
| 1660 |
end |
| 1661 |
local function fastcopy(old,metatabletoo) |
| 1662 |
if old then |
| 1663 |
local new={} |
| 1664 |
for k,v in next,old do |
| 1665 |
if type(v)=="table" then |
| 1666 |
new[k]=fastcopy(v,metatabletoo) |
| 1667 |
else |
| 1668 |
new[k]=v |
| 1669 |
end |
| 1670 |
end |
| 1671 |
if metatabletoo then |
| 1672 |
local mt=getmetatable(old) |
| 1673 |
if mt then |
| 1674 |
setmetatable(new,mt) |
| 1675 |
end |
| 1676 |
end |
| 1677 |
return new |
| 1678 |
else |
| 1679 |
return {} |
| 1680 |
end |
| 1681 |
end |
| 1682 |
local function copy(t,tables) |
| 1683 |
if not tables then |
| 1684 |
tables={} |
| 1685 |
end |
| 1686 |
local tcopy={} |
| 1687 |
if not tables[t] then |
| 1688 |
tables[t]=tcopy |
| 1689 |
end |
| 1690 |
for i,v in next,t do |
| 1691 |
if type(i)=="table" then |
| 1692 |
if tables[i] then |
| 1693 |
i=tables[i] |
| 1694 |
else |
| 1695 |
i=copy(i,tables) |
| 1696 |
end |
| 1697 |
end |
| 1698 |
if type(v)~="table" then |
| 1699 |
tcopy[i]=v |
| 1700 |
elseif tables[v] then |
| 1701 |
tcopy[i]=tables[v] |
| 1702 |
else |
| 1703 |
tcopy[i]=copy(v,tables) |
| 1704 |
end |
| 1705 |
end |
| 1706 |
local mt=getmetatable(t) |
| 1707 |
if mt then |
| 1708 |
setmetatable(tcopy,mt) |
| 1709 |
end |
| 1710 |
return tcopy |
| 1711 |
end |
| 1712 |
table.fastcopy=fastcopy |
| 1713 |
table.copy=copy |
| 1714 |
function table.derive(parent) |
| 1715 |
local child={} |
| 1716 |
if parent then |
| 1717 |
setmetatable(child,{ __index=parent }) |
| 1718 |
end |
| 1719 |
return child |
| 1720 |
end |
| 1721 |
function table.tohash(t,value) |
| 1722 |
local h={} |
| 1723 |
if t then |
| 1724 |
if value==nil then value=true end |
| 1725 |
for _,v in next,t do |
| 1726 |
h[v]=value |
| 1727 |
end |
| 1728 |
end |
| 1729 |
return h |
| 1730 |
end |
| 1731 |
function table.fromhash(t) |
| 1732 |
local hsh={} |
| 1733 |
local h=0 |
| 1734 |
for k,v in next,t do |
| 1735 |
if v then |
| 1736 |
h=h+1 |
| 1737 |
hsh[h]=k |
| 1738 |
end |
| 1739 |
end |
| 1740 |
return hsh |
| 1741 |
end |
| 1742 |
local noquotes,hexify,handle,compact,inline,functions,metacheck,accurate |
| 1743 |
local reserved=table.tohash { |
| 1744 |
'and','break','do','else','elseif','end','false','for','function','if', |
| 1745 |
'in','local','nil','not','or','repeat','return','then','true','until','while', |
| 1746 |
'NaN','goto','const', |
| 1747 |
} |
| 1748 |
local function is_simple_table(t,hexify,accurate) |
| 1749 |
local nt=#t |
| 1750 |
if nt>0 then |
| 1751 |
local n=0 |
| 1752 |
for _,v in next,t do |
| 1753 |
n=n+1 |
| 1754 |
if type(v)=="table" then |
| 1755 |
return nil |
| 1756 |
end |
| 1757 |
end |
| 1758 |
local haszero=rawget(t,0) |
| 1759 |
if n==nt then |
| 1760 |
local tt={} |
| 1761 |
for i=1,nt do |
| 1762 |
local v=t[i] |
| 1763 |
local tv=type(v) |
| 1764 |
if tv=="number" then |
| 1765 |
if hexify then |
| 1766 |
tt[i]=format("0x%X",v) |
| 1767 |
elseif accurate then |
| 1768 |
tt[i]=format("%q",v) |
| 1769 |
else |
| 1770 |
tt[i]=v |
| 1771 |
end |
| 1772 |
elseif tv=="string" then |
| 1773 |
tt[i]=format("%q",v) |
| 1774 |
elseif tv=="boolean" then |
| 1775 |
tt[i]=v and "true" or "false" |
| 1776 |
else |
| 1777 |
return nil |
| 1778 |
end |
| 1779 |
end |
| 1780 |
return tt |
| 1781 |
elseif haszero and (n==nt+1) then |
| 1782 |
local tt={} |
| 1783 |
for i=0,nt do |
| 1784 |
local v=t[i] |
| 1785 |
local tv=type(v) |
| 1786 |
if tv=="number" then |
| 1787 |
if hexify then |
| 1788 |
tt[i+1]=format("0x%X",v) |
| 1789 |
elseif accurate then |
| 1790 |
tt[i+1]=format("%q",v) |
| 1791 |
else |
| 1792 |
tt[i+1]=v |
| 1793 |
end |
| 1794 |
elseif tv=="string" then |
| 1795 |
tt[i+1]=format("%q",v) |
| 1796 |
elseif tv=="boolean" then |
| 1797 |
tt[i+1]=v and "true" or "false" |
| 1798 |
else |
| 1799 |
return nil |
| 1800 |
end |
| 1801 |
end |
| 1802 |
tt[1]="[0] = "..tt[1] |
| 1803 |
return tt |
| 1804 |
end |
| 1805 |
end |
| 1806 |
return nil |
| 1807 |
end |
| 1808 |
table.is_simple_table=is_simple_table |
| 1809 |
local propername=patterns.propername |
| 1810 |
local function dummy() end |
| 1811 |
local function do_serialize(root,name,depth,level,indexed) |
| 1812 |
if level>0 then |
| 1813 |
depth=depth.." " |
| 1814 |
if indexed then |
| 1815 |
handle(format("%s{",depth)) |
| 1816 |
else |
| 1817 |
local tn=type(name) |
| 1818 |
if tn=="number" then |
| 1819 |
if hexify then |
| 1820 |
handle(format("%s[0x%X]={",depth,name)) |
| 1821 |
else |
| 1822 |
handle(format("%s[%s]={",depth,name)) |
| 1823 |
end |
| 1824 |
elseif tn=="string" then |
| 1825 |
if noquotes and not reserved[name] and lpegmatch(propername,name) then |
| 1826 |
handle(format("%s%s={",depth,name)) |
| 1827 |
else |
| 1828 |
handle(format("%s[%q]={",depth,name)) |
| 1829 |
end |
| 1830 |
elseif tn=="boolean" then |
| 1831 |
handle(format("%s[%s]={",depth,name and "true" or "false")) |
| 1832 |
else |
| 1833 |
handle(format("%s{",depth)) |
| 1834 |
end |
| 1835 |
end |
| 1836 |
end |
| 1837 |
if root and next(root)~=nil then |
| 1838 |
local first=nil |
| 1839 |
local last=0 |
| 1840 |
if compact then |
| 1841 |
last=#root |
| 1842 |
for k=1,last do |
| 1843 |
if rawget(root,k)==nil then |
| 1844 |
last=k-1 |
| 1845 |
break |
| 1846 |
end |
| 1847 |
end |
| 1848 |
if last>0 then |
| 1849 |
first=1 |
| 1850 |
end |
| 1851 |
end |
| 1852 |
local sk=sortedkeys(root) |
| 1853 |
for i=1,#sk do |
| 1854 |
local k=sk[i] |
| 1855 |
local v=root[k] |
| 1856 |
local tv=type(v) |
| 1857 |
local tk=type(k) |
| 1858 |
if compact and first and tk=="number" and k>=first and k<=last then |
| 1859 |
if tv=="number" then |
| 1860 |
if hexify then |
| 1861 |
handle(format("%s 0x%X,",depth,v)) |
| 1862 |
elseif accurate then |
| 1863 |
handle(format("%s %q,",depth,v)) |
| 1864 |
else |
| 1865 |
handle(format("%s %s,",depth,v)) |
| 1866 |
end |
| 1867 |
elseif tv=="string" then |
| 1868 |
handle(format("%s %q,",depth,v)) |
| 1869 |
elseif tv=="table" then |
| 1870 |
if next(v)==nil then |
| 1871 |
handle(format("%s {},",depth)) |
| 1872 |
elseif inline then |
| 1873 |
local st=is_simple_table(v,hexify,accurate) |
| 1874 |
if st then |
| 1875 |
handle(format("%s { %s },",depth,concat(st,", "))) |
| 1876 |
else |
| 1877 |
do_serialize(v,k,depth,level+1,true) |
| 1878 |
end |
| 1879 |
else |
| 1880 |
do_serialize(v,k,depth,level+1,true) |
| 1881 |
end |
| 1882 |
elseif tv=="boolean" then |
| 1883 |
handle(format("%s %s,",depth,v and "true" or "false")) |
| 1884 |
elseif tv=="function" then |
| 1885 |
if functions then |
| 1886 |
handle(format('%s load(%q),',depth,dump(v))) |
| 1887 |
else |
| 1888 |
handle(format('%s "function",',depth)) |
| 1889 |
end |
| 1890 |
else |
| 1891 |
handle(format("%s %q,",depth,tostring(v))) |
| 1892 |
end |
| 1893 |
elseif k=="__p__" then |
| 1894 |
if false then |
| 1895 |
handle(format("%s __p__=nil,",depth)) |
| 1896 |
end |
| 1897 |
elseif tv=="number" then |
| 1898 |
if tk=="number" then |
| 1899 |
if hexify then |
| 1900 |
handle(format("%s [0x%X]=0x%X,",depth,k,v)) |
| 1901 |
elseif accurate then |
| 1902 |
handle(format("%s [%s]=%q,",depth,k,v)) |
| 1903 |
else |
| 1904 |
handle(format("%s [%s]=%s,",depth,k,v)) |
| 1905 |
end |
| 1906 |
elseif tk=="boolean" then |
| 1907 |
if hexify then |
| 1908 |
handle(format("%s [%s]=0x%X,",depth,k and "true" or "false",v)) |
| 1909 |
elseif accurate then |
| 1910 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",v)) |
| 1911 |
else |
| 1912 |
handle(format("%s [%s]=%s,",depth,k and "true" or "false",v)) |
| 1913 |
end |
| 1914 |
elseif tk~="string" then |
| 1915 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 1916 |
if hexify then |
| 1917 |
handle(format("%s %s=0x%X,",depth,k,v)) |
| 1918 |
elseif accurate then |
| 1919 |
handle(format("%s %s=%q,",depth,k,v)) |
| 1920 |
else |
| 1921 |
handle(format("%s %s=%s,",depth,k,v)) |
| 1922 |
end |
| 1923 |
else |
| 1924 |
if hexify then |
| 1925 |
handle(format("%s [%q]=0x%X,",depth,k,v)) |
| 1926 |
elseif accurate then |
| 1927 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 1928 |
else |
| 1929 |
handle(format("%s [%q]=%s,",depth,k,v)) |
| 1930 |
end |
| 1931 |
end |
| 1932 |
elseif tv=="string" then |
| 1933 |
if tk=="number" then |
| 1934 |
if hexify then |
| 1935 |
handle(format("%s [0x%X]=%q,",depth,k,v)) |
| 1936 |
elseif accurate then |
| 1937 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 1938 |
else |
| 1939 |
handle(format("%s [%s]=%q,",depth,k,v)) |
| 1940 |
end |
| 1941 |
elseif tk=="boolean" then |
| 1942 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",v)) |
| 1943 |
elseif tk~="string" then |
| 1944 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 1945 |
handle(format("%s %s=%q,",depth,k,v)) |
| 1946 |
else |
| 1947 |
handle(format("%s [%q]=%q,",depth,k,v)) |
| 1948 |
end |
| 1949 |
elseif tv=="table" then |
| 1950 |
if next(v)==nil then |
| 1951 |
if tk=="number" then |
| 1952 |
if hexify then |
| 1953 |
handle(format("%s [0x%X]={},",depth,k)) |
| 1954 |
elseif accurate then |
| 1955 |
handle(format("%s [%q]={},",depth,k)) |
| 1956 |
else |
| 1957 |
handle(format("%s [%s]={},",depth,k)) |
| 1958 |
end |
| 1959 |
elseif tk=="boolean" then |
| 1960 |
handle(format("%s [%s]={},",depth,k and "true" or "false")) |
| 1961 |
elseif tk~="string" then |
| 1962 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 1963 |
handle(format("%s %s={},",depth,k)) |
| 1964 |
else |
| 1965 |
handle(format("%s [%q]={},",depth,k)) |
| 1966 |
end |
| 1967 |
elseif inline then |
| 1968 |
local st=is_simple_table(v,hexify,accurate) |
| 1969 |
if st then |
| 1970 |
if tk=="number" then |
| 1971 |
if hexify then |
| 1972 |
handle(format("%s [0x%X]={ %s },",depth,k,concat(st,", "))) |
| 1973 |
elseif accurate then |
| 1974 |
handle(format("%s [%q]={ %s },",depth,k,concat(st,", "))) |
| 1975 |
else |
| 1976 |
handle(format("%s [%s]={ %s },",depth,k,concat(st,", "))) |
| 1977 |
end |
| 1978 |
elseif tk=="boolean" then |
| 1979 |
handle(format("%s [%s]={ %s },",depth,k and "true" or "false",concat(st,", "))) |
| 1980 |
elseif tk~="string" then |
| 1981 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 1982 |
handle(format("%s %s={ %s },",depth,k,concat(st,", "))) |
| 1983 |
else |
| 1984 |
handle(format("%s [%q]={ %s },",depth,k,concat(st,", "))) |
| 1985 |
end |
| 1986 |
else |
| 1987 |
do_serialize(v,k,depth,level+1) |
| 1988 |
end |
| 1989 |
else |
| 1990 |
do_serialize(v,k,depth,level+1) |
| 1991 |
end |
| 1992 |
elseif tv=="boolean" then |
| 1993 |
if tk=="number" then |
| 1994 |
if hexify then |
| 1995 |
handle(format("%s [0x%X]=%s,",depth,k,v and "true" or "false")) |
| 1996 |
elseif accurate then |
| 1997 |
handle(format("%s [%q]=%s,",depth,k,v and "true" or "false")) |
| 1998 |
else |
| 1999 |
handle(format("%s [%s]=%s,",depth,k,v and "true" or "false")) |
| 2000 |
end |
| 2001 |
elseif tk=="boolean" then |
| 2002 |
handle(format("%s [%s]=%s,",depth,tostring(k),v and "true" or "false")) |
| 2003 |
elseif tk~="string" then |
| 2004 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2005 |
handle(format("%s %s=%s,",depth,k,v and "true" or "false")) |
| 2006 |
else |
| 2007 |
handle(format("%s [%q]=%s,",depth,k,v and "true" or "false")) |
| 2008 |
end |
| 2009 |
elseif tv=="function" then |
| 2010 |
if functions then |
| 2011 |
local getinfo=debug and debug.getinfo |
| 2012 |
if getinfo then |
| 2013 |
local f=getinfo(v).what=="C" and dump(dummy) or dump(v) |
| 2014 |
if tk=="number" then |
| 2015 |
if hexify then |
| 2016 |
handle(format("%s [0x%X]=load(%q),",depth,k,f)) |
| 2017 |
elseif accurate then |
| 2018 |
handle(format("%s [%q]=load(%q),",depth,k,f)) |
| 2019 |
else |
| 2020 |
handle(format("%s [%s]=load(%q),",depth,k,f)) |
| 2021 |
end |
| 2022 |
elseif tk=="boolean" then |
| 2023 |
handle(format("%s [%s]=load(%q),",depth,k and "true" or "false",f)) |
| 2024 |
elseif tk~="string" then |
| 2025 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2026 |
handle(format("%s %s=load(%q),",depth,k,f)) |
| 2027 |
else |
| 2028 |
handle(format("%s [%q]=load(%q),",depth,k,f)) |
| 2029 |
end |
| 2030 |
end |
| 2031 |
end |
| 2032 |
else |
| 2033 |
if tk=="number" then |
| 2034 |
if hexify then |
| 2035 |
handle(format("%s [0x%X]=%q,",depth,k,tostring(v))) |
| 2036 |
elseif accurate then |
| 2037 |
handle(format("%s [%q]=%q,",depth,k,tostring(v))) |
| 2038 |
else |
| 2039 |
handle(format("%s [%s]=%q,",depth,k,tostring(v))) |
| 2040 |
end |
| 2041 |
elseif tk=="boolean" then |
| 2042 |
handle(format("%s [%s]=%q,",depth,k and "true" or "false",tostring(v))) |
| 2043 |
elseif tk~="string" then |
| 2044 |
elseif noquotes and not reserved[k] and lpegmatch(propername,k) then |
| 2045 |
handle(format("%s %s=%q,",depth,k,tostring(v))) |
| 2046 |
else |
| 2047 |
handle(format("%s [%q]=%q,",depth,k,tostring(v))) |
| 2048 |
end |
| 2049 |
end |
| 2050 |
end |
| 2051 |
end |
| 2052 |
if level>0 then |
| 2053 |
handle(format("%s},",depth)) |
| 2054 |
end |
| 2055 |
end |
| 2056 |
local function serialize(_handle,root,name,specification) |
| 2057 |
local tname=type(name) |
| 2058 |
if type(specification)=="table" then |
| 2059 |
noquotes=specification.noquotes |
| 2060 |
hexify=specification.hexify |
| 2061 |
accurate=specification.accurate |
| 2062 |
handle=_handle or specification.handle or print |
| 2063 |
functions=specification.functions |
| 2064 |
compact=specification.compact |
| 2065 |
inline=specification.inline and compact |
| 2066 |
metacheck=specification.metacheck |
| 2067 |
if functions==nil then |
| 2068 |
functions=true |
| 2069 |
end |
| 2070 |
if compact==nil then |
| 2071 |
compact=true |
| 2072 |
end |
| 2073 |
if inline==nil then |
| 2074 |
inline=compact |
| 2075 |
end |
| 2076 |
if metacheck==nil then |
| 2077 |
metacheck=true |
| 2078 |
end |
| 2079 |
else |
| 2080 |
noquotes=false |
| 2081 |
hexify=false |
| 2082 |
handle=_handle or print |
| 2083 |
compact=true |
| 2084 |
inline=true |
| 2085 |
functions=true |
| 2086 |
metacheck=true |
| 2087 |
end |
| 2088 |
if tname=="string" then |
| 2089 |
if name=="return" then |
| 2090 |
handle("return {") |
| 2091 |
else |
| 2092 |
handle(name.."={") |
| 2093 |
end |
| 2094 |
elseif tname=="number" then |
| 2095 |
if hexify then |
| 2096 |
handle(format("[0x%X]={",name)) |
| 2097 |
else |
| 2098 |
handle("["..name.."]={") |
| 2099 |
end |
| 2100 |
elseif tname=="boolean" then |
| 2101 |
if name then |
| 2102 |
handle("return {") |
| 2103 |
else |
| 2104 |
handle("{") |
| 2105 |
end |
| 2106 |
else |
| 2107 |
handle("t={") |
| 2108 |
end |
| 2109 |
if root then |
| 2110 |
if metacheck and getmetatable(root) then |
| 2111 |
local dummy=root._w_h_a_t_e_v_e_r_ |
| 2112 |
root._w_h_a_t_e_v_e_r_=nil |
| 2113 |
end |
| 2114 |
if next(root)~=nil then |
| 2115 |
do_serialize(root,name,"",0) |
| 2116 |
end |
| 2117 |
end |
| 2118 |
handle("}") |
| 2119 |
end |
| 2120 |
function table.serialize(root,name,specification) |
| 2121 |
local t={} |
| 2122 |
local n=0 |
| 2123 |
local function flush(s) |
| 2124 |
n=n+1 |
| 2125 |
t[n]=s |
| 2126 |
end |
| 2127 |
serialize(flush,root,name,specification) |
| 2128 |
return concat(t,"\n") |
| 2129 |
end |
| 2130 |
table.tohandle=serialize |
| 2131 |
local maxtab=2*1024 |
| 2132 |
function table.tofile(filename,root,name,specification) |
| 2133 |
local f=io.open(filename,'w') |
| 2134 |
if f then |
| 2135 |
if maxtab>1 then |
| 2136 |
local t={} |
| 2137 |
local n=0 |
| 2138 |
local function flush(s) |
| 2139 |
n=n+1 |
| 2140 |
t[n]=s |
| 2141 |
if n>maxtab then |
| 2142 |
f:write(concat(t,"\n"),"\n") |
| 2143 |
t={} |
| 2144 |
n=0 |
| 2145 |
end |
| 2146 |
end |
| 2147 |
serialize(flush,root,name,specification) |
| 2148 |
f:write(concat(t,"\n"),"\n") |
| 2149 |
else |
| 2150 |
local function flush(s) |
| 2151 |
f:write(s,"\n") |
| 2152 |
end |
| 2153 |
serialize(flush,root,name,specification) |
| 2154 |
end |
| 2155 |
f:close() |
| 2156 |
io.flush() |
| 2157 |
end |
| 2158 |
end |
| 2159 |
local function flattened(t,f,depth) |
| 2160 |
if f==nil then |
| 2161 |
f={} |
| 2162 |
depth=0xFFFF |
| 2163 |
elseif tonumber(f) then |
| 2164 |
depth=f |
| 2165 |
f={} |
| 2166 |
elseif not depth then |
| 2167 |
depth=0xFFFF |
| 2168 |
end |
| 2169 |
for k,v in next,t do |
| 2170 |
if type(k)~="number" then |
| 2171 |
if depth>0 and type(v)=="table" then |
| 2172 |
flattened(v,f,depth-1) |
| 2173 |
else |
| 2174 |
f[#f+1]=v |
| 2175 |
end |
| 2176 |
end |
| 2177 |
end |
| 2178 |
for k=1,#t do |
| 2179 |
local v=t[k] |
| 2180 |
if depth>0 and type(v)=="table" then |
| 2181 |
flattened(v,f,depth-1) |
| 2182 |
else |
| 2183 |
f[#f+1]=v |
| 2184 |
end |
| 2185 |
end |
| 2186 |
return f |
| 2187 |
end |
| 2188 |
table.flattened=flattened |
| 2189 |
local function collapsed(t,f,h) |
| 2190 |
if f==nil then |
| 2191 |
f={} |
| 2192 |
h={} |
| 2193 |
end |
| 2194 |
for k=1,#t do |
| 2195 |
local v=t[k] |
| 2196 |
if type(v)=="table" then |
| 2197 |
collapsed(v,f,h) |
| 2198 |
elseif not h[v] then |
| 2199 |
f[#f+1]=v |
| 2200 |
h[v]=true |
| 2201 |
end |
| 2202 |
end |
| 2203 |
return f |
| 2204 |
end |
| 2205 |
local function collapsedhash(t,h) |
| 2206 |
if h==nil then |
| 2207 |
h={} |
| 2208 |
end |
| 2209 |
for k=1,#t do |
| 2210 |
local v=t[k] |
| 2211 |
if type(v)=="table" then |
| 2212 |
collapsedhash(v,h) |
| 2213 |
else |
| 2214 |
h[v]=true |
| 2215 |
end |
| 2216 |
end |
| 2217 |
return h |
| 2218 |
end |
| 2219 |
table.collapsed=collapsed |
| 2220 |
table.collapsedhash=collapsedhash |
| 2221 |
local function unnest(t,f) |
| 2222 |
if not f then |
| 2223 |
f={} |
| 2224 |
end |
| 2225 |
for i=1,#t do |
| 2226 |
local v=t[i] |
| 2227 |
if type(v)=="table" then |
| 2228 |
if type(v[1])=="table" then |
| 2229 |
unnest(v,f) |
| 2230 |
else |
| 2231 |
f[#f+1]=v |
| 2232 |
end |
| 2233 |
else |
| 2234 |
f[#f+1]=v |
| 2235 |
end |
| 2236 |
end |
| 2237 |
return f |
| 2238 |
end |
| 2239 |
function table.unnest(t) |
| 2240 |
return unnest(t) |
| 2241 |
end |
| 2242 |
local function are_equal(a,b,n,m) |
| 2243 |
if a==b then |
| 2244 |
return true |
| 2245 |
elseif a and b and #a==#b then |
| 2246 |
if not n then |
| 2247 |
n=1 |
| 2248 |
end |
| 2249 |
if not m then |
| 2250 |
m=#a |
| 2251 |
end |
| 2252 |
for i=n,m do |
| 2253 |
local ai,bi=a[i],b[i] |
| 2254 |
if ai==bi then |
| 2255 |
elseif type(ai)=="table" and type(bi)=="table" then |
| 2256 |
if not are_equal(ai,bi) then |
| 2257 |
return false |
| 2258 |
end |
| 2259 |
else |
| 2260 |
return false |
| 2261 |
end |
| 2262 |
end |
| 2263 |
return true |
| 2264 |
else |
| 2265 |
return false |
| 2266 |
end |
| 2267 |
end |
| 2268 |
local function identical(a,b) |
| 2269 |
if a~=b then |
| 2270 |
for ka,va in next,a do |
| 2271 |
local vb=b[ka] |
| 2272 |
if va==vb then |
| 2273 |
elseif type(va)=="table" and type(vb)=="table" then |
| 2274 |
if not identical(va,vb) then |
| 2275 |
return false |
| 2276 |
end |
| 2277 |
else |
| 2278 |
return false |
| 2279 |
end |
| 2280 |
end |
| 2281 |
end |
| 2282 |
return true |
| 2283 |
end |
| 2284 |
table.identical=identical |
| 2285 |
table.are_equal=are_equal |
| 2286 |
local function sparse(old,nest,keeptables) |
| 2287 |
local new={} |
| 2288 |
for k,v in next,old do |
| 2289 |
if not (v=="" or v==false) then |
| 2290 |
if nest and type(v)=="table" then |
| 2291 |
v=sparse(v,nest) |
| 2292 |
if keeptables or next(v)~=nil then |
| 2293 |
new[k]=v |
| 2294 |
end |
| 2295 |
else |
| 2296 |
new[k]=v |
| 2297 |
end |
| 2298 |
end |
| 2299 |
end |
| 2300 |
return new |
| 2301 |
end |
| 2302 |
table.sparse=sparse |
| 2303 |
function table.compact(t) |
| 2304 |
return sparse(t,true,true) |
| 2305 |
end |
| 2306 |
function table.contains(t,v) |
| 2307 |
if t then |
| 2308 |
for i=1,#t do |
| 2309 |
if t[i]==v then |
| 2310 |
return i |
| 2311 |
end |
| 2312 |
end |
| 2313 |
end |
| 2314 |
return false |
| 2315 |
end |
| 2316 |
function table.count(t) |
| 2317 |
local n=0 |
| 2318 |
for k,v in next,t do |
| 2319 |
n=n+1 |
| 2320 |
end |
| 2321 |
return n |
| 2322 |
end |
| 2323 |
function table.swapped(t,s) |
| 2324 |
local n={} |
| 2325 |
if s then |
| 2326 |
for k,v in next,s do |
| 2327 |
n[k]=v |
| 2328 |
end |
| 2329 |
end |
| 2330 |
for k,v in next,t do |
| 2331 |
n[v]=k |
| 2332 |
end |
| 2333 |
return n |
| 2334 |
end |
| 2335 |
function table.hashed(t) |
| 2336 |
for i=1,#t do |
| 2337 |
t[t[i]]=i |
| 2338 |
end |
| 2339 |
return t |
| 2340 |
end |
| 2341 |
function table.mirrored(t) |
| 2342 |
local n={} |
| 2343 |
for k,v in next,t do |
| 2344 |
n[v]=k |
| 2345 |
n[k]=v |
| 2346 |
end |
| 2347 |
return n |
| 2348 |
end |
| 2349 |
function table.reversed(t) |
| 2350 |
if t then |
| 2351 |
local tt={} |
| 2352 |
local tn=#t |
| 2353 |
if tn>0 then |
| 2354 |
local ttn=0 |
| 2355 |
for i=tn,1,-1 do |
| 2356 |
ttn=ttn+1 |
| 2357 |
tt[ttn]=t[i] |
| 2358 |
end |
| 2359 |
end |
| 2360 |
return tt |
| 2361 |
end |
| 2362 |
end |
| 2363 |
function table.reverse(t) |
| 2364 |
if t then |
| 2365 |
local n=#t |
| 2366 |
local m=n+1 |
| 2367 |
for i=1,floor(n/2) do |
| 2368 |
local j=m-i |
| 2369 |
t[i],t[j]=t[j],t[i] |
| 2370 |
end |
| 2371 |
return t |
| 2372 |
end |
| 2373 |
end |
| 2374 |
local function sequenced(t,sep,simple) |
| 2375 |
if not t then |
| 2376 |
return "" |
| 2377 |
elseif type(t)~="table" then |
| 2378 |
return t |
| 2379 |
end |
| 2380 |
local n=#t |
| 2381 |
local s={} |
| 2382 |
if n>0 then |
| 2383 |
for i=1,n do |
| 2384 |
local v=t[i] |
| 2385 |
if type(v)=="table" then |
| 2386 |
s[i]="{"..sequenced(v,sep,simple).."}" |
| 2387 |
else |
| 2388 |
s[i]=tostring(t[i]) |
| 2389 |
end |
| 2390 |
end |
| 2391 |
else |
| 2392 |
n=0 |
| 2393 |
for k,v in sortedhash(t) do |
| 2394 |
if simple then |
| 2395 |
if v==true then |
| 2396 |
n=n+1 |
| 2397 |
s[n]=k |
| 2398 |
elseif v and v~="" then |
| 2399 |
n=n+1 |
| 2400 |
if type(v)=="table" then |
| 2401 |
s[n]=k.."={"..sequenced(v,sep,simple).."}" |
| 2402 |
else |
| 2403 |
s[n]=k.."="..tostring(v) |
| 2404 |
end |
| 2405 |
end |
| 2406 |
else |
| 2407 |
n=n+1 |
| 2408 |
if type(v)=="table" then |
| 2409 |
s[n]=k.."={"..sequenced(v,sep,simple).."}" |
| 2410 |
else |
| 2411 |
s[n]=k.."="..tostring(v) |
| 2412 |
end |
| 2413 |
end |
| 2414 |
end |
| 2415 |
end |
| 2416 |
if sep==true then |
| 2417 |
return "{ "..concat(s,", ").." }" |
| 2418 |
else |
| 2419 |
return concat(s,sep or " | ") |
| 2420 |
end |
| 2421 |
end |
| 2422 |
table.sequenced=sequenced |
| 2423 |
function table.print(t,...) |
| 2424 |
if type(t)~="table" then |
| 2425 |
print(tostring(t)) |
| 2426 |
else |
| 2427 |
serialize(print,t,...) |
| 2428 |
end |
| 2429 |
end |
| 2430 |
if setinspector then |
| 2431 |
setinspector("table",function(v) if type(v)=="table" then serialize(print,v,"table") return true end end) |
| 2432 |
end |
| 2433 |
function table.sub(t,i,j) |
| 2434 |
return { unpack(t,i,j) } |
| 2435 |
end |
| 2436 |
function table.is_empty(t) |
| 2437 |
return not t or next(t)==nil |
| 2438 |
end |
| 2439 |
function table.has_one_entry(t) |
| 2440 |
return t and next(t,next(t))==nil |
| 2441 |
end |
| 2442 |
function table.loweredkeys(t) |
| 2443 |
local l={} |
| 2444 |
for k,v in next,t do |
| 2445 |
l[lower(k)]=v |
| 2446 |
end |
| 2447 |
return l |
| 2448 |
end |
| 2449 |
function table.unique(old) |
| 2450 |
local hash={} |
| 2451 |
local new={} |
| 2452 |
local n=0 |
| 2453 |
for i=1,#old do |
| 2454 |
local oi=old[i] |
| 2455 |
if not hash[oi] then |
| 2456 |
n=n+1 |
| 2457 |
new[n]=oi |
| 2458 |
hash[oi]=true |
| 2459 |
end |
| 2460 |
end |
| 2461 |
return new |
| 2462 |
end |
| 2463 |
function table.sorted(t,...) |
| 2464 |
sort(t,...) |
| 2465 |
return t |
| 2466 |
end |
| 2467 |
function table.values(t,s) |
| 2468 |
if t then |
| 2469 |
local values={} |
| 2470 |
local keys={} |
| 2471 |
local v=0 |
| 2472 |
for key,value in next,t do |
| 2473 |
if not keys[value] then |
| 2474 |
v=v+1 |
| 2475 |
values[v]=value |
| 2476 |
keys[k]=key |
| 2477 |
end |
| 2478 |
end |
| 2479 |
if s then |
| 2480 |
sort(values) |
| 2481 |
end |
| 2482 |
return values |
| 2483 |
else |
| 2484 |
return {} |
| 2485 |
end |
| 2486 |
end |
| 2487 |
function table.filtered(t,pattern,sort,cmp) |
| 2488 |
if t and type(pattern)=="string" then |
| 2489 |
if sort then |
| 2490 |
local s |
| 2491 |
if cmp then |
| 2492 |
s=sortedhashkeys(t,function(a,b) return cmp(t,a,b) end) |
| 2493 |
else |
| 2494 |
s=sortedkeys(t) |
| 2495 |
end |
| 2496 |
local n=0 |
| 2497 |
local m=#s |
| 2498 |
local function kv(s) |
| 2499 |
while n<m do |
| 2500 |
n=n+1 |
| 2501 |
local k=s[n] |
| 2502 |
if find(k,pattern) then |
| 2503 |
return k,t[k] |
| 2504 |
end |
| 2505 |
end |
| 2506 |
end |
| 2507 |
return kv,s |
| 2508 |
else |
| 2509 |
local n=next(t) |
| 2510 |
local function iterator() |
| 2511 |
while n~=nil do |
| 2512 |
local k=n |
| 2513 |
n=next(t,k) |
| 2514 |
if find(k,pattern) then |
| 2515 |
return k,t[k] |
| 2516 |
end |
| 2517 |
end |
| 2518 |
end |
| 2519 |
return iterator,t |
| 2520 |
end |
| 2521 |
else |
| 2522 |
return nothing |
| 2523 |
end |
| 2524 |
end |
| 2525 |
if not table.move then |
| 2526 |
function table.move(a1,f,e,t,a2) |
| 2527 |
if a2 and a1~=a2 then |
| 2528 |
for i=f,e do |
| 2529 |
a2[t]=a1[i] |
| 2530 |
t=t+1 |
| 2531 |
end |
| 2532 |
return a2 |
| 2533 |
else |
| 2534 |
t=t+e-f |
| 2535 |
for i=e,f,-1 do |
| 2536 |
a1[t]=a1[i] |
| 2537 |
t=t-1 |
| 2538 |
end |
| 2539 |
return a1 |
| 2540 |
end |
| 2541 |
end |
| 2542 |
end |
| 2543 |
|
| 2544 |
end -- closure |
| 2545 |
|
| 2546 |
do -- begin closure to overcome local limits and interference |
| 2547 |
|
| 2548 |
if not modules then modules={} end modules ['l-boolean']={ |
| 2549 |
version=1.001, |
| 2550 |
comment="companion to luat-lib.mkiv", |
| 2551 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2552 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2553 |
license="see context related readme files" |
| 2554 |
} |
| 2555 |
local type,tonumber=type,tonumber |
| 2556 |
boolean=boolean or {} |
| 2557 |
local boolean=boolean |
| 2558 |
function boolean.tonumber(b) |
| 2559 |
if b then return 1 else return 0 end |
| 2560 |
end |
| 2561 |
function toboolean(str,tolerant) |
| 2562 |
if str==nil then |
| 2563 |
return false |
| 2564 |
elseif str==false then |
| 2565 |
return false |
| 2566 |
elseif str==true then |
| 2567 |
return true |
| 2568 |
elseif str=="true" then |
| 2569 |
return true |
| 2570 |
elseif str=="false" then |
| 2571 |
return false |
| 2572 |
elseif not tolerant then |
| 2573 |
return false |
| 2574 |
elseif str==0 then |
| 2575 |
return false |
| 2576 |
elseif (tonumber(str) or 0)>0 then |
| 2577 |
return true |
| 2578 |
else |
| 2579 |
return str=="yes" or str=="on" or str=="t" |
| 2580 |
end |
| 2581 |
end |
| 2582 |
string.toboolean=toboolean |
| 2583 |
function string.booleanstring(str) |
| 2584 |
if str=="0" then |
| 2585 |
return false |
| 2586 |
elseif str=="1" then |
| 2587 |
return true |
| 2588 |
elseif str=="" then |
| 2589 |
return false |
| 2590 |
elseif str=="false" then |
| 2591 |
return false |
| 2592 |
elseif str=="true" then |
| 2593 |
return true |
| 2594 |
elseif (tonumber(str) or 0)>0 then |
| 2595 |
return true |
| 2596 |
else |
| 2597 |
return str=="yes" or str=="on" or str=="t" |
| 2598 |
end |
| 2599 |
end |
| 2600 |
function string.is_boolean(str,default,strict) |
| 2601 |
if type(str)=="string" then |
| 2602 |
if str=="true" or str=="yes" or str=="on" or str=="t" or (not strict and str=="1") then |
| 2603 |
return true |
| 2604 |
elseif str=="false" or str=="no" or str=="off" or str=="f" or (not strict and str=="0") then |
| 2605 |
return false |
| 2606 |
end |
| 2607 |
end |
| 2608 |
return default |
| 2609 |
end |
| 2610 |
|
| 2611 |
end -- closure |
| 2612 |
|
| 2613 |
do -- begin closure to overcome local limits and interference |
| 2614 |
|
| 2615 |
if not modules then modules={} end modules ['l-number']={ |
| 2616 |
version=1.001, |
| 2617 |
comment="companion to luat-lib.mkxl", |
| 2618 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2619 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2620 |
license="see context related readme files" |
| 2621 |
} |
| 2622 |
local tostring,tonumber=tostring,tonumber |
| 2623 |
local format,match,rep=string.format,string.match,string.rep |
| 2624 |
local concat,insert=table.concat,table.insert |
| 2625 |
local lpegmatch=lpeg.match |
| 2626 |
local floor=math.floor |
| 2627 |
number=number or {} |
| 2628 |
local number=number |
| 2629 |
if bit32 then |
| 2630 |
local bextract=bit32.extract |
| 2631 |
local t={ |
| 2632 |
"0","0","0","0","0","0","0","0", |
| 2633 |
"0","0","0","0","0","0","0","0", |
| 2634 |
"0","0","0","0","0","0","0","0", |
| 2635 |
"0","0","0","0","0","0","0","0", |
| 2636 |
} |
| 2637 |
function number.tobitstring(b,m,w) |
| 2638 |
if not w then |
| 2639 |
w=32 |
| 2640 |
end |
| 2641 |
local n=w |
| 2642 |
for i=0,w-1 do |
| 2643 |
local v=bextract(b,i) |
| 2644 |
local k=w-i |
| 2645 |
if v==1 then |
| 2646 |
n=k |
| 2647 |
t[k]="1" |
| 2648 |
else |
| 2649 |
t[k]="0" |
| 2650 |
end |
| 2651 |
end |
| 2652 |
if w then |
| 2653 |
return concat(t,"",1,w) |
| 2654 |
elseif m then |
| 2655 |
m=33-m*8 |
| 2656 |
if m<1 then |
| 2657 |
m=1 |
| 2658 |
end |
| 2659 |
return concat(t,"",1,m) |
| 2660 |
elseif n<8 then |
| 2661 |
return concat(t) |
| 2662 |
elseif n<16 then |
| 2663 |
return concat(t,"",9) |
| 2664 |
elseif n<24 then |
| 2665 |
return concat(t,"",17) |
| 2666 |
else |
| 2667 |
return concat(t,"",25) |
| 2668 |
end |
| 2669 |
end |
| 2670 |
else |
| 2671 |
function number.tobitstring(n,m) |
| 2672 |
if n>0 then |
| 2673 |
local t={} |
| 2674 |
while n>0 do |
| 2675 |
insert(t,1,n%2>0 and 1 or 0) |
| 2676 |
n=floor(n/2) |
| 2677 |
end |
| 2678 |
local nn=8-#t%8 |
| 2679 |
if nn>0 and nn<8 then |
| 2680 |
for i=1,nn do |
| 2681 |
insert(t,1,0) |
| 2682 |
end |
| 2683 |
end |
| 2684 |
if m then |
| 2685 |
m=m*8-#t |
| 2686 |
if m>0 then |
| 2687 |
insert(t,1,rep("0",m)) |
| 2688 |
end |
| 2689 |
end |
| 2690 |
return concat(t) |
| 2691 |
elseif m then |
| 2692 |
rep("00000000",m) |
| 2693 |
else |
| 2694 |
return "00000000" |
| 2695 |
end |
| 2696 |
end |
| 2697 |
end |
| 2698 |
function number.valid(str,default) |
| 2699 |
return tonumber(str) or default or nil |
| 2700 |
end |
| 2701 |
function number.toevenhex(n) |
| 2702 |
local s=format("%X",n) |
| 2703 |
if #s%2==0 then |
| 2704 |
return s |
| 2705 |
else |
| 2706 |
return "0"..s |
| 2707 |
end |
| 2708 |
end |
| 2709 |
function number.bytetodecimal(b) |
| 2710 |
local d=floor(b*100/255+0.5) |
| 2711 |
if d>100 then |
| 2712 |
return 100 |
| 2713 |
elseif d<-100 then |
| 2714 |
return -100 |
| 2715 |
else |
| 2716 |
return d |
| 2717 |
end |
| 2718 |
end |
| 2719 |
function number.decimaltobyte(d) |
| 2720 |
local b=floor(d*255/100+0.5) |
| 2721 |
if b>255 then |
| 2722 |
return 255 |
| 2723 |
elseif b<-255 then |
| 2724 |
return -255 |
| 2725 |
else |
| 2726 |
return b |
| 2727 |
end |
| 2728 |
end |
| 2729 |
function number.idiv(i,d) |
| 2730 |
return floor(i/d) |
| 2731 |
end |
| 2732 |
|
| 2733 |
end -- closure |
| 2734 |
|
| 2735 |
do -- begin closure to overcome local limits and interference |
| 2736 |
|
| 2737 |
if not modules then modules={} end modules ['l-math']={ |
| 2738 |
version=1.001, |
| 2739 |
comment="companion to luat-lib.mkiv", |
| 2740 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2741 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2742 |
license="see context related readme files" |
| 2743 |
} |
| 2744 |
if not math.ceiling then |
| 2745 |
math.ceiling=math.ceil |
| 2746 |
end |
| 2747 |
if not math.round then |
| 2748 |
if xmath then |
| 2749 |
math.round=xmath.round |
| 2750 |
else |
| 2751 |
local floor=math.floor |
| 2752 |
function math.round(x) |
| 2753 |
return x<0 and -floor(-x+0.5) or floor(x+0.5) |
| 2754 |
end |
| 2755 |
end |
| 2756 |
end |
| 2757 |
if not math.div then |
| 2758 |
local floor=math.floor |
| 2759 |
function math.div(n,m) return floor(n/m) end |
| 2760 |
end |
| 2761 |
if not math.mod then |
| 2762 |
function math.mod(n,m) return n%m end |
| 2763 |
end |
| 2764 |
if not math.sind then |
| 2765 |
local sin,cos,tan=math.sin,math.cos,math.tan |
| 2766 |
local pipi=2*math.pi/360 |
| 2767 |
function math.sind(d) return sin(d*pipi) end |
| 2768 |
function math.cosd(d) return cos(d*pipi) end |
| 2769 |
function math.tand(d) return tan(d*pipi) end |
| 2770 |
end |
| 2771 |
if not math.odd then |
| 2772 |
function math.odd (n) return n%2~=0 end |
| 2773 |
function math.even(n) return n%2==0 end |
| 2774 |
end |
| 2775 |
if not math.cosh then |
| 2776 |
local exp=math.exp |
| 2777 |
function math.cosh(x) |
| 2778 |
local xx=exp(x) |
| 2779 |
return (xx+1/xx)/2 |
| 2780 |
end |
| 2781 |
function math.sinh(x) |
| 2782 |
local xx=exp(x) |
| 2783 |
return (xx-1/xx)/2 |
| 2784 |
end |
| 2785 |
function math.tanh(x) |
| 2786 |
local xx=exp(x) |
| 2787 |
return (xx-1/xx)/(xx+1/xx) |
| 2788 |
end |
| 2789 |
end |
| 2790 |
if not math.pow then |
| 2791 |
function math.pow(x,y) |
| 2792 |
return x^y |
| 2793 |
end |
| 2794 |
end |
| 2795 |
if not math.atan2 then |
| 2796 |
math.atan2=math.atan |
| 2797 |
end |
| 2798 |
if not math.ldexp then |
| 2799 |
function math.ldexp(x,e) |
| 2800 |
return x*2.0^e |
| 2801 |
end |
| 2802 |
end |
| 2803 |
if not math.log10 then |
| 2804 |
local log=math.log |
| 2805 |
function math.log10(x) |
| 2806 |
return log(x,10) |
| 2807 |
end |
| 2808 |
end |
| 2809 |
if not math.type then |
| 2810 |
function math.type() |
| 2811 |
return "float" |
| 2812 |
end |
| 2813 |
end |
| 2814 |
if not math.tointeger then |
| 2815 |
math.mininteger=-0x4FFFFFFFFFFF |
| 2816 |
math.maxinteger=0x4FFFFFFFFFFF |
| 2817 |
local floor=math.floor |
| 2818 |
function math.tointeger(n) |
| 2819 |
local f=floor(n) |
| 2820 |
return f==n and f or nil |
| 2821 |
end |
| 2822 |
end |
| 2823 |
if not math.ult then |
| 2824 |
local floor=math.floor |
| 2825 |
function math.ult(m,n) |
| 2826 |
return floor(m)<floor(n) |
| 2827 |
end |
| 2828 |
end |
| 2829 |
|
| 2830 |
end -- closure |
| 2831 |
|
| 2832 |
do -- begin closure to overcome local limits and interference |
| 2833 |
|
| 2834 |
if not modules then modules={} end modules ['l-io']={ |
| 2835 |
version=1.001, |
| 2836 |
comment="companion to luat-lib.mkiv", |
| 2837 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2838 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2839 |
license="see context related readme files" |
| 2840 |
} |
| 2841 |
local io=io |
| 2842 |
local open,flush,write,read=io.open,io.flush,io.write,io.read |
| 2843 |
local byte,find,gsub,format=string.byte,string.find,string.gsub,string.format |
| 2844 |
local concat=table.concat |
| 2845 |
local type=type |
| 2846 |
if string.find(os.getenv("PATH") or "",";",1,true) then |
| 2847 |
io.fileseparator,io.pathseparator="\\",";" |
| 2848 |
else |
| 2849 |
io.fileseparator,io.pathseparator="/",":" |
| 2850 |
end |
| 2851 |
local large=0x01000000 |
| 2852 |
local medium=0x00100000 |
| 2853 |
local small=0x00020000 |
| 2854 |
local function readall(f) |
| 2855 |
local size=f:seek("end") |
| 2856 |
if size>0 then |
| 2857 |
f:seek("set",0) |
| 2858 |
return f:read(size) |
| 2859 |
else |
| 2860 |
return "" |
| 2861 |
end |
| 2862 |
end |
| 2863 |
io.readall=readall |
| 2864 |
function io.loaddata(filename,textmode) |
| 2865 |
local f=open(filename,(textmode and 'r') or 'rb') |
| 2866 |
if f then |
| 2867 |
local size=f:seek("end") |
| 2868 |
local data=nil |
| 2869 |
if size>0 then |
| 2870 |
f:seek("set",0) |
| 2871 |
data=f:read(size) |
| 2872 |
end |
| 2873 |
f:close() |
| 2874 |
return data |
| 2875 |
end |
| 2876 |
end |
| 2877 |
function io.copydata(source,target,action) |
| 2878 |
local f=open(source,"rb") |
| 2879 |
if f then |
| 2880 |
local g=open(target,"wb") |
| 2881 |
if g then |
| 2882 |
local size=f:seek("end") |
| 2883 |
if size>0 then |
| 2884 |
f:seek("set",0) |
| 2885 |
local data=f:read(size) |
| 2886 |
if action then |
| 2887 |
data=action(data) |
| 2888 |
end |
| 2889 |
if data then |
| 2890 |
g:write(data) |
| 2891 |
end |
| 2892 |
end |
| 2893 |
g:close() |
| 2894 |
end |
| 2895 |
f:close() |
| 2896 |
flush() |
| 2897 |
end |
| 2898 |
end |
| 2899 |
function io.savedata(filename,data,joiner,append) |
| 2900 |
local f=open(filename,append and "ab" or "wb") |
| 2901 |
if f then |
| 2902 |
if append and joiner and f:seek("end")>0 then |
| 2903 |
f:write(joiner) |
| 2904 |
end |
| 2905 |
if type(data)=="table" then |
| 2906 |
f:write(concat(data,joiner or "")) |
| 2907 |
elseif type(data)=="function" then |
| 2908 |
data(f) |
| 2909 |
else |
| 2910 |
f:write(data or "") |
| 2911 |
end |
| 2912 |
f:close() |
| 2913 |
flush() |
| 2914 |
return true |
| 2915 |
else |
| 2916 |
return false |
| 2917 |
end |
| 2918 |
end |
| 2919 |
if fio and fio.readline then |
| 2920 |
local readline=fio.readline |
| 2921 |
function io.loadlines(filename,n) |
| 2922 |
local f=open(filename,'r') |
| 2923 |
if not f then |
| 2924 |
elseif n then |
| 2925 |
local lines={} |
| 2926 |
for i=1,n do |
| 2927 |
local line=readline(f) |
| 2928 |
if line then |
| 2929 |
lines[i]=line |
| 2930 |
else |
| 2931 |
break |
| 2932 |
end |
| 2933 |
end |
| 2934 |
f:close() |
| 2935 |
lines=concat(lines,"\n") |
| 2936 |
if #lines>0 then |
| 2937 |
return lines |
| 2938 |
end |
| 2939 |
else |
| 2940 |
local line=readline(f) |
| 2941 |
f:close() |
| 2942 |
if line and #line>0 then |
| 2943 |
return line |
| 2944 |
end |
| 2945 |
end |
| 2946 |
end |
| 2947 |
else |
| 2948 |
function io.loadlines(filename,n) |
| 2949 |
local f=open(filename,'r') |
| 2950 |
if not f then |
| 2951 |
elseif n then |
| 2952 |
local lines={} |
| 2953 |
for i=1,n do |
| 2954 |
local line=f:read("*lines") |
| 2955 |
if line then |
| 2956 |
lines[i]=line |
| 2957 |
else |
| 2958 |
break |
| 2959 |
end |
| 2960 |
end |
| 2961 |
f:close() |
| 2962 |
lines=concat(lines,"\n") |
| 2963 |
if #lines>0 then |
| 2964 |
return lines |
| 2965 |
end |
| 2966 |
else |
| 2967 |
local line=f:read("*line") or "" |
| 2968 |
f:close() |
| 2969 |
if #line>0 then |
| 2970 |
return line |
| 2971 |
end |
| 2972 |
end |
| 2973 |
end |
| 2974 |
end |
| 2975 |
function io.loadchunk(filename,n) |
| 2976 |
local f=open(filename,'rb') |
| 2977 |
if f then |
| 2978 |
local data=f:read(n or 1024) |
| 2979 |
f:close() |
| 2980 |
if #data>0 then |
| 2981 |
return data |
| 2982 |
end |
| 2983 |
end |
| 2984 |
end |
| 2985 |
function io.exists(filename) |
| 2986 |
local f=open(filename) |
| 2987 |
if f==nil then |
| 2988 |
return false |
| 2989 |
else |
| 2990 |
f:close() |
| 2991 |
return true |
| 2992 |
end |
| 2993 |
end |
| 2994 |
function io.size(filename) |
| 2995 |
local f=open(filename) |
| 2996 |
if f==nil then |
| 2997 |
return 0 |
| 2998 |
else |
| 2999 |
local s=f:seek("end") |
| 3000 |
f:close() |
| 3001 |
return s |
| 3002 |
end |
| 3003 |
end |
| 3004 |
local function noflines(f) |
| 3005 |
if type(f)=="string" then |
| 3006 |
local f=open(filename) |
| 3007 |
if f then |
| 3008 |
local n=f and noflines(f) or 0 |
| 3009 |
f:close() |
| 3010 |
return n |
| 3011 |
else |
| 3012 |
return 0 |
| 3013 |
end |
| 3014 |
else |
| 3015 |
local n=0 |
| 3016 |
for _ in f:lines() do |
| 3017 |
n=n+1 |
| 3018 |
end |
| 3019 |
f:seek('set',0) |
| 3020 |
return n |
| 3021 |
end |
| 3022 |
end |
| 3023 |
io.noflines=noflines |
| 3024 |
local nextchar={ |
| 3025 |
[ 4]=function(f) |
| 3026 |
return f:read(1,1,1,1) |
| 3027 |
end, |
| 3028 |
[ 2]=function(f) |
| 3029 |
return f:read(1,1) |
| 3030 |
end, |
| 3031 |
[ 1]=function(f) |
| 3032 |
return f:read(1) |
| 3033 |
end, |
| 3034 |
[-2]=function(f) |
| 3035 |
local a,b=f:read(1,1) |
| 3036 |
return b,a |
| 3037 |
end, |
| 3038 |
[-4]=function(f) |
| 3039 |
local a,b,c,d=f:read(1,1,1,1) |
| 3040 |
return d,c,b,a |
| 3041 |
end |
| 3042 |
} |
| 3043 |
function io.characters(f,n) |
| 3044 |
if f then |
| 3045 |
return nextchar[n or 1],f |
| 3046 |
end |
| 3047 |
end |
| 3048 |
local nextbyte={ |
| 3049 |
[4]=function(f) |
| 3050 |
local a,b,c,d=f:read(1,1,1,1) |
| 3051 |
if d then |
| 3052 |
return byte(a),byte(b),byte(c),byte(d) |
| 3053 |
end |
| 3054 |
end, |
| 3055 |
[3]=function(f) |
| 3056 |
local a,b,c=f:read(1,1,1) |
| 3057 |
if b then |
| 3058 |
return byte(a),byte(b),byte(c) |
| 3059 |
end |
| 3060 |
end, |
| 3061 |
[2]=function(f) |
| 3062 |
local a,b=f:read(1,1) |
| 3063 |
if b then |
| 3064 |
return byte(a),byte(b) |
| 3065 |
end |
| 3066 |
end, |
| 3067 |
[1]=function (f) |
| 3068 |
local a=f:read(1) |
| 3069 |
if a then |
| 3070 |
return byte(a) |
| 3071 |
end |
| 3072 |
end, |
| 3073 |
[-2]=function (f) |
| 3074 |
local a,b=f:read(1,1) |
| 3075 |
if b then |
| 3076 |
return byte(b),byte(a) |
| 3077 |
end |
| 3078 |
end, |
| 3079 |
[-3]=function(f) |
| 3080 |
local a,b,c=f:read(1,1,1) |
| 3081 |
if b then |
| 3082 |
return byte(c),byte(b),byte(a) |
| 3083 |
end |
| 3084 |
end, |
| 3085 |
[-4]=function(f) |
| 3086 |
local a,b,c,d=f:read(1,1,1,1) |
| 3087 |
if d then |
| 3088 |
return byte(d),byte(c),byte(b),byte(a) |
| 3089 |
end |
| 3090 |
end |
| 3091 |
} |
| 3092 |
function io.bytes(f,n) |
| 3093 |
if f then |
| 3094 |
return nextbyte[n or 1],f |
| 3095 |
else |
| 3096 |
return nil,nil |
| 3097 |
end |
| 3098 |
end |
| 3099 |
function io.ask(question,default,options) |
| 3100 |
while true do |
| 3101 |
write(question) |
| 3102 |
if options then |
| 3103 |
write(format(" [%s]",concat(options,"|"))) |
| 3104 |
end |
| 3105 |
if default then |
| 3106 |
write(format(" [%s]",default)) |
| 3107 |
end |
| 3108 |
write(format(" ")) |
| 3109 |
flush() |
| 3110 |
local answer=read() |
| 3111 |
answer=gsub(answer,"^%s*(.*)%s*$","%1") |
| 3112 |
if answer=="" and default then |
| 3113 |
return default |
| 3114 |
elseif not options then |
| 3115 |
return answer |
| 3116 |
else |
| 3117 |
for k=1,#options do |
| 3118 |
if options[k]==answer then |
| 3119 |
return answer |
| 3120 |
end |
| 3121 |
end |
| 3122 |
local pattern="^"..answer |
| 3123 |
for k=1,#options do |
| 3124 |
local v=options[k] |
| 3125 |
if find(v,pattern) then |
| 3126 |
return v |
| 3127 |
end |
| 3128 |
end |
| 3129 |
end |
| 3130 |
end |
| 3131 |
end |
| 3132 |
local function readnumber(f,n,m) |
| 3133 |
if m then |
| 3134 |
f:seek("set",n) |
| 3135 |
n=m |
| 3136 |
end |
| 3137 |
if n==1 then |
| 3138 |
return byte(f:read(1)) |
| 3139 |
elseif n==2 then |
| 3140 |
local a,b=byte(f:read(2),1,2) |
| 3141 |
return 0x100*a+b |
| 3142 |
elseif n==3 then |
| 3143 |
local a,b,c=byte(f:read(3),1,3) |
| 3144 |
return 0x10000*a+0x100*b+c |
| 3145 |
elseif n==4 then |
| 3146 |
local a,b,c,d=byte(f:read(4),1,4) |
| 3147 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 3148 |
elseif n==8 then |
| 3149 |
local a,b=readnumber(f,4),readnumber(f,4) |
| 3150 |
return 0x100*a+b |
| 3151 |
elseif n==12 then |
| 3152 |
local a,b,c=readnumber(f,4),readnumber(f,4),readnumber(f,4) |
| 3153 |
return 0x10000*a+0x100*b+c |
| 3154 |
elseif n==-2 then |
| 3155 |
local b,a=byte(f:read(2),1,2) |
| 3156 |
return 0x100*a+b |
| 3157 |
elseif n==-3 then |
| 3158 |
local c,b,a=byte(f:read(3),1,3) |
| 3159 |
return 0x10000*a+0x100*b+c |
| 3160 |
elseif n==-4 then |
| 3161 |
local d,c,b,a=byte(f:read(4),1,4) |
| 3162 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 3163 |
elseif n==-8 then |
| 3164 |
local h,g,f,e,d,c,b,a=byte(f:read(8),1,8) |
| 3165 |
return 0x100000000000000*a+0x1000000000000*b+0x10000000000*c+0x100000000*d+0x1000000*e+0x10000*f+0x100*g+h |
| 3166 |
else |
| 3167 |
return 0 |
| 3168 |
end |
| 3169 |
end |
| 3170 |
io.readnumber=readnumber |
| 3171 |
function io.readstring(f,n,m) |
| 3172 |
if m then |
| 3173 |
f:seek("set",n) |
| 3174 |
n=m |
| 3175 |
end |
| 3176 |
local str=gsub(f:read(n),"\000","") |
| 3177 |
return str |
| 3178 |
end |
| 3179 |
|
| 3180 |
end -- closure |
| 3181 |
|
| 3182 |
do -- begin closure to overcome local limits and interference |
| 3183 |
|
| 3184 |
if not modules then modules={} end modules ['l-os']={ |
| 3185 |
version=1.001, |
| 3186 |
comment="companion to luat-lib.mkiv", |
| 3187 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3188 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3189 |
license="see context related readme files" |
| 3190 |
} |
| 3191 |
local os=os |
| 3192 |
local date,time,difftime=os.date,os.time,os.difftime |
| 3193 |
local find,format,gsub,upper,gmatch=string.find,string.format,string.gsub,string.upper,string.gmatch |
| 3194 |
local concat=table.concat |
| 3195 |
local random,ceil,randomseed,modf=math.random,math.ceil,math.randomseed,math.modf |
| 3196 |
local type,setmetatable,tonumber,tostring=type,setmetatable,tonumber,tostring |
| 3197 |
do |
| 3198 |
local selfdir=os.selfdir |
| 3199 |
if selfdir=="" then |
| 3200 |
selfdir=nil |
| 3201 |
end |
| 3202 |
if not selfdir then |
| 3203 |
if arg then |
| 3204 |
for i=1,#arg do |
| 3205 |
local a=arg[i] |
| 3206 |
if find(a,"^%-%-[c:]*texmfbinpath=") then |
| 3207 |
selfdir=gsub(a,"^.-=","") |
| 3208 |
break |
| 3209 |
end |
| 3210 |
end |
| 3211 |
end |
| 3212 |
if not selfdir then |
| 3213 |
selfdir=os.selfbin or "luatex" |
| 3214 |
if find(selfdir,"[/\\]") then |
| 3215 |
selfdir=gsub(selfdir,"[/\\][^/\\]*$","") |
| 3216 |
elseif os.getenv then |
| 3217 |
local path=os.getenv("PATH") |
| 3218 |
local name=gsub(selfdir,"^.*[/\\][^/\\]","") |
| 3219 |
local patt="[^:]+" |
| 3220 |
if os.type=="windows" then |
| 3221 |
patt="[^;]+" |
| 3222 |
name=name..".exe" |
| 3223 |
end |
| 3224 |
local isfile |
| 3225 |
if lfs then |
| 3226 |
local attributes=lfs.attributes |
| 3227 |
isfile=function(name) |
| 3228 |
local a=attributes(name,"mode") |
| 3229 |
return a=="file" or a=="link" or nil |
| 3230 |
end |
| 3231 |
else |
| 3232 |
local open=io.open |
| 3233 |
isfile=function(name) |
| 3234 |
local f=open(name) |
| 3235 |
if f then |
| 3236 |
f:close() |
| 3237 |
return true |
| 3238 |
end |
| 3239 |
end |
| 3240 |
end |
| 3241 |
for p in gmatch(path,patt) do |
| 3242 |
if isfile(p.."/"..name) then |
| 3243 |
selfdir=p |
| 3244 |
break |
| 3245 |
end |
| 3246 |
end |
| 3247 |
end |
| 3248 |
end |
| 3249 |
os.selfdir=selfdir or "." |
| 3250 |
end |
| 3251 |
end |
| 3252 |
math.initialseed=tonumber(string.sub(string.reverse(tostring(ceil(socket and socket.gettime()*10000 or time()))),1,6)) |
| 3253 |
randomseed(math.initialseed) |
| 3254 |
if not os.__getenv__ then |
| 3255 |
os.__getenv__=os.getenv |
| 3256 |
os.__setenv__=os.setenv |
| 3257 |
if os.env then |
| 3258 |
local osgetenv=os.getenv |
| 3259 |
local ossetenv=os.setenv |
| 3260 |
local osenv=os.env local _=osenv.PATH |
| 3261 |
function os.setenv(k,v) |
| 3262 |
if v==nil then |
| 3263 |
v="" |
| 3264 |
end |
| 3265 |
local K=upper(k) |
| 3266 |
osenv[K]=v |
| 3267 |
if type(v)=="table" then |
| 3268 |
v=concat(v,";") |
| 3269 |
end |
| 3270 |
ossetenv(K,v) |
| 3271 |
end |
| 3272 |
function os.getenv(k) |
| 3273 |
local K=upper(k) |
| 3274 |
local v=osenv[K] or osenv[k] or osgetenv(K) or osgetenv(k) |
| 3275 |
if v=="" then |
| 3276 |
return nil |
| 3277 |
else |
| 3278 |
return v |
| 3279 |
end |
| 3280 |
end |
| 3281 |
else |
| 3282 |
local ossetenv=os.setenv |
| 3283 |
local osgetenv=os.getenv |
| 3284 |
local osenv={} |
| 3285 |
function os.setenv(k,v) |
| 3286 |
if v==nil then |
| 3287 |
v="" |
| 3288 |
end |
| 3289 |
local K=upper(k) |
| 3290 |
osenv[K]=v |
| 3291 |
end |
| 3292 |
function os.getenv(k) |
| 3293 |
local K=upper(k) |
| 3294 |
local v=osenv[K] or osgetenv(K) or osgetenv(k) |
| 3295 |
if v=="" then |
| 3296 |
return nil |
| 3297 |
else |
| 3298 |
return v |
| 3299 |
end |
| 3300 |
end |
| 3301 |
local function __index(t,k) |
| 3302 |
return os.getenv(k) |
| 3303 |
end |
| 3304 |
local function __newindex(t,k,v) |
| 3305 |
os.setenv(k,v) |
| 3306 |
end |
| 3307 |
os.env={} |
| 3308 |
setmetatable(os.env,{ __index=__index,__newindex=__newindex } ) |
| 3309 |
end |
| 3310 |
end |
| 3311 |
if not io.fileseparator then |
| 3312 |
if find(os.getenv("PATH"),";",1,true) then |
| 3313 |
io.fileseparator,io.pathseparator,os.type="\\",";",os.type or "windows" |
| 3314 |
else |
| 3315 |
io.fileseparator,io.pathseparator,os.type="/",":",os.type or "unix" |
| 3316 |
end |
| 3317 |
end |
| 3318 |
os.type=os.type or (io.pathseparator==";" and "windows") or "unix" |
| 3319 |
os.name=os.name or (os.type=="windows" and "mswin" ) or "linux" |
| 3320 |
if os.type=="windows" then |
| 3321 |
os.libsuffix,os.binsuffix,os.binsuffixes='dll','exe',{ 'exe','cmd','bat' } |
| 3322 |
else |
| 3323 |
os.libsuffix,os.binsuffix,os.binsuffixes='so','',{ '' } |
| 3324 |
end |
| 3325 |
do |
| 3326 |
local execute=os.execute |
| 3327 |
local iopopen=io.popen |
| 3328 |
local ostype=os.type |
| 3329 |
local function resultof(command) |
| 3330 |
local handle=iopopen(command,ostype=="windows" and "rb" or "r") |
| 3331 |
if handle then |
| 3332 |
local result=handle:read("*all") or "" |
| 3333 |
handle:close() |
| 3334 |
return result |
| 3335 |
else |
| 3336 |
return "" |
| 3337 |
end |
| 3338 |
end |
| 3339 |
os.resultof=resultof |
| 3340 |
function os.pipeto(command) |
| 3341 |
return iopopen(command,"w") |
| 3342 |
end |
| 3343 |
local launchers={ |
| 3344 |
windows="start %s", |
| 3345 |
macosx="open %s", |
| 3346 |
unix="xdg-open %s &> /dev/null &", |
| 3347 |
} |
| 3348 |
function os.launch(str) |
| 3349 |
local command=format(launchers[os.name] or launchers.unix,str) |
| 3350 |
execute(command) |
| 3351 |
end |
| 3352 |
end |
| 3353 |
do |
| 3354 |
local gettimeofday=os.gettimeofday or os.clock |
| 3355 |
os.gettimeofday=gettimeofday |
| 3356 |
local startuptime=gettimeofday() |
| 3357 |
function os.runtime() |
| 3358 |
return gettimeofday()-startuptime |
| 3359 |
end |
| 3360 |
end |
| 3361 |
do |
| 3362 |
local name=os.name or "linux" |
| 3363 |
local platform=os.getenv("MTX_PLATFORM") or "" |
| 3364 |
local architecture=os.uname and os.uname().machine |
| 3365 |
local bits=os.getenv("MTX_BITS") or find(platform,"64") and 64 or 32 |
| 3366 |
if platform~="" then |
| 3367 |
elseif os.type=="windows" then |
| 3368 |
architecture=string.lower(architecture or os.getenv("PROCESSOR_ARCHITECTURE") or "") |
| 3369 |
if architecture=="x86_64" then |
| 3370 |
bits,platform=64,"win64" |
| 3371 |
elseif find(architecture,"amd64") then |
| 3372 |
bits,platform=64,"win64" |
| 3373 |
elseif find(architecture,"arm64") then |
| 3374 |
bits,platform=64,"windows-arm64" |
| 3375 |
elseif find(architecture,"arm32") then |
| 3376 |
bits,platform=32,"windows-arm32" |
| 3377 |
else |
| 3378 |
bits,platform=32,"mswin" |
| 3379 |
end |
| 3380 |
elseif name=="linux" then |
| 3381 |
architecture=architecture or os.getenv("HOSTTYPE") or resultof("uname -m") or "" |
| 3382 |
local musl=find(os.selfdir or "","linuxmusl") |
| 3383 |
if find(architecture,"x86_64") then |
| 3384 |
bits,platform=64,musl and "linuxmusl" or "linux-64" |
| 3385 |
elseif find(architecture,"ppc") then |
| 3386 |
bits,platform=32,"linux-ppc" |
| 3387 |
else |
| 3388 |
bits,platform=32,musl and "linuxmusl" or "linux" |
| 3389 |
end |
| 3390 |
elseif name=="macosx" then |
| 3391 |
architecture=architecture or resultof("echo $HOSTTYPE") or "" |
| 3392 |
if architecture=="" then |
| 3393 |
bits,platform=64,"osx-intel" |
| 3394 |
elseif find(architecture,"i386") then |
| 3395 |
bits,platform=64,"osx-intel" |
| 3396 |
elseif find(architecture,"x86_64") then |
| 3397 |
bits,platform=64,"osx-64" |
| 3398 |
elseif find(architecture,"arm64") then |
| 3399 |
bits,platform=64,"osx-arm" |
| 3400 |
else |
| 3401 |
bits,platform=32,"osx-ppc" |
| 3402 |
end |
| 3403 |
elseif name=="sunos" then |
| 3404 |
architecture=architecture or resultof("uname -m") or "" |
| 3405 |
if find(architecture,"sparc") then |
| 3406 |
bits,platform=32,"solaris-sparc" |
| 3407 |
else |
| 3408 |
bits,platform=32,"solaris-intel" |
| 3409 |
end |
| 3410 |
elseif name=="freebsd" then |
| 3411 |
architecture=architecture or os.getenv("MACHTYPE") or resultof("uname -m") or "" |
| 3412 |
if find(architecture,"amd64") or find(architecture,"AMD64") then |
| 3413 |
bits,platform=64,"freebsd-amd64" |
| 3414 |
else |
| 3415 |
bits,platform=32,"freebsd" |
| 3416 |
end |
| 3417 |
elseif name=="kfreebsd" then |
| 3418 |
architecture=architecture or os.getenv("HOSTTYPE") or resultof("uname -m") or "" |
| 3419 |
if architecture=="x86_64" then |
| 3420 |
bits,platform=64,"kfreebsd-amd64" |
| 3421 |
else |
| 3422 |
bits,platform=32,"kfreebsd-i386" |
| 3423 |
end |
| 3424 |
else |
| 3425 |
architecture=architecture or resultof("uname -m") or "" |
| 3426 |
if find(architecture,"aarch64") then |
| 3427 |
bits,platform="linux-aarch64" |
| 3428 |
elseif find(architecture,"armv7l") then |
| 3429 |
bits,platform=32,"linux-armhf" |
| 3430 |
elseif find(architecture,"mips64") or find(architecture,"mips64el") then |
| 3431 |
bits,platform=64,"linux-mipsel" |
| 3432 |
elseif find(architecture,"mipsel") or find(architecture,"mips") then |
| 3433 |
bits,platform=32,"linux-mipsel" |
| 3434 |
else |
| 3435 |
bits,platform=64,"linux-64" |
| 3436 |
end |
| 3437 |
end |
| 3438 |
os.setenv("MTX_PLATFORM",platform) |
| 3439 |
os.setenv("MTX_BITS",bits) |
| 3440 |
os.platform=platform |
| 3441 |
os.bits=bits |
| 3442 |
os.newline=name=="windows" and "\013\010" or "\010" |
| 3443 |
end |
| 3444 |
do |
| 3445 |
local t={ 8,9,"a","b" } |
| 3446 |
function os.uuid() |
| 3447 |
return format("%04x%04x-4%03x-%s%03x-%04x-%04x%04x%04x", |
| 3448 |
random(0xFFFF),random(0xFFFF), |
| 3449 |
random(0x0FFF), |
| 3450 |
t[ceil(random(4))] or 8,random(0x0FFF), |
| 3451 |
random(0xFFFF), |
| 3452 |
random(0xFFFF),random(0xFFFF),random(0xFFFF) |
| 3453 |
) |
| 3454 |
end |
| 3455 |
end |
| 3456 |
do |
| 3457 |
local hour,min |
| 3458 |
function os.timezone(difference) |
| 3459 |
if not hour then |
| 3460 |
local current=time() |
| 3461 |
local utcdate=date("!*t",current) |
| 3462 |
local localdate=date("*t",current) |
| 3463 |
localdate.isdst=false |
| 3464 |
local timediff=difftime(time(localdate),time(utcdate)) |
| 3465 |
hour,min=modf(timediff/3600) |
| 3466 |
min=min*60 |
| 3467 |
end |
| 3468 |
if difference then |
| 3469 |
return hour,min |
| 3470 |
else |
| 3471 |
return format("%+03d:%02d",hour,min) |
| 3472 |
end |
| 3473 |
end |
| 3474 |
local timeformat=format("%%s%s",os.timezone()) |
| 3475 |
local dateformat="%Y-%m-%d %H:%M:%S" |
| 3476 |
local lasttime=nil |
| 3477 |
local lastdate=nil |
| 3478 |
function os.fulltime(t,default) |
| 3479 |
t=t and tonumber(t) or 0 |
| 3480 |
if t>0 then |
| 3481 |
elseif default then |
| 3482 |
return default |
| 3483 |
else |
| 3484 |
t=time() |
| 3485 |
end |
| 3486 |
if t~=lasttime then |
| 3487 |
lasttime=t |
| 3488 |
lastdate=format(timeformat,date(dateformat)) |
| 3489 |
end |
| 3490 |
return lastdate |
| 3491 |
end |
| 3492 |
local dateformat="%Y-%m-%d %H:%M:%S" |
| 3493 |
local lasttime=nil |
| 3494 |
local lastdate=nil |
| 3495 |
function os.localtime(t,default) |
| 3496 |
t=t and tonumber(t) or 0 |
| 3497 |
if t>0 then |
| 3498 |
elseif default then |
| 3499 |
return default |
| 3500 |
else |
| 3501 |
t=time() |
| 3502 |
end |
| 3503 |
if t~=lasttime then |
| 3504 |
lasttime=t |
| 3505 |
lastdate=date(dateformat,t) |
| 3506 |
end |
| 3507 |
return lastdate |
| 3508 |
end |
| 3509 |
function os.converttime(t,default) |
| 3510 |
local t=tonumber(t) |
| 3511 |
if t and t>0 then |
| 3512 |
return date(dateformat,t) |
| 3513 |
else |
| 3514 |
return default or "-" |
| 3515 |
end |
| 3516 |
end |
| 3517 |
function os.today() |
| 3518 |
return date("!*t") |
| 3519 |
end |
| 3520 |
function os.now() |
| 3521 |
return date("!%Y-%m-%d %H:%M:%S") |
| 3522 |
end |
| 3523 |
end |
| 3524 |
do |
| 3525 |
local cache={} |
| 3526 |
local function which(filename) |
| 3527 |
local fullname=cache[filename] |
| 3528 |
if fullname==nil then |
| 3529 |
local suffix=file.suffix(filename) |
| 3530 |
local suffixes=suffix=="" and os.binsuffixes or { suffix } |
| 3531 |
for directory in gmatch(os.getenv("PATH"),"[^"..io.pathseparator.."]+") do |
| 3532 |
local df=file.join(directory,filename) |
| 3533 |
for i=1,#suffixes do |
| 3534 |
local dfs=file.addsuffix(df,suffixes[i]) |
| 3535 |
if io.exists(dfs) then |
| 3536 |
fullname=dfs |
| 3537 |
break |
| 3538 |
end |
| 3539 |
end |
| 3540 |
end |
| 3541 |
if not fullname then |
| 3542 |
fullname=false |
| 3543 |
end |
| 3544 |
cache[filename]=fullname |
| 3545 |
end |
| 3546 |
return fullname |
| 3547 |
end |
| 3548 |
os.which=which |
| 3549 |
os.where=which |
| 3550 |
end |
| 3551 |
if not os.sleep then |
| 3552 |
local socket=socket |
| 3553 |
function os.sleep(n) |
| 3554 |
if not socket then |
| 3555 |
socket=require("socket") |
| 3556 |
end |
| 3557 |
socket.sleep(n) |
| 3558 |
end |
| 3559 |
end |
| 3560 |
do |
| 3561 |
local function isleapyear(year) |
| 3562 |
return (year%4==0) and (year%100~=0 or year%400==0) |
| 3563 |
end |
| 3564 |
os.isleapyear=isleapyear |
| 3565 |
local days={ 31,28,31,30,31,30,31,31,30,31,30,31 } |
| 3566 |
local function nofdays(year,month,day) |
| 3567 |
if not month then |
| 3568 |
return isleapyear(year) and 365 or 364 |
| 3569 |
elseif not day then |
| 3570 |
return month==2 and isleapyear(year) and 29 or days[month] |
| 3571 |
else |
| 3572 |
for i=1,month-1 do |
| 3573 |
day=day+days[i] |
| 3574 |
end |
| 3575 |
if month>2 and isleapyear(year) then |
| 3576 |
day=day+1 |
| 3577 |
end |
| 3578 |
return day |
| 3579 |
end |
| 3580 |
end |
| 3581 |
os.nofdays=nofdays |
| 3582 |
function os.weekday(day,month,year) |
| 3583 |
return date("%w",time { year=year,month=month,day=day })+1 |
| 3584 |
end |
| 3585 |
function os.validdate(year,month,day) |
| 3586 |
if month<1 then |
| 3587 |
month=1 |
| 3588 |
elseif month>12 then |
| 3589 |
month=12 |
| 3590 |
end |
| 3591 |
if day<1 then |
| 3592 |
day=1 |
| 3593 |
else |
| 3594 |
local max=nofdays(year,month) |
| 3595 |
if day>max then |
| 3596 |
day=max |
| 3597 |
end |
| 3598 |
end |
| 3599 |
return year,month,day |
| 3600 |
end |
| 3601 |
function os.date(fmt,...) |
| 3602 |
if not fmt then |
| 3603 |
fmt="%Y-%m-%d %H:%M" |
| 3604 |
end |
| 3605 |
return date(fmt,...) |
| 3606 |
end |
| 3607 |
end |
| 3608 |
do |
| 3609 |
local osexit=os.exit |
| 3610 |
local exitcode=nil |
| 3611 |
function os.setexitcode(code) |
| 3612 |
exitcode=code |
| 3613 |
end |
| 3614 |
function os.exit(c) |
| 3615 |
if exitcode~=nil then |
| 3616 |
return osexit(exitcode) |
| 3617 |
end |
| 3618 |
if c~=nil then |
| 3619 |
return osexit(c) |
| 3620 |
end |
| 3621 |
return osexit() |
| 3622 |
end |
| 3623 |
end |
| 3624 |
|
| 3625 |
end -- closure |
| 3626 |
|
| 3627 |
do -- begin closure to overcome local limits and interference |
| 3628 |
|
| 3629 |
if not modules then modules={} end modules ['l-file']={ |
| 3630 |
version=1.001, |
| 3631 |
comment="companion to luat-lib.mkiv", |
| 3632 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3633 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3634 |
license="see context related readme files" |
| 3635 |
} |
| 3636 |
file=file or {} |
| 3637 |
local file=file |
| 3638 |
if not lfs then |
| 3639 |
lfs=optionalrequire("lfs") |
| 3640 |
end |
| 3641 |
local insert,concat=table.insert,table.concat |
| 3642 |
local match,find,gmatch=string.match,string.find,string.gmatch |
| 3643 |
local lpegmatch=lpeg.match |
| 3644 |
local getcurrentdir,attributes=lfs.currentdir,lfs.attributes |
| 3645 |
local checkedsplit=string.checkedsplit |
| 3646 |
local P,R,S,C,Cs,Cp,Cc,Ct=lpeg.P,lpeg.R,lpeg.S,lpeg.C,lpeg.Cs,lpeg.Cp,lpeg.Cc,lpeg.Ct |
| 3647 |
local attributes=lfs.attributes |
| 3648 |
function lfs.isdir(name) |
| 3649 |
if name then |
| 3650 |
return attributes(name,"mode")=="directory" |
| 3651 |
end |
| 3652 |
end |
| 3653 |
function lfs.isfile(name) |
| 3654 |
if name then |
| 3655 |
local a=attributes(name,"mode") |
| 3656 |
return a=="file" or a=="link" or nil |
| 3657 |
end |
| 3658 |
end |
| 3659 |
function lfs.isfound(name) |
| 3660 |
if name then |
| 3661 |
local a=attributes(name,"mode") |
| 3662 |
return (a=="file" or a=="link") and name or nil |
| 3663 |
end |
| 3664 |
end |
| 3665 |
function lfs.modification(name) |
| 3666 |
return name and attributes(name,"modification") or nil |
| 3667 |
end |
| 3668 |
if sandbox then |
| 3669 |
sandbox.redefine(lfs.isfile,"lfs.isfile") |
| 3670 |
sandbox.redefine(lfs.isdir,"lfs.isdir") |
| 3671 |
sandbox.redefine(lfs.isfound,"lfs.isfound") |
| 3672 |
end |
| 3673 |
local colon=P(":") |
| 3674 |
local period=P(".") |
| 3675 |
local periods=P("..") |
| 3676 |
local fwslash=P("/") |
| 3677 |
local bwslash=P("\\") |
| 3678 |
local slashes=S("\\/") |
| 3679 |
local noperiod=1-period |
| 3680 |
local noslashes=1-slashes |
| 3681 |
local name=noperiod^1 |
| 3682 |
local suffix=period/""*(1-period-slashes)^1*-1 |
| 3683 |
local pattern=C((1-(slashes^1*noslashes^1*-1))^1)*P(1) |
| 3684 |
local function pathpart(name,default) |
| 3685 |
return name and lpegmatch(pattern,name) or default or "" |
| 3686 |
end |
| 3687 |
local pattern=(noslashes^0*slashes)^1*C(noslashes^1)*-1 |
| 3688 |
local function basename(name) |
| 3689 |
return name and lpegmatch(pattern,name) or name |
| 3690 |
end |
| 3691 |
local pattern=(noslashes^0*slashes^1)^0*Cs((1-suffix)^1)*suffix^0 |
| 3692 |
local function nameonly(name) |
| 3693 |
return name and lpegmatch(pattern,name) or name |
| 3694 |
end |
| 3695 |
local pattern=(noslashes^0*slashes)^0*(noperiod^1*period)^1*C(noperiod^1)*-1 |
| 3696 |
local function suffixonly(name) |
| 3697 |
return name and lpegmatch(pattern,name) or "" |
| 3698 |
end |
| 3699 |
local pattern=(noslashes^0*slashes)^0*noperiod^1*((period*C(noperiod^1))^1)*-1+Cc("") |
| 3700 |
local function suffixesonly(name) |
| 3701 |
if name then |
| 3702 |
return lpegmatch(pattern,name) |
| 3703 |
else |
| 3704 |
return "" |
| 3705 |
end |
| 3706 |
end |
| 3707 |
file.pathpart=pathpart |
| 3708 |
file.basename=basename |
| 3709 |
file.nameonly=nameonly |
| 3710 |
file.suffixonly=suffixonly |
| 3711 |
file.suffix=suffixonly |
| 3712 |
file.suffixesonly=suffixesonly |
| 3713 |
file.suffixes=suffixesonly |
| 3714 |
file.dirname=pathpart |
| 3715 |
file.extname=suffixonly |
| 3716 |
local drive=C(R("az","AZ"))*colon |
| 3717 |
local path=C((noslashes^0*slashes)^0) |
| 3718 |
local suffix=period*C(P(1-period)^0*P(-1)) |
| 3719 |
local base=C((1-suffix)^0) |
| 3720 |
local rest=C(P(1)^0) |
| 3721 |
drive=drive+Cc("") |
| 3722 |
path=path+Cc("") |
| 3723 |
base=base+Cc("") |
| 3724 |
suffix=suffix+Cc("") |
| 3725 |
local pattern_a=drive*path*base*suffix |
| 3726 |
local pattern_b=path*base*suffix |
| 3727 |
local pattern_c=C(drive*path)*C(base*suffix) |
| 3728 |
local pattern_d=path*rest |
| 3729 |
function file.splitname(str,splitdrive) |
| 3730 |
if not str then |
| 3731 |
elseif splitdrive then |
| 3732 |
return lpegmatch(pattern_a,str) |
| 3733 |
else |
| 3734 |
return lpegmatch(pattern_b,str) |
| 3735 |
end |
| 3736 |
end |
| 3737 |
function file.splitbase(str) |
| 3738 |
if str then |
| 3739 |
return lpegmatch(pattern_d,str) |
| 3740 |
else |
| 3741 |
return "",str |
| 3742 |
end |
| 3743 |
end |
| 3744 |
function file.nametotable(str,splitdrive) |
| 3745 |
if str then |
| 3746 |
local path,drive,subpath,name,base,suffix=lpegmatch(pattern_c,str) |
| 3747 |
if splitdrive then |
| 3748 |
return { |
| 3749 |
path=path, |
| 3750 |
drive=drive, |
| 3751 |
subpath=subpath, |
| 3752 |
name=name, |
| 3753 |
base=base, |
| 3754 |
suffix=suffix, |
| 3755 |
} |
| 3756 |
else |
| 3757 |
return { |
| 3758 |
path=path, |
| 3759 |
name=name, |
| 3760 |
base=base, |
| 3761 |
suffix=suffix, |
| 3762 |
} |
| 3763 |
end |
| 3764 |
end |
| 3765 |
end |
| 3766 |
local pattern=Cs(((period*(1-period-slashes)^1*-1)/""+1)^1) |
| 3767 |
function file.removesuffix(name) |
| 3768 |
return name and lpegmatch(pattern,name) |
| 3769 |
end |
| 3770 |
local suffix=period/""*(1-period-slashes)^1*-1 |
| 3771 |
local pattern=Cs((noslashes^0*slashes^1)^0*((1-suffix)^1))*Cs(suffix) |
| 3772 |
function file.addsuffix(filename,suffix,criterium) |
| 3773 |
if not filename or not suffix or suffix=="" then |
| 3774 |
return filename |
| 3775 |
elseif criterium==true then |
| 3776 |
return filename.."."..suffix |
| 3777 |
elseif not criterium then |
| 3778 |
local n,s=lpegmatch(pattern,filename) |
| 3779 |
if not s or s=="" then |
| 3780 |
return filename.."."..suffix |
| 3781 |
else |
| 3782 |
return filename |
| 3783 |
end |
| 3784 |
else |
| 3785 |
local n,s=lpegmatch(pattern,filename) |
| 3786 |
if s and s~="" then |
| 3787 |
local t=type(criterium) |
| 3788 |
if t=="table" then |
| 3789 |
for i=1,#criterium do |
| 3790 |
if s==criterium[i] then |
| 3791 |
return filename |
| 3792 |
end |
| 3793 |
end |
| 3794 |
elseif t=="string" then |
| 3795 |
if s==criterium then |
| 3796 |
return filename |
| 3797 |
end |
| 3798 |
end |
| 3799 |
end |
| 3800 |
return (n or filename).."."..suffix |
| 3801 |
end |
| 3802 |
end |
| 3803 |
local suffix=period*(1-period-slashes)^1*-1 |
| 3804 |
local pattern=Cs((1-suffix)^0) |
| 3805 |
function file.replacesuffix(name,suffix) |
| 3806 |
if name and suffix and suffix~="" then |
| 3807 |
return lpegmatch(pattern,name).."."..suffix |
| 3808 |
else |
| 3809 |
return name |
| 3810 |
end |
| 3811 |
end |
| 3812 |
local reslasher=lpeg.replacer(P("\\"),"/") |
| 3813 |
function file.reslash(str) |
| 3814 |
return str and lpegmatch(reslasher,str) |
| 3815 |
end |
| 3816 |
if lfs.isreadablefile and lfs.iswritablefile then |
| 3817 |
file.is_readable=lfs.isreadablefile |
| 3818 |
file.is_writable=lfs.iswritablefile |
| 3819 |
else |
| 3820 |
function file.is_writable(name) |
| 3821 |
if not name then |
| 3822 |
elseif lfs.isdir(name) then |
| 3823 |
name=name.."/m_t_x_t_e_s_t.tmp" |
| 3824 |
local f=io.open(name,"wb") |
| 3825 |
if f then |
| 3826 |
f:close() |
| 3827 |
os.remove(name) |
| 3828 |
return true |
| 3829 |
end |
| 3830 |
elseif lfs.isfile(name) then |
| 3831 |
local f=io.open(name,"ab") |
| 3832 |
if f then |
| 3833 |
f:close() |
| 3834 |
return true |
| 3835 |
end |
| 3836 |
else |
| 3837 |
local f=io.open(name,"ab") |
| 3838 |
if f then |
| 3839 |
f:close() |
| 3840 |
os.remove(name) |
| 3841 |
return true |
| 3842 |
end |
| 3843 |
end |
| 3844 |
return false |
| 3845 |
end |
| 3846 |
local readable=P("r")*Cc(true) |
| 3847 |
function file.is_readable(name) |
| 3848 |
if name then |
| 3849 |
local a=attributes(name) |
| 3850 |
return a and lpegmatch(readable,a.permissions) or false |
| 3851 |
else |
| 3852 |
return false |
| 3853 |
end |
| 3854 |
end |
| 3855 |
end |
| 3856 |
file.isreadable=file.is_readable |
| 3857 |
file.iswritable=file.is_writable |
| 3858 |
function file.size(name) |
| 3859 |
if name then |
| 3860 |
local a=attributes(name) |
| 3861 |
return a and a.size or 0 |
| 3862 |
else |
| 3863 |
return 0 |
| 3864 |
end |
| 3865 |
end |
| 3866 |
function file.splitpath(str,separator) |
| 3867 |
return str and checkedsplit(lpegmatch(reslasher,str),separator or io.pathseparator) |
| 3868 |
end |
| 3869 |
function file.joinpath(tab,separator) |
| 3870 |
return tab and concat(tab,separator or io.pathseparator) |
| 3871 |
end |
| 3872 |
local someslash=S("\\/") |
| 3873 |
local stripper=Cs(P(fwslash)^0/""*reslasher) |
| 3874 |
local isnetwork=someslash*someslash*(1-someslash)+(1-fwslash-colon)^1*colon |
| 3875 |
local isroot=fwslash^1*-1 |
| 3876 |
local hasroot=fwslash^1 |
| 3877 |
local reslasher=lpeg.replacer(S("\\/"),"/") |
| 3878 |
local deslasher=lpeg.replacer(S("\\/")^1,"/") |
| 3879 |
function file.join(one,two,three,...) |
| 3880 |
if not two then |
| 3881 |
return one=="" and one or lpegmatch(reslasher,one) |
| 3882 |
end |
| 3883 |
if not one or one=="" then |
| 3884 |
return lpegmatch(stripper,three and concat({ two,three,... },"/") or two) |
| 3885 |
end |
| 3886 |
if lpegmatch(isnetwork,one) then |
| 3887 |
local one=lpegmatch(reslasher,one) |
| 3888 |
local two=lpegmatch(deslasher,three and concat({ two,three,... },"/") or two) |
| 3889 |
if lpegmatch(hasroot,two) then |
| 3890 |
return one..two |
| 3891 |
else |
| 3892 |
return one.."/"..two |
| 3893 |
end |
| 3894 |
elseif lpegmatch(isroot,one) then |
| 3895 |
local two=lpegmatch(deslasher,three and concat({ two,three,... },"/") or two) |
| 3896 |
if lpegmatch(hasroot,two) then |
| 3897 |
return two |
| 3898 |
else |
| 3899 |
return "/"..two |
| 3900 |
end |
| 3901 |
else |
| 3902 |
return lpegmatch(deslasher,concat({ one,two,three,... },"/")) |
| 3903 |
end |
| 3904 |
end |
| 3905 |
local drivespec=R("az","AZ")^1*colon |
| 3906 |
local anchors=fwslash+drivespec |
| 3907 |
local untouched=periods+(1-period)^1*P(-1) |
| 3908 |
local mswindrive=Cs(drivespec*(bwslash/"/"+fwslash)^0) |
| 3909 |
local mswinuncpath=(bwslash+fwslash)*(bwslash+fwslash)*Cc("//") |
| 3910 |
local splitstarter=(mswindrive+mswinuncpath+Cc(false))*Ct(lpeg.splitat(S("/\\")^1)) |
| 3911 |
local absolute=fwslash |
| 3912 |
function file.collapsepath(str,anchor) |
| 3913 |
if not str then |
| 3914 |
return |
| 3915 |
end |
| 3916 |
if anchor==true and not lpegmatch(anchors,str) then |
| 3917 |
str=getcurrentdir().."/"..str |
| 3918 |
end |
| 3919 |
if str=="" or str=="." then |
| 3920 |
return "." |
| 3921 |
elseif lpegmatch(untouched,str) then |
| 3922 |
return lpegmatch(reslasher,str) |
| 3923 |
end |
| 3924 |
local starter,oldelements=lpegmatch(splitstarter,str) |
| 3925 |
local newelements={} |
| 3926 |
local i=#oldelements |
| 3927 |
while i>0 do |
| 3928 |
local element=oldelements[i] |
| 3929 |
if element=='.' then |
| 3930 |
elseif element=='..' then |
| 3931 |
local n=i-1 |
| 3932 |
while n>0 do |
| 3933 |
local element=oldelements[n] |
| 3934 |
if element~='..' and element~='.' then |
| 3935 |
oldelements[n]='.' |
| 3936 |
break |
| 3937 |
else |
| 3938 |
n=n-1 |
| 3939 |
end |
| 3940 |
end |
| 3941 |
if n<1 then |
| 3942 |
insert(newelements,1,'..') |
| 3943 |
end |
| 3944 |
elseif element~="" then |
| 3945 |
insert(newelements,1,element) |
| 3946 |
end |
| 3947 |
i=i-1 |
| 3948 |
end |
| 3949 |
if #newelements==0 then |
| 3950 |
return starter or "." |
| 3951 |
elseif starter then |
| 3952 |
return starter..concat(newelements,'/') |
| 3953 |
elseif lpegmatch(absolute,str) then |
| 3954 |
return "/"..concat(newelements,'/') |
| 3955 |
else |
| 3956 |
newelements=concat(newelements,'/') |
| 3957 |
if anchor=="." and find(str,"^%./") then |
| 3958 |
return "./"..newelements |
| 3959 |
else |
| 3960 |
return newelements |
| 3961 |
end |
| 3962 |
end |
| 3963 |
end |
| 3964 |
local validchars=R("az","09","AZ","--","..") |
| 3965 |
local pattern_a=lpeg.replacer(1-validchars) |
| 3966 |
local pattern_a=Cs((validchars+P(1)/"-")^1) |
| 3967 |
local whatever=P("-")^0/"" |
| 3968 |
local pattern_b=Cs(whatever*(1-whatever*-1)^1) |
| 3969 |
function file.robustname(str,strict) |
| 3970 |
if str then |
| 3971 |
str=lpegmatch(pattern_a,str) or str |
| 3972 |
if strict then |
| 3973 |
return lpegmatch(pattern_b,str) or str |
| 3974 |
else |
| 3975 |
return str |
| 3976 |
end |
| 3977 |
end |
| 3978 |
end |
| 3979 |
local loaddata=io.loaddata |
| 3980 |
local savedata=io.savedata |
| 3981 |
file.readdata=loaddata |
| 3982 |
file.savedata=savedata |
| 3983 |
function file.copy(oldname,newname) |
| 3984 |
if oldname and newname then |
| 3985 |
local data=loaddata(oldname) |
| 3986 |
if data and data~="" then |
| 3987 |
savedata(newname,data) |
| 3988 |
end |
| 3989 |
end |
| 3990 |
end |
| 3991 |
local letter=R("az","AZ")+S("_-+") |
| 3992 |
local separator=P("://") |
| 3993 |
local qualified=period^0*fwslash+letter*colon+letter^1*separator+letter^1*fwslash |
| 3994 |
local rootbased=fwslash+letter*colon |
| 3995 |
lpeg.patterns.qualified=qualified |
| 3996 |
lpeg.patterns.rootbased=rootbased |
| 3997 |
function file.is_qualified_path(filename) |
| 3998 |
return filename and lpegmatch(qualified,filename)~=nil |
| 3999 |
end |
| 4000 |
function file.is_rootbased_path(filename) |
| 4001 |
return filename and lpegmatch(rootbased,filename)~=nil |
| 4002 |
end |
| 4003 |
function file.strip(name,dir) |
| 4004 |
if name then |
| 4005 |
local b,a=match(name,"^(.-)"..dir.."(.*)$") |
| 4006 |
return a~="" and a or name |
| 4007 |
end |
| 4008 |
end |
| 4009 |
function lfs.mkdirs(path) |
| 4010 |
local full="" |
| 4011 |
for sub in gmatch(path,"(/*[^\\/]+)") do |
| 4012 |
full=full..sub |
| 4013 |
lfs.mkdir(full) |
| 4014 |
end |
| 4015 |
end |
| 4016 |
function file.withinbase(path) |
| 4017 |
local l=0 |
| 4018 |
if not find(path,"^/") then |
| 4019 |
path="/"..path |
| 4020 |
end |
| 4021 |
for dir in gmatch(path,"/([^/]+)") do |
| 4022 |
if dir==".." then |
| 4023 |
l=l-1 |
| 4024 |
elseif dir~="." then |
| 4025 |
l=l+1 |
| 4026 |
end |
| 4027 |
if l<0 then |
| 4028 |
return false |
| 4029 |
end |
| 4030 |
end |
| 4031 |
return true |
| 4032 |
end |
| 4033 |
do |
| 4034 |
local symlinktarget=lfs.symlinktarget |
| 4035 |
local symlinkattributes=lfs.symlinkattributes |
| 4036 |
if symlinktarget then |
| 4037 |
function lfs.readlink(name) |
| 4038 |
local target=symlinktarget(name) |
| 4039 |
return name~=target and name or nil |
| 4040 |
end |
| 4041 |
elseif symlinkattributes then |
| 4042 |
function lfs.readlink(name) |
| 4043 |
return symlinkattributes(name,"target") or nil |
| 4044 |
end |
| 4045 |
else |
| 4046 |
function lfs.readlink(name) |
| 4047 |
return nil |
| 4048 |
end |
| 4049 |
end |
| 4050 |
end |
| 4051 |
|
| 4052 |
end -- closure |
| 4053 |
|
| 4054 |
do -- begin closure to overcome local limits and interference |
| 4055 |
|
| 4056 |
if not modules then modules={} end modules ['l-md5']={ |
| 4057 |
version=1.001, |
| 4058 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4059 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4060 |
license="see context related readme files" |
| 4061 |
} |
| 4062 |
if not md5 then |
| 4063 |
md5=optionalrequire("md5") |
| 4064 |
end |
| 4065 |
if not md5 then |
| 4066 |
md5={ |
| 4067 |
sum=function(str) print("error: md5 is not loaded (sum ignored)") return str end, |
| 4068 |
sumhexa=function(str) print("error: md5 is not loaded (sumhexa ignored)") return str end, |
| 4069 |
} |
| 4070 |
end |
| 4071 |
local md5,file=md5,file |
| 4072 |
local gsub=string.gsub |
| 4073 |
local modification,isfile,touch=lfs.modification,lfs.isfile,lfs.touch |
| 4074 |
local loaddata,savedata=io.loaddata,io.savedata |
| 4075 |
do |
| 4076 |
local patterns=lpeg and lpeg.patterns |
| 4077 |
if patterns then |
| 4078 |
local bytestoHEX=patterns.bytestoHEX |
| 4079 |
local bytestohex=patterns.bytestohex |
| 4080 |
local bytestodec=patterns.bytestodec |
| 4081 |
local lpegmatch=lpeg.match |
| 4082 |
local md5sum=md5.sum |
| 4083 |
if not md5.HEX then function md5.HEX(str) if str then return lpegmatch(bytestoHEX,md5sum(str)) end end end |
| 4084 |
if not md5.hex then function md5.hex(str) if str then return lpegmatch(bytestohex,md5sum(str)) end end end |
| 4085 |
if not md5.dec then function md5.dec(str) if str then return lpegmatch(bytestodec,md5sum(str)) end end end |
| 4086 |
md5.sumhexa=md5.hex |
| 4087 |
md5.sumHEXA=md5.HEX |
| 4088 |
end |
| 4089 |
end |
| 4090 |
local md5HEX=md5.HEX |
| 4091 |
function file.needsupdating(oldname,newname,threshold) |
| 4092 |
local oldtime=modification(oldname) |
| 4093 |
if oldtime then |
| 4094 |
local newtime=modification(newname) |
| 4095 |
if not newtime then |
| 4096 |
return true |
| 4097 |
elseif newtime>=oldtime then |
| 4098 |
return false |
| 4099 |
elseif oldtime-newtime<(threshold or 1) then |
| 4100 |
return false |
| 4101 |
else |
| 4102 |
return true |
| 4103 |
end |
| 4104 |
else |
| 4105 |
return false |
| 4106 |
end |
| 4107 |
end |
| 4108 |
file.needs_updating=file.needsupdating |
| 4109 |
function file.syncmtimes(oldname,newname) |
| 4110 |
local oldtime=modification(oldname) |
| 4111 |
if oldtime and isfile(newname) then |
| 4112 |
touch(newname,oldtime,oldtime) |
| 4113 |
end |
| 4114 |
end |
| 4115 |
local function checksum(name) |
| 4116 |
if md5 then |
| 4117 |
local data=loaddata(name) |
| 4118 |
if data then |
| 4119 |
return md5HEX(data) |
| 4120 |
end |
| 4121 |
end |
| 4122 |
return nil |
| 4123 |
end |
| 4124 |
file.checksum=checksum |
| 4125 |
function file.loadchecksum(name) |
| 4126 |
if md5 then |
| 4127 |
local data=loaddata(name..".md5") |
| 4128 |
return data and (gsub(data,"%s","")) |
| 4129 |
end |
| 4130 |
return nil |
| 4131 |
end |
| 4132 |
function file.savechecksum(name,checksum) |
| 4133 |
if not checksum then checksum=checksum(name) end |
| 4134 |
if checksum then |
| 4135 |
savedata(name..".md5",checksum) |
| 4136 |
return checksum |
| 4137 |
end |
| 4138 |
return nil |
| 4139 |
end |
| 4140 |
|
| 4141 |
end -- closure |
| 4142 |
|
| 4143 |
do -- begin closure to overcome local limits and interference |
| 4144 |
|
| 4145 |
if not modules then modules={} end modules ['l-dir']={ |
| 4146 |
version=1.001, |
| 4147 |
comment="companion to luat-lib.mkiv", |
| 4148 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4149 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4150 |
license="see context related readme files" |
| 4151 |
} |
| 4152 |
local type,select=type,select |
| 4153 |
local find,gmatch,match,gsub,sub=string.find,string.gmatch,string.match,string.gsub,string.sub |
| 4154 |
local concat,insert,remove,unpack=table.concat,table.insert,table.remove,table.unpack |
| 4155 |
local lpegmatch=lpeg.match |
| 4156 |
local P,S,R,C,Cc,Cs,Ct,Cv,V=lpeg.P,lpeg.S,lpeg.R,lpeg.C,lpeg.Cc,lpeg.Cs,lpeg.Ct,lpeg.Cv,lpeg.V |
| 4157 |
dir=dir or {} |
| 4158 |
local dir=dir |
| 4159 |
local lfs=lfs |
| 4160 |
local attributes=lfs.attributes |
| 4161 |
local scandir=lfs.dir |
| 4162 |
local isdir=lfs.isdir |
| 4163 |
local isfile=lfs.isfile |
| 4164 |
local currentdir=lfs.currentdir |
| 4165 |
local chdir=lfs.chdir |
| 4166 |
local mkdir=lfs.mkdir |
| 4167 |
local onwindows=os.type=="windows" or find(os.getenv("PATH"),";",1,true) |
| 4168 |
if onwindows then |
| 4169 |
local tricky=S("/\\")*P(-1) |
| 4170 |
isdir=function(name) |
| 4171 |
if lpegmatch(tricky,name) then |
| 4172 |
return attributes(name,"mode")=="directory" |
| 4173 |
else |
| 4174 |
return attributes(name.."/.","mode")=="directory" |
| 4175 |
end |
| 4176 |
end |
| 4177 |
isfile=function(name) |
| 4178 |
return attributes(name,"mode")=="file" |
| 4179 |
end |
| 4180 |
lfs.isdir=isdir |
| 4181 |
lfs.isfile=isfile |
| 4182 |
else |
| 4183 |
isdir=function(name) |
| 4184 |
return attributes(name,"mode")=="directory" |
| 4185 |
end |
| 4186 |
isfile=function(name) |
| 4187 |
return attributes(name,"mode")=="file" |
| 4188 |
end |
| 4189 |
lfs.isdir=isdir |
| 4190 |
lfs.isfile=isfile |
| 4191 |
end |
| 4192 |
local isreadable=file.isreadable |
| 4193 |
local walkdir=function(p,...) |
| 4194 |
if isreadable(p.."/.") then |
| 4195 |
return scandir(p,...) |
| 4196 |
else |
| 4197 |
return function() end |
| 4198 |
end |
| 4199 |
end |
| 4200 |
lfs.walkdir=walkdir |
| 4201 |
function dir.current() |
| 4202 |
return (gsub(currentdir(),"\\","/")) |
| 4203 |
end |
| 4204 |
local function glob_pattern_function(path,patt,recurse,action) |
| 4205 |
if isdir(path) then |
| 4206 |
local usedpath |
| 4207 |
if path=="/" then |
| 4208 |
usedpath="/." |
| 4209 |
elseif not find(path,"/$") then |
| 4210 |
usedpath=path.."/." |
| 4211 |
path=path.."/" |
| 4212 |
else |
| 4213 |
usedpath=path |
| 4214 |
end |
| 4215 |
local dirs |
| 4216 |
local nofdirs=0 |
| 4217 |
for name,mode,size,time in walkdir(usedpath) do |
| 4218 |
if name~="." and name~=".." then |
| 4219 |
local full=path..name |
| 4220 |
if mode==nil then |
| 4221 |
mode=attributes(full,'mode') |
| 4222 |
end |
| 4223 |
if mode=='file' then |
| 4224 |
if not patt or find(full,patt) then |
| 4225 |
action(full,size,time) |
| 4226 |
end |
| 4227 |
elseif recurse and mode=="directory" then |
| 4228 |
if dirs then |
| 4229 |
nofdirs=nofdirs+1 |
| 4230 |
dirs[nofdirs]=full |
| 4231 |
else |
| 4232 |
nofdirs=1 |
| 4233 |
dirs={ full } |
| 4234 |
end |
| 4235 |
end |
| 4236 |
end |
| 4237 |
end |
| 4238 |
if dirs then |
| 4239 |
for i=1,nofdirs do |
| 4240 |
glob_pattern_function(dirs[i],patt,recurse,action) |
| 4241 |
end |
| 4242 |
end |
| 4243 |
end |
| 4244 |
end |
| 4245 |
local function glob_pattern_table(path,patt,recurse,result) |
| 4246 |
if not result then |
| 4247 |
result={} |
| 4248 |
end |
| 4249 |
local usedpath |
| 4250 |
if path=="/" then |
| 4251 |
usedpath="/." |
| 4252 |
elseif not find(path,"/$") then |
| 4253 |
usedpath=path.."/." |
| 4254 |
path=path.."/" |
| 4255 |
else |
| 4256 |
usedpath=path |
| 4257 |
end |
| 4258 |
local dirs |
| 4259 |
local nofdirs=0 |
| 4260 |
local noffiles=#result |
| 4261 |
for name,mode in walkdir(usedpath) do |
| 4262 |
if name~="." and name~=".." then |
| 4263 |
local full=path..name |
| 4264 |
if mode==nil then |
| 4265 |
mode=attributes(full,'mode') |
| 4266 |
end |
| 4267 |
if mode=='file' then |
| 4268 |
if not patt or find(full,patt) then |
| 4269 |
noffiles=noffiles+1 |
| 4270 |
result[noffiles]=full |
| 4271 |
end |
| 4272 |
elseif recurse and mode=="directory" then |
| 4273 |
if dirs then |
| 4274 |
nofdirs=nofdirs+1 |
| 4275 |
dirs[nofdirs]=full |
| 4276 |
else |
| 4277 |
nofdirs=1 |
| 4278 |
dirs={ full } |
| 4279 |
end |
| 4280 |
end |
| 4281 |
end |
| 4282 |
end |
| 4283 |
if dirs then |
| 4284 |
for i=1,nofdirs do |
| 4285 |
glob_pattern_table(dirs[i],patt,recurse,result) |
| 4286 |
end |
| 4287 |
end |
| 4288 |
return result |
| 4289 |
end |
| 4290 |
local function globpattern(path,patt,recurse,method) |
| 4291 |
local kind=type(method) |
| 4292 |
if patt and sub(patt,1,-3)==path then |
| 4293 |
patt=false |
| 4294 |
end |
| 4295 |
local okay=isdir(path) |
| 4296 |
if kind=="function" then |
| 4297 |
return okay and glob_pattern_function(path,patt,recurse,method) or {} |
| 4298 |
elseif kind=="table" then |
| 4299 |
return okay and glob_pattern_table(path,patt,recurse,method) or method |
| 4300 |
else |
| 4301 |
return okay and glob_pattern_table(path,patt,recurse,{}) or {} |
| 4302 |
end |
| 4303 |
end |
| 4304 |
dir.globpattern=globpattern |
| 4305 |
local function collectpattern(path,patt,recurse,result) |
| 4306 |
local ok,scanner |
| 4307 |
result=result or {} |
| 4308 |
if path=="/" then |
| 4309 |
ok,scanner,first=xpcall(function() return walkdir(path..".") end,function() end) |
| 4310 |
else |
| 4311 |
ok,scanner,first=xpcall(function() return walkdir(path) end,function() end) |
| 4312 |
end |
| 4313 |
if ok and type(scanner)=="function" then |
| 4314 |
if not find(path,"/$") then |
| 4315 |
path=path..'/' |
| 4316 |
end |
| 4317 |
for name in scanner,first do |
| 4318 |
if name=="." then |
| 4319 |
elseif name==".." then |
| 4320 |
else |
| 4321 |
local full=path..name |
| 4322 |
local attr=attributes(full) |
| 4323 |
local mode=attr.mode |
| 4324 |
if mode=='file' then |
| 4325 |
if find(full,patt) then |
| 4326 |
result[name]=attr |
| 4327 |
end |
| 4328 |
elseif recurse and mode=="directory" then |
| 4329 |
attr.list=collectpattern(full,patt,recurse) |
| 4330 |
result[name]=attr |
| 4331 |
end |
| 4332 |
end |
| 4333 |
end |
| 4334 |
end |
| 4335 |
return result |
| 4336 |
end |
| 4337 |
dir.collectpattern=collectpattern |
| 4338 |
local separator,pattern |
| 4339 |
if onwindows then |
| 4340 |
local slash=S("/\\")/"/" |
| 4341 |
pattern={ |
| 4342 |
(Cs(P(".")+slash^1)+Cs(R("az","AZ")*P(":")*slash^0)+Cc("./"))*V(2)*V(3), |
| 4343 |
Cs(((1-S("*?/\\"))^0*slash)^0), |
| 4344 |
Cs(P(1)^0) |
| 4345 |
} |
| 4346 |
else |
| 4347 |
pattern={ |
| 4348 |
(C(P(".")+P("/")^1)+Cc("./"))*V(2)*V(3), |
| 4349 |
C(((1-S("*?/"))^0*P("/"))^0), |
| 4350 |
C(P(1)^0) |
| 4351 |
} |
| 4352 |
end |
| 4353 |
local filter=Cs (( |
| 4354 |
P("**")/".*"+P("*")/"[^/]*"+P("?")/"[^/]"+P(".")/"%%."+P("+")/"%%+"+P("-")/"%%-"+P(1) |
| 4355 |
)^0 ) |
| 4356 |
local function glob(str,t) |
| 4357 |
if type(t)=="function" then |
| 4358 |
if type(str)=="table" then |
| 4359 |
for s=1,#str do |
| 4360 |
glob(str[s],t) |
| 4361 |
end |
| 4362 |
elseif isfile(str) then |
| 4363 |
t(str) |
| 4364 |
else |
| 4365 |
local root,path,base=lpegmatch(pattern,str) |
| 4366 |
if root and path and base then |
| 4367 |
local recurse=find(base,"**",1,true) |
| 4368 |
local start=root..path |
| 4369 |
local result=lpegmatch(filter,start..base) |
| 4370 |
globpattern(start,result,recurse,t) |
| 4371 |
end |
| 4372 |
end |
| 4373 |
else |
| 4374 |
if type(str)=="table" then |
| 4375 |
local t=t or {} |
| 4376 |
for s=1,#str do |
| 4377 |
glob(str[s],t) |
| 4378 |
end |
| 4379 |
return t |
| 4380 |
elseif isfile(str) then |
| 4381 |
if t then |
| 4382 |
t[#t+1]=str |
| 4383 |
return t |
| 4384 |
else |
| 4385 |
return { str } |
| 4386 |
end |
| 4387 |
else |
| 4388 |
local root,path,base=lpegmatch(pattern,str) |
| 4389 |
if root and path and base then |
| 4390 |
local recurse=find(base,"**",1,true) |
| 4391 |
local start=root..path |
| 4392 |
local result=lpegmatch(filter,start..base) |
| 4393 |
return globpattern(start,result,recurse,t) |
| 4394 |
else |
| 4395 |
return {} |
| 4396 |
end |
| 4397 |
end |
| 4398 |
end |
| 4399 |
end |
| 4400 |
dir.glob=glob |
| 4401 |
local function globfiles(path,recurse,func,files) |
| 4402 |
if type(func)=="string" then |
| 4403 |
local s=func |
| 4404 |
func=function(name) return find(name,s) end |
| 4405 |
end |
| 4406 |
files=files or {} |
| 4407 |
local noffiles=#files |
| 4408 |
for name,mode in walkdir(path) do |
| 4409 |
if find(name,"^%.") then |
| 4410 |
else |
| 4411 |
if mode==nil then |
| 4412 |
mode=attributes(name,'mode') |
| 4413 |
end |
| 4414 |
if mode=="directory" then |
| 4415 |
if recurse then |
| 4416 |
globfiles(path.."/"..name,recurse,func,files) |
| 4417 |
end |
| 4418 |
elseif mode=="file" then |
| 4419 |
if not func or func(name) then |
| 4420 |
noffiles=noffiles+1 |
| 4421 |
files[noffiles]=path.."/"..name |
| 4422 |
end |
| 4423 |
end |
| 4424 |
end |
| 4425 |
end |
| 4426 |
return files |
| 4427 |
end |
| 4428 |
dir.globfiles=globfiles |
| 4429 |
local function globdirs(path,recurse,func,files) |
| 4430 |
if type(func)=="string" then |
| 4431 |
local s=func |
| 4432 |
func=function(name) return find(name,s) end |
| 4433 |
end |
| 4434 |
files=files or {} |
| 4435 |
local noffiles=#files |
| 4436 |
for name,mode in walkdir(path) do |
| 4437 |
if find(name,"^%.") then |
| 4438 |
else |
| 4439 |
if mode==nil then |
| 4440 |
mode=attributes(name,'mode') |
| 4441 |
end |
| 4442 |
if mode=="directory" then |
| 4443 |
if not func or func(name) then |
| 4444 |
noffiles=noffiles+1 |
| 4445 |
files[noffiles]=path.."/"..name |
| 4446 |
if recurse then |
| 4447 |
globdirs(path.."/"..name,recurse,func,files) |
| 4448 |
end |
| 4449 |
end |
| 4450 |
end |
| 4451 |
end |
| 4452 |
end |
| 4453 |
return files |
| 4454 |
end |
| 4455 |
dir.globdirs=globdirs |
| 4456 |
function dir.ls(pattern) |
| 4457 |
return concat(glob(pattern),"\n") |
| 4458 |
end |
| 4459 |
local make_indeed=true |
| 4460 |
if onwindows then |
| 4461 |
function dir.mkdirs(...) |
| 4462 |
local n=select("#",...) |
| 4463 |
local str |
| 4464 |
if n==1 then |
| 4465 |
str=select(1,...) |
| 4466 |
if isdir(str) then |
| 4467 |
return str,true |
| 4468 |
end |
| 4469 |
else |
| 4470 |
str="" |
| 4471 |
for i=1,n do |
| 4472 |
local s=select(i,...) |
| 4473 |
if s=="" then |
| 4474 |
elseif str=="" then |
| 4475 |
str=s |
| 4476 |
else |
| 4477 |
str=str.."/"..s |
| 4478 |
end |
| 4479 |
end |
| 4480 |
end |
| 4481 |
local pth="" |
| 4482 |
local drive=false |
| 4483 |
local first,middle,last=match(str,"^(//)(//*)(.*)$") |
| 4484 |
if first then |
| 4485 |
else |
| 4486 |
first,last=match(str,"^(//)/*(.-)$") |
| 4487 |
if first then |
| 4488 |
middle,last=match(str,"([^/]+)/+(.-)$") |
| 4489 |
if middle then |
| 4490 |
pth="//"..middle |
| 4491 |
else |
| 4492 |
pth="//"..last |
| 4493 |
last="" |
| 4494 |
end |
| 4495 |
else |
| 4496 |
first,middle,last=match(str,"^([a-zA-Z]:)(/*)(.-)$") |
| 4497 |
if first then |
| 4498 |
pth,drive=first..middle,true |
| 4499 |
else |
| 4500 |
middle,last=match(str,"^(/*)(.-)$") |
| 4501 |
if not middle then |
| 4502 |
last=str |
| 4503 |
end |
| 4504 |
end |
| 4505 |
end |
| 4506 |
end |
| 4507 |
for s in gmatch(last,"[^/]+") do |
| 4508 |
if pth=="" then |
| 4509 |
pth=s |
| 4510 |
elseif drive then |
| 4511 |
pth,drive=pth..s,false |
| 4512 |
else |
| 4513 |
pth=pth.."/"..s |
| 4514 |
end |
| 4515 |
if make_indeed and not isdir(pth) then |
| 4516 |
mkdir(pth) |
| 4517 |
end |
| 4518 |
end |
| 4519 |
return pth,(isdir(pth)==true) |
| 4520 |
end |
| 4521 |
else |
| 4522 |
function dir.mkdirs(...) |
| 4523 |
local n=select("#",...) |
| 4524 |
local str,pth |
| 4525 |
if n==1 then |
| 4526 |
str=select(1,...) |
| 4527 |
if isdir(str) then |
| 4528 |
return str,true |
| 4529 |
end |
| 4530 |
else |
| 4531 |
str="" |
| 4532 |
for i=1,n do |
| 4533 |
local s=select(i,...) |
| 4534 |
if s and s~="" then |
| 4535 |
if str~="" then |
| 4536 |
str=str.."/"..s |
| 4537 |
else |
| 4538 |
str=s |
| 4539 |
end |
| 4540 |
end |
| 4541 |
end |
| 4542 |
end |
| 4543 |
str=gsub(str,"/+","/") |
| 4544 |
if find(str,"^/") then |
| 4545 |
pth="/" |
| 4546 |
for s in gmatch(str,"[^/]+") do |
| 4547 |
local first=(pth=="/") |
| 4548 |
if first then |
| 4549 |
pth=pth..s |
| 4550 |
else |
| 4551 |
pth=pth.."/"..s |
| 4552 |
end |
| 4553 |
if make_indeed and not first and not isdir(pth) then |
| 4554 |
mkdir(pth) |
| 4555 |
end |
| 4556 |
end |
| 4557 |
else |
| 4558 |
pth="." |
| 4559 |
for s in gmatch(str,"[^/]+") do |
| 4560 |
pth=pth.."/"..s |
| 4561 |
if make_indeed and not isdir(pth) then |
| 4562 |
mkdir(pth) |
| 4563 |
end |
| 4564 |
end |
| 4565 |
end |
| 4566 |
return pth,(isdir(pth)==true) |
| 4567 |
end |
| 4568 |
end |
| 4569 |
dir.makedirs=dir.mkdirs |
| 4570 |
do |
| 4571 |
local chdir=sandbox and sandbox.original(chdir) or chdir |
| 4572 |
if onwindows then |
| 4573 |
local xcurrentdir=dir.current |
| 4574 |
function dir.expandname(str) |
| 4575 |
local first,nothing,last=match(str,"^(//)(//*)(.*)$") |
| 4576 |
if first then |
| 4577 |
first=xcurrentdir().."/" |
| 4578 |
end |
| 4579 |
if not first then |
| 4580 |
first,last=match(str,"^(//)/*(.*)$") |
| 4581 |
end |
| 4582 |
if not first then |
| 4583 |
first,last=match(str,"^([a-zA-Z]:)(.*)$") |
| 4584 |
if first and not find(last,"^/") then |
| 4585 |
local d=currentdir() |
| 4586 |
if chdir(first) then |
| 4587 |
first=xcurrentdir() |
| 4588 |
end |
| 4589 |
chdir(d) |
| 4590 |
end |
| 4591 |
end |
| 4592 |
if not first then |
| 4593 |
first,last=xcurrentdir(),str |
| 4594 |
end |
| 4595 |
last=gsub(last,"//","/") |
| 4596 |
last=gsub(last,"/%./","/") |
| 4597 |
last=gsub(last,"^/*","") |
| 4598 |
first=gsub(first,"/*$","") |
| 4599 |
if last=="" or last=="." then |
| 4600 |
return first |
| 4601 |
else |
| 4602 |
return first.."/"..last |
| 4603 |
end |
| 4604 |
end |
| 4605 |
else |
| 4606 |
function dir.expandname(str) |
| 4607 |
if not find(str,"^/") then |
| 4608 |
str=currentdir().."/"..str |
| 4609 |
end |
| 4610 |
str=gsub(str,"//","/") |
| 4611 |
str=gsub(str,"/%./","/") |
| 4612 |
str=gsub(str,"(.)/%.$","%1") |
| 4613 |
return str |
| 4614 |
end |
| 4615 |
end |
| 4616 |
function dir.expandlink(dir,report) |
| 4617 |
local curdir=currentdir() |
| 4618 |
local trace=type(report)=="function" |
| 4619 |
if chdir(dir) then |
| 4620 |
local newdir=currentdir() |
| 4621 |
if newdir~=dir and trace then |
| 4622 |
report("following symlink %a to %a",dir,newdir) |
| 4623 |
end |
| 4624 |
chdir(curdir) |
| 4625 |
return newdir |
| 4626 |
else |
| 4627 |
if trace then |
| 4628 |
report("unable to check path %a",dir) |
| 4629 |
end |
| 4630 |
return dir |
| 4631 |
end |
| 4632 |
end |
| 4633 |
end |
| 4634 |
file.expandname=dir.expandname |
| 4635 |
local stack={} |
| 4636 |
function dir.push(newdir) |
| 4637 |
local curdir=currentdir() |
| 4638 |
insert(stack,curdir) |
| 4639 |
if newdir and newdir~="" and chdir(newdir) then |
| 4640 |
return newdir |
| 4641 |
else |
| 4642 |
return curdir |
| 4643 |
end |
| 4644 |
end |
| 4645 |
function dir.pop() |
| 4646 |
local d=remove(stack) |
| 4647 |
if d then |
| 4648 |
chdir(d) |
| 4649 |
end |
| 4650 |
return d |
| 4651 |
end |
| 4652 |
local function found(...) |
| 4653 |
for i=1,select("#",...) do |
| 4654 |
local path=select(i,...) |
| 4655 |
local kind=type(path) |
| 4656 |
if kind=="string" then |
| 4657 |
if isdir(path) then |
| 4658 |
return path |
| 4659 |
end |
| 4660 |
elseif kind=="table" then |
| 4661 |
local path=found(unpack(path)) |
| 4662 |
if path then |
| 4663 |
return path |
| 4664 |
end |
| 4665 |
end |
| 4666 |
end |
| 4667 |
end |
| 4668 |
dir.found=found |
| 4669 |
|
| 4670 |
end -- closure |
| 4671 |
|
| 4672 |
do -- begin closure to overcome local limits and interference |
| 4673 |
|
| 4674 |
if not modules then modules={} end modules ['l-unicode']={ |
| 4675 |
version=1.001, |
| 4676 |
optimize=true, |
| 4677 |
comment="companion to luat-lib.mkiv", |
| 4678 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4679 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4680 |
license="see context related readme files" |
| 4681 |
} |
| 4682 |
utf=utf or {} |
| 4683 |
if not string.utfcharacters then |
| 4684 |
local gmatch=string.gmatch |
| 4685 |
function string.characters(str) |
| 4686 |
return gmatch(str,".[\128-\191]*") |
| 4687 |
end |
| 4688 |
end |
| 4689 |
utf.characters=string.utfcharacters |
| 4690 |
local type=type |
| 4691 |
local char,byte,format,sub,gmatch=string.char,string.byte,string.format,string.sub,string.gmatch |
| 4692 |
local concat=table.concat |
| 4693 |
local P,C,R,Cs,Ct,Cmt,Cc,Carg,Cp=lpeg.P,lpeg.C,lpeg.R,lpeg.Cs,lpeg.Ct,lpeg.Cmt,lpeg.Cc,lpeg.Carg,lpeg.Cp |
| 4694 |
local lpegmatch=lpeg.match |
| 4695 |
local patterns=lpeg.patterns |
| 4696 |
local tabletopattern=lpeg.utfchartabletopattern |
| 4697 |
local bytepairs=string.bytepairs |
| 4698 |
local finder=lpeg.finder |
| 4699 |
local replacer=lpeg.replacer |
| 4700 |
local p_utftype=patterns.utftype |
| 4701 |
local p_utfstricttype=patterns.utfstricttype |
| 4702 |
local p_utfoffset=patterns.utfoffset |
| 4703 |
local p_utf8character=patterns.utf8character |
| 4704 |
local p_utf8char=patterns.utf8char |
| 4705 |
local p_utf8byte=patterns.utf8byte |
| 4706 |
local p_utfbom=patterns.utfbom |
| 4707 |
local p_newline=patterns.newline |
| 4708 |
local p_whitespace=patterns.whitespace |
| 4709 |
if not utf.char then |
| 4710 |
utf.char=string.utfcharacter or (utf8 and utf8.char) |
| 4711 |
if not utf.char then |
| 4712 |
local char=string.char |
| 4713 |
if bit32 then |
| 4714 |
local rshift=bit32.rshift |
| 4715 |
function utf.char(n) |
| 4716 |
if n<0x80 then |
| 4717 |
return char(n) |
| 4718 |
elseif n<0x800 then |
| 4719 |
return char( |
| 4720 |
0xC0+rshift(n,6), |
| 4721 |
0x80+(n%0x40) |
| 4722 |
) |
| 4723 |
elseif n<0x10000 then |
| 4724 |
return char( |
| 4725 |
0xE0+rshift(n,12), |
| 4726 |
0x80+(rshift(n,6)%0x40), |
| 4727 |
0x80+(n%0x40) |
| 4728 |
) |
| 4729 |
elseif n<0x200000 then |
| 4730 |
return char( |
| 4731 |
0xF0+rshift(n,18), |
| 4732 |
0x80+(rshift(n,12)%0x40), |
| 4733 |
0x80+(rshift(n,6)%0x40), |
| 4734 |
0x80+(n%0x40) |
| 4735 |
) |
| 4736 |
else |
| 4737 |
return "" |
| 4738 |
end |
| 4739 |
end |
| 4740 |
else |
| 4741 |
local floor=math.floor |
| 4742 |
function utf.char(n) |
| 4743 |
if n<0x80 then |
| 4744 |
return char(n) |
| 4745 |
elseif n<0x800 then |
| 4746 |
return char( |
| 4747 |
0xC0+floor(n/0x40), |
| 4748 |
0x80+(n%0x40) |
| 4749 |
) |
| 4750 |
elseif n<0x10000 then |
| 4751 |
return char( |
| 4752 |
0xE0+floor(n/0x1000), |
| 4753 |
0x80+(floor(n/0x40)%0x40), |
| 4754 |
0x80+(n%0x40) |
| 4755 |
) |
| 4756 |
elseif n<0x200000 then |
| 4757 |
return char( |
| 4758 |
0xF0+floor(n/0x40000), |
| 4759 |
0x80+(floor(n/0x1000)%0x40), |
| 4760 |
0x80+(floor(n/0x40)%0x40), |
| 4761 |
0x80+(n%0x40) |
| 4762 |
) |
| 4763 |
else |
| 4764 |
return "" |
| 4765 |
end |
| 4766 |
end |
| 4767 |
end |
| 4768 |
end |
| 4769 |
end |
| 4770 |
if not utf.byte then |
| 4771 |
utf.byte=string.utfvalue or (utf8 and utf8.codepoint) |
| 4772 |
if not utf.byte then |
| 4773 |
function utf.byte(c) |
| 4774 |
return lpegmatch(p_utf8byte,c) |
| 4775 |
end |
| 4776 |
end |
| 4777 |
end |
| 4778 |
local utfchar,utfbyte=utf.char,utf.byte |
| 4779 |
function utf.filetype(data) |
| 4780 |
return data and lpegmatch(p_utftype,data) or "unknown" |
| 4781 |
end |
| 4782 |
local toentities=Cs ( |
| 4783 |
( |
| 4784 |
patterns.utf8one+( |
| 4785 |
patterns.utf8two+patterns.utf8three+patterns.utf8four |
| 4786 |
)/function(s) local b=utfbyte(s) if b<127 then return s else return format("&#%X;",b) end end |
| 4787 |
)^0 |
| 4788 |
) |
| 4789 |
patterns.toentities=toentities |
| 4790 |
function utf.toentities(str) |
| 4791 |
return lpegmatch(toentities,str) |
| 4792 |
end |
| 4793 |
local one=P(1) |
| 4794 |
local two=C(1)*C(1) |
| 4795 |
local four=C(R(utfchar(0xD8),utfchar(0xFF)))*C(1)*C(1)*C(1) |
| 4796 |
local pattern=P("\254\255")*Cs(( |
| 4797 |
four/function(a,b,c,d) |
| 4798 |
local ab=0xFF*byte(a)+byte(b) |
| 4799 |
local cd=0xFF*byte(c)+byte(d) |
| 4800 |
return utfchar((ab-0xD800)*0x400+(cd-0xDC00)+0x10000) |
| 4801 |
end+two/function(a,b) |
| 4802 |
return utfchar(byte(a)*256+byte(b)) |
| 4803 |
end+one |
| 4804 |
)^1 )+P("\255\254")*Cs(( |
| 4805 |
four/function(b,a,d,c) |
| 4806 |
local ab=0xFF*byte(a)+byte(b) |
| 4807 |
local cd=0xFF*byte(c)+byte(d) |
| 4808 |
return utfchar((ab-0xD800)*0x400+(cd-0xDC00)+0x10000) |
| 4809 |
end+two/function(b,a) |
| 4810 |
return utfchar(byte(a)*256+byte(b)) |
| 4811 |
end+one |
| 4812 |
)^1 ) |
| 4813 |
function string.toutf(s) |
| 4814 |
return lpegmatch(pattern,s) or s |
| 4815 |
end |
| 4816 |
local validatedutf=Cs ( |
| 4817 |
( |
| 4818 |
patterns.utf8one+patterns.utf8two+patterns.utf8three+patterns.utf8four+P(1)/"�" |
| 4819 |
)^0 |
| 4820 |
) |
| 4821 |
patterns.validatedutf=validatedutf |
| 4822 |
function utf.is_valid(str) |
| 4823 |
return type(str)=="string" and lpegmatch(validatedutf,str) or false |
| 4824 |
end |
| 4825 |
if not utf.len then |
| 4826 |
utf.len=string.utflength or (utf8 and utf8.len) |
| 4827 |
if not utf.len then |
| 4828 |
local n,f=0,1 |
| 4829 |
local utfcharcounter=patterns.utfbom^-1*Cmt ( |
| 4830 |
Cc(1)*patterns.utf8one^1+Cc(2)*patterns.utf8two^1+Cc(3)*patterns.utf8three^1+Cc(4)*patterns.utf8four^1, |
| 4831 |
function(_,t,d) |
| 4832 |
n=n+(t-f)/d |
| 4833 |
f=t |
| 4834 |
return true |
| 4835 |
end |
| 4836 |
)^0 |
| 4837 |
function utf.len(str) |
| 4838 |
n,f=0,1 |
| 4839 |
lpegmatch(utfcharcounter,str or "") |
| 4840 |
return n |
| 4841 |
end |
| 4842 |
end |
| 4843 |
end |
| 4844 |
utf.length=utf.len |
| 4845 |
if not utf.sub then |
| 4846 |
local utflength=utf.length |
| 4847 |
local b,e,n,first,last=0,0,0,0,0 |
| 4848 |
local function slide_zero(s,p) |
| 4849 |
n=n+1 |
| 4850 |
if n>=last then |
| 4851 |
e=p-1 |
| 4852 |
else |
| 4853 |
return p |
| 4854 |
end |
| 4855 |
end |
| 4856 |
local function slide_one(s,p) |
| 4857 |
n=n+1 |
| 4858 |
if n==first then |
| 4859 |
b=p |
| 4860 |
end |
| 4861 |
if n>=last then |
| 4862 |
e=p-1 |
| 4863 |
else |
| 4864 |
return p |
| 4865 |
end |
| 4866 |
end |
| 4867 |
local function slide_two(s,p) |
| 4868 |
n=n+1 |
| 4869 |
if n==first then |
| 4870 |
b=p |
| 4871 |
else |
| 4872 |
return true |
| 4873 |
end |
| 4874 |
end |
| 4875 |
local pattern_zero=Cmt(p_utf8character,slide_zero)^0 |
| 4876 |
local pattern_one=Cmt(p_utf8character,slide_one )^0 |
| 4877 |
local pattern_two=Cmt(p_utf8character,slide_two )^0 |
| 4878 |
local pattern_first=C(p_utf8character) |
| 4879 |
function utf.sub(str,start,stop) |
| 4880 |
if not start then |
| 4881 |
return str |
| 4882 |
end |
| 4883 |
if start==0 then |
| 4884 |
start=1 |
| 4885 |
end |
| 4886 |
if not stop then |
| 4887 |
if start<0 then |
| 4888 |
local l=utflength(str) |
| 4889 |
start=l+start |
| 4890 |
else |
| 4891 |
start=start-1 |
| 4892 |
end |
| 4893 |
b,n,first=0,0,start |
| 4894 |
lpegmatch(pattern_two,str) |
| 4895 |
if n>=first then |
| 4896 |
return sub(str,b) |
| 4897 |
else |
| 4898 |
return "" |
| 4899 |
end |
| 4900 |
end |
| 4901 |
if start<0 or stop<0 then |
| 4902 |
local l=utf.length(str) |
| 4903 |
if start<0 then |
| 4904 |
start=l+start |
| 4905 |
if start<=0 then |
| 4906 |
start=1 |
| 4907 |
else |
| 4908 |
start=start+1 |
| 4909 |
end |
| 4910 |
end |
| 4911 |
if stop<0 then |
| 4912 |
stop=l+stop |
| 4913 |
if stop==0 then |
| 4914 |
stop=1 |
| 4915 |
else |
| 4916 |
stop=stop+1 |
| 4917 |
end |
| 4918 |
end |
| 4919 |
end |
| 4920 |
if start==1 and stop==1 then |
| 4921 |
return lpegmatch(pattern_first,str) or "" |
| 4922 |
elseif start>stop then |
| 4923 |
return "" |
| 4924 |
elseif start>1 then |
| 4925 |
b,e,n,first,last=0,0,0,start-1,stop |
| 4926 |
lpegmatch(pattern_one,str) |
| 4927 |
if n>=first and e==0 then |
| 4928 |
e=#str |
| 4929 |
end |
| 4930 |
return sub(str,b,e) |
| 4931 |
else |
| 4932 |
b,e,n,last=1,0,0,stop |
| 4933 |
lpegmatch(pattern_zero,str) |
| 4934 |
if e==0 then |
| 4935 |
e=#str |
| 4936 |
end |
| 4937 |
return sub(str,b,e) |
| 4938 |
end |
| 4939 |
end |
| 4940 |
end |
| 4941 |
function utf.remapper(mapping,option,action) |
| 4942 |
local variant=type(mapping) |
| 4943 |
if variant=="table" then |
| 4944 |
action=action or mapping |
| 4945 |
if option=="dynamic" then |
| 4946 |
local pattern=false |
| 4947 |
table.setmetatablenewindex(mapping,function(t,k,v) rawset(t,k,v) pattern=false end) |
| 4948 |
return function(str) |
| 4949 |
if not str or str=="" then |
| 4950 |
return "" |
| 4951 |
else |
| 4952 |
if not pattern then |
| 4953 |
pattern=Cs((tabletopattern(mapping)/action+p_utf8character)^0) |
| 4954 |
end |
| 4955 |
return lpegmatch(pattern,str) |
| 4956 |
end |
| 4957 |
end |
| 4958 |
elseif option=="pattern" then |
| 4959 |
return Cs((tabletopattern(mapping)/action+p_utf8character)^0) |
| 4960 |
else |
| 4961 |
local pattern=Cs((tabletopattern(mapping)/action+p_utf8character)^0) |
| 4962 |
return function(str) |
| 4963 |
if not str or str=="" then |
| 4964 |
return "" |
| 4965 |
else |
| 4966 |
return lpegmatch(pattern,str) |
| 4967 |
end |
| 4968 |
end,pattern |
| 4969 |
end |
| 4970 |
elseif variant=="function" then |
| 4971 |
if option=="pattern" then |
| 4972 |
return Cs((p_utf8character/mapping+p_utf8character)^0) |
| 4973 |
else |
| 4974 |
local pattern=Cs((p_utf8character/mapping+p_utf8character)^0) |
| 4975 |
return function(str) |
| 4976 |
if not str or str=="" then |
| 4977 |
return "" |
| 4978 |
else |
| 4979 |
return lpegmatch(pattern,str) |
| 4980 |
end |
| 4981 |
end,pattern |
| 4982 |
end |
| 4983 |
else |
| 4984 |
return function(str) |
| 4985 |
return str or "" |
| 4986 |
end |
| 4987 |
end |
| 4988 |
end |
| 4989 |
function utf.replacer(t) |
| 4990 |
local r=replacer(t,false,false,true) |
| 4991 |
return function(str) |
| 4992 |
return lpegmatch(r,str) |
| 4993 |
end |
| 4994 |
end |
| 4995 |
function utf.subtituter(t) |
| 4996 |
local f=finder (t) |
| 4997 |
local r=replacer(t,false,false,true) |
| 4998 |
return function(str) |
| 4999 |
local i=lpegmatch(f,str) |
| 5000 |
if not i then |
| 5001 |
return str |
| 5002 |
elseif i>#str then |
| 5003 |
return str |
| 5004 |
else |
| 5005 |
return lpegmatch(r,str) |
| 5006 |
end |
| 5007 |
end |
| 5008 |
end |
| 5009 |
local utflinesplitter=p_utfbom^-1*lpeg.tsplitat(p_newline) |
| 5010 |
local utfcharsplitter_ows=p_utfbom^-1*Ct(C(p_utf8character)^0) |
| 5011 |
local utfcharsplitter_iws=p_utfbom^-1*Ct((p_whitespace^1+C(p_utf8character))^0) |
| 5012 |
local utfcharsplitter_raw=Ct(C(p_utf8character)^0) |
| 5013 |
patterns.utflinesplitter=utflinesplitter |
| 5014 |
function utf.splitlines(str) |
| 5015 |
return lpegmatch(utflinesplitter,str or "") |
| 5016 |
end |
| 5017 |
function utf.split(str,ignorewhitespace) |
| 5018 |
if ignorewhitespace then |
| 5019 |
return lpegmatch(utfcharsplitter_iws,str or "") |
| 5020 |
else |
| 5021 |
return lpegmatch(utfcharsplitter_ows,str or "") |
| 5022 |
end |
| 5023 |
end |
| 5024 |
function utf.totable(str) |
| 5025 |
return lpegmatch(utfcharsplitter_raw,str) |
| 5026 |
end |
| 5027 |
function utf.magic(f) |
| 5028 |
local str=f:read(4) or "" |
| 5029 |
local off=lpegmatch(p_utfoffset,str) |
| 5030 |
if off<4 then |
| 5031 |
f:seek('set',off) |
| 5032 |
end |
| 5033 |
return lpegmatch(p_utftype,str) |
| 5034 |
end |
| 5035 |
local utf16_to_utf8_be,utf16_to_utf8_le |
| 5036 |
local utf32_to_utf8_be,utf32_to_utf8_le |
| 5037 |
local utf_16_be_getbom=patterns.utfbom_16_be^-1 |
| 5038 |
local utf_16_le_getbom=patterns.utfbom_16_le^-1 |
| 5039 |
local utf_32_be_getbom=patterns.utfbom_32_be^-1 |
| 5040 |
local utf_32_le_getbom=patterns.utfbom_32_le^-1 |
| 5041 |
local utf_16_be_linesplitter=utf_16_be_getbom*lpeg.tsplitat(patterns.utf_16_be_nl) |
| 5042 |
local utf_16_le_linesplitter=utf_16_le_getbom*lpeg.tsplitat(patterns.utf_16_le_nl) |
| 5043 |
local utf_32_be_linesplitter=utf_32_be_getbom*lpeg.tsplitat(patterns.utf_32_be_nl) |
| 5044 |
local utf_32_le_linesplitter=utf_32_le_getbom*lpeg.tsplitat(patterns.utf_32_le_nl) |
| 5045 |
local more=0 |
| 5046 |
local p_utf16_to_utf8_be=C(1)*C(1)/function(left,right) |
| 5047 |
local now=256*byte(left)+byte(right) |
| 5048 |
if more>0 then |
| 5049 |
now=(more-0xD800)*0x400+(now-0xDC00)+0x10000 |
| 5050 |
more=0 |
| 5051 |
return utfchar(now) |
| 5052 |
elseif now>=0xD800 and now<=0xDBFF then |
| 5053 |
more=now |
| 5054 |
return "" |
| 5055 |
else |
| 5056 |
return utfchar(now) |
| 5057 |
end |
| 5058 |
end |
| 5059 |
local p_utf16_to_utf8_le=C(1)*C(1)/function(right,left) |
| 5060 |
local now=256*byte(left)+byte(right) |
| 5061 |
if more>0 then |
| 5062 |
now=(more-0xD800)*0x400+(now-0xDC00)+0x10000 |
| 5063 |
more=0 |
| 5064 |
return utfchar(now) |
| 5065 |
elseif now>=0xD800 and now<=0xDBFF then |
| 5066 |
more=now |
| 5067 |
return "" |
| 5068 |
else |
| 5069 |
return utfchar(now) |
| 5070 |
end |
| 5071 |
end |
| 5072 |
local p_utf32_to_utf8_be=C(1)*C(1)*C(1)*C(1)/function(a,b,c,d) |
| 5073 |
return utfchar(256*256*256*byte(a)+256*256*byte(b)+256*byte(c)+byte(d)) |
| 5074 |
end |
| 5075 |
local p_utf32_to_utf8_le=C(1)*C(1)*C(1)*C(1)/function(a,b,c,d) |
| 5076 |
return utfchar(256*256*256*byte(d)+256*256*byte(c)+256*byte(b)+byte(a)) |
| 5077 |
end |
| 5078 |
p_utf16_to_utf8_be=P(true)/function() more=0 end*utf_16_be_getbom*Cs(p_utf16_to_utf8_be^0) |
| 5079 |
p_utf16_to_utf8_le=P(true)/function() more=0 end*utf_16_le_getbom*Cs(p_utf16_to_utf8_le^0) |
| 5080 |
p_utf32_to_utf8_be=P(true)/function() more=0 end*utf_32_be_getbom*Cs(p_utf32_to_utf8_be^0) |
| 5081 |
p_utf32_to_utf8_le=P(true)/function() more=0 end*utf_32_le_getbom*Cs(p_utf32_to_utf8_le^0) |
| 5082 |
patterns.utf16_to_utf8_be=p_utf16_to_utf8_be |
| 5083 |
patterns.utf16_to_utf8_le=p_utf16_to_utf8_le |
| 5084 |
patterns.utf32_to_utf8_be=p_utf32_to_utf8_be |
| 5085 |
patterns.utf32_to_utf8_le=p_utf32_to_utf8_le |
| 5086 |
utf16_to_utf8_be=function(s) |
| 5087 |
if s and s~="" then |
| 5088 |
return lpegmatch(p_utf16_to_utf8_be,s) |
| 5089 |
else |
| 5090 |
return s |
| 5091 |
end |
| 5092 |
end |
| 5093 |
local utf16_to_utf8_be_t=function(t) |
| 5094 |
if not t then |
| 5095 |
return nil |
| 5096 |
elseif type(t)=="string" then |
| 5097 |
t=lpegmatch(utf_16_be_linesplitter,t) |
| 5098 |
end |
| 5099 |
for i=1,#t do |
| 5100 |
local s=t[i] |
| 5101 |
if s~="" then |
| 5102 |
t[i]=lpegmatch(p_utf16_to_utf8_be,s) |
| 5103 |
end |
| 5104 |
end |
| 5105 |
return t |
| 5106 |
end |
| 5107 |
utf16_to_utf8_le=function(s) |
| 5108 |
if s and s~="" then |
| 5109 |
return lpegmatch(p_utf16_to_utf8_le,s) |
| 5110 |
else |
| 5111 |
return s |
| 5112 |
end |
| 5113 |
end |
| 5114 |
local utf16_to_utf8_le_t=function(t) |
| 5115 |
if not t then |
| 5116 |
return nil |
| 5117 |
elseif type(t)=="string" then |
| 5118 |
t=lpegmatch(utf_16_le_linesplitter,t) |
| 5119 |
end |
| 5120 |
for i=1,#t do |
| 5121 |
local s=t[i] |
| 5122 |
if s~="" then |
| 5123 |
t[i]=lpegmatch(p_utf16_to_utf8_le,s) |
| 5124 |
end |
| 5125 |
end |
| 5126 |
return t |
| 5127 |
end |
| 5128 |
utf32_to_utf8_be=function(s) |
| 5129 |
if s and s~="" then |
| 5130 |
return lpegmatch(p_utf32_to_utf8_be,s) |
| 5131 |
else |
| 5132 |
return s |
| 5133 |
end |
| 5134 |
end |
| 5135 |
local utf32_to_utf8_be_t=function(t) |
| 5136 |
if not t then |
| 5137 |
return nil |
| 5138 |
elseif type(t)=="string" then |
| 5139 |
t=lpegmatch(utf_32_be_linesplitter,t) |
| 5140 |
end |
| 5141 |
for i=1,#t do |
| 5142 |
local s=t[i] |
| 5143 |
if s~="" then |
| 5144 |
t[i]=lpegmatch(p_utf32_to_utf8_be,s) |
| 5145 |
end |
| 5146 |
end |
| 5147 |
return t |
| 5148 |
end |
| 5149 |
utf32_to_utf8_le=function(s) |
| 5150 |
if s and s~="" then |
| 5151 |
return lpegmatch(p_utf32_to_utf8_le,s) |
| 5152 |
else |
| 5153 |
return s |
| 5154 |
end |
| 5155 |
end |
| 5156 |
local utf32_to_utf8_le_t=function(t) |
| 5157 |
if not t then |
| 5158 |
return nil |
| 5159 |
elseif type(t)=="string" then |
| 5160 |
t=lpegmatch(utf_32_le_linesplitter,t) |
| 5161 |
end |
| 5162 |
for i=1,#t do |
| 5163 |
local s=t[i] |
| 5164 |
if s~="" then |
| 5165 |
t[i]=lpegmatch(p_utf32_to_utf8_le,s) |
| 5166 |
end |
| 5167 |
end |
| 5168 |
return t |
| 5169 |
end |
| 5170 |
utf.utf16_to_utf8_le_t=utf16_to_utf8_le_t |
| 5171 |
utf.utf16_to_utf8_be_t=utf16_to_utf8_be_t |
| 5172 |
utf.utf32_to_utf8_le_t=utf32_to_utf8_le_t |
| 5173 |
utf.utf32_to_utf8_be_t=utf32_to_utf8_be_t |
| 5174 |
utf.utf16_to_utf8_le=utf16_to_utf8_le |
| 5175 |
utf.utf16_to_utf8_be=utf16_to_utf8_be |
| 5176 |
utf.utf32_to_utf8_le=utf32_to_utf8_le |
| 5177 |
utf.utf32_to_utf8_be=utf32_to_utf8_be |
| 5178 |
function utf.utf8_to_utf8_t(t) |
| 5179 |
return type(t)=="string" and lpegmatch(utflinesplitter,t) or t |
| 5180 |
end |
| 5181 |
function utf.utf16_to_utf8_t(t,endian) |
| 5182 |
return endian and utf16_to_utf8_be_t(t) or utf16_to_utf8_le_t(t) or t |
| 5183 |
end |
| 5184 |
function utf.utf32_to_utf8_t(t,endian) |
| 5185 |
return endian and utf32_to_utf8_be_t(t) or utf32_to_utf8_le_t(t) or t |
| 5186 |
end |
| 5187 |
if bit32 then |
| 5188 |
local rshift=bit32.rshift |
| 5189 |
local function little(b) |
| 5190 |
if b<0x10000 then |
| 5191 |
return char(b%256,rshift(b,8)) |
| 5192 |
else |
| 5193 |
b=b-0x10000 |
| 5194 |
local b1=rshift(b,10)+0xD800 |
| 5195 |
local b2=b%1024+0xDC00 |
| 5196 |
return char(b1%256,rshift(b1,8),b2%256,rshift(b2,8)) |
| 5197 |
end |
| 5198 |
end |
| 5199 |
local function big(b) |
| 5200 |
if b<0x10000 then |
| 5201 |
return char(rshift(b,8),b%256) |
| 5202 |
else |
| 5203 |
b=b-0x10000 |
| 5204 |
local b1=rshift(b,10)+0xD800 |
| 5205 |
local b2=b%1024+0xDC00 |
| 5206 |
return char(rshift(b1,8),b1%256,rshift(b2,8),b2%256) |
| 5207 |
end |
| 5208 |
end |
| 5209 |
local l_remap=Cs((p_utf8byte/little+P(1)/"")^0) |
| 5210 |
local b_remap=Cs((p_utf8byte/big+P(1)/"")^0) |
| 5211 |
local function utf8_to_utf16_be(str,nobom) |
| 5212 |
if nobom then |
| 5213 |
return lpegmatch(b_remap,str) |
| 5214 |
else |
| 5215 |
return char(254,255)..lpegmatch(b_remap,str) |
| 5216 |
end |
| 5217 |
end |
| 5218 |
local function utf8_to_utf16_le(str,nobom) |
| 5219 |
if nobom then |
| 5220 |
return lpegmatch(l_remap,str) |
| 5221 |
else |
| 5222 |
return char(255,254)..lpegmatch(l_remap,str) |
| 5223 |
end |
| 5224 |
end |
| 5225 |
utf.utf8_to_utf16_be=utf8_to_utf16_be |
| 5226 |
utf.utf8_to_utf16_le=utf8_to_utf16_le |
| 5227 |
function utf.utf8_to_utf16(str,littleendian,nobom) |
| 5228 |
if littleendian then |
| 5229 |
return utf8_to_utf16_le(str,nobom) |
| 5230 |
else |
| 5231 |
return utf8_to_utf16_be(str,nobom) |
| 5232 |
end |
| 5233 |
end |
| 5234 |
end |
| 5235 |
local pattern=Cs ( |
| 5236 |
(p_utf8byte/function(unicode ) return format("0x%04X",unicode) end)*(p_utf8byte*Carg(1)/function(unicode,separator) return format("%s0x%04X",separator,unicode) end)^0 |
| 5237 |
) |
| 5238 |
function utf.tocodes(str,separator) |
| 5239 |
return lpegmatch(pattern,str,1,separator or " ") |
| 5240 |
end |
| 5241 |
function utf.ustring(s) |
| 5242 |
return format("U+%05X",type(s)=="number" and s or utfbyte(s)) |
| 5243 |
end |
| 5244 |
function utf.xstring(s) |
| 5245 |
return format("0x%05X",type(s)=="number" and s or utfbyte(s)) |
| 5246 |
end |
| 5247 |
function utf.toeight(str) |
| 5248 |
if not str or str=="" then |
| 5249 |
return nil |
| 5250 |
end |
| 5251 |
local utftype=lpegmatch(p_utfstricttype,str) |
| 5252 |
if utftype=="utf-8" then |
| 5253 |
return sub(str,4) |
| 5254 |
elseif utftype=="utf-16-be" then |
| 5255 |
return utf16_to_utf8_be(str) |
| 5256 |
elseif utftype=="utf-16-le" then |
| 5257 |
return utf16_to_utf8_le(str) |
| 5258 |
else |
| 5259 |
return str |
| 5260 |
end |
| 5261 |
end |
| 5262 |
do |
| 5263 |
local p_nany=p_utf8character/"" |
| 5264 |
local cache={} |
| 5265 |
function utf.count(str,what) |
| 5266 |
if type(what)=="string" then |
| 5267 |
local p=cache[what] |
| 5268 |
if not p then |
| 5269 |
p=Cs((P(what)/" "+p_nany)^0) |
| 5270 |
cache[p]=p |
| 5271 |
end |
| 5272 |
return #lpegmatch(p,str) |
| 5273 |
else |
| 5274 |
return #lpegmatch(Cs((P(what)/" "+p_nany)^0),str) |
| 5275 |
end |
| 5276 |
end |
| 5277 |
end |
| 5278 |
if not string.utfvalues then |
| 5279 |
local find=string.find |
| 5280 |
local dummy=function() |
| 5281 |
end |
| 5282 |
function string.utfvalues(str) |
| 5283 |
local n=#str |
| 5284 |
if n==0 then |
| 5285 |
return dummy |
| 5286 |
elseif n==1 then |
| 5287 |
return function() return utfbyte(str) end |
| 5288 |
else |
| 5289 |
local p=1 |
| 5290 |
return function() |
| 5291 |
local b,e=find(str,".[\128-\191]*",p) |
| 5292 |
if b then |
| 5293 |
p=e+1 |
| 5294 |
return utfbyte(sub(str,b,e)) |
| 5295 |
end |
| 5296 |
end |
| 5297 |
end |
| 5298 |
end |
| 5299 |
end |
| 5300 |
utf.values=string.utfvalues |
| 5301 |
function utf.chrlen(u) |
| 5302 |
return |
| 5303 |
(u<0x80 and 1) or |
| 5304 |
(u<0xE0 and 2) or |
| 5305 |
(u<0xF0 and 3) or |
| 5306 |
(u<0xF8 and 4) or |
| 5307 |
(u<0xFC and 5) or |
| 5308 |
(u<0xFE and 6) or 0 |
| 5309 |
end |
| 5310 |
if bit32 then |
| 5311 |
local extract=bit32.extract |
| 5312 |
local char=string.char |
| 5313 |
function utf.toutf32string(n) |
| 5314 |
if n<=0xFF then |
| 5315 |
return |
| 5316 |
char(n).."\000\000\000" |
| 5317 |
elseif n<=0xFFFF then |
| 5318 |
return |
| 5319 |
char(extract(n,0,8))..char(extract(n,8,8)).."\000\000" |
| 5320 |
elseif n<=0xFFFFFF then |
| 5321 |
return |
| 5322 |
char(extract(n,0,8))..char(extract(n,8,8))..char(extract(n,16,8)).."\000" |
| 5323 |
else |
| 5324 |
return |
| 5325 |
char(extract(n,0,8))..char(extract(n,8,8))..char(extract(n,16,8))..char(extract(n,24,8)) |
| 5326 |
end |
| 5327 |
end |
| 5328 |
end |
| 5329 |
local len=utf.len |
| 5330 |
local rep=rep |
| 5331 |
function string.utfpadd(s,n) |
| 5332 |
if n and n~=0 then |
| 5333 |
local l=len(s) |
| 5334 |
if n>0 then |
| 5335 |
local d=n-l |
| 5336 |
if d>0 then |
| 5337 |
return rep(c or " ",d)..s |
| 5338 |
end |
| 5339 |
else |
| 5340 |
local d=- n-l |
| 5341 |
if d>0 then |
| 5342 |
return s..rep(c or " ",d) |
| 5343 |
end |
| 5344 |
end |
| 5345 |
end |
| 5346 |
return s |
| 5347 |
end |
| 5348 |
do |
| 5349 |
local utfcharacters=utf.characters or string.utfcharacters |
| 5350 |
local utfchar=utf.char or string.utfcharacter |
| 5351 |
lpeg.UP=P |
| 5352 |
if utfcharacters then |
| 5353 |
function lpeg.US(str) |
| 5354 |
local p=P(false) |
| 5355 |
for uc in utfcharacters(str) do |
| 5356 |
p=p+P(uc) |
| 5357 |
end |
| 5358 |
return p |
| 5359 |
end |
| 5360 |
else |
| 5361 |
function lpeg.US(str) |
| 5362 |
local p=P(false) |
| 5363 |
local f=function(uc) |
| 5364 |
p=p+P(uc) |
| 5365 |
end |
| 5366 |
lpegmatch((p_utf8char/f)^0,str) |
| 5367 |
return p |
| 5368 |
end |
| 5369 |
end |
| 5370 |
local range=p_utf8byte*p_utf8byte+Cc(false) |
| 5371 |
function lpeg.UR(str,more) |
| 5372 |
local first,last |
| 5373 |
if type(str)=="number" then |
| 5374 |
first=str |
| 5375 |
last=more or first |
| 5376 |
else |
| 5377 |
first,last=lpegmatch(range,str) |
| 5378 |
if not last then |
| 5379 |
return P(str) |
| 5380 |
end |
| 5381 |
end |
| 5382 |
if first==last then |
| 5383 |
return P(str) |
| 5384 |
end |
| 5385 |
if not utfchar then |
| 5386 |
utfchar=utf.char |
| 5387 |
end |
| 5388 |
if utfchar and (last-first<8) then |
| 5389 |
local p=P(false) |
| 5390 |
for i=first,last do |
| 5391 |
p=p+P(utfchar(i)) |
| 5392 |
end |
| 5393 |
return p |
| 5394 |
else |
| 5395 |
local f=function(b) |
| 5396 |
return b>=first and b<=last |
| 5397 |
end |
| 5398 |
return p_utf8byte/f |
| 5399 |
end |
| 5400 |
end |
| 5401 |
end |
| 5402 |
|
| 5403 |
end -- closure |
| 5404 |
|
| 5405 |
do -- begin closure to overcome local limits and interference |
| 5406 |
|
| 5407 |
if not modules then modules={} end modules ['l-url']={ |
| 5408 |
version=1.001, |
| 5409 |
comment="companion to luat-lib.mkiv", |
| 5410 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5411 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 5412 |
license="see context related readme files" |
| 5413 |
} |
| 5414 |
local char,format,byte=string.char,string.format,string.byte |
| 5415 |
local concat=table.concat |
| 5416 |
local tonumber,type,next=tonumber,type,next |
| 5417 |
local P,C,R,S,Cs,Cc,Ct,Cf,Cg,V=lpeg.P,lpeg.C,lpeg.R,lpeg.S,lpeg.Cs,lpeg.Cc,lpeg.Ct,lpeg.Cf,lpeg.Cg,lpeg.V |
| 5418 |
local lpegmatch,lpegpatterns,replacer=lpeg.match,lpeg.patterns,lpeg.replacer |
| 5419 |
local sortedhash=table.sortedhash |
| 5420 |
url=url or {} |
| 5421 |
local url=url |
| 5422 |
local unescapes={} |
| 5423 |
local escapes={} |
| 5424 |
setmetatable(unescapes,{ __index=function(t,k) |
| 5425 |
local v=char(tonumber(k,16)) |
| 5426 |
t[k]=v |
| 5427 |
return v |
| 5428 |
end }) |
| 5429 |
setmetatable(escapes,{ __index=function(t,k) |
| 5430 |
local v=format("%%%02X",byte(k)) |
| 5431 |
t[k]=v |
| 5432 |
return v |
| 5433 |
end }) |
| 5434 |
local colon=P(":") |
| 5435 |
local qmark=P("?") |
| 5436 |
local hash=P("#") |
| 5437 |
local slash=P("/") |
| 5438 |
local atsign=P("@") |
| 5439 |
local percent=P("%") |
| 5440 |
local endofstring=P(-1) |
| 5441 |
local hexdigit=R("09","AF","af") |
| 5442 |
local plus=P("+") |
| 5443 |
local nothing=Cc("") |
| 5444 |
local okay=R("09","AZ","az")+S("-_.,:=+*~!'()@&$") |
| 5445 |
local escapedchar=(percent*C(hexdigit*hexdigit))/unescapes |
| 5446 |
local unescapedchar=P(1)/escapes |
| 5447 |
local escaped=(plus/" ")+escapedchar |
| 5448 |
local noslash=P("/")/"" |
| 5449 |
local plustospace=P("+")/" " |
| 5450 |
local decoder=Cs(( |
| 5451 |
plustospace+escapedchar+P("\r\n")/"\n"+P(1) |
| 5452 |
)^0 ) |
| 5453 |
local encoder=Cs(( |
| 5454 |
R("09","AZ","az")^1+S("-./_")^1+P(" ")/"+"+P("\n")/"\r\n"+unescapedchar |
| 5455 |
)^0 ) |
| 5456 |
lpegpatterns.urldecoder=decoder |
| 5457 |
lpegpatterns.urlencoder=encoder |
| 5458 |
function url.decode (str) return str and lpegmatch(decoder,str) or str end |
| 5459 |
function url.encode (str) return str and lpegmatch(encoder,str) or str end |
| 5460 |
function url.unescape(str) return str and lpegmatch(unescaper,str) or str end |
| 5461 |
local schemestr=Cs((escaped+(1-colon-slash-qmark-hash))^2) |
| 5462 |
local authoritystr=Cs((escaped+(1- slash-qmark-hash))^0) |
| 5463 |
local pathstr=Cs((escaped+(1- qmark-hash))^0) |
| 5464 |
local querystr=Cs(((1- hash))^0) |
| 5465 |
local fragmentstr=Cs((escaped+(1- endofstring))^0) |
| 5466 |
local scheme=schemestr*colon+nothing |
| 5467 |
local authority=slash*slash*authoritystr+nothing |
| 5468 |
local path=slash*pathstr+nothing |
| 5469 |
local query=qmark*querystr+nothing |
| 5470 |
local fragment=hash*fragmentstr+nothing |
| 5471 |
local validurl=scheme*authority*path*query*fragment |
| 5472 |
local parser=Ct(validurl) |
| 5473 |
lpegpatterns.url=validurl |
| 5474 |
lpegpatterns.urlsplitter=parser |
| 5475 |
local escaper=Cs((R("09","AZ","az")^1+P(" ")/"%%20"+S("-./_:")^1+P(1)/escapes)^0) |
| 5476 |
local unescaper=Cs((escapedchar+1)^0) |
| 5477 |
local getcleaner=Cs((P("+++")/"%%2B"+P("+")/"%%20"+P(1))^1) |
| 5478 |
lpegpatterns.urlunescaped=escapedchar |
| 5479 |
lpegpatterns.urlescaper=escaper |
| 5480 |
lpegpatterns.urlunescaper=unescaper |
| 5481 |
lpegpatterns.urlgetcleaner=getcleaner |
| 5482 |
function url.unescapeget(str) |
| 5483 |
return lpegmatch(getcleaner,str) |
| 5484 |
end |
| 5485 |
local function split(str) |
| 5486 |
return (type(str)=="string" and lpegmatch(parser,str)) or str |
| 5487 |
end |
| 5488 |
local isscheme=schemestr*colon*slash*slash |
| 5489 |
local function hasscheme(str) |
| 5490 |
if str then |
| 5491 |
local scheme=lpegmatch(isscheme,str) |
| 5492 |
return scheme~="" and scheme or false |
| 5493 |
else |
| 5494 |
return false |
| 5495 |
end |
| 5496 |
end |
| 5497 |
local rootletter=R("az","AZ")+S("_-+") |
| 5498 |
local separator=P("://") |
| 5499 |
local qualified=P(".")^0*P("/")+rootletter*P(":")+rootletter^1*separator+rootletter^1*P("/") |
| 5500 |
local rootbased=P("/")+rootletter*P(":") |
| 5501 |
local barswapper=replacer("|",":") |
| 5502 |
local backslashswapper=replacer("\\","/") |
| 5503 |
local equal=P("=") |
| 5504 |
local amp=P("&") |
| 5505 |
local key=Cs(((plustospace+escapedchar+1)-equal )^0) |
| 5506 |
local value=Cs(((plustospace+escapedchar+1)-amp-endofstring)^0) |
| 5507 |
local splitquery=Cf (Ct("")*P { "sequence", |
| 5508 |
sequence=V("pair")*(amp*V("pair"))^0, |
| 5509 |
pair=Cg(key*equal*value), |
| 5510 |
},rawset) |
| 5511 |
local userpart=(1-atsign-colon)^1 |
| 5512 |
local serverpart=(1-colon)^1 |
| 5513 |
local splitauthority=((Cs(userpart)*colon*Cs(userpart)+Cs(userpart)*Cc(nil))*atsign+Cc(nil)*Cc(nil))*Cs(serverpart)*(colon*(serverpart/tonumber)+Cc(nil)) |
| 5514 |
local function hashed(str) |
| 5515 |
if not str or str=="" then |
| 5516 |
return { |
| 5517 |
scheme="invalid", |
| 5518 |
original=str, |
| 5519 |
} |
| 5520 |
end |
| 5521 |
local detailed=split(str) |
| 5522 |
local rawscheme="" |
| 5523 |
local rawquery="" |
| 5524 |
local somescheme=false |
| 5525 |
local somequery=false |
| 5526 |
if detailed then |
| 5527 |
rawscheme=detailed[1] |
| 5528 |
rawquery=detailed[4] |
| 5529 |
somescheme=rawscheme~="" |
| 5530 |
somequery=rawquery~="" |
| 5531 |
end |
| 5532 |
if not somescheme and not somequery then |
| 5533 |
return { |
| 5534 |
scheme="file", |
| 5535 |
authority="", |
| 5536 |
path=str, |
| 5537 |
query="", |
| 5538 |
fragment="", |
| 5539 |
original=str, |
| 5540 |
noscheme=true, |
| 5541 |
filename=str, |
| 5542 |
} |
| 5543 |
end |
| 5544 |
local authority=detailed[2] |
| 5545 |
local path=detailed[3] |
| 5546 |
local filename |
| 5547 |
local username |
| 5548 |
local password |
| 5549 |
local host |
| 5550 |
local port |
| 5551 |
if authority~="" then |
| 5552 |
username,password,host,port=lpegmatch(splitauthority,authority) |
| 5553 |
end |
| 5554 |
if authority=="" then |
| 5555 |
filename=path |
| 5556 |
elseif path=="" then |
| 5557 |
filename="" |
| 5558 |
else |
| 5559 |
filename=authority.."/"..path |
| 5560 |
end |
| 5561 |
return { |
| 5562 |
scheme=rawscheme, |
| 5563 |
authority=authority, |
| 5564 |
path=path, |
| 5565 |
query=lpegmatch(unescaper,rawquery), |
| 5566 |
queries=lpegmatch(splitquery,rawquery), |
| 5567 |
fragment=detailed[5], |
| 5568 |
original=str, |
| 5569 |
noscheme=false, |
| 5570 |
filename=filename, |
| 5571 |
host=host, |
| 5572 |
port=port, |
| 5573 |
} |
| 5574 |
end |
| 5575 |
url.split=split |
| 5576 |
url.hasscheme=hasscheme |
| 5577 |
url.hashed=hashed |
| 5578 |
function url.addscheme(str,scheme) |
| 5579 |
if hasscheme(str) then |
| 5580 |
return str |
| 5581 |
elseif not scheme then |
| 5582 |
return "file:///"..str |
| 5583 |
else |
| 5584 |
return scheme..":///"..str |
| 5585 |
end |
| 5586 |
end |
| 5587 |
function url.construct(hash) |
| 5588 |
local result,r={},0 |
| 5589 |
local scheme=hash.scheme |
| 5590 |
local authority=hash.authority |
| 5591 |
local path=hash.path |
| 5592 |
local queries=hash.queries |
| 5593 |
local fragment=hash.fragment |
| 5594 |
if scheme and scheme~="" then |
| 5595 |
r=r+1;result[r]=lpegmatch(escaper,scheme) |
| 5596 |
r=r+1;result[r]="://" |
| 5597 |
end |
| 5598 |
if authority and authority~="" then |
| 5599 |
r=r+1;result[r]=lpegmatch(escaper,authority) |
| 5600 |
end |
| 5601 |
if path and path~="" then |
| 5602 |
r=r+1;result[r]="/" |
| 5603 |
r=r+1;result[r]=lpegmatch(escaper,path) |
| 5604 |
end |
| 5605 |
if queries then |
| 5606 |
local done=false |
| 5607 |
for k,v in sortedhash(queries) do |
| 5608 |
r=r+1;result[r]=done and "&" or "?" |
| 5609 |
r=r+1;result[r]=lpegmatch(escaper,k) |
| 5610 |
r=r+1;result[r]="=" |
| 5611 |
r=r+1;result[r]=lpegmatch(escaper,v) |
| 5612 |
done=true |
| 5613 |
end |
| 5614 |
end |
| 5615 |
if fragment and fragment~="" then |
| 5616 |
r=r+1;result[r]="#" |
| 5617 |
r=r+1;result[r]=lpegmatch(escaper,fragment) |
| 5618 |
end |
| 5619 |
return concat(result) |
| 5620 |
end |
| 5621 |
local pattern=Cs(slash^-1/""*R("az","AZ")*((S(":|")/":")+P(":"))*slash*P(1)^0) |
| 5622 |
function url.filename(filename) |
| 5623 |
local spec=hashed(filename) |
| 5624 |
local path=spec.path |
| 5625 |
return (spec.scheme=="file" and path and lpegmatch(pattern,path)) or filename |
| 5626 |
end |
| 5627 |
local function escapestring(str) |
| 5628 |
return lpegmatch(escaper,str) |
| 5629 |
end |
| 5630 |
url.escape=escapestring |
| 5631 |
function url.query(str) |
| 5632 |
if type(str)=="string" then |
| 5633 |
return lpegmatch(splitquery,str) or "" |
| 5634 |
else |
| 5635 |
return str |
| 5636 |
end |
| 5637 |
end |
| 5638 |
function url.toquery(data) |
| 5639 |
local td=type(data) |
| 5640 |
if td=="string" then |
| 5641 |
return #str and escape(data) or nil |
| 5642 |
elseif td=="table" then |
| 5643 |
if next(data) then |
| 5644 |
local t={} |
| 5645 |
for k,v in next,data do |
| 5646 |
t[#t+1]=format("%s=%s",k,escapestring(v)) |
| 5647 |
end |
| 5648 |
return concat(t,"&") |
| 5649 |
end |
| 5650 |
else |
| 5651 |
end |
| 5652 |
end |
| 5653 |
local pattern=Cs(noslash^0*(1-noslash*P(-1))^0) |
| 5654 |
function url.barepath(path) |
| 5655 |
if not path or path=="" then |
| 5656 |
return "" |
| 5657 |
else |
| 5658 |
return lpegmatch(pattern,path) |
| 5659 |
end |
| 5660 |
end |
| 5661 |
|
| 5662 |
end -- closure |
| 5663 |
|
| 5664 |
do -- begin closure to overcome local limits and interference |
| 5665 |
|
| 5666 |
if not modules then modules={} end modules ['l-set']={ |
| 5667 |
version=1.001, |
| 5668 |
comment="companion to luat-lib.mkiv", |
| 5669 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5670 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 5671 |
license="see context related readme files" |
| 5672 |
} |
| 5673 |
set=set or {} |
| 5674 |
local nums={} |
| 5675 |
local tabs={} |
| 5676 |
local concat=table.concat |
| 5677 |
local next,type=next,type |
| 5678 |
set.create=table.tohash |
| 5679 |
function set.tonumber(t) |
| 5680 |
if next(t) then |
| 5681 |
local s="" |
| 5682 |
for k,v in next,t do |
| 5683 |
if v then |
| 5684 |
s=s.." "..k |
| 5685 |
end |
| 5686 |
end |
| 5687 |
local n=nums[s] |
| 5688 |
if not n then |
| 5689 |
n=#tabs+1 |
| 5690 |
tabs[n]=t |
| 5691 |
nums[s]=n |
| 5692 |
end |
| 5693 |
return n |
| 5694 |
else |
| 5695 |
return 0 |
| 5696 |
end |
| 5697 |
end |
| 5698 |
function set.totable(n) |
| 5699 |
if n==0 then |
| 5700 |
return {} |
| 5701 |
else |
| 5702 |
return tabs[n] or {} |
| 5703 |
end |
| 5704 |
end |
| 5705 |
function set.tolist(n) |
| 5706 |
if n==0 or not tabs[n] then |
| 5707 |
return "" |
| 5708 |
else |
| 5709 |
local t,n={},0 |
| 5710 |
for k,v in next,tabs[n] do |
| 5711 |
if v then |
| 5712 |
n=n+1 |
| 5713 |
t[n]=k |
| 5714 |
end |
| 5715 |
end |
| 5716 |
return concat(t," ") |
| 5717 |
end |
| 5718 |
end |
| 5719 |
function set.contains(n,s) |
| 5720 |
if type(n)=="table" then |
| 5721 |
return n[s] |
| 5722 |
elseif n==0 then |
| 5723 |
return false |
| 5724 |
else |
| 5725 |
local t=tabs[n] |
| 5726 |
return t and t[s] |
| 5727 |
end |
| 5728 |
end |
| 5729 |
|
| 5730 |
end -- closure |