| 1 |
-- merged file : lualibs-extended-merged.lua |
| 2 |
-- parent file : lualibs-extended.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 ['util-sac']={ |
| 8 |
version=1.001, |
| 9 |
optimize=true, |
| 10 |
comment="companion to luat-lib.mkiv", |
| 11 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 12 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 13 |
license="see context related readme files" |
| 14 |
} |
| 15 |
local byte,sub=string.byte,string.sub |
| 16 |
local tonumber=tonumber |
| 17 |
utilities=utilities or {} |
| 18 |
local streams={} |
| 19 |
utilities.streams=streams |
| 20 |
function streams.open(filename,zerobased) |
| 21 |
local f=filename and io.loaddata(filename) |
| 22 |
if f then |
| 23 |
return { f,1,#f,zerobased or false } |
| 24 |
end |
| 25 |
end |
| 26 |
function streams.openstring(f,zerobased) |
| 27 |
if f then |
| 28 |
return { f,1,#f,zerobased or false } |
| 29 |
end |
| 30 |
end |
| 31 |
function streams.getstring(f) |
| 32 |
if f then |
| 33 |
return f[1] |
| 34 |
end |
| 35 |
end |
| 36 |
function streams.close() |
| 37 |
end |
| 38 |
function streams.size(f) |
| 39 |
return f and f[3] or 0 |
| 40 |
end |
| 41 |
streams.getsize=streams.size |
| 42 |
function streams.setposition(f,i) |
| 43 |
if f[4] then |
| 44 |
if i<=0 then |
| 45 |
f[2]=1 |
| 46 |
else |
| 47 |
f[2]=i+1 |
| 48 |
end |
| 49 |
else |
| 50 |
if i<=1 then |
| 51 |
f[2]=1 |
| 52 |
else |
| 53 |
f[2]=i |
| 54 |
end |
| 55 |
end |
| 56 |
end |
| 57 |
function streams.getposition(f) |
| 58 |
if f[4] then |
| 59 |
return f[2]-1 |
| 60 |
else |
| 61 |
return f[2] |
| 62 |
end |
| 63 |
end |
| 64 |
function streams.look(f,n,chars) |
| 65 |
local b=f[2] |
| 66 |
local e=b+n-1 |
| 67 |
if chars then |
| 68 |
return sub(f[1],b,e) |
| 69 |
else |
| 70 |
return byte(f[1],b,e) |
| 71 |
end |
| 72 |
end |
| 73 |
function streams.skip(f,n) |
| 74 |
f[2]=f[2]+n |
| 75 |
end |
| 76 |
function streams.readbyte(f) |
| 77 |
local i=f[2] |
| 78 |
f[2]=i+1 |
| 79 |
return byte(f[1],i) |
| 80 |
end |
| 81 |
function streams.readbytes(f,n) |
| 82 |
local i=f[2] |
| 83 |
local j=i+n |
| 84 |
f[2]=j |
| 85 |
return byte(f[1],i,j-1) |
| 86 |
end |
| 87 |
function streams.readbytetable(f,n) |
| 88 |
local i=f[2] |
| 89 |
local j=i+n |
| 90 |
f[2]=j |
| 91 |
return { byte(f[1],i,j-1) } |
| 92 |
end |
| 93 |
function streams.skipbytes(f,n) |
| 94 |
f[2]=f[2]+n |
| 95 |
end |
| 96 |
function streams.readchar(f) |
| 97 |
local i=f[2] |
| 98 |
f[2]=i+1 |
| 99 |
return sub(f[1],i,i) |
| 100 |
end |
| 101 |
function streams.readstring(f,n) |
| 102 |
local i=f[2] |
| 103 |
local j=i+n |
| 104 |
f[2]=j |
| 105 |
return sub(f[1],i,j-1) |
| 106 |
end |
| 107 |
function streams.readinteger1(f) |
| 108 |
local i=f[2] |
| 109 |
f[2]=i+1 |
| 110 |
local n=byte(f[1],i) |
| 111 |
if n>=0x80 then |
| 112 |
return n-0x100 |
| 113 |
else |
| 114 |
return n |
| 115 |
end |
| 116 |
end |
| 117 |
streams.readcardinal1=streams.readbyte |
| 118 |
streams.readcardinal=streams.readcardinal1 |
| 119 |
streams.readinteger=streams.readinteger1 |
| 120 |
function streams.readcardinal2(f) |
| 121 |
local i=f[2] |
| 122 |
local j=i+1 |
| 123 |
f[2]=j+1 |
| 124 |
local a,b=byte(f[1],i,j) |
| 125 |
return 0x100*a+b |
| 126 |
end |
| 127 |
function streams.readcardinal2le(f) |
| 128 |
local i=f[2] |
| 129 |
local j=i+1 |
| 130 |
f[2]=j+1 |
| 131 |
local b,a=byte(f[1],i,j) |
| 132 |
return 0x100*a+b |
| 133 |
end |
| 134 |
function streams.readinteger2(f) |
| 135 |
local i=f[2] |
| 136 |
local j=i+1 |
| 137 |
f[2]=j+1 |
| 138 |
local a,b=byte(f[1],i,j) |
| 139 |
if a>=0x80 then |
| 140 |
return 0x100*a+b-0x10000 |
| 141 |
else |
| 142 |
return 0x100*a+b |
| 143 |
end |
| 144 |
end |
| 145 |
function streams.readinteger2le(f) |
| 146 |
local i=f[2] |
| 147 |
local j=i+1 |
| 148 |
f[2]=j+1 |
| 149 |
local b,a=byte(f[1],i,j) |
| 150 |
if a>=0x80 then |
| 151 |
return 0x100*a+b-0x10000 |
| 152 |
else |
| 153 |
return 0x100*a+b |
| 154 |
end |
| 155 |
end |
| 156 |
function streams.readcardinal3(f) |
| 157 |
local i=f[2] |
| 158 |
local j=i+2 |
| 159 |
f[2]=j+1 |
| 160 |
local a,b,c=byte(f[1],i,j) |
| 161 |
return 0x10000*a+0x100*b+c |
| 162 |
end |
| 163 |
function streams.readcardinal3le(f) |
| 164 |
local i=f[2] |
| 165 |
local j=i+2 |
| 166 |
f[2]=j+1 |
| 167 |
local c,b,a=byte(f[1],i,j) |
| 168 |
return 0x10000*a+0x100*b+c |
| 169 |
end |
| 170 |
function streams.readinteger3(f) |
| 171 |
local i=f[2] |
| 172 |
local j=i+3 |
| 173 |
f[2]=j+1 |
| 174 |
local a,b,c=byte(f[1],i,j) |
| 175 |
if a>=0x80 then |
| 176 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 177 |
else |
| 178 |
return 0x10000*a+0x100*b+c |
| 179 |
end |
| 180 |
end |
| 181 |
function streams.readinteger3le(f) |
| 182 |
local i=f[2] |
| 183 |
local j=i+3 |
| 184 |
f[2]=j+1 |
| 185 |
local c,b,a=byte(f[1],i,j) |
| 186 |
if a>=0x80 then |
| 187 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 188 |
else |
| 189 |
return 0x10000*a+0x100*b+c |
| 190 |
end |
| 191 |
end |
| 192 |
function streams.readcardinal4(f) |
| 193 |
local i=f[2] |
| 194 |
local j=i+3 |
| 195 |
f[2]=j+1 |
| 196 |
local a,b,c,d=byte(f[1],i,j) |
| 197 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 198 |
end |
| 199 |
function streams.readcardinal4le(f) |
| 200 |
local i=f[2] |
| 201 |
local j=i+3 |
| 202 |
f[2]=j+1 |
| 203 |
local d,c,b,a=byte(f[1],i,j) |
| 204 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 205 |
end |
| 206 |
function streams.readinteger4(f) |
| 207 |
local i=f[2] |
| 208 |
local j=i+3 |
| 209 |
f[2]=j+1 |
| 210 |
local a,b,c,d=byte(f[1],i,j) |
| 211 |
if a>=0x80 then |
| 212 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 213 |
else |
| 214 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 215 |
end |
| 216 |
end |
| 217 |
function streams.readinteger4le(f) |
| 218 |
local i=f[2] |
| 219 |
local j=i+3 |
| 220 |
f[2]=j+1 |
| 221 |
local d,c,b,a=byte(f[1],i,j) |
| 222 |
if a>=0x80 then |
| 223 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 224 |
else |
| 225 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 226 |
end |
| 227 |
end |
| 228 |
function streams.readfixed2(f) |
| 229 |
local i=f[2] |
| 230 |
local j=i+1 |
| 231 |
f[2]=j+1 |
| 232 |
local n1,n2=byte(f[1],i,j) |
| 233 |
if n1>=0x80 then |
| 234 |
n1=n1-0x100 |
| 235 |
end |
| 236 |
return n1+n2/0xFF |
| 237 |
end |
| 238 |
function streams.readfixed4(f) |
| 239 |
local i=f[2] |
| 240 |
local j=i+3 |
| 241 |
f[2]=j+1 |
| 242 |
local a,b,c,d=byte(f[1],i,j) |
| 243 |
local n1=0x100*a+b |
| 244 |
local n2=0x100*c+d |
| 245 |
if n1>=0x8000 then |
| 246 |
n1=n1-0x10000 |
| 247 |
end |
| 248 |
return n1+n2/0xFFFF |
| 249 |
end |
| 250 |
if bit32 then |
| 251 |
local extract=bit32.extract |
| 252 |
local band=bit32.band |
| 253 |
function streams.read2dot14(f) |
| 254 |
local i=f[2] |
| 255 |
local j=i+1 |
| 256 |
f[2]=j+1 |
| 257 |
local a,b=byte(f[1],i,j) |
| 258 |
if a>=0x80 then |
| 259 |
local n=-(0x100*a+b) |
| 260 |
return-(extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 261 |
else |
| 262 |
local n=0x100*a+b |
| 263 |
return (extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 264 |
end |
| 265 |
end |
| 266 |
end |
| 267 |
function streams.skipshort(f,n) |
| 268 |
f[2]=f[2]+2*(n or 1) |
| 269 |
end |
| 270 |
function streams.skiplong(f,n) |
| 271 |
f[2]=f[2]+4*(n or 1) |
| 272 |
end |
| 273 |
if sio and sio.readcardinal2 then |
| 274 |
local readcardinal1=sio.readcardinal1 |
| 275 |
local readcardinal2=sio.readcardinal2 |
| 276 |
local readcardinal3=sio.readcardinal3 |
| 277 |
local readcardinal4=sio.readcardinal4 |
| 278 |
local readinteger1=sio.readinteger1 |
| 279 |
local readinteger2=sio.readinteger2 |
| 280 |
local readinteger3=sio.readinteger3 |
| 281 |
local readinteger4=sio.readinteger4 |
| 282 |
local readfixed2=sio.readfixed2 |
| 283 |
local readfixed4=sio.readfixed4 |
| 284 |
local read2dot14=sio.read2dot14 |
| 285 |
local readbytes=sio.readbytes |
| 286 |
local readbytetable=sio.readbytetable |
| 287 |
function streams.readcardinal1(f) |
| 288 |
local i=f[2] |
| 289 |
f[2]=i+1 |
| 290 |
return readcardinal1(f[1],i) |
| 291 |
end |
| 292 |
function streams.readcardinal2(f) |
| 293 |
local i=f[2] |
| 294 |
f[2]=i+2 |
| 295 |
return readcardinal2(f[1],i) |
| 296 |
end |
| 297 |
function streams.readcardinal3(f) |
| 298 |
local i=f[2] |
| 299 |
f[2]=i+3 |
| 300 |
return readcardinal3(f[1],i) |
| 301 |
end |
| 302 |
function streams.readcardinal4(f) |
| 303 |
local i=f[2] |
| 304 |
f[2]=i+4 |
| 305 |
return readcardinal4(f[1],i) |
| 306 |
end |
| 307 |
function streams.readinteger1(f) |
| 308 |
local i=f[2] |
| 309 |
f[2]=i+1 |
| 310 |
return readinteger1(f[1],i) |
| 311 |
end |
| 312 |
function streams.readinteger2(f) |
| 313 |
local i=f[2] |
| 314 |
f[2]=i+2 |
| 315 |
return readinteger2(f[1],i) |
| 316 |
end |
| 317 |
function streams.readinteger3(f) |
| 318 |
local i=f[2] |
| 319 |
f[2]=i+3 |
| 320 |
return readinteger3(f[1],i) |
| 321 |
end |
| 322 |
function streams.readinteger4(f) |
| 323 |
local i=f[2] |
| 324 |
f[2]=i+4 |
| 325 |
return readinteger4(f[1],i) |
| 326 |
end |
| 327 |
function streams.readfixed2(f) |
| 328 |
local i=f[2] |
| 329 |
f[2]=i+2 |
| 330 |
return readfixed2(f[1],i) |
| 331 |
end |
| 332 |
function streams.readfixed4(f) |
| 333 |
local i=f[2] |
| 334 |
f[2]=i+4 |
| 335 |
return readfixed4(f[1],i) |
| 336 |
end |
| 337 |
function streams.read2dot14(f) |
| 338 |
local i=f[2] |
| 339 |
f[2]=i+2 |
| 340 |
return read2dot14(f[1],i) |
| 341 |
end |
| 342 |
function streams.readbytes(f,n) |
| 343 |
local i=f[2] |
| 344 |
local s=f[3] |
| 345 |
local p=i+n |
| 346 |
if p>s then |
| 347 |
f[2]=s+1 |
| 348 |
else |
| 349 |
f[2]=p |
| 350 |
end |
| 351 |
return readbytes(f[1],i,n) |
| 352 |
end |
| 353 |
function streams.readbytetable(f,n) |
| 354 |
local i=f[2] |
| 355 |
local s=f[3] |
| 356 |
local p=i+n |
| 357 |
if p>s then |
| 358 |
f[2]=s+1 |
| 359 |
else |
| 360 |
f[2]=p |
| 361 |
end |
| 362 |
return readbytetable(f[1],i,n) |
| 363 |
end |
| 364 |
streams.readbyte=streams.readcardinal1 |
| 365 |
streams.readsignedbyte=streams.readinteger1 |
| 366 |
streams.readcardinal=streams.readcardinal1 |
| 367 |
streams.readinteger=streams.readinteger1 |
| 368 |
end |
| 369 |
if sio and sio.readcardinaltable then |
| 370 |
local readcardinaltable=sio.readcardinaltable |
| 371 |
local readintegertable=sio.readintegertable |
| 372 |
function utilities.streams.readcardinaltable(f,n,b) |
| 373 |
local i=f[2] |
| 374 |
local s=f[3] |
| 375 |
local p=i+n*b |
| 376 |
if p>s then |
| 377 |
f[2]=s+1 |
| 378 |
else |
| 379 |
f[2]=p |
| 380 |
end |
| 381 |
return readcardinaltable(f[1],i,n,b) |
| 382 |
end |
| 383 |
function utilities.streams.readintegertable(f,n,b) |
| 384 |
local i=f[2] |
| 385 |
local s=f[3] |
| 386 |
local p=i+n*b |
| 387 |
if p>s then |
| 388 |
f[2]=s+1 |
| 389 |
else |
| 390 |
f[2]=p |
| 391 |
end |
| 392 |
return readintegertable(f[1],i,n,b) |
| 393 |
end |
| 394 |
else |
| 395 |
local readcardinal1=streams.readcardinal1 |
| 396 |
local readcardinal2=streams.readcardinal2 |
| 397 |
local readcardinal3=streams.readcardinal3 |
| 398 |
local readcardinal4=streams.readcardinal4 |
| 399 |
function streams.readcardinaltable(f,n,b) |
| 400 |
local i=f[2] |
| 401 |
local s=f[3] |
| 402 |
local p=i+n*b |
| 403 |
if p>s then |
| 404 |
f[2]=s+1 |
| 405 |
else |
| 406 |
f[2]=p |
| 407 |
end |
| 408 |
local t={} |
| 409 |
if b==1 then for i=1,n do t[i]=readcardinal1(f[1],i) end |
| 410 |
elseif b==2 then for i=1,n do t[i]=readcardinal2(f[1],i) end |
| 411 |
elseif b==3 then for i=1,n do t[i]=readcardinal3(f[1],i) end |
| 412 |
elseif b==4 then for i=1,n do t[i]=readcardinal4(f[1],i) end end |
| 413 |
return t |
| 414 |
end |
| 415 |
local readinteger1=streams.readinteger1 |
| 416 |
local readinteger2=streams.readinteger2 |
| 417 |
local readinteger3=streams.readinteger3 |
| 418 |
local readinteger4=streams.readinteger4 |
| 419 |
function streams.readintegertable(f,n,b) |
| 420 |
local i=f[2] |
| 421 |
local s=f[3] |
| 422 |
local p=i+n*b |
| 423 |
if p>s then |
| 424 |
f[2]=s+1 |
| 425 |
else |
| 426 |
f[2]=p |
| 427 |
end |
| 428 |
local t={} |
| 429 |
if b==1 then for i=1,n do t[i]=readinteger1(f[1],i) end |
| 430 |
elseif b==2 then for i=1,n do t[i]=readinteger2(f[1],i) end |
| 431 |
elseif b==3 then for i=1,n do t[i]=readinteger3(f[1],i) end |
| 432 |
elseif b==4 then for i=1,n do t[i]=readinteger4(f[1],i) end end |
| 433 |
return t |
| 434 |
end |
| 435 |
end |
| 436 |
do |
| 437 |
local files=utilities.files |
| 438 |
if files then |
| 439 |
local openfile=files.open |
| 440 |
local openstream=streams.open |
| 441 |
local openstring=streams.openstring |
| 442 |
local setmetatable=setmetatable |
| 443 |
function io.newreader(str,method) |
| 444 |
local f,m |
| 445 |
if method=="string" then |
| 446 |
f=openstring(str,true) |
| 447 |
m=streams |
| 448 |
elseif method=="stream" then |
| 449 |
f=openstream(str,true) |
| 450 |
m=streams |
| 451 |
else |
| 452 |
f=openfile(str,"rb") |
| 453 |
m=files |
| 454 |
end |
| 455 |
if f then |
| 456 |
local t={} |
| 457 |
setmetatable(t,{ |
| 458 |
__index=function(t,k) |
| 459 |
local r=m[k] |
| 460 |
if k=="close" then |
| 461 |
if f then |
| 462 |
m.close(f) |
| 463 |
f=nil |
| 464 |
end |
| 465 |
return function() end |
| 466 |
elseif r then |
| 467 |
local v=function(_,a,b) return r(f,a,b) end |
| 468 |
t[k]=v |
| 469 |
return v |
| 470 |
else |
| 471 |
print("unknown key",k) |
| 472 |
end |
| 473 |
end |
| 474 |
} ) |
| 475 |
return t |
| 476 |
end |
| 477 |
end |
| 478 |
end |
| 479 |
end |
| 480 |
if bit32 and not streams.tocardinal1 then |
| 481 |
local extract=bit32.extract |
| 482 |
local char=string.char |
| 483 |
streams.tocardinal1=char |
| 484 |
function streams.tocardinal2(n) return char(extract(n,8,8),extract(n,0,8)) end |
| 485 |
function streams.tocardinal3(n) return char(extract(n,16,8),extract(n,8,8),extract(n,0,8)) end |
| 486 |
function streams.tocardinal4(n) return char(extract(n,24,8),extract(n,16,8),extract(n,8,8),extract(n,0,8)) end |
| 487 |
streams.tocardinal1le=char |
| 488 |
function streams.tocardinal2le(n) return char(extract(n,0,8),extract(n,8,8)) end |
| 489 |
function streams.tocardinal3le(n) return char(extract(n,0,8),extract(n,8,8),extract(n,16,8)) end |
| 490 |
function streams.tocardinal4le(n) return char(extract(n,0,8),extract(n,8,8),extract(n,16,8),extract(n,24,8)) end |
| 491 |
end |
| 492 |
if not streams.readcstring then |
| 493 |
local readchar=streams.readchar |
| 494 |
local concat=table.concat |
| 495 |
function streams.readcstring(f) |
| 496 |
local t={} |
| 497 |
while true do |
| 498 |
local c=readchar(f) |
| 499 |
if c and c~="\0" then |
| 500 |
t[#t+1]=c |
| 501 |
else |
| 502 |
return concat(t) |
| 503 |
end |
| 504 |
end |
| 505 |
end |
| 506 |
end |
| 507 |
|
| 508 |
end -- closure |
| 509 |
|
| 510 |
do -- begin closure to overcome local limits and interference |
| 511 |
|
| 512 |
if not modules then modules={} end modules ['util-str']={ |
| 513 |
version=1.001, |
| 514 |
comment="companion to luat-lib.mkiv", |
| 515 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 516 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 517 |
license="see context related readme files" |
| 518 |
} |
| 519 |
utilities=utilities or {} |
| 520 |
utilities.strings=utilities.strings or {} |
| 521 |
local strings=utilities.strings |
| 522 |
local format,gsub,rep,sub,find,char=string.format,string.gsub,string.rep,string.sub,string.find,string.char |
| 523 |
local load,dump=load,string.dump |
| 524 |
local tonumber,type,tostring,next,setmetatable=tonumber,type,tostring,next,setmetatable |
| 525 |
local unpack,concat=table.unpack,table.concat |
| 526 |
local P,V,C,S,R,Ct,Cs,Cp,Carg,Cc=lpeg.P,lpeg.V,lpeg.C,lpeg.S,lpeg.R,lpeg.Ct,lpeg.Cs,lpeg.Cp,lpeg.Carg,lpeg.Cc |
| 527 |
local patterns,lpegmatch=lpeg.patterns,lpeg.match |
| 528 |
local tsplitat=lpeg.tsplitat |
| 529 |
local utfchar,utfbyte,utflen=utf.char,utf.byte,utf.len |
| 530 |
local loadstripped=function(str,shortcuts) |
| 531 |
if shortcuts then |
| 532 |
return load(dump(load(str),true),nil,nil,shortcuts) |
| 533 |
else |
| 534 |
return load(dump(load(str),true)) |
| 535 |
end |
| 536 |
end |
| 537 |
if not number then number={} end |
| 538 |
local stripzero=patterns.stripzero |
| 539 |
local stripzeros=patterns.stripzeros |
| 540 |
local newline=patterns.newline |
| 541 |
local endofstring=patterns.endofstring |
| 542 |
local anything=patterns.anything |
| 543 |
local whitespace=patterns.whitespace |
| 544 |
local space=patterns.space |
| 545 |
local spacer=patterns.spacer |
| 546 |
local spaceortab=patterns.spaceortab |
| 547 |
local digit=patterns.digit |
| 548 |
local sign=patterns.sign |
| 549 |
local period=patterns.period |
| 550 |
local ptf=1/65536 |
| 551 |
local bpf=(7200/7227)/65536 |
| 552 |
local function points(n) |
| 553 |
if n==0 then |
| 554 |
return "0pt" |
| 555 |
end |
| 556 |
n=tonumber(n) |
| 557 |
if not n or n==0 then |
| 558 |
return "0pt" |
| 559 |
end |
| 560 |
n=n*ptf |
| 561 |
if n%1==0 then |
| 562 |
return format("%ipt",n) |
| 563 |
else |
| 564 |
return lpegmatch(stripzeros,format("%.5fpt",n)) |
| 565 |
end |
| 566 |
end |
| 567 |
local function nupoints(n) |
| 568 |
if n==0 then |
| 569 |
return "0" |
| 570 |
end |
| 571 |
n=tonumber(n) |
| 572 |
if not n or n==0 then |
| 573 |
return "0" |
| 574 |
end |
| 575 |
n=n*ptf |
| 576 |
if n%1==0 then |
| 577 |
return format("%i",n) |
| 578 |
else |
| 579 |
return format("%.5f",n) |
| 580 |
end |
| 581 |
end |
| 582 |
local function basepoints(n) |
| 583 |
if n==0 then |
| 584 |
return "0bp" |
| 585 |
end |
| 586 |
n=tonumber(n) |
| 587 |
if not n or n==0 then |
| 588 |
return "0bp" |
| 589 |
end |
| 590 |
n=n*bpf |
| 591 |
if n%1==0 then |
| 592 |
return format("%ibp",n) |
| 593 |
else |
| 594 |
return lpegmatch(stripzeros,format("%.5fbp",n)) |
| 595 |
end |
| 596 |
end |
| 597 |
local function nubasepoints(n) |
| 598 |
if n==0 then |
| 599 |
return "0" |
| 600 |
end |
| 601 |
n=tonumber(n) |
| 602 |
if not n or n==0 then |
| 603 |
return "0" |
| 604 |
end |
| 605 |
n=n*bpf |
| 606 |
if n%1==0 then |
| 607 |
return format("%i",n) |
| 608 |
else |
| 609 |
return format("%.5f",n) |
| 610 |
end |
| 611 |
end |
| 612 |
number.points=points |
| 613 |
number.nupoints=nupoints |
| 614 |
number.basepoints=basepoints |
| 615 |
number.nubasepoints=nubasepoints |
| 616 |
local rubish=spaceortab^0*newline |
| 617 |
local anyrubish=spaceortab+newline |
| 618 |
local stripped=(spaceortab^1/"")*newline |
| 619 |
local leading=rubish^0/"" |
| 620 |
local trailing=(anyrubish^1*endofstring)/"" |
| 621 |
local redundant=rubish^3/"\n" |
| 622 |
local pattern=Cs(leading*(trailing+redundant+stripped+anything)^0) |
| 623 |
function strings.collapsecrlf(str) |
| 624 |
return lpegmatch(pattern,str) |
| 625 |
end |
| 626 |
local repeaters={} |
| 627 |
function strings.newrepeater(str,offset) |
| 628 |
offset=offset or 0 |
| 629 |
local s=repeaters[str] |
| 630 |
if not s then |
| 631 |
s={} |
| 632 |
repeaters[str]=s |
| 633 |
end |
| 634 |
local t=s[offset] |
| 635 |
if t then |
| 636 |
return t |
| 637 |
end |
| 638 |
t={} |
| 639 |
setmetatable(t,{ __index=function(t,k) |
| 640 |
if not k then |
| 641 |
return "" |
| 642 |
end |
| 643 |
local n=k+offset |
| 644 |
local s=n>0 and rep(str,n) or "" |
| 645 |
t[k]=s |
| 646 |
return s |
| 647 |
end }) |
| 648 |
s[offset]=t |
| 649 |
return t |
| 650 |
end |
| 651 |
local extra,tab,start=0,0,4,0 |
| 652 |
local nspaces=strings.newrepeater(" ") |
| 653 |
string.nspaces=nspaces |
| 654 |
local pattern=Carg(1)/function(t) |
| 655 |
extra,tab,start=0,t or 7,1 |
| 656 |
end*Cs(( |
| 657 |
Cp()*patterns.tab/function(position) |
| 658 |
local current=(position-start+1)+extra |
| 659 |
local spaces=tab-(current-1)%tab |
| 660 |
if spaces>0 then |
| 661 |
extra=extra+spaces-1 |
| 662 |
return nspaces[spaces] |
| 663 |
else |
| 664 |
return "" |
| 665 |
end |
| 666 |
end+newline*Cp()/function(position) |
| 667 |
extra,start=0,position |
| 668 |
end+anything |
| 669 |
)^1) |
| 670 |
function strings.tabtospace(str,tab) |
| 671 |
return lpegmatch(pattern,str,1,tab or 7) |
| 672 |
end |
| 673 |
function string.utfpadding(s,n) |
| 674 |
if not n or n==0 then |
| 675 |
return "" |
| 676 |
end |
| 677 |
local l=utflen(s) |
| 678 |
if n>0 then |
| 679 |
return nspaces[n-l] |
| 680 |
else |
| 681 |
return nspaces[-n-l] |
| 682 |
end |
| 683 |
end |
| 684 |
local optionalspace=spacer^0 |
| 685 |
local nospace=optionalspace/"" |
| 686 |
local endofline=nospace*newline |
| 687 |
local stripend=(whitespace^1*endofstring)/"" |
| 688 |
local normalline=(nospace*((1-optionalspace*(newline+endofstring))^1)*nospace) |
| 689 |
local stripempty=endofline^1/"" |
| 690 |
local normalempty=endofline^1 |
| 691 |
local singleempty=endofline*(endofline^0/"") |
| 692 |
local doubleempty=endofline*endofline^-1*(endofline^0/"") |
| 693 |
local stripstart=stripempty^0 |
| 694 |
local intospace=whitespace^1/" " |
| 695 |
local noleading=whitespace^1/"" |
| 696 |
local notrailing=noleading*endofstring |
| 697 |
local p_prune_normal=Cs (stripstart*(stripend+normalline+normalempty )^0 ) |
| 698 |
local p_prune_collapse=Cs (stripstart*(stripend+normalline+doubleempty )^0 ) |
| 699 |
local p_prune_noempty=Cs (stripstart*(stripend+normalline+singleempty )^0 ) |
| 700 |
local p_prune_intospace=Cs (noleading*(notrailing+intospace+1 )^0 ) |
| 701 |
local p_retain_normal=Cs ((normalline+normalempty )^0 ) |
| 702 |
local p_retain_collapse=Cs ((normalline+doubleempty )^0 ) |
| 703 |
local p_retain_noempty=Cs ((normalline+singleempty )^0 ) |
| 704 |
local p_collapse_all=Cs (stripstart*(stripend+((whitespace+newline)^1/" ")+1)^0 ) |
| 705 |
local striplinepatterns={ |
| 706 |
["prune"]=p_prune_normal, |
| 707 |
["prune and collapse"]=p_prune_collapse, |
| 708 |
["prune and no empty"]=p_prune_noempty, |
| 709 |
["prune and to space"]=p_prune_intospace, |
| 710 |
["retain"]=p_retain_normal, |
| 711 |
["retain and collapse"]=p_retain_collapse, |
| 712 |
["retain and no empty"]=p_retain_noempty, |
| 713 |
["collapse all"]=p_collapse_all, |
| 714 |
["collapse"]=patterns.collapser, |
| 715 |
} |
| 716 |
setmetatable(striplinepatterns,{ __index=function(t,k) return p_prune_collapse end }) |
| 717 |
strings.striplinepatterns=striplinepatterns |
| 718 |
function strings.striplines(str,how) |
| 719 |
return str and lpegmatch(striplinepatterns[how],str) or str |
| 720 |
end |
| 721 |
function strings.collapse(str) |
| 722 |
return str and lpegmatch(p_prune_intospace,str) or str |
| 723 |
end |
| 724 |
strings.striplong=strings.striplines |
| 725 |
function strings.nice(str) |
| 726 |
str=gsub(str,"[:%-+_]+"," ") |
| 727 |
return str |
| 728 |
end |
| 729 |
local n=0 |
| 730 |
local sequenced=table.sequenced |
| 731 |
function string.autodouble(s,sep) |
| 732 |
if s==nil then |
| 733 |
return '""' |
| 734 |
end |
| 735 |
local t=type(s) |
| 736 |
if t=="number" then |
| 737 |
return tostring(s) |
| 738 |
end |
| 739 |
if t=="table" then |
| 740 |
return ('"'..sequenced(s,sep or ",")..'"') |
| 741 |
end |
| 742 |
return ('"'..tostring(s)..'"') |
| 743 |
end |
| 744 |
function string.autosingle(s,sep) |
| 745 |
if s==nil then |
| 746 |
return "''" |
| 747 |
end |
| 748 |
local t=type(s) |
| 749 |
if t=="number" then |
| 750 |
return tostring(s) |
| 751 |
end |
| 752 |
if t=="table" then |
| 753 |
return ("'"..sequenced(s,sep or ",").."'") |
| 754 |
end |
| 755 |
return ("'"..tostring(s).."'") |
| 756 |
end |
| 757 |
local tracedchars={ [0]= |
| 758 |
"[null]","[soh]","[stx]","[etx]","[eot]","[enq]","[ack]","[bel]", |
| 759 |
"[bs]","[ht]","[lf]","[vt]","[ff]","[cr]","[so]","[si]", |
| 760 |
"[dle]","[dc1]","[dc2]","[dc3]","[dc4]","[nak]","[syn]","[etb]", |
| 761 |
"[can]","[em]","[sub]","[esc]","[fs]","[gs]","[rs]","[us]", |
| 762 |
"[space]", |
| 763 |
} |
| 764 |
string.tracedchars=tracedchars |
| 765 |
strings.tracers=tracedchars |
| 766 |
function string.tracedchar(b) |
| 767 |
if type(b)=="number" then |
| 768 |
return tracedchars[b] or (utfchar(b).." (U+"..format("%05X",b)..")") |
| 769 |
else |
| 770 |
local c=utfbyte(b) |
| 771 |
return tracedchars[c] or (b.." (U+"..(c and format("%05X",c) or "?????")..")") |
| 772 |
end |
| 773 |
end |
| 774 |
function number.signed(i) |
| 775 |
if i>0 then |
| 776 |
return "+",i |
| 777 |
else |
| 778 |
return "-",-i |
| 779 |
end |
| 780 |
end |
| 781 |
local two=digit*digit |
| 782 |
local three=two*digit |
| 783 |
local prefix=(Carg(1)*three)^1 |
| 784 |
local splitter=Cs ( |
| 785 |
(((1-(three^1*period))^1+C(three))*prefix+C((1-period)^1))*(anything/""*Carg(2))*C(2) |
| 786 |
) |
| 787 |
local splitter3=Cs ( |
| 788 |
three*prefix*endofstring+two*prefix*endofstring+digit*prefix*endofstring+three+two+digit |
| 789 |
) |
| 790 |
patterns.formattednumber=splitter |
| 791 |
function number.formatted(n,sep1,sep2) |
| 792 |
if sep1==false then |
| 793 |
if type(n)=="number" then |
| 794 |
n=tostring(n) |
| 795 |
end |
| 796 |
return lpegmatch(splitter3,n,1,sep2 or ".") |
| 797 |
else |
| 798 |
if type(n)=="number" then |
| 799 |
n=format("%0.2f",n) |
| 800 |
end |
| 801 |
if sep1==true then |
| 802 |
return lpegmatch(splitter,n,1,".",",") |
| 803 |
elseif sep1=="." then |
| 804 |
return lpegmatch(splitter,n,1,sep1,sep2 or ",") |
| 805 |
elseif sep1=="," then |
| 806 |
return lpegmatch(splitter,n,1,sep1,sep2 or ".") |
| 807 |
else |
| 808 |
return lpegmatch(splitter,n,1,sep1 or ",",sep2 or ".") |
| 809 |
end |
| 810 |
end |
| 811 |
end |
| 812 |
local p=Cs( |
| 813 |
P("-")^0*(P("0")^1/"")^0*(1-period)^0*(period*P("0")^1*endofstring/""+period^0)*P(1-P("0")^1*endofstring)^0 |
| 814 |
) |
| 815 |
function number.compactfloat(n,fmt) |
| 816 |
if n==0 then |
| 817 |
return "0" |
| 818 |
elseif n==1 then |
| 819 |
return "1" |
| 820 |
end |
| 821 |
n=lpegmatch(p,format(fmt or "%0.3f",n)) |
| 822 |
if n=="." or n=="" or n=="-" then |
| 823 |
return "0" |
| 824 |
end |
| 825 |
return n |
| 826 |
end |
| 827 |
local zero=P("0")^1/"" |
| 828 |
local plus=P("+")/"" |
| 829 |
local minus=P("-") |
| 830 |
local separator=period |
| 831 |
local trailing=zero^1*#S("eE") |
| 832 |
local exponent=(S("eE")*(plus+Cs((minus*zero^0*endofstring)/"")+minus)*zero^0*(endofstring*Cc("0")+anything^1)) |
| 833 |
local pattern_a=Cs(minus^0*digit^1*(separator/""*trailing+separator*(trailing+digit)^0)*exponent) |
| 834 |
local pattern_b=Cs((exponent+anything)^0) |
| 835 |
function number.sparseexponent(f,n) |
| 836 |
if not n then |
| 837 |
n=f |
| 838 |
f="%e" |
| 839 |
end |
| 840 |
local tn=type(n) |
| 841 |
if tn=="string" then |
| 842 |
local m=tonumber(n) |
| 843 |
if m then |
| 844 |
return lpegmatch((f=="%e" or f=="%E") and pattern_a or pattern_b,format(f,m)) |
| 845 |
end |
| 846 |
elseif tn=="number" then |
| 847 |
return lpegmatch((f=="%e" or f=="%E") and pattern_a or pattern_b,format(f,n)) |
| 848 |
end |
| 849 |
return tostring(n) |
| 850 |
end |
| 851 |
local hf={} |
| 852 |
local hs={} |
| 853 |
setmetatable(hf,{ __index=function(t,k) |
| 854 |
local v="%."..k.."f" |
| 855 |
t[k]=v |
| 856 |
return v |
| 857 |
end } ) |
| 858 |
setmetatable(hs,{ __index=function(t,k) |
| 859 |
local v="%"..k.."s" |
| 860 |
t[k]=v |
| 861 |
return v |
| 862 |
end } ) |
| 863 |
function number.formattedfloat(n,b,a) |
| 864 |
local s=format(hf[a],n) |
| 865 |
local l=(b or 0)+(a or 0)+1 |
| 866 |
if #s<l then |
| 867 |
return format(hs[l],s) |
| 868 |
else |
| 869 |
return s |
| 870 |
end |
| 871 |
end |
| 872 |
local template=[[ |
| 873 |
%s |
| 874 |
%s |
| 875 |
return function(%s) return %s end |
| 876 |
]] |
| 877 |
local pattern=Cs(Cc('"')*( |
| 878 |
(1-S('"\\\n\r'))^1+P('"')/'\\"'+P('\\')/'\\\\'+P('\n')/'\\n'+P('\r')/'\\r' |
| 879 |
)^0*Cc('"')) |
| 880 |
patterns.escapedquotes=pattern |
| 881 |
function string.escapedquotes(s) |
| 882 |
return lpegmatch(pattern,s) |
| 883 |
end |
| 884 |
local pattern=(1-P("\\"))^1;pattern=Cs ( |
| 885 |
pattern*((P("\\")/""*(digit^-3/function(s) return char(tonumber(s)) end))+pattern )^1 |
| 886 |
) |
| 887 |
patterns.unescapedquotes=pattern |
| 888 |
function string.unescapedquotes(s) |
| 889 |
return lpegmatch(pattern,s) or s |
| 890 |
end |
| 891 |
string.texnewlines=lpeg.replacer(patterns.newline,"\r",true) |
| 892 |
local preamble="" |
| 893 |
local environment={ |
| 894 |
global=global or _G, |
| 895 |
lpeg=lpeg, |
| 896 |
type=type, |
| 897 |
tostring=tostring, |
| 898 |
tonumber=tonumber, |
| 899 |
format=string.format, |
| 900 |
concat=table.concat, |
| 901 |
signed=number.signed, |
| 902 |
points=number.points, |
| 903 |
nupoints=number.nupoints, |
| 904 |
basepoints=number.basepoints, |
| 905 |
nubasepoints=number.nubasepoints, |
| 906 |
utfchar=utf.char, |
| 907 |
utfbyte=utf.byte, |
| 908 |
lpegmatch=lpeg.match, |
| 909 |
nspaces=string.nspaces, |
| 910 |
utfpadding=string.utfpadding, |
| 911 |
tracedchar=string.tracedchar, |
| 912 |
autosingle=string.autosingle, |
| 913 |
autodouble=string.autodouble, |
| 914 |
sequenced=table.sequenced, |
| 915 |
formattednumber=number.formatted, |
| 916 |
sparseexponent=number.sparseexponent, |
| 917 |
formattedfloat=number.formattedfloat, |
| 918 |
stripzero=patterns.stripzero, |
| 919 |
stripzeros=patterns.stripzeros, |
| 920 |
escapedquotes=string.escapedquotes, |
| 921 |
FORMAT=string.f6, |
| 922 |
} |
| 923 |
local arguments={ "a1" } |
| 924 |
setmetatable(arguments,{ __index=function(t,k) |
| 925 |
local v=t[k-1]..",a"..k |
| 926 |
t[k]=v |
| 927 |
return v |
| 928 |
end |
| 929 |
}) |
| 930 |
local prefix_any=C((sign+space+period+digit)^0) |
| 931 |
local prefix_sub=(C((sign+digit)^0)+Cc(0))*period*(C((sign+digit)^0)+Cc(0)) |
| 932 |
local prefix_tab=P("{")*C((1-P("}"))^0)*P("}")+C((1-R("az","AZ","09","%%"))^0) |
| 933 |
local format_s=function(f) |
| 934 |
n=n+1 |
| 935 |
if f and f~="" then |
| 936 |
return format("format('%%%ss',a%s)",f,n) |
| 937 |
else |
| 938 |
return format("(a%s or '')",n) |
| 939 |
end |
| 940 |
end |
| 941 |
local format_S=function(f) |
| 942 |
n=n+1 |
| 943 |
if f and f~="" then |
| 944 |
return format("format('%%%ss',tostring(a%s))",f,n) |
| 945 |
else |
| 946 |
return format("tostring(a%s)",n) |
| 947 |
end |
| 948 |
end |
| 949 |
local format_right=function(f) |
| 950 |
n=n+1 |
| 951 |
f=tonumber(f) |
| 952 |
if not f or f==0 then |
| 953 |
return format("(a%s or '')",n) |
| 954 |
elseif f>0 then |
| 955 |
return format("utfpadding(a%s,%i)..a%s",n,f,n) |
| 956 |
else |
| 957 |
return format("a%s..utfpadding(a%s,%i)",n,n,f) |
| 958 |
end |
| 959 |
end |
| 960 |
local format_left=function(f) |
| 961 |
n=n+1 |
| 962 |
f=tonumber(f) |
| 963 |
if not f or f==0 then |
| 964 |
return format("(a%s or '')",n) |
| 965 |
end |
| 966 |
if f<0 then |
| 967 |
return format("utfpadding(a%s,%i)..a%s",n,-f,n) |
| 968 |
else |
| 969 |
return format("a%s..utfpadding(a%s,%i)",n,n,-f) |
| 970 |
end |
| 971 |
end |
| 972 |
local format_q=JITSUPPORTED and function() |
| 973 |
n=n+1 |
| 974 |
return format("(a%s ~= nil and format('%%q',tostring(a%s)) or '')",n,n) |
| 975 |
end or function() |
| 976 |
n=n+1 |
| 977 |
return format("(a%s ~= nil and format('%%q',a%s) or '')",n,n) |
| 978 |
end |
| 979 |
local format_Q=function() |
| 980 |
n=n+1 |
| 981 |
return format("escapedquotes(tostring(a%s))",n) |
| 982 |
end |
| 983 |
local format_i=function(f) |
| 984 |
n=n+1 |
| 985 |
if f and f~="" then |
| 986 |
return format("format('%%%si',a%s)",f,n) |
| 987 |
else |
| 988 |
return format("format('%%i',a%s)",n) |
| 989 |
end |
| 990 |
end |
| 991 |
local format_d=format_i |
| 992 |
local format_I=function(f) |
| 993 |
n=n+1 |
| 994 |
return format("format('%%s%%%si',signed(a%s))",f,n) |
| 995 |
end |
| 996 |
local format_f=function(f) |
| 997 |
n=n+1 |
| 998 |
return format("format('%%%sf',a%s)",f,n) |
| 999 |
end |
| 1000 |
local format_F=function(f) |
| 1001 |
n=n+1 |
| 1002 |
if not f or f=="" then |
| 1003 |
return format("(((a%s > -0.0000000005 and a%s < 0.0000000005) and '0') or format((a%s %% 1 == 0) and '%%i' or '%%.9f',a%s))",n,n,n,n) |
| 1004 |
else |
| 1005 |
return format("format((a%s %% 1 == 0) and '%%i' or '%%%sf',a%s)",n,f,n) |
| 1006 |
end |
| 1007 |
end |
| 1008 |
local format_k=function(b,a) |
| 1009 |
n=n+1 |
| 1010 |
return format("formattedfloat(a%s,%s,%s)",n,b or 0,a or 0) |
| 1011 |
end |
| 1012 |
local format_g=function(f) |
| 1013 |
n=n+1 |
| 1014 |
return format("format('%%%sg',a%s)",f,n) |
| 1015 |
end |
| 1016 |
local format_G=function(f) |
| 1017 |
n=n+1 |
| 1018 |
return format("format('%%%sG',a%s)",f,n) |
| 1019 |
end |
| 1020 |
local format_e=function(f) |
| 1021 |
n=n+1 |
| 1022 |
return format("format('%%%se',a%s)",f,n) |
| 1023 |
end |
| 1024 |
local format_E=function(f) |
| 1025 |
n=n+1 |
| 1026 |
return format("format('%%%sE',a%s)",f,n) |
| 1027 |
end |
| 1028 |
local format_j=function(f) |
| 1029 |
n=n+1 |
| 1030 |
return format("sparseexponent('%%%se',a%s)",f,n) |
| 1031 |
end |
| 1032 |
local format_J=function(f) |
| 1033 |
n=n+1 |
| 1034 |
return format("sparseexponent('%%%sE',a%s)",f,n) |
| 1035 |
end |
| 1036 |
local format_x=function(f) |
| 1037 |
n=n+1 |
| 1038 |
return format("format('%%%sx',a%s)",f,n) |
| 1039 |
end |
| 1040 |
local format_X=function(f) |
| 1041 |
n=n+1 |
| 1042 |
return format("format('%%%sX',a%s)",f,n) |
| 1043 |
end |
| 1044 |
local format_o=function(f) |
| 1045 |
n=n+1 |
| 1046 |
return format("format('%%%so',a%s)",f,n) |
| 1047 |
end |
| 1048 |
local format_c=function() |
| 1049 |
n=n+1 |
| 1050 |
return format("utfchar(a%s)",n) |
| 1051 |
end |
| 1052 |
local format_C=function() |
| 1053 |
n=n+1 |
| 1054 |
return format("tracedchar(a%s)",n) |
| 1055 |
end |
| 1056 |
local format_r=function(f) |
| 1057 |
n=n+1 |
| 1058 |
return format("format('%%%s.0f',a%s)",f,n) |
| 1059 |
end |
| 1060 |
local format_h=function(f) |
| 1061 |
n=n+1 |
| 1062 |
if f=="-" then |
| 1063 |
f=sub(f,2) |
| 1064 |
return format("format('%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1065 |
else |
| 1066 |
return format("format('0x%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1067 |
end |
| 1068 |
end |
| 1069 |
local format_H=function(f) |
| 1070 |
n=n+1 |
| 1071 |
if f=="-" then |
| 1072 |
f=sub(f,2) |
| 1073 |
return format("format('%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1074 |
else |
| 1075 |
return format("format('0x%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1076 |
end |
| 1077 |
end |
| 1078 |
local format_u=function(f) |
| 1079 |
n=n+1 |
| 1080 |
if f=="-" then |
| 1081 |
f=sub(f,2) |
| 1082 |
return format("format('%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1083 |
else |
| 1084 |
return format("format('u+%%%sx',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1085 |
end |
| 1086 |
end |
| 1087 |
local format_U=function(f) |
| 1088 |
n=n+1 |
| 1089 |
if f=="-" then |
| 1090 |
f=sub(f,2) |
| 1091 |
return format("format('%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1092 |
else |
| 1093 |
return format("format('U+%%%sX',type(a%s) == 'number' and a%s or utfbyte(a%s))",f=="" and "05" or f,n,n,n) |
| 1094 |
end |
| 1095 |
end |
| 1096 |
local format_p=function() |
| 1097 |
n=n+1 |
| 1098 |
return format("points(a%s)",n) |
| 1099 |
end |
| 1100 |
local format_P=function() |
| 1101 |
n=n+1 |
| 1102 |
return format("nupoints(a%s)",n) |
| 1103 |
end |
| 1104 |
local format_b=function() |
| 1105 |
n=n+1 |
| 1106 |
return format("basepoints(a%s)",n) |
| 1107 |
end |
| 1108 |
local format_B=function() |
| 1109 |
n=n+1 |
| 1110 |
return format("nubasepoints(a%s)",n) |
| 1111 |
end |
| 1112 |
local format_t=function(f) |
| 1113 |
n=n+1 |
| 1114 |
if f and f~="" then |
| 1115 |
return format("concat(a%s,%q)",n,f) |
| 1116 |
else |
| 1117 |
return format("concat(a%s)",n) |
| 1118 |
end |
| 1119 |
end |
| 1120 |
local format_T=function(f) |
| 1121 |
n=n+1 |
| 1122 |
if f and f~="" then |
| 1123 |
return format("sequenced(a%s,%q)",n,f) |
| 1124 |
else |
| 1125 |
return format("sequenced(a%s)",n) |
| 1126 |
end |
| 1127 |
end |
| 1128 |
local format_l=function() |
| 1129 |
n=n+1 |
| 1130 |
return format("(a%s and 'true' or 'false')",n) |
| 1131 |
end |
| 1132 |
local format_L=function() |
| 1133 |
n=n+1 |
| 1134 |
return format("(a%s and 'TRUE' or 'FALSE')",n) |
| 1135 |
end |
| 1136 |
local format_n=function() |
| 1137 |
n=n+1 |
| 1138 |
return format("((a%s %% 1 == 0) and format('%%i',a%s) or tostring(a%s))",n,n,n) |
| 1139 |
end |
| 1140 |
local format_N if environment.FORMAT then |
| 1141 |
format_N=function(f) |
| 1142 |
n=n+1 |
| 1143 |
if not f or f=="" then |
| 1144 |
return format("FORMAT(a%s,'%%.9f')",n) |
| 1145 |
elseif f==".6" or f=="0.6" then |
| 1146 |
return format("FORMAT(a%s)",n) |
| 1147 |
else |
| 1148 |
return format("FORMAT(a%s,'%%%sf')",n,f) |
| 1149 |
end |
| 1150 |
end |
| 1151 |
else |
| 1152 |
format_N=function(f) |
| 1153 |
n=n+1 |
| 1154 |
if not f or f=="" then |
| 1155 |
f=".9" |
| 1156 |
end |
| 1157 |
return format("(((a%s %% 1 == 0) and format('%%i',a%s)) or lpegmatch(stripzero,format('%%%sf',a%s)))",n,n,f,n) |
| 1158 |
end |
| 1159 |
end |
| 1160 |
local format_a=function(f) |
| 1161 |
n=n+1 |
| 1162 |
if f and f~="" then |
| 1163 |
return format("autosingle(a%s,%q)",n,f) |
| 1164 |
else |
| 1165 |
return format("autosingle(a%s)",n) |
| 1166 |
end |
| 1167 |
end |
| 1168 |
local format_A=function(f) |
| 1169 |
n=n+1 |
| 1170 |
if f and f~="" then |
| 1171 |
return format("autodouble(a%s,%q)",n,f) |
| 1172 |
else |
| 1173 |
return format("autodouble(a%s)",n) |
| 1174 |
end |
| 1175 |
end |
| 1176 |
local format_w=function(f) |
| 1177 |
n=n+1 |
| 1178 |
f=tonumber(f) |
| 1179 |
if f then |
| 1180 |
return format("nspaces[%s+a%s]",f,n) |
| 1181 |
else |
| 1182 |
return format("nspaces[a%s]",n) |
| 1183 |
end |
| 1184 |
end |
| 1185 |
local format_W=function(f) |
| 1186 |
return format("nspaces[%s]",tonumber(f) or 0) |
| 1187 |
end |
| 1188 |
local format_m=function(f) |
| 1189 |
n=n+1 |
| 1190 |
if not f or f=="" then |
| 1191 |
f="," |
| 1192 |
end |
| 1193 |
if f=="0" then |
| 1194 |
return format([[formattednumber(a%s,false)]],n) |
| 1195 |
else |
| 1196 |
return format([[formattednumber(a%s,%q,".")]],n,f) |
| 1197 |
end |
| 1198 |
end |
| 1199 |
local format_M=function(f) |
| 1200 |
n=n+1 |
| 1201 |
if not f or f=="" then |
| 1202 |
f="." |
| 1203 |
end |
| 1204 |
if f=="0" then |
| 1205 |
return format([[formattednumber(a%s,false)]],n) |
| 1206 |
else |
| 1207 |
return format([[formattednumber(a%s,%q,",")]],n,f) |
| 1208 |
end |
| 1209 |
end |
| 1210 |
local format_z=function(f) |
| 1211 |
n=n+(tonumber(f) or 1) |
| 1212 |
return "''" |
| 1213 |
end |
| 1214 |
local format_rest=function(s) |
| 1215 |
return format("%q",s) |
| 1216 |
end |
| 1217 |
local format_extension=function(extensions,f,name) |
| 1218 |
local extension=extensions[name] or "tostring(%s)" |
| 1219 |
local f=tonumber(f) or 1 |
| 1220 |
local w=find(extension,"%.%.%.") |
| 1221 |
if f==0 then |
| 1222 |
if w then |
| 1223 |
extension=gsub(extension,"%.%.%.","") |
| 1224 |
end |
| 1225 |
return extension |
| 1226 |
elseif f==1 then |
| 1227 |
if w then |
| 1228 |
extension=gsub(extension,"%.%.%.","%%s") |
| 1229 |
end |
| 1230 |
n=n+1 |
| 1231 |
local a="a"..n |
| 1232 |
return format(extension,a,a) |
| 1233 |
elseif f<0 then |
| 1234 |
if w then |
| 1235 |
extension=gsub(extension,"%.%.%.","") |
| 1236 |
return extension |
| 1237 |
else |
| 1238 |
local a="a"..(n+f+1) |
| 1239 |
return format(extension,a,a) |
| 1240 |
end |
| 1241 |
else |
| 1242 |
if w then |
| 1243 |
extension=gsub(extension,"%.%.%.",rep("%%s,",f-1).."%%s") |
| 1244 |
end |
| 1245 |
local t={} |
| 1246 |
for i=1,f do |
| 1247 |
n=n+1 |
| 1248 |
t[i]="a"..n |
| 1249 |
end |
| 1250 |
return format(extension,unpack(t)) |
| 1251 |
end |
| 1252 |
end |
| 1253 |
local builder=Cs { "start", |
| 1254 |
start=( |
| 1255 |
( |
| 1256 |
P("%")/""*( |
| 1257 |
V("!") |
| 1258 |
+V("s")+V("q")+V("i")+V("d")+V("f")+V("F")+V("g")+V("G")+V("e")+V("E")+V("x")+V("X")+V("o") |
| 1259 |
+V("c")+V("C")+V("S") |
| 1260 |
+V("Q") |
| 1261 |
+V("n") |
| 1262 |
+V("N") |
| 1263 |
+V("k") |
| 1264 |
+V("r")+V("h")+V("H")+V("u")+V("U")+V("p")+V("P")+V("b")+V("B")+V("t")+V("T")+V("l")+V("L")+V("I")+V("w") |
| 1265 |
+V("W") |
| 1266 |
+V("a") |
| 1267 |
+V("A") |
| 1268 |
+V("j")+V("J") |
| 1269 |
+V("m")+V("M") |
| 1270 |
+V("z") |
| 1271 |
+V(">") |
| 1272 |
+V("<") |
| 1273 |
)+V("*") |
| 1274 |
)*(endofstring+Carg(1)) |
| 1275 |
)^0, |
| 1276 |
["s"]=(prefix_any*P("s"))/format_s, |
| 1277 |
["q"]=(prefix_any*P("q"))/format_q, |
| 1278 |
["i"]=(prefix_any*P("i"))/format_i, |
| 1279 |
["d"]=(prefix_any*P("d"))/format_d, |
| 1280 |
["f"]=(prefix_any*P("f"))/format_f, |
| 1281 |
["F"]=(prefix_any*P("F"))/format_F, |
| 1282 |
["g"]=(prefix_any*P("g"))/format_g, |
| 1283 |
["G"]=(prefix_any*P("G"))/format_G, |
| 1284 |
["e"]=(prefix_any*P("e"))/format_e, |
| 1285 |
["E"]=(prefix_any*P("E"))/format_E, |
| 1286 |
["x"]=(prefix_any*P("x"))/format_x, |
| 1287 |
["X"]=(prefix_any*P("X"))/format_X, |
| 1288 |
["o"]=(prefix_any*P("o"))/format_o, |
| 1289 |
["S"]=(prefix_any*P("S"))/format_S, |
| 1290 |
["Q"]=(prefix_any*P("Q"))/format_Q, |
| 1291 |
["n"]=(prefix_any*P("n"))/format_n, |
| 1292 |
["N"]=(prefix_any*P("N"))/format_N, |
| 1293 |
["k"]=(prefix_sub*P("k"))/format_k, |
| 1294 |
["c"]=(prefix_any*P("c"))/format_c, |
| 1295 |
["C"]=(prefix_any*P("C"))/format_C, |
| 1296 |
["r"]=(prefix_any*P("r"))/format_r, |
| 1297 |
["h"]=(prefix_any*P("h"))/format_h, |
| 1298 |
["H"]=(prefix_any*P("H"))/format_H, |
| 1299 |
["u"]=(prefix_any*P("u"))/format_u, |
| 1300 |
["U"]=(prefix_any*P("U"))/format_U, |
| 1301 |
["p"]=(prefix_any*P("p"))/format_p, |
| 1302 |
["P"]=(prefix_any*P("P"))/format_P, |
| 1303 |
["b"]=(prefix_any*P("b"))/format_b, |
| 1304 |
["B"]=(prefix_any*P("B"))/format_B, |
| 1305 |
["t"]=(prefix_tab*P("t"))/format_t, |
| 1306 |
["T"]=(prefix_tab*P("T"))/format_T, |
| 1307 |
["l"]=(prefix_any*P("l"))/format_l, |
| 1308 |
["L"]=(prefix_any*P("L"))/format_L, |
| 1309 |
["I"]=(prefix_any*P("I"))/format_I, |
| 1310 |
["w"]=(prefix_any*P("w"))/format_w, |
| 1311 |
["W"]=(prefix_any*P("W"))/format_W, |
| 1312 |
["j"]=(prefix_any*P("j"))/format_j, |
| 1313 |
["J"]=(prefix_any*P("J"))/format_J, |
| 1314 |
["m"]=(prefix_any*P("m"))/format_m, |
| 1315 |
["M"]=(prefix_any*P("M"))/format_M, |
| 1316 |
["z"]=(prefix_any*P("z"))/format_z, |
| 1317 |
["a"]=(prefix_any*P("a"))/format_a, |
| 1318 |
["A"]=(prefix_any*P("A"))/format_A, |
| 1319 |
["<"]=(prefix_any*P("<"))/format_left, |
| 1320 |
[">"]=(prefix_any*P(">"))/format_right, |
| 1321 |
["*"]=Cs(((1-P("%"))^1+P("%%")/"%%")^1)/format_rest, |
| 1322 |
["?"]=Cs(((1-P("%"))^1 )^1)/format_rest, |
| 1323 |
["!"]=Carg(2)*prefix_any*P("!")*C((1-P("!"))^1)*P("!")/format_extension, |
| 1324 |
} |
| 1325 |
local xx=setmetatable({},{ __index=function(t,k) local v=format("%02x",k) t[k]=v return v end }) |
| 1326 |
local XX=setmetatable({},{ __index=function(t,k) local v=format("%02X",k) t[k]=v return v end }) |
| 1327 |
local preset={ |
| 1328 |
["%02x"]=function(n) return xx[n] end, |
| 1329 |
["%02X"]=function(n) return XX[n] end, |
| 1330 |
} |
| 1331 |
local direct=P("%")*(sign+space+period+digit)^0*S("sqidfgGeExXo")*endofstring/[[local format = string.format return function(str) return format("%0",str) end]] |
| 1332 |
local function make(t,str) |
| 1333 |
local f=preset[str] |
| 1334 |
if f then |
| 1335 |
return f |
| 1336 |
end |
| 1337 |
local p=lpegmatch(direct,str) |
| 1338 |
if p then |
| 1339 |
f=loadstripped(p)() |
| 1340 |
else |
| 1341 |
n=0 |
| 1342 |
p=lpegmatch(builder,str,1,t._connector_,t._extensions_) |
| 1343 |
if n>0 then |
| 1344 |
p=format(template,preamble,t._preamble_,arguments[n],p) |
| 1345 |
f=loadstripped(p,t._environment_)() |
| 1346 |
else |
| 1347 |
f=function() return str end |
| 1348 |
end |
| 1349 |
end |
| 1350 |
t[str]=f |
| 1351 |
return f |
| 1352 |
end |
| 1353 |
local function use(t,fmt,...) |
| 1354 |
return t[fmt](...) |
| 1355 |
end |
| 1356 |
strings.formatters={} |
| 1357 |
function strings.formatters.new(noconcat) |
| 1358 |
local e={} |
| 1359 |
for k,v in next,environment do |
| 1360 |
e[k]=v |
| 1361 |
end |
| 1362 |
local t={ |
| 1363 |
_type_="formatter", |
| 1364 |
_connector_=noconcat and "," or "..", |
| 1365 |
_extensions_={}, |
| 1366 |
_preamble_="", |
| 1367 |
_environment_=e, |
| 1368 |
} |
| 1369 |
setmetatable(t,{ __index=make,__call=use }) |
| 1370 |
return t |
| 1371 |
end |
| 1372 |
local formatters=strings.formatters.new() |
| 1373 |
string.formatters=formatters |
| 1374 |
string.formatter=function(str,...) return formatters[str](...) end |
| 1375 |
local function add(t,name,template,preamble) |
| 1376 |
if type(t)=="table" and t._type_=="formatter" then |
| 1377 |
t._extensions_[name]=template or "%s" |
| 1378 |
if type(preamble)=="string" then |
| 1379 |
t._preamble_=preamble.."\n"..t._preamble_ |
| 1380 |
elseif type(preamble)=="table" then |
| 1381 |
for k,v in next,preamble do |
| 1382 |
t._environment_[k]=v |
| 1383 |
end |
| 1384 |
end |
| 1385 |
end |
| 1386 |
end |
| 1387 |
strings.formatters.add=add |
| 1388 |
patterns.xmlescape=Cs((P("<")/"<"+P(">")/">"+P("&")/"&"+P('"')/"""+anything)^0) |
| 1389 |
patterns.texescape=Cs((C(S("#$%\\{}"))/"\\%1"+anything)^0) |
| 1390 |
patterns.luaescape=Cs(((1-S('"\n'))^1+P('"')/'\\"'+P('\n')/'\\n"')^0) |
| 1391 |
patterns.luaquoted=Cs(Cc('"')*((1-S('"\n'))^1+P('"')/'\\"'+P('\n')/'\\n"')^0*Cc('"')) |
| 1392 |
add(formatters,"xml",[[lpegmatch(xmlescape,%s)]],{ xmlescape=patterns.xmlescape }) |
| 1393 |
add(formatters,"tex",[[lpegmatch(texescape,%s)]],{ texescape=patterns.texescape }) |
| 1394 |
add(formatters,"lua",[[lpegmatch(luaescape,%s)]],{ luaescape=patterns.luaescape }) |
| 1395 |
local dquote=patterns.dquote |
| 1396 |
local equote=patterns.escaped+dquote/'\\"'+1 |
| 1397 |
local cquote=Cc('"') |
| 1398 |
local pattern=Cs(dquote*(equote-P(-2))^0*dquote) |
| 1399 |
+Cs(cquote*(equote-space)^0*space*equote^0*cquote) |
| 1400 |
function string.optionalquoted(str) |
| 1401 |
return lpegmatch(pattern,str) or str |
| 1402 |
end |
| 1403 |
local pattern=Cs((newline/(os.newline or "\r")+1)^0) |
| 1404 |
function string.replacenewlines(str) |
| 1405 |
return lpegmatch(pattern,str) |
| 1406 |
end |
| 1407 |
function strings.newcollector() |
| 1408 |
local result,r={},0 |
| 1409 |
return |
| 1410 |
function(fmt,str,...) |
| 1411 |
r=r+1 |
| 1412 |
result[r]=str==nil and fmt or formatters[fmt](str,...) |
| 1413 |
end, |
| 1414 |
function(connector) |
| 1415 |
if result then |
| 1416 |
local str=concat(result,connector) |
| 1417 |
result,r={},0 |
| 1418 |
return str |
| 1419 |
end |
| 1420 |
end |
| 1421 |
end |
| 1422 |
local f_16_16=formatters["%0.5N"] |
| 1423 |
function number.to16dot16(n) |
| 1424 |
return f_16_16(n/65536.0) |
| 1425 |
end |
| 1426 |
if not string.explode then |
| 1427 |
local p_utf=patterns.utf8character |
| 1428 |
local p_check=C(p_utf)*(P("+")*Cc(true))^0 |
| 1429 |
local p_split=Ct(C(p_utf)^0) |
| 1430 |
local p_space=Ct((C(1-P(" ")^1)+P(" ")^1)^0) |
| 1431 |
function string.explode(str,symbol) |
| 1432 |
if symbol=="" then |
| 1433 |
return lpegmatch(p_split,str) |
| 1434 |
elseif symbol then |
| 1435 |
local a,b=lpegmatch(p_check,symbol) |
| 1436 |
if b then |
| 1437 |
return lpegmatch(tsplitat(P(a)^1),str) |
| 1438 |
else |
| 1439 |
return lpegmatch(tsplitat(a),str) |
| 1440 |
end |
| 1441 |
else |
| 1442 |
return lpegmatch(p_space,str) |
| 1443 |
end |
| 1444 |
end |
| 1445 |
end |
| 1446 |
do |
| 1447 |
local p_whitespace=patterns.whitespace^1 |
| 1448 |
local cache=setmetatable({},{ __index=function(t,k) |
| 1449 |
local p=tsplitat(p_whitespace*P(k)*p_whitespace) |
| 1450 |
local v=function(s) |
| 1451 |
return lpegmatch(p,s) |
| 1452 |
end |
| 1453 |
t[k]=v |
| 1454 |
return v |
| 1455 |
end }) |
| 1456 |
function string.wordsplitter(s) |
| 1457 |
return cache[s] |
| 1458 |
end |
| 1459 |
end |
| 1460 |
|
| 1461 |
end -- closure |
| 1462 |
|
| 1463 |
do -- begin closure to overcome local limits and interference |
| 1464 |
|
| 1465 |
if not modules then modules={} end modules ['util-fil']={ |
| 1466 |
version=1.001, |
| 1467 |
comment="companion to luat-lib.mkiv", |
| 1468 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 1469 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 1470 |
license="see context related readme files" |
| 1471 |
} |
| 1472 |
local byte=string.byte |
| 1473 |
local char=string.char |
| 1474 |
utilities=utilities or {} |
| 1475 |
local files={} |
| 1476 |
utilities.files=files |
| 1477 |
local zerobased={} |
| 1478 |
function files.open(filename,zb) |
| 1479 |
local f=io.open(filename,"rb") |
| 1480 |
if f then |
| 1481 |
zerobased[f]=zb or false |
| 1482 |
end |
| 1483 |
return f |
| 1484 |
end |
| 1485 |
function files.close(f) |
| 1486 |
zerobased[f]=nil |
| 1487 |
f:close() |
| 1488 |
end |
| 1489 |
function files.size(f) |
| 1490 |
local current=f:seek() |
| 1491 |
local size=f:seek("end") |
| 1492 |
f:seek("set",current) |
| 1493 |
return size |
| 1494 |
end |
| 1495 |
files.getsize=files.size |
| 1496 |
function files.setposition(f,n) |
| 1497 |
if zerobased[f] then |
| 1498 |
f:seek("set",n) |
| 1499 |
else |
| 1500 |
f:seek("set",n-1) |
| 1501 |
end |
| 1502 |
end |
| 1503 |
function files.getposition(f) |
| 1504 |
if zerobased[f] then |
| 1505 |
return f:seek() |
| 1506 |
else |
| 1507 |
return f:seek()+1 |
| 1508 |
end |
| 1509 |
end |
| 1510 |
function files.look(f,n,chars) |
| 1511 |
local p=f:seek() |
| 1512 |
local s=f:read(n) |
| 1513 |
f:seek("set",p) |
| 1514 |
if chars then |
| 1515 |
return s |
| 1516 |
else |
| 1517 |
return byte(s,1,#s) |
| 1518 |
end |
| 1519 |
end |
| 1520 |
function files.skip(f,n) |
| 1521 |
if n==1 then |
| 1522 |
f:read(n) |
| 1523 |
else |
| 1524 |
f:seek("set",f:seek()+n) |
| 1525 |
end |
| 1526 |
end |
| 1527 |
function files.readbyte(f) |
| 1528 |
return byte(f:read(1)) |
| 1529 |
end |
| 1530 |
function files.readbytes(f,n) |
| 1531 |
return byte(f:read(n),1,n) |
| 1532 |
end |
| 1533 |
function files.readbytetable(f,n) |
| 1534 |
local s=f:read(n or 1) |
| 1535 |
return { byte(s,1,#s) } |
| 1536 |
end |
| 1537 |
function files.readchar(f) |
| 1538 |
return f:read(1) |
| 1539 |
end |
| 1540 |
function files.readstring(f,n) |
| 1541 |
return f:read(n or 1) |
| 1542 |
end |
| 1543 |
function files.readinteger1(f) |
| 1544 |
local n=byte(f:read(1)) |
| 1545 |
if n>=0x80 then |
| 1546 |
return n-0x100 |
| 1547 |
else |
| 1548 |
return n |
| 1549 |
end |
| 1550 |
end |
| 1551 |
files.readcardinal1=files.readbyte |
| 1552 |
files.readcardinal=files.readcardinal1 |
| 1553 |
files.readinteger=files.readinteger1 |
| 1554 |
files.readsignedbyte=files.readinteger1 |
| 1555 |
function files.readcardinal2(f) |
| 1556 |
local a,b=byte(f:read(2),1,2) |
| 1557 |
return 0x100*a+b |
| 1558 |
end |
| 1559 |
function files.readcardinal2le(f) |
| 1560 |
local b,a=byte(f:read(2),1,2) |
| 1561 |
return 0x100*a+b |
| 1562 |
end |
| 1563 |
function files.readinteger2(f) |
| 1564 |
local a,b=byte(f:read(2),1,2) |
| 1565 |
if a>=0x80 then |
| 1566 |
return 0x100*a+b-0x10000 |
| 1567 |
else |
| 1568 |
return 0x100*a+b |
| 1569 |
end |
| 1570 |
end |
| 1571 |
function files.readinteger2le(f) |
| 1572 |
local b,a=byte(f:read(2),1,2) |
| 1573 |
if a>=0x80 then |
| 1574 |
return 0x100*a+b-0x10000 |
| 1575 |
else |
| 1576 |
return 0x100*a+b |
| 1577 |
end |
| 1578 |
end |
| 1579 |
function files.readcardinal3(f) |
| 1580 |
local a,b,c=byte(f:read(3),1,3) |
| 1581 |
return 0x10000*a+0x100*b+c |
| 1582 |
end |
| 1583 |
function files.readcardinal3le(f) |
| 1584 |
local c,b,a=byte(f:read(3),1,3) |
| 1585 |
return 0x10000*a+0x100*b+c |
| 1586 |
end |
| 1587 |
function files.readinteger3(f) |
| 1588 |
local a,b,c=byte(f:read(3),1,3) |
| 1589 |
if a>=0x80 then |
| 1590 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 1591 |
else |
| 1592 |
return 0x10000*a+0x100*b+c |
| 1593 |
end |
| 1594 |
end |
| 1595 |
function files.readinteger3le(f) |
| 1596 |
local c,b,a=byte(f:read(3),1,3) |
| 1597 |
if a>=0x80 then |
| 1598 |
return 0x10000*a+0x100*b+c-0x1000000 |
| 1599 |
else |
| 1600 |
return 0x10000*a+0x100*b+c |
| 1601 |
end |
| 1602 |
end |
| 1603 |
function files.readcardinal4(f) |
| 1604 |
local a,b,c,d=byte(f:read(4),1,4) |
| 1605 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 1606 |
end |
| 1607 |
function files.readcardinal4le(f) |
| 1608 |
local d,c,b,a=byte(f:read(4),1,4) |
| 1609 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 1610 |
end |
| 1611 |
function files.readinteger4(f) |
| 1612 |
local a,b,c,d=byte(f:read(4),1,4) |
| 1613 |
if a>=0x80 then |
| 1614 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 1615 |
else |
| 1616 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 1617 |
end |
| 1618 |
end |
| 1619 |
function files.readinteger4le(f) |
| 1620 |
local d,c,b,a=byte(f:read(4),1,4) |
| 1621 |
if a>=0x80 then |
| 1622 |
return 0x1000000*a+0x10000*b+0x100*c+d-0x100000000 |
| 1623 |
else |
| 1624 |
return 0x1000000*a+0x10000*b+0x100*c+d |
| 1625 |
end |
| 1626 |
end |
| 1627 |
function files.readfixed2(f) |
| 1628 |
local a,b=byte(f:read(2),1,2) |
| 1629 |
if a>=0x80 then |
| 1630 |
return (a-0x100)+b/0x100 |
| 1631 |
else |
| 1632 |
return (a )+b/0x100 |
| 1633 |
end |
| 1634 |
end |
| 1635 |
function files.readfixed4(f) |
| 1636 |
local a,b,c,d=byte(f:read(4),1,4) |
| 1637 |
if a>=0x80 then |
| 1638 |
return (0x100*a+b-0x10000)+(0x100*c+d)/0x10000 |
| 1639 |
else |
| 1640 |
return (0x100*a+b )+(0x100*c+d)/0x10000 |
| 1641 |
end |
| 1642 |
end |
| 1643 |
if bit32 then |
| 1644 |
local extract=bit32.extract |
| 1645 |
local band=bit32.band |
| 1646 |
function files.read2dot14(f) |
| 1647 |
local a,b=byte(f:read(2),1,2) |
| 1648 |
if a>=0x80 then |
| 1649 |
local n=-(0x100*a+b) |
| 1650 |
return-(extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 1651 |
else |
| 1652 |
local n=0x100*a+b |
| 1653 |
return (extract(n,14,2)+(band(n,0x3FFF)/16384.0)) |
| 1654 |
end |
| 1655 |
end |
| 1656 |
end |
| 1657 |
function files.skipshort(f,n) |
| 1658 |
f:read(2*(n or 1)) |
| 1659 |
end |
| 1660 |
function files.skiplong(f,n) |
| 1661 |
f:read(4*(n or 1)) |
| 1662 |
end |
| 1663 |
if bit32 then |
| 1664 |
local rshift=bit32.rshift |
| 1665 |
function files.writecardinal2(f,n) |
| 1666 |
local a=char(n%256) |
| 1667 |
n=rshift(n,8) |
| 1668 |
local b=char(n%256) |
| 1669 |
f:write(b,a) |
| 1670 |
end |
| 1671 |
else |
| 1672 |
local floor=math.floor |
| 1673 |
function files.writecardinal2(f,n) |
| 1674 |
local a=char(n%256) |
| 1675 |
n=floor(n/256) |
| 1676 |
local b=char(n%256) |
| 1677 |
f:write(b,a) |
| 1678 |
end |
| 1679 |
end |
| 1680 |
function files.writecardinal4(f,n) |
| 1681 |
local a=char(n%256) |
| 1682 |
n=rshift(n,8) |
| 1683 |
local b=char(n%256) |
| 1684 |
n=rshift(n,8) |
| 1685 |
local c=char(n%256) |
| 1686 |
n=rshift(n,8) |
| 1687 |
local d=char(n%256) |
| 1688 |
f:write(d,c,b,a) |
| 1689 |
end |
| 1690 |
function files.writestring(f,s) |
| 1691 |
f:write(char(byte(s,1,#s))) |
| 1692 |
end |
| 1693 |
function files.writebyte(f,b) |
| 1694 |
f:write(char(b)) |
| 1695 |
end |
| 1696 |
if fio and fio.readcardinal1 then |
| 1697 |
files.readcardinal1=fio.readcardinal1 |
| 1698 |
files.readcardinal2=fio.readcardinal2 |
| 1699 |
files.readcardinal3=fio.readcardinal3 |
| 1700 |
files.readcardinal4=fio.readcardinal4 |
| 1701 |
files.readinteger1=fio.readinteger1 |
| 1702 |
files.readinteger2=fio.readinteger2 |
| 1703 |
files.readinteger3=fio.readinteger3 |
| 1704 |
files.readinteger4=fio.readinteger4 |
| 1705 |
files.readfixed2=fio.readfixed2 |
| 1706 |
files.readfixed4=fio.readfixed4 |
| 1707 |
files.read2dot14=fio.read2dot14 |
| 1708 |
files.setposition=fio.setposition |
| 1709 |
files.getposition=fio.getposition |
| 1710 |
files.readbyte=files.readcardinal1 |
| 1711 |
files.readsignedbyte=files.readinteger1 |
| 1712 |
files.readcardinal=files.readcardinal1 |
| 1713 |
files.readinteger=files.readinteger1 |
| 1714 |
local skipposition=fio.skipposition |
| 1715 |
files.skipposition=skipposition |
| 1716 |
files.readbytes=fio.readbytes |
| 1717 |
files.readbytetable=fio.readbytetable |
| 1718 |
function files.skipshort(f,n) |
| 1719 |
skipposition(f,2*(n or 1)) |
| 1720 |
end |
| 1721 |
function files.skiplong(f,n) |
| 1722 |
skipposition(f,4*(n or 1)) |
| 1723 |
end |
| 1724 |
end |
| 1725 |
if fio and fio.readcardinaltable then |
| 1726 |
files.readcardinaltable=fio.readcardinaltable |
| 1727 |
files.readintegertable=fio.readintegertable |
| 1728 |
else |
| 1729 |
local readcardinal1=files.readcardinal1 |
| 1730 |
local readcardinal2=files.readcardinal2 |
| 1731 |
local readcardinal3=files.readcardinal3 |
| 1732 |
local readcardinal4=files.readcardinal4 |
| 1733 |
function files.readcardinaltable(f,n,b) |
| 1734 |
local t={} |
| 1735 |
if b==1 then for i=1,n do t[i]=readcardinal1(f) end |
| 1736 |
elseif b==2 then for i=1,n do t[i]=readcardinal2(f) end |
| 1737 |
elseif b==3 then for i=1,n do t[i]=readcardinal3(f) end |
| 1738 |
elseif b==4 then for i=1,n do t[i]=readcardinal4(f) end end |
| 1739 |
return t |
| 1740 |
end |
| 1741 |
local readinteger1=files.readinteger1 |
| 1742 |
local readinteger2=files.readinteger2 |
| 1743 |
local readinteger3=files.readinteger3 |
| 1744 |
local readinteger4=files.readinteger4 |
| 1745 |
function files.readintegertable(f,n,b) |
| 1746 |
local t={} |
| 1747 |
if b==1 then for i=1,n do t[i]=readinteger1(f) end |
| 1748 |
elseif b==2 then for i=1,n do t[i]=readinteger2(f) end |
| 1749 |
elseif b==3 then for i=1,n do t[i]=readinteger3(f) end |
| 1750 |
elseif b==4 then for i=1,n do t[i]=readinteger4(f) end end |
| 1751 |
return t |
| 1752 |
end |
| 1753 |
end |
| 1754 |
|
| 1755 |
end -- closure |
| 1756 |
|
| 1757 |
do -- begin closure to overcome local limits and interference |
| 1758 |
|
| 1759 |
if not modules then modules={} end modules ['util-tab']={ |
| 1760 |
version=1.001, |
| 1761 |
comment="companion to luat-lib.mkiv", |
| 1762 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 1763 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 1764 |
license="see context related readme files" |
| 1765 |
} |
| 1766 |
utilities=utilities or {} |
| 1767 |
utilities.tables=utilities.tables or {} |
| 1768 |
local tables=utilities.tables |
| 1769 |
local format,gmatch,gsub,sub=string.format,string.gmatch,string.gsub,string.sub |
| 1770 |
local concat,insert,remove,sort=table.concat,table.insert,table.remove,table.sort |
| 1771 |
local setmetatable,getmetatable,tonumber,tostring,rawget=setmetatable,getmetatable,tonumber,tostring,rawget |
| 1772 |
local type,next,rawset,tonumber,tostring,load,select=type,next,rawset,tonumber,tostring,load,select |
| 1773 |
local lpegmatch,P,Cs,Cc=lpeg.match,lpeg.P,lpeg.Cs,lpeg.Cc |
| 1774 |
local sortedkeys,sortedpairs=table.sortedkeys,table.sortedpairs |
| 1775 |
local formatters=string.formatters |
| 1776 |
local utftoeight=utf.toeight |
| 1777 |
local splitter=lpeg.tsplitat(".") |
| 1778 |
function utilities.tables.definetable(target,nofirst,nolast) |
| 1779 |
local composed=nil |
| 1780 |
local t={} |
| 1781 |
local snippets=lpegmatch(splitter,target) |
| 1782 |
for i=1,#snippets-(nolast and 1 or 0) do |
| 1783 |
local name=snippets[i] |
| 1784 |
if composed then |
| 1785 |
composed=composed.."."..name |
| 1786 |
t[#t+1]=formatters["if not %s then %s = { } end"](composed,composed) |
| 1787 |
else |
| 1788 |
composed=name |
| 1789 |
if not nofirst then |
| 1790 |
t[#t+1]=formatters["%s = %s or { }"](composed,composed) |
| 1791 |
end |
| 1792 |
end |
| 1793 |
end |
| 1794 |
if composed then |
| 1795 |
if nolast then |
| 1796 |
composed=composed.."."..snippets[#snippets] |
| 1797 |
end |
| 1798 |
return concat(t,"\n"),composed |
| 1799 |
else |
| 1800 |
return "",target |
| 1801 |
end |
| 1802 |
end |
| 1803 |
function tables.definedtable(...) |
| 1804 |
local t=_G |
| 1805 |
for i=1,select("#",...) do |
| 1806 |
local li=select(i,...) |
| 1807 |
local tl=t[li] |
| 1808 |
if not tl then |
| 1809 |
tl={} |
| 1810 |
t[li]=tl |
| 1811 |
end |
| 1812 |
t=tl |
| 1813 |
end |
| 1814 |
return t |
| 1815 |
end |
| 1816 |
function tables.accesstable(target,root) |
| 1817 |
local t=root or _G |
| 1818 |
for name in gmatch(target,"([^%.]+)") do |
| 1819 |
t=t[name] |
| 1820 |
if not t then |
| 1821 |
return |
| 1822 |
end |
| 1823 |
end |
| 1824 |
return t |
| 1825 |
end |
| 1826 |
function tables.migratetable(target,v,root) |
| 1827 |
local t=root or _G |
| 1828 |
local names=lpegmatch(splitter,target) |
| 1829 |
for i=1,#names-1 do |
| 1830 |
local name=names[i] |
| 1831 |
t[name]=t[name] or {} |
| 1832 |
t=t[name] |
| 1833 |
if not t then |
| 1834 |
return |
| 1835 |
end |
| 1836 |
end |
| 1837 |
t[names[#names]]=v |
| 1838 |
end |
| 1839 |
function tables.removevalue(t,value) |
| 1840 |
if value then |
| 1841 |
for i=1,#t do |
| 1842 |
if t[i]==value then |
| 1843 |
remove(t,i) |
| 1844 |
end |
| 1845 |
end |
| 1846 |
end |
| 1847 |
end |
| 1848 |
function tables.replacevalue(t,oldvalue,newvalue) |
| 1849 |
if oldvalue and newvalue then |
| 1850 |
for i=1,#t do |
| 1851 |
if t[i]==oldvalue then |
| 1852 |
t[i]=newvalue |
| 1853 |
end |
| 1854 |
end |
| 1855 |
end |
| 1856 |
end |
| 1857 |
function tables.insertbeforevalue(t,value,extra) |
| 1858 |
for i=1,#t do |
| 1859 |
if t[i]==extra then |
| 1860 |
remove(t,i) |
| 1861 |
end |
| 1862 |
end |
| 1863 |
for i=1,#t do |
| 1864 |
if t[i]==value then |
| 1865 |
insert(t,i,extra) |
| 1866 |
return |
| 1867 |
end |
| 1868 |
end |
| 1869 |
insert(t,1,extra) |
| 1870 |
end |
| 1871 |
function tables.insertaftervalue(t,value,extra) |
| 1872 |
for i=1,#t do |
| 1873 |
if t[i]==extra then |
| 1874 |
remove(t,i) |
| 1875 |
end |
| 1876 |
end |
| 1877 |
for i=1,#t do |
| 1878 |
if t[i]==value then |
| 1879 |
insert(t,i+1,extra) |
| 1880 |
return |
| 1881 |
end |
| 1882 |
end |
| 1883 |
insert(t,#t+1,extra) |
| 1884 |
end |
| 1885 |
local escape=Cs(Cc('"')*((P('"')/'""'+P(1))^0)*Cc('"')) |
| 1886 |
function table.tocsv(t,specification) |
| 1887 |
if t and #t>0 then |
| 1888 |
local result={} |
| 1889 |
local r={} |
| 1890 |
specification=specification or {} |
| 1891 |
local fields=specification.fields |
| 1892 |
if type(fields)~="string" then |
| 1893 |
fields=sortedkeys(t[1]) |
| 1894 |
end |
| 1895 |
local separator=specification.separator or "," |
| 1896 |
local noffields=#fields |
| 1897 |
if specification.preamble==true then |
| 1898 |
for f=1,noffields do |
| 1899 |
r[f]=lpegmatch(escape,tostring(fields[f])) |
| 1900 |
end |
| 1901 |
result[1]=concat(r,separator) |
| 1902 |
end |
| 1903 |
for i=1,#t do |
| 1904 |
local ti=t[i] |
| 1905 |
for f=1,noffields do |
| 1906 |
local field=ti[fields[f]] |
| 1907 |
if type(field)=="string" then |
| 1908 |
r[f]=lpegmatch(escape,field) |
| 1909 |
else |
| 1910 |
r[f]=tostring(field) |
| 1911 |
end |
| 1912 |
end |
| 1913 |
result[i+1]=concat(r,separator) |
| 1914 |
end |
| 1915 |
return concat(result,"\n") |
| 1916 |
else |
| 1917 |
return "" |
| 1918 |
end |
| 1919 |
end |
| 1920 |
local nspaces=utilities.strings.newrepeater(" ") |
| 1921 |
local function toxml(t,d,result,step) |
| 1922 |
local r=#result |
| 1923 |
for k,v in sortedpairs(t) do |
| 1924 |
local s=nspaces[d] |
| 1925 |
local tk=type(k) |
| 1926 |
local tv=type(v) |
| 1927 |
if tv=="table" then |
| 1928 |
if tk=="number" then |
| 1929 |
r=r+1 result[r]=formatters["%s<entry n='%s'>"](s,k) |
| 1930 |
toxml(v,d+step,result,step) |
| 1931 |
r=r+1 result[r]=formatters["%s</entry>"](s,k) |
| 1932 |
else |
| 1933 |
r=r+1 result[r]=formatters["%s<%s>"](s,k) |
| 1934 |
toxml(v,d+step,result,step) |
| 1935 |
r=r+1 result[r]=formatters["%s</%s>"](s,k) |
| 1936 |
end |
| 1937 |
elseif tv=="string" then |
| 1938 |
if tk=="number" then |
| 1939 |
r=r+1 result[r]=formatters["%s<entry n='%s'>%!xml!</entry>"](s,k,v,k) |
| 1940 |
else |
| 1941 |
r=r+1 result[r]=formatters["%s<%s>%!xml!</%s>"](s,k,v,k) |
| 1942 |
end |
| 1943 |
elseif tk=="number" then |
| 1944 |
r=r+1 result[r]=formatters["%s<entry n='%s'>%S</entry>"](s,k,v,k) |
| 1945 |
else |
| 1946 |
r=r+1 result[r]=formatters["%s<%s>%S</%s>"](s,k,v,k) |
| 1947 |
end |
| 1948 |
end |
| 1949 |
end |
| 1950 |
function table.toxml(t,specification) |
| 1951 |
specification=specification or {} |
| 1952 |
local name=specification.name |
| 1953 |
local noroot=name==false |
| 1954 |
local result=(specification.nobanner or noroot) and {} or { "<?xml version='1.0' standalone='yes' ?>" } |
| 1955 |
local indent=specification.indent or 0 |
| 1956 |
local spaces=specification.spaces or 1 |
| 1957 |
if noroot then |
| 1958 |
toxml(t,indent,result,spaces) |
| 1959 |
else |
| 1960 |
toxml({ [name or "data"]=t },indent,result,spaces) |
| 1961 |
end |
| 1962 |
return concat(result,"\n") |
| 1963 |
end |
| 1964 |
function tables.encapsulate(core,capsule,protect) |
| 1965 |
if type(capsule)~="table" then |
| 1966 |
protect=true |
| 1967 |
capsule={} |
| 1968 |
end |
| 1969 |
for key,value in next,core do |
| 1970 |
if capsule[key] then |
| 1971 |
print(formatters["\ninvalid %s %a in %a"]("inheritance",key,core)) |
| 1972 |
os.exit() |
| 1973 |
else |
| 1974 |
capsule[key]=value |
| 1975 |
end |
| 1976 |
end |
| 1977 |
if protect then |
| 1978 |
for key,value in next,core do |
| 1979 |
core[key]=nil |
| 1980 |
end |
| 1981 |
setmetatable(core,{ |
| 1982 |
__index=capsule, |
| 1983 |
__newindex=function(t,key,value) |
| 1984 |
if capsule[key] then |
| 1985 |
print(formatters["\ninvalid %s %a' in %a"]("overload",key,core)) |
| 1986 |
os.exit() |
| 1987 |
else |
| 1988 |
rawset(t,key,value) |
| 1989 |
end |
| 1990 |
end |
| 1991 |
} ) |
| 1992 |
end |
| 1993 |
end |
| 1994 |
if JITSUPPORTED then |
| 1995 |
local f_hashed_string=formatters["[%Q]=%Q,"] |
| 1996 |
local f_hashed_number=formatters["[%Q]=%s,"] |
| 1997 |
local f_hashed_boolean=formatters["[%Q]=%l,"] |
| 1998 |
local f_hashed_table=formatters["[%Q]="] |
| 1999 |
local f_indexed_string=formatters["[%s]=%Q,"] |
| 2000 |
local f_indexed_number=formatters["[%s]=%s,"] |
| 2001 |
local f_indexed_boolean=formatters["[%s]=%l,"] |
| 2002 |
local f_indexed_table=formatters["[%s]="] |
| 2003 |
local f_ordered_string=formatters["%Q,"] |
| 2004 |
local f_ordered_number=formatters["%s,"] |
| 2005 |
local f_ordered_boolean=formatters["%l,"] |
| 2006 |
function table.fastserialize(t,prefix) |
| 2007 |
local r={ type(prefix)=="string" and prefix or "return" } |
| 2008 |
local m=1 |
| 2009 |
local function fastserialize(t,outer) |
| 2010 |
local n=#t |
| 2011 |
m=m+1 |
| 2012 |
r[m]="{" |
| 2013 |
if n>0 then |
| 2014 |
local v=t[0] |
| 2015 |
if v then |
| 2016 |
local tv=type(v) |
| 2017 |
if tv=="string" then |
| 2018 |
m=m+1 r[m]=f_indexed_string(0,v) |
| 2019 |
elseif tv=="number" then |
| 2020 |
m=m+1 r[m]=f_indexed_number(0,v) |
| 2021 |
elseif tv=="table" then |
| 2022 |
m=m+1 r[m]=f_indexed_table(0) |
| 2023 |
fastserialize(v) |
| 2024 |
m=m+1 r[m]=f_indexed_table(0) |
| 2025 |
elseif tv=="boolean" then |
| 2026 |
m=m+1 r[m]=f_indexed_boolean(0,v) |
| 2027 |
end |
| 2028 |
end |
| 2029 |
for i=1,n do |
| 2030 |
local v=t[i] |
| 2031 |
local tv=type(v) |
| 2032 |
if tv=="string" then |
| 2033 |
m=m+1 r[m]=f_ordered_string(v) |
| 2034 |
elseif tv=="number" then |
| 2035 |
m=m+1 r[m]=f_ordered_number(v) |
| 2036 |
elseif tv=="table" then |
| 2037 |
fastserialize(v) |
| 2038 |
elseif tv=="boolean" then |
| 2039 |
m=m+1 r[m]=f_ordered_boolean(v) |
| 2040 |
end |
| 2041 |
end |
| 2042 |
end |
| 2043 |
for k,v in next,t do |
| 2044 |
local tk=type(k) |
| 2045 |
if tk=="number" then |
| 2046 |
if k>n or k<0 then |
| 2047 |
local tv=type(v) |
| 2048 |
if tv=="string" then |
| 2049 |
m=m+1 r[m]=f_indexed_string(k,v) |
| 2050 |
elseif tv=="number" then |
| 2051 |
m=m+1 r[m]=f_indexed_number(k,v) |
| 2052 |
elseif tv=="table" then |
| 2053 |
m=m+1 r[m]=f_indexed_table(k) |
| 2054 |
fastserialize(v) |
| 2055 |
elseif tv=="boolean" then |
| 2056 |
m=m+1 r[m]=f_indexed_boolean(k,v) |
| 2057 |
end |
| 2058 |
end |
| 2059 |
else |
| 2060 |
local tv=type(v) |
| 2061 |
if tv=="string" then |
| 2062 |
m=m+1 r[m]=f_hashed_string(k,v) |
| 2063 |
elseif tv=="number" then |
| 2064 |
m=m+1 r[m]=f_hashed_number(k,v) |
| 2065 |
elseif tv=="table" then |
| 2066 |
m=m+1 r[m]=f_hashed_table(k) |
| 2067 |
fastserialize(v) |
| 2068 |
elseif tv=="boolean" then |
| 2069 |
m=m+1 r[m]=f_hashed_boolean(k,v) |
| 2070 |
end |
| 2071 |
end |
| 2072 |
end |
| 2073 |
m=m+1 |
| 2074 |
if outer then |
| 2075 |
r[m]="}" |
| 2076 |
else |
| 2077 |
r[m]="}," |
| 2078 |
end |
| 2079 |
return r |
| 2080 |
end |
| 2081 |
return concat(fastserialize(t,true)) |
| 2082 |
end |
| 2083 |
else |
| 2084 |
function table.fastserialize(t,prefix) |
| 2085 |
local r={ type(prefix)=="string" and prefix or "return" } |
| 2086 |
local m=1 |
| 2087 |
local function fastserialize(t,outer) |
| 2088 |
local n=#t |
| 2089 |
m=m+1 |
| 2090 |
r[m]="{" |
| 2091 |
if n>0 then |
| 2092 |
local v=t[0] |
| 2093 |
if v then |
| 2094 |
m=m+1 |
| 2095 |
r[m]="[0]=" |
| 2096 |
if type(v)=="table" then |
| 2097 |
fastserialize(v) |
| 2098 |
else |
| 2099 |
r[m]=format("%q,",v) |
| 2100 |
end |
| 2101 |
end |
| 2102 |
for i=1,n do |
| 2103 |
local v=t[i] |
| 2104 |
m=m+1 |
| 2105 |
if type(v)=="table" then |
| 2106 |
r[m]=format("[%i]=",i) |
| 2107 |
fastserialize(v) |
| 2108 |
else |
| 2109 |
r[m]=format("[%i]=%q,",i,v) |
| 2110 |
end |
| 2111 |
end |
| 2112 |
end |
| 2113 |
for k,v in next,t do |
| 2114 |
local tk=type(k) |
| 2115 |
if tk=="number" then |
| 2116 |
if k>n or k<0 then |
| 2117 |
m=m+1 |
| 2118 |
if type(v)=="table" then |
| 2119 |
r[m]=format("[%i]=",k) |
| 2120 |
fastserialize(v) |
| 2121 |
else |
| 2122 |
r[m]=format("[%i]=%q,",k,v) |
| 2123 |
end |
| 2124 |
end |
| 2125 |
else |
| 2126 |
m=m+1 |
| 2127 |
if type(v)=="table" then |
| 2128 |
r[m]=format("[%q]=",k) |
| 2129 |
fastserialize(v) |
| 2130 |
else |
| 2131 |
r[m]=format("[%q]=%q,",k,v) |
| 2132 |
end |
| 2133 |
end |
| 2134 |
end |
| 2135 |
m=m+1 |
| 2136 |
if outer then |
| 2137 |
r[m]="}" |
| 2138 |
else |
| 2139 |
r[m]="}," |
| 2140 |
end |
| 2141 |
return r |
| 2142 |
end |
| 2143 |
return concat(fastserialize(t,true)) |
| 2144 |
end |
| 2145 |
end |
| 2146 |
function table.deserialize(str) |
| 2147 |
if not str or str=="" then |
| 2148 |
return |
| 2149 |
end |
| 2150 |
local code=load(str) |
| 2151 |
if not code then |
| 2152 |
return |
| 2153 |
end |
| 2154 |
code=code() |
| 2155 |
if not code then |
| 2156 |
return |
| 2157 |
end |
| 2158 |
return code |
| 2159 |
end |
| 2160 |
function table.load(filename,loader) |
| 2161 |
if filename then |
| 2162 |
local t=(loader or io.loaddata)(filename) |
| 2163 |
if t and t~="" then |
| 2164 |
local t=utftoeight(t) |
| 2165 |
t=load(t) |
| 2166 |
if type(t)=="function" then |
| 2167 |
t=t() |
| 2168 |
if type(t)=="table" then |
| 2169 |
return t |
| 2170 |
end |
| 2171 |
end |
| 2172 |
end |
| 2173 |
end |
| 2174 |
end |
| 2175 |
function table.save(filename,t,n,...) |
| 2176 |
io.savedata(filename,table.serialize(t,n==nil and true or n,...)) |
| 2177 |
end |
| 2178 |
local f_key_value=formatters["%s=%q"] |
| 2179 |
local f_add_table=formatters[" {%t},\n"] |
| 2180 |
local f_return_table=formatters["return {\n%t}"] |
| 2181 |
local function slowdrop(t) |
| 2182 |
local r={} |
| 2183 |
local l={} |
| 2184 |
for i=1,#t do |
| 2185 |
local ti=t[i] |
| 2186 |
local j=0 |
| 2187 |
for k,v in next,ti do |
| 2188 |
j=j+1 |
| 2189 |
l[j]=f_key_value(k,v) |
| 2190 |
end |
| 2191 |
r[i]=f_add_table(l) |
| 2192 |
end |
| 2193 |
return f_return_table(r) |
| 2194 |
end |
| 2195 |
local function fastdrop(t) |
| 2196 |
local r={ "return {\n" } |
| 2197 |
local m=1 |
| 2198 |
for i=1,#t do |
| 2199 |
local ti=t[i] |
| 2200 |
m=m+1 r[m]=" {" |
| 2201 |
for k,v in next,ti do |
| 2202 |
m=m+1 r[m]=f_key_value(k,v) |
| 2203 |
end |
| 2204 |
m=m+1 r[m]="},\n" |
| 2205 |
end |
| 2206 |
m=m+1 |
| 2207 |
r[m]="}" |
| 2208 |
return concat(r) |
| 2209 |
end |
| 2210 |
function table.drop(t,slow) |
| 2211 |
if #t==0 then |
| 2212 |
return "return { }" |
| 2213 |
elseif slow==true then |
| 2214 |
return slowdrop(t) |
| 2215 |
else |
| 2216 |
return fastdrop(t) |
| 2217 |
end |
| 2218 |
end |
| 2219 |
local selfmapper={ __index=function(t,k) t[k]=k return k end } |
| 2220 |
function table.twowaymapper(t) |
| 2221 |
if not t then |
| 2222 |
t={} |
| 2223 |
else |
| 2224 |
local zero=rawget(t,0) |
| 2225 |
for i=zero and 0 or 1,#t do |
| 2226 |
local ti=t[i] |
| 2227 |
if ti then |
| 2228 |
local i=tostring(i) |
| 2229 |
t[i]=ti |
| 2230 |
t[ti]=i |
| 2231 |
end |
| 2232 |
end |
| 2233 |
end |
| 2234 |
setmetatable(t,selfmapper) |
| 2235 |
return t |
| 2236 |
end |
| 2237 |
local f_start_key_idx=formatters["%w{"] |
| 2238 |
local f_start_key_num=JITSUPPORTED and formatters["%w[%s]={"] or formatters["%w[%q]={"] |
| 2239 |
local f_start_key_str=formatters["%w[%q]={"] |
| 2240 |
local f_start_key_boo=formatters["%w[%l]={"] |
| 2241 |
local f_start_key_nop=formatters["%w{"] |
| 2242 |
local f_stop=formatters["%w},"] |
| 2243 |
local f_key_num_value_num=JITSUPPORTED and formatters["%w[%s]=%s,"] or formatters["%w[%s]=%q,"] |
| 2244 |
local f_key_str_value_num=JITSUPPORTED and formatters["%w[%Q]=%s,"] or formatters["%w[%Q]=%q,"] |
| 2245 |
local f_key_boo_value_num=JITSUPPORTED and formatters["%w[%l]=%s,"] or formatters["%w[%l]=%q,"] |
| 2246 |
local f_key_num_value_str=JITSUPPORTED and formatters["%w[%s]=%Q,"] or formatters["%w[%q]=%Q,"] |
| 2247 |
local f_key_str_value_str=formatters["%w[%Q]=%Q,"] |
| 2248 |
local f_key_boo_value_str=formatters["%w[%l]=%Q,"] |
| 2249 |
local f_key_num_value_boo=JITSUPPORTED and formatters["%w[%s]=%l,"] or formatters["%w[%q]=%l,"] |
| 2250 |
local f_key_str_value_boo=formatters["%w[%Q]=%l,"] |
| 2251 |
local f_key_boo_value_boo=formatters["%w[%l]=%l,"] |
| 2252 |
local f_key_num_value_not=JITSUPPORTED and formatters["%w[%s]={},"] or formatters["%w[%q]={},"] |
| 2253 |
local f_key_str_value_not=formatters["%w[%Q]={},"] |
| 2254 |
local f_key_boo_value_not=formatters["%w[%l]={},"] |
| 2255 |
local f_key_num_value_seq=JITSUPPORTED and formatters["%w[%s]={ %, t },"] or formatters["%w[%q]={ %, t },"] |
| 2256 |
local f_key_str_value_seq=formatters["%w[%Q]={ %, t },"] |
| 2257 |
local f_key_boo_value_seq=formatters["%w[%l]={ %, t },"] |
| 2258 |
local f_val_num=JITSUPPORTED and formatters["%w%s,"] or formatters["%w%q,"] |
| 2259 |
local f_val_str=formatters["%w%Q,"] |
| 2260 |
local f_val_boo=formatters["%w%l,"] |
| 2261 |
local f_val_not=formatters["%w{},"] |
| 2262 |
local f_val_seq=formatters["%w{ %, t },"] |
| 2263 |
local f_fin_seq=formatters[" %, t }"] |
| 2264 |
local f_table_return=formatters["return {"] |
| 2265 |
local f_table_name=formatters["%s={"] |
| 2266 |
local f_table_direct=formatters["{"] |
| 2267 |
local f_table_entry=formatters["[%Q]={"] |
| 2268 |
local f_table_finish=formatters["}"] |
| 2269 |
local spaces=utilities.strings.newrepeater(" ") |
| 2270 |
local original_serialize=table.serialize |
| 2271 |
local is_simple_table=table.is_simple_table |
| 2272 |
local function serialize(root,name,specification) |
| 2273 |
if type(specification)=="table" then |
| 2274 |
return original_serialize(root,name,specification) |
| 2275 |
end |
| 2276 |
local t |
| 2277 |
local n=1 |
| 2278 |
local unknown=false |
| 2279 |
local function do_serialize(root,name,depth,level,indexed) |
| 2280 |
if level>0 then |
| 2281 |
n=n+1 |
| 2282 |
if indexed then |
| 2283 |
t[n]=f_start_key_idx(depth) |
| 2284 |
else |
| 2285 |
local tn=type(name) |
| 2286 |
if tn=="number" then |
| 2287 |
t[n]=f_start_key_num(depth,name) |
| 2288 |
elseif tn=="string" then |
| 2289 |
t[n]=f_start_key_str(depth,name) |
| 2290 |
elseif tn=="boolean" then |
| 2291 |
t[n]=f_start_key_boo(depth,name) |
| 2292 |
else |
| 2293 |
t[n]=f_start_key_nop(depth) |
| 2294 |
end |
| 2295 |
end |
| 2296 |
depth=depth+1 |
| 2297 |
end |
| 2298 |
if root and next(root)~=nil then |
| 2299 |
local first=nil |
| 2300 |
local last=#root |
| 2301 |
if last>0 then |
| 2302 |
for k=1,last do |
| 2303 |
if rawget(root,k)==nil then |
| 2304 |
last=k-1 |
| 2305 |
break |
| 2306 |
end |
| 2307 |
end |
| 2308 |
if last>0 then |
| 2309 |
first=1 |
| 2310 |
end |
| 2311 |
end |
| 2312 |
local sk=sortedkeys(root) |
| 2313 |
for i=1,#sk do |
| 2314 |
local k=sk[i] |
| 2315 |
local v=root[k] |
| 2316 |
local tv=type(v) |
| 2317 |
local tk=type(k) |
| 2318 |
if first and tk=="number" and k<=last and k>=first then |
| 2319 |
if tv=="number" then |
| 2320 |
n=n+1 t[n]=f_val_num(depth,v) |
| 2321 |
elseif tv=="string" then |
| 2322 |
n=n+1 t[n]=f_val_str(depth,v) |
| 2323 |
elseif tv=="table" then |
| 2324 |
if next(v)==nil then |
| 2325 |
n=n+1 t[n]=f_val_not(depth) |
| 2326 |
else |
| 2327 |
local st=is_simple_table(v) |
| 2328 |
if st then |
| 2329 |
n=n+1 t[n]=f_val_seq(depth,st) |
| 2330 |
else |
| 2331 |
do_serialize(v,k,depth,level+1,true) |
| 2332 |
end |
| 2333 |
end |
| 2334 |
elseif tv=="boolean" then |
| 2335 |
n=n+1 t[n]=f_val_boo(depth,v) |
| 2336 |
elseif unknown then |
| 2337 |
n=n+1 t[n]=f_val_str(depth,tostring(v)) |
| 2338 |
end |
| 2339 |
elseif tv=="number" then |
| 2340 |
if tk=="number" then |
| 2341 |
n=n+1 t[n]=f_key_num_value_num(depth,k,v) |
| 2342 |
elseif tk=="string" then |
| 2343 |
n=n+1 t[n]=f_key_str_value_num(depth,k,v) |
| 2344 |
elseif tk=="boolean" then |
| 2345 |
n=n+1 t[n]=f_key_boo_value_num(depth,k,v) |
| 2346 |
elseif unknown then |
| 2347 |
n=n+1 t[n]=f_key_str_value_num(depth,tostring(k),v) |
| 2348 |
end |
| 2349 |
elseif tv=="string" then |
| 2350 |
if tk=="number" then |
| 2351 |
n=n+1 t[n]=f_key_num_value_str(depth,k,v) |
| 2352 |
elseif tk=="string" then |
| 2353 |
n=n+1 t[n]=f_key_str_value_str(depth,k,v) |
| 2354 |
elseif tk=="boolean" then |
| 2355 |
n=n+1 t[n]=f_key_boo_value_str(depth,k,v) |
| 2356 |
elseif unknown then |
| 2357 |
n=n+1 t[n]=f_key_str_value_str(depth,tostring(k),v) |
| 2358 |
end |
| 2359 |
elseif tv=="table" then |
| 2360 |
if next(v)==nil then |
| 2361 |
if tk=="number" then |
| 2362 |
n=n+1 t[n]=f_key_num_value_not(depth,k) |
| 2363 |
elseif tk=="string" then |
| 2364 |
n=n+1 t[n]=f_key_str_value_not(depth,k) |
| 2365 |
elseif tk=="boolean" then |
| 2366 |
n=n+1 t[n]=f_key_boo_value_not(depth,k) |
| 2367 |
elseif unknown then |
| 2368 |
n=n+1 t[n]=f_key_str_value_not(depth,tostring(k)) |
| 2369 |
end |
| 2370 |
else |
| 2371 |
local st=is_simple_table(v) |
| 2372 |
if not st then |
| 2373 |
do_serialize(v,k,depth,level+1) |
| 2374 |
elseif tk=="number" then |
| 2375 |
n=n+1 t[n]=f_key_num_value_seq(depth,k,st) |
| 2376 |
elseif tk=="string" then |
| 2377 |
n=n+1 t[n]=f_key_str_value_seq(depth,k,st) |
| 2378 |
elseif tk=="boolean" then |
| 2379 |
n=n+1 t[n]=f_key_boo_value_seq(depth,k,st) |
| 2380 |
elseif unknown then |
| 2381 |
n=n+1 t[n]=f_key_str_value_seq(depth,tostring(k),st) |
| 2382 |
end |
| 2383 |
end |
| 2384 |
elseif tv=="boolean" then |
| 2385 |
if tk=="number" then |
| 2386 |
n=n+1 t[n]=f_key_num_value_boo(depth,k,v) |
| 2387 |
elseif tk=="string" then |
| 2388 |
n=n+1 t[n]=f_key_str_value_boo(depth,k,v) |
| 2389 |
elseif tk=="boolean" then |
| 2390 |
n=n+1 t[n]=f_key_boo_value_boo(depth,k,v) |
| 2391 |
elseif unknown then |
| 2392 |
n=n+1 t[n]=f_key_str_value_boo(depth,tostring(k),v) |
| 2393 |
end |
| 2394 |
else |
| 2395 |
if tk=="number" then |
| 2396 |
n=n+1 t[n]=f_key_num_value_str(depth,k,tostring(v)) |
| 2397 |
elseif tk=="string" then |
| 2398 |
n=n+1 t[n]=f_key_str_value_str(depth,k,tostring(v)) |
| 2399 |
elseif tk=="boolean" then |
| 2400 |
n=n+1 t[n]=f_key_boo_value_str(depth,k,tostring(v)) |
| 2401 |
elseif unknown then |
| 2402 |
n=n+1 t[n]=f_key_str_value_str(depth,tostring(k),tostring(v)) |
| 2403 |
end |
| 2404 |
end |
| 2405 |
end |
| 2406 |
end |
| 2407 |
if level>0 then |
| 2408 |
n=n+1 t[n]=f_stop(depth-1) |
| 2409 |
end |
| 2410 |
end |
| 2411 |
local tname=type(name) |
| 2412 |
if tname=="string" then |
| 2413 |
if name=="return" then |
| 2414 |
t={ f_table_return() } |
| 2415 |
else |
| 2416 |
t={ f_table_name(name) } |
| 2417 |
end |
| 2418 |
elseif tname=="number" then |
| 2419 |
t={ f_table_entry(name) } |
| 2420 |
elseif tname=="boolean" then |
| 2421 |
if name then |
| 2422 |
t={ f_table_return() } |
| 2423 |
else |
| 2424 |
t={ f_table_direct() } |
| 2425 |
end |
| 2426 |
else |
| 2427 |
t={ f_table_name("t") } |
| 2428 |
end |
| 2429 |
if root then |
| 2430 |
if getmetatable(root) then |
| 2431 |
local dummy=root._w_h_a_t_e_v_e_r_ |
| 2432 |
root._w_h_a_t_e_v_e_r_=nil |
| 2433 |
end |
| 2434 |
if next(root)~=nil then |
| 2435 |
local st=is_simple_table(root) |
| 2436 |
if st then |
| 2437 |
return t[1]..f_fin_seq(st) |
| 2438 |
else |
| 2439 |
do_serialize(root,name,1,0) |
| 2440 |
end |
| 2441 |
end |
| 2442 |
end |
| 2443 |
n=n+1 |
| 2444 |
t[n]=f_table_finish() |
| 2445 |
return concat(t,"\n") |
| 2446 |
end |
| 2447 |
table.serialize=serialize |
| 2448 |
if setinspector then |
| 2449 |
setinspector("table",function(v) |
| 2450 |
if type(v)=="table" then |
| 2451 |
print(serialize(v,"table",{ metacheck=false })) |
| 2452 |
return true |
| 2453 |
end |
| 2454 |
end) |
| 2455 |
end |
| 2456 |
local mt={ |
| 2457 |
__newindex=function(t,k,v) |
| 2458 |
local n=t.last+1 |
| 2459 |
t.last=n |
| 2460 |
t.list[n]=k |
| 2461 |
t.hash[k]=v |
| 2462 |
end, |
| 2463 |
__index=function(t,k) |
| 2464 |
return t.hash[k] |
| 2465 |
end, |
| 2466 |
__len=function(t) |
| 2467 |
return t.last |
| 2468 |
end, |
| 2469 |
} |
| 2470 |
function table.orderedhash() |
| 2471 |
return setmetatable({ list={},hash={},last=0 },mt) |
| 2472 |
end |
| 2473 |
function table.ordered(t) |
| 2474 |
local n=t.last |
| 2475 |
if n>0 then |
| 2476 |
local l=t.list |
| 2477 |
local i=1 |
| 2478 |
local h=t.hash |
| 2479 |
local f=function() |
| 2480 |
if i<=n then |
| 2481 |
local k=i |
| 2482 |
local v=h[l[k]] |
| 2483 |
i=i+1 |
| 2484 |
return k,v |
| 2485 |
end |
| 2486 |
end |
| 2487 |
return f,1,h[l[1]] |
| 2488 |
else |
| 2489 |
return function() end |
| 2490 |
end |
| 2491 |
end |
| 2492 |
function combine(target,source) |
| 2493 |
if target then |
| 2494 |
for k,v in next,source do |
| 2495 |
if type(v)=="table" then |
| 2496 |
target[k]=combine(target[k],source[k]) |
| 2497 |
else |
| 2498 |
target[k]=v |
| 2499 |
end |
| 2500 |
end |
| 2501 |
return target |
| 2502 |
else |
| 2503 |
return source |
| 2504 |
end |
| 2505 |
end |
| 2506 |
table.combine=combine |
| 2507 |
|
| 2508 |
end -- closure |
| 2509 |
|
| 2510 |
do -- begin closure to overcome local limits and interference |
| 2511 |
|
| 2512 |
if not modules then modules={} end modules ['util-sto']={ |
| 2513 |
version=1.001, |
| 2514 |
comment="companion to luat-lib.mkiv", |
| 2515 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2516 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2517 |
license="see context related readme files" |
| 2518 |
} |
| 2519 |
local setmetatable,getmetatable,rawset,type=setmetatable,getmetatable,rawset,type |
| 2520 |
utilities=utilities or {} |
| 2521 |
utilities.storage=utilities.storage or {} |
| 2522 |
local storage=utilities.storage |
| 2523 |
function storage.mark(t) |
| 2524 |
if not t then |
| 2525 |
print("\nfatal error: storage cannot be marked\n") |
| 2526 |
os.exit() |
| 2527 |
return |
| 2528 |
end |
| 2529 |
local m=getmetatable(t) |
| 2530 |
if not m then |
| 2531 |
m={} |
| 2532 |
setmetatable(t,m) |
| 2533 |
end |
| 2534 |
m.__storage__=true |
| 2535 |
return t |
| 2536 |
end |
| 2537 |
function storage.allocate(t) |
| 2538 |
t=t or {} |
| 2539 |
local m=getmetatable(t) |
| 2540 |
if not m then |
| 2541 |
m={} |
| 2542 |
setmetatable(t,m) |
| 2543 |
end |
| 2544 |
m.__storage__=true |
| 2545 |
return t |
| 2546 |
end |
| 2547 |
function storage.marked(t) |
| 2548 |
local m=getmetatable(t) |
| 2549 |
return m and m.__storage__ |
| 2550 |
end |
| 2551 |
function storage.checked(t) |
| 2552 |
if not t then |
| 2553 |
report("\nfatal error: storage has not been allocated\n") |
| 2554 |
os.exit() |
| 2555 |
return |
| 2556 |
end |
| 2557 |
return t |
| 2558 |
end |
| 2559 |
function storage.setinitializer(data,initialize) |
| 2560 |
local m=getmetatable(data) or {} |
| 2561 |
m.__index=function(data,k) |
| 2562 |
m.__index=nil |
| 2563 |
initialize() |
| 2564 |
return data[k] |
| 2565 |
end |
| 2566 |
setmetatable(data,m) |
| 2567 |
end |
| 2568 |
local keyisvalue={ __index=function(t,k) |
| 2569 |
t[k]=k |
| 2570 |
return k |
| 2571 |
end } |
| 2572 |
function storage.sparse(t) |
| 2573 |
t=t or {} |
| 2574 |
setmetatable(t,keyisvalue) |
| 2575 |
return t |
| 2576 |
end |
| 2577 |
local function f_empty () return "" end |
| 2578 |
local function f_self (t,k) t[k]=k return k end |
| 2579 |
local function f_table (t,k) local v={} t[k]=v return v end |
| 2580 |
local function f_number(t,k) t[k]=0 return 0 end |
| 2581 |
local function f_ignore() end |
| 2582 |
local f_index={ |
| 2583 |
["empty"]=f_empty, |
| 2584 |
["self"]=f_self, |
| 2585 |
["table"]=f_table, |
| 2586 |
["number"]=f_number, |
| 2587 |
} |
| 2588 |
function table.setmetatableindex(t,f) |
| 2589 |
if type(t)~="table" then |
| 2590 |
f,t=t,{} |
| 2591 |
end |
| 2592 |
local m=getmetatable(t) |
| 2593 |
local i=f_index[f] or f |
| 2594 |
if m then |
| 2595 |
m.__index=i |
| 2596 |
else |
| 2597 |
setmetatable(t,{ __index=i }) |
| 2598 |
end |
| 2599 |
return t |
| 2600 |
end |
| 2601 |
local f_index={ |
| 2602 |
["ignore"]=f_ignore, |
| 2603 |
} |
| 2604 |
function table.setmetatablenewindex(t,f) |
| 2605 |
if type(t)~="table" then |
| 2606 |
f,t=t,{} |
| 2607 |
end |
| 2608 |
local m=getmetatable(t) |
| 2609 |
local i=f_index[f] or f |
| 2610 |
if m then |
| 2611 |
m.__newindex=i |
| 2612 |
else |
| 2613 |
setmetatable(t,{ __newindex=i }) |
| 2614 |
end |
| 2615 |
return t |
| 2616 |
end |
| 2617 |
function table.setmetatablecall(t,f) |
| 2618 |
if type(t)~="table" then |
| 2619 |
f,t=t,{} |
| 2620 |
end |
| 2621 |
local m=getmetatable(t) |
| 2622 |
if m then |
| 2623 |
m.__call=f |
| 2624 |
else |
| 2625 |
setmetatable(t,{ __call=f }) |
| 2626 |
end |
| 2627 |
return t |
| 2628 |
end |
| 2629 |
function table.setmetatableindices(t,f,n,c) |
| 2630 |
if type(t)~="table" then |
| 2631 |
f,t=t,{} |
| 2632 |
end |
| 2633 |
local m=getmetatable(t) |
| 2634 |
local i=f_index[f] or f |
| 2635 |
if m then |
| 2636 |
m.__index=i |
| 2637 |
m.__newindex=n |
| 2638 |
m.__call=c |
| 2639 |
else |
| 2640 |
setmetatable(t,{ |
| 2641 |
__index=i, |
| 2642 |
__newindex=n, |
| 2643 |
__call=c, |
| 2644 |
}) |
| 2645 |
end |
| 2646 |
return t |
| 2647 |
end |
| 2648 |
function table.setmetatablekey(t,key,value) |
| 2649 |
local m=getmetatable(t) |
| 2650 |
if not m then |
| 2651 |
m={} |
| 2652 |
setmetatable(t,m) |
| 2653 |
end |
| 2654 |
m[key]=value |
| 2655 |
return t |
| 2656 |
end |
| 2657 |
function table.getmetatablekey(t,key,value) |
| 2658 |
local m=getmetatable(t) |
| 2659 |
return m and m[key] |
| 2660 |
end |
| 2661 |
function table.makeweak(t) |
| 2662 |
if not t then |
| 2663 |
t={} |
| 2664 |
end |
| 2665 |
local m=getmetatable(t) |
| 2666 |
if m then |
| 2667 |
m.__mode="v" |
| 2668 |
else |
| 2669 |
setmetatable(t,{ __mode="v" }) |
| 2670 |
end |
| 2671 |
return t |
| 2672 |
end |
| 2673 |
|
| 2674 |
end -- closure |
| 2675 |
|
| 2676 |
do -- begin closure to overcome local limits and interference |
| 2677 |
|
| 2678 |
if not modules then modules={} end modules ['util-prs']={ |
| 2679 |
version=1.001, |
| 2680 |
comment="companion to luat-lib.mkiv", |
| 2681 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 2682 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 2683 |
license="see context related readme files" |
| 2684 |
} |
| 2685 |
local lpeg,table,string=lpeg,table,string |
| 2686 |
local P,R,V,S,C,Ct,Cs,Carg,Cc,Cg,Cf,Cp=lpeg.P,lpeg.R,lpeg.V,lpeg.S,lpeg.C,lpeg.Ct,lpeg.Cs,lpeg.Carg,lpeg.Cc,lpeg.Cg,lpeg.Cf,lpeg.Cp |
| 2687 |
local lpegmatch,lpegpatterns=lpeg.match,lpeg.patterns |
| 2688 |
local concat,gmatch,find=table.concat,string.gmatch,string.find |
| 2689 |
local tonumber,tostring,type,next,rawset=tonumber,tostring,type,next,rawset |
| 2690 |
local mod,div=math.mod,math.div |
| 2691 |
utilities=utilities or {} |
| 2692 |
local parsers=utilities.parsers or {} |
| 2693 |
utilities.parsers=parsers |
| 2694 |
local patterns=parsers.patterns or {} |
| 2695 |
parsers.patterns=patterns |
| 2696 |
local setmetatableindex=table.setmetatableindex |
| 2697 |
local sortedhash=table.sortedhash |
| 2698 |
local sortedkeys=table.sortedkeys |
| 2699 |
local tohash=table.tohash |
| 2700 |
local hashes={} |
| 2701 |
parsers.hashes=hashes |
| 2702 |
local digit=R("09") |
| 2703 |
local space=P(' ') |
| 2704 |
local equal=P("=") |
| 2705 |
local colon=P(":") |
| 2706 |
local comma=P(",") |
| 2707 |
local lbrace=P("{") |
| 2708 |
local rbrace=P("}") |
| 2709 |
local lparent=P("(") |
| 2710 |
local rparent=P(")") |
| 2711 |
local lbracket=P("[") |
| 2712 |
local rbracket=P("]") |
| 2713 |
local period=S(".") |
| 2714 |
local punctuation=S(".,:;") |
| 2715 |
local spacer=lpegpatterns.spacer |
| 2716 |
local whitespace=lpegpatterns.whitespace |
| 2717 |
local newline=lpegpatterns.newline |
| 2718 |
local anything=lpegpatterns.anything |
| 2719 |
local endofstring=lpegpatterns.endofstring |
| 2720 |
local nobrace=1-(lbrace+rbrace ) |
| 2721 |
local noparent=1-(lparent+rparent) |
| 2722 |
local nobracket=1-(lbracket+rbracket) |
| 2723 |
local escape,left,right=P("\\"),P('{'),P('}') |
| 2724 |
lpegpatterns.balanced=P { |
| 2725 |
((escape*(left+right))+(1-(left+right))+V(2))^0, |
| 2726 |
left*V(1)*right |
| 2727 |
} |
| 2728 |
local nestedbraces=P { lbrace*(nobrace+V(1))^0*rbrace } |
| 2729 |
local nestedparents=P { lparent*(noparent+V(1))^0*rparent } |
| 2730 |
local nestedbrackets=P { lbracket*(nobracket+V(1))^0*rbracket } |
| 2731 |
local spaces=space^0 |
| 2732 |
local argument=Cs((lbrace/"")*((nobrace+nestedbraces)^0)*(rbrace/"")) |
| 2733 |
local content=(1-endofstring)^0 |
| 2734 |
lpegpatterns.nestedbraces=nestedbraces |
| 2735 |
lpegpatterns.nestedparents=nestedparents |
| 2736 |
lpegpatterns.nestedbrackets=nestedbrackets |
| 2737 |
lpegpatterns.nested=nestedbraces |
| 2738 |
lpegpatterns.argument=argument |
| 2739 |
lpegpatterns.content=content |
| 2740 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace+C((nestedbraces+(1-comma))^0) |
| 2741 |
local key=C((1-equal-comma)^1) |
| 2742 |
local pattern_a=(space+comma)^0*(key*equal*value+key*C("")) |
| 2743 |
local pattern_c=(space+comma)^0*(key*equal*value) |
| 2744 |
local pattern_d=(space+comma)^0*(key*(equal+colon)*value+key*C("")) |
| 2745 |
local key=C((1-space-equal-comma)^1) |
| 2746 |
local pattern_b=spaces*comma^0*spaces*(key*((spaces*equal*spaces*value)+C(""))) |
| 2747 |
local hash={} |
| 2748 |
local function set(key,value) |
| 2749 |
hash[key]=value |
| 2750 |
end |
| 2751 |
local pattern_a_s=(pattern_a/set)^1 |
| 2752 |
local pattern_b_s=(pattern_b/set)^1 |
| 2753 |
local pattern_c_s=(pattern_c/set)^1 |
| 2754 |
local pattern_d_s=(pattern_d/set)^1 |
| 2755 |
patterns.settings_to_hash_a=pattern_a_s |
| 2756 |
patterns.settings_to_hash_b=pattern_b_s |
| 2757 |
patterns.settings_to_hash_c=pattern_c_s |
| 2758 |
patterns.settings_to_hash_d=pattern_d_s |
| 2759 |
function parsers.make_settings_to_hash_pattern(set,how) |
| 2760 |
if how=="strict" then |
| 2761 |
return (pattern_c/set)^1 |
| 2762 |
elseif how=="tolerant" then |
| 2763 |
return (pattern_b/set)^1 |
| 2764 |
else |
| 2765 |
return (pattern_a/set)^1 |
| 2766 |
end |
| 2767 |
end |
| 2768 |
function parsers.settings_to_hash(str,existing) |
| 2769 |
if not str or str=="" then |
| 2770 |
return {} |
| 2771 |
elseif type(str)=="table" then |
| 2772 |
if existing then |
| 2773 |
for k,v in next,str do |
| 2774 |
existing[k]=v |
| 2775 |
end |
| 2776 |
return exiting |
| 2777 |
else |
| 2778 |
return str |
| 2779 |
end |
| 2780 |
else |
| 2781 |
hash=existing or {} |
| 2782 |
lpegmatch(pattern_a_s,str) |
| 2783 |
return hash |
| 2784 |
end |
| 2785 |
end |
| 2786 |
function parsers.settings_to_hash_colon_too(str) |
| 2787 |
if not str or str=="" then |
| 2788 |
return {} |
| 2789 |
elseif type(str)=="table" then |
| 2790 |
return str |
| 2791 |
else |
| 2792 |
hash={} |
| 2793 |
lpegmatch(pattern_d_s,str) |
| 2794 |
return hash |
| 2795 |
end |
| 2796 |
end |
| 2797 |
function parsers.settings_to_hash_tolerant(str,existing) |
| 2798 |
if not str or str=="" then |
| 2799 |
return {} |
| 2800 |
elseif type(str)=="table" then |
| 2801 |
if existing then |
| 2802 |
for k,v in next,str do |
| 2803 |
existing[k]=v |
| 2804 |
end |
| 2805 |
return exiting |
| 2806 |
else |
| 2807 |
return str |
| 2808 |
end |
| 2809 |
else |
| 2810 |
hash=existing or {} |
| 2811 |
lpegmatch(pattern_b_s,str) |
| 2812 |
return hash |
| 2813 |
end |
| 2814 |
end |
| 2815 |
function parsers.settings_to_hash_strict(str,existing) |
| 2816 |
if not str or str=="" then |
| 2817 |
return nil |
| 2818 |
elseif type(str)=="table" then |
| 2819 |
if existing then |
| 2820 |
for k,v in next,str do |
| 2821 |
existing[k]=v |
| 2822 |
end |
| 2823 |
return exiting |
| 2824 |
else |
| 2825 |
return str |
| 2826 |
end |
| 2827 |
elseif str and str~="" then |
| 2828 |
hash=existing or {} |
| 2829 |
lpegmatch(pattern_c_s,str) |
| 2830 |
return next(hash) and hash |
| 2831 |
end |
| 2832 |
end |
| 2833 |
local separator=comma*space^0 |
| 2834 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace+C((nestedbraces+(1-comma))^0) |
| 2835 |
local pattern=spaces*Ct(value*(separator*value)^0) |
| 2836 |
patterns.settings_to_array=pattern |
| 2837 |
function parsers.settings_to_array(str,strict) |
| 2838 |
if not str or str=="" then |
| 2839 |
return {} |
| 2840 |
elseif type(str)=="table" then |
| 2841 |
return str |
| 2842 |
elseif strict then |
| 2843 |
if find(str,"{",1,true) then |
| 2844 |
return lpegmatch(pattern,str) |
| 2845 |
else |
| 2846 |
return { str } |
| 2847 |
end |
| 2848 |
elseif find(str,",",1,true) then |
| 2849 |
return lpegmatch(pattern,str) |
| 2850 |
else |
| 2851 |
return { str } |
| 2852 |
end |
| 2853 |
end |
| 2854 |
function parsers.settings_to_numbers(str) |
| 2855 |
if not str or str=="" then |
| 2856 |
return {} |
| 2857 |
end |
| 2858 |
if type(str)=="table" then |
| 2859 |
elseif find(str,",",1,true) then |
| 2860 |
str=lpegmatch(pattern,str) |
| 2861 |
else |
| 2862 |
return { tonumber(str) } |
| 2863 |
end |
| 2864 |
for i=1,#str do |
| 2865 |
str[i]=tonumber(str[i]) |
| 2866 |
end |
| 2867 |
return str |
| 2868 |
end |
| 2869 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace+C((nestedbraces+nestedbrackets+nestedparents+(1-comma))^0) |
| 2870 |
local pattern=spaces*Ct(value*(separator*value)^0) |
| 2871 |
function parsers.settings_to_array_obey_fences(str) |
| 2872 |
return lpegmatch(pattern,str) |
| 2873 |
end |
| 2874 |
local cache_a={} |
| 2875 |
local cache_b={} |
| 2876 |
function parsers.groupedsplitat(symbol,withaction) |
| 2877 |
if not symbol then |
| 2878 |
symbol="," |
| 2879 |
end |
| 2880 |
local pattern=(withaction and cache_b or cache_a)[symbol] |
| 2881 |
if not pattern then |
| 2882 |
local symbols=S(symbol) |
| 2883 |
local separator=space^0*symbols*space^0 |
| 2884 |
local value=lbrace*C((nobrace+nestedbraces)^0) |
| 2885 |
*(rbrace*(#symbols+P(-1))) |
| 2886 |
+C((nestedbraces+(1-(space^0*(symbols+P(-1)))))^0) |
| 2887 |
if withaction then |
| 2888 |
local withvalue=Carg(1)*value/function(f,s) return f(s) end |
| 2889 |
pattern=spaces*withvalue*(separator*withvalue)^0 |
| 2890 |
cache_b[symbol]=pattern |
| 2891 |
else |
| 2892 |
pattern=spaces*Ct(value*(separator*value)^0) |
| 2893 |
cache_a[symbol]=pattern |
| 2894 |
end |
| 2895 |
end |
| 2896 |
return pattern |
| 2897 |
end |
| 2898 |
local pattern_a=parsers.groupedsplitat(",",false) |
| 2899 |
local pattern_b=parsers.groupedsplitat(",",true) |
| 2900 |
function parsers.stripped_settings_to_array(str) |
| 2901 |
if not str or str=="" then |
| 2902 |
return {} |
| 2903 |
else |
| 2904 |
return lpegmatch(pattern_a,str) |
| 2905 |
end |
| 2906 |
end |
| 2907 |
function parsers.process_stripped_settings(str,action) |
| 2908 |
if not str or str=="" then |
| 2909 |
return {} |
| 2910 |
else |
| 2911 |
return lpegmatch(pattern_b,str,1,action) |
| 2912 |
end |
| 2913 |
end |
| 2914 |
local function set(t,v) |
| 2915 |
t[#t+1]=v |
| 2916 |
end |
| 2917 |
local value=P(Carg(1)*value)/set |
| 2918 |
local pattern=value*(separator*value)^0*Carg(1) |
| 2919 |
function parsers.add_settings_to_array(t,str) |
| 2920 |
return lpegmatch(pattern,str,nil,t) |
| 2921 |
end |
| 2922 |
function parsers.hash_to_string(h,separator,yes,no,strict,omit) |
| 2923 |
if h then |
| 2924 |
local t={} |
| 2925 |
local tn=0 |
| 2926 |
local s=sortedkeys(h) |
| 2927 |
omit=omit and tohash(omit) |
| 2928 |
for i=1,#s do |
| 2929 |
local key=s[i] |
| 2930 |
if not omit or not omit[key] then |
| 2931 |
local value=h[key] |
| 2932 |
if type(value)=="boolean" then |
| 2933 |
if yes and no then |
| 2934 |
if value then |
| 2935 |
tn=tn+1 |
| 2936 |
t[tn]=key..'='..yes |
| 2937 |
elseif not strict then |
| 2938 |
tn=tn+1 |
| 2939 |
t[tn]=key..'='..no |
| 2940 |
end |
| 2941 |
elseif value or not strict then |
| 2942 |
tn=tn+1 |
| 2943 |
t[tn]=key..'='..tostring(value) |
| 2944 |
end |
| 2945 |
else |
| 2946 |
tn=tn+1 |
| 2947 |
t[tn]=key..'='..value |
| 2948 |
end |
| 2949 |
end |
| 2950 |
end |
| 2951 |
return concat(t,separator or ",") |
| 2952 |
else |
| 2953 |
return "" |
| 2954 |
end |
| 2955 |
end |
| 2956 |
function parsers.array_to_string(a,separator) |
| 2957 |
if a then |
| 2958 |
return concat(a,separator or ",") |
| 2959 |
else |
| 2960 |
return "" |
| 2961 |
end |
| 2962 |
end |
| 2963 |
local pattern=Cf(Ct("")*Cg(C((1-S(", "))^1)*S(", ")^0*Cc(true))^1,rawset) |
| 2964 |
function parsers.settings_to_set(str) |
| 2965 |
return str and lpegmatch(pattern,str) or {} |
| 2966 |
end |
| 2967 |
hashes.settings_to_set=table.setmetatableindex(function(t,k) |
| 2968 |
local v=k and lpegmatch(pattern,k) or {} |
| 2969 |
t[k]=v |
| 2970 |
return v |
| 2971 |
end) |
| 2972 |
function parsers.settings_to_set(str) |
| 2973 |
return str and lpegmatch(pattern,str) or {} |
| 2974 |
end |
| 2975 |
local pattern=Ct((C((1-S(", "))^1)*S(", ")^0)^1) |
| 2976 |
hashes.settings_to_list=table.setmetatableindex(function(t,k) |
| 2977 |
local v=k and lpegmatch(pattern,k) or {} |
| 2978 |
t[k]=v |
| 2979 |
return v |
| 2980 |
end) |
| 2981 |
getmetatable(hashes.settings_to_set ).__mode="kv" |
| 2982 |
getmetatable(hashes.settings_to_list).__mode="kv" |
| 2983 |
function parsers.simple_hash_to_string(h,separator) |
| 2984 |
local t={} |
| 2985 |
local tn=0 |
| 2986 |
for k,v in sortedhash(h) do |
| 2987 |
if v then |
| 2988 |
tn=tn+1 |
| 2989 |
t[tn]=k |
| 2990 |
end |
| 2991 |
end |
| 2992 |
return concat(t,separator or ",") |
| 2993 |
end |
| 2994 |
local str=Cs(lpegpatterns.unquoted)+C((1-whitespace-equal)^1) |
| 2995 |
local setting=Cf(Carg(1)*(whitespace^0*Cg(str*whitespace^0*(equal*whitespace^0*str+Cc(""))))^1,rawset) |
| 2996 |
local splitter=setting^1 |
| 2997 |
function parsers.options_to_hash(str,target) |
| 2998 |
return str and lpegmatch(splitter,str,1,target or {}) or {} |
| 2999 |
end |
| 3000 |
local splitter=lpeg.tsplitat(" ") |
| 3001 |
function parsers.options_to_array(str) |
| 3002 |
return str and lpegmatch(splitter,str) or {} |
| 3003 |
end |
| 3004 |
local value=P(lbrace*C((nobrace+nestedbraces)^0)*rbrace)+C(digit^1*lparent*(noparent+nestedparents)^1*rparent)+C((nestedbraces+(1-comma))^1) |
| 3005 |
local pattern_a=spaces*Ct(value*(separator*value)^0) |
| 3006 |
local function repeater(n,str) |
| 3007 |
if not n then |
| 3008 |
return str |
| 3009 |
else |
| 3010 |
local s=lpegmatch(pattern_a,str) |
| 3011 |
if n==1 then |
| 3012 |
return unpack(s) |
| 3013 |
else |
| 3014 |
local t={} |
| 3015 |
local tn=0 |
| 3016 |
for i=1,n do |
| 3017 |
for j=1,#s do |
| 3018 |
tn=tn+1 |
| 3019 |
t[tn]=s[j] |
| 3020 |
end |
| 3021 |
end |
| 3022 |
return unpack(t) |
| 3023 |
end |
| 3024 |
end |
| 3025 |
end |
| 3026 |
local value=P(lbrace*C((nobrace+nestedbraces)^0)*rbrace)+(C(digit^1)/tonumber*lparent*Cs((noparent+nestedparents)^1)*rparent)/repeater+C((nestedbraces+(1-comma))^1) |
| 3027 |
local pattern_b=spaces*Ct(value*(separator*value)^0) |
| 3028 |
function parsers.settings_to_array_with_repeat(str,expand) |
| 3029 |
if expand then |
| 3030 |
return lpegmatch(pattern_b,str) or {} |
| 3031 |
else |
| 3032 |
return lpegmatch(pattern_a,str) or {} |
| 3033 |
end |
| 3034 |
end |
| 3035 |
local value=lbrace*C((nobrace+nestedbraces)^0)*rbrace |
| 3036 |
local pattern=Ct((space+value)^0) |
| 3037 |
function parsers.arguments_to_table(str) |
| 3038 |
return lpegmatch(pattern,str) |
| 3039 |
end |
| 3040 |
function parsers.getparameters(self,class,parentclass,settings) |
| 3041 |
local sc=self[class] |
| 3042 |
if not sc then |
| 3043 |
sc={} |
| 3044 |
self[class]=sc |
| 3045 |
if parentclass then |
| 3046 |
local sp=self[parentclass] |
| 3047 |
if not sp then |
| 3048 |
sp={} |
| 3049 |
self[parentclass]=sp |
| 3050 |
end |
| 3051 |
setmetatableindex(sc,sp) |
| 3052 |
end |
| 3053 |
end |
| 3054 |
parsers.settings_to_hash(settings,sc) |
| 3055 |
end |
| 3056 |
function parsers.listitem(str) |
| 3057 |
return gmatch(str,"[^, ]+") |
| 3058 |
end |
| 3059 |
local pattern=Cs { "start", |
| 3060 |
start=V("one")+V("two")+V("three"), |
| 3061 |
rest=(Cc(",")*V("thousand"))^0*(P(".")+endofstring)*anything^0, |
| 3062 |
thousand=digit*digit*digit, |
| 3063 |
one=digit*V("rest"), |
| 3064 |
two=digit*digit*V("rest"), |
| 3065 |
three=V("thousand")*V("rest"), |
| 3066 |
} |
| 3067 |
lpegpatterns.splitthousands=pattern |
| 3068 |
function parsers.splitthousands(str) |
| 3069 |
return lpegmatch(pattern,str) or str |
| 3070 |
end |
| 3071 |
local optionalwhitespace=whitespace^0 |
| 3072 |
lpegpatterns.words=Ct((Cs((1-punctuation-whitespace)^1)+anything)^1) |
| 3073 |
lpegpatterns.sentences=Ct((optionalwhitespace*Cs((1-period)^0*period))^1) |
| 3074 |
lpegpatterns.paragraphs=Ct((optionalwhitespace*Cs((whitespace^1*endofstring/""+1-(spacer^0*newline*newline))^1))^1) |
| 3075 |
local dquote=P('"') |
| 3076 |
local equal=P('=') |
| 3077 |
local escape=P('\\') |
| 3078 |
local separator=S(' ,') |
| 3079 |
local key=C((1-equal)^1) |
| 3080 |
local value=dquote*C((1-dquote-escape*dquote)^0)*dquote |
| 3081 |
local pattern=Cf(Ct("")*(Cg(key*equal*value)*separator^0)^1,rawset)^0*P(-1) |
| 3082 |
function parsers.keq_to_hash(str) |
| 3083 |
if str and str~="" then |
| 3084 |
return lpegmatch(pattern,str) |
| 3085 |
else |
| 3086 |
return {} |
| 3087 |
end |
| 3088 |
end |
| 3089 |
local defaultspecification={ separator=",",quote='"' } |
| 3090 |
function parsers.csvsplitter(specification) |
| 3091 |
specification=specification and setmetatableindex(specification,defaultspecification) or defaultspecification |
| 3092 |
local separator=specification.separator |
| 3093 |
local quotechar=specification.quote |
| 3094 |
local numbers=specification.numbers |
| 3095 |
local separator=S(separator~="" and separator or ",") |
| 3096 |
local whatever=C((1-separator-newline)^0) |
| 3097 |
if quotechar and quotechar~="" then |
| 3098 |
local quotedata=nil |
| 3099 |
for chr in gmatch(quotechar,".") do |
| 3100 |
local quotechar=P(chr) |
| 3101 |
local quoteitem=(1-quotechar)^0 |
| 3102 |
local quoteword=quotechar*(numbers and (quoteitem/tonumber) or C(quoteitem))*quotechar |
| 3103 |
if quotedata then |
| 3104 |
quotedata=quotedata+quoteword |
| 3105 |
else |
| 3106 |
quotedata=quoteword |
| 3107 |
end |
| 3108 |
end |
| 3109 |
whatever=quotedata+whatever |
| 3110 |
end |
| 3111 |
local parser=Ct((Ct(whatever*(separator*whatever)^0)*S("\n\r")^1)^0 ) |
| 3112 |
return function(data) |
| 3113 |
return lpegmatch(parser,data) |
| 3114 |
end |
| 3115 |
end |
| 3116 |
function parsers.rfc4180splitter(specification) |
| 3117 |
specification=specification and setmetatableindex(specification,defaultspecification) or defaultspecification |
| 3118 |
local numbers=specification.numbers |
| 3119 |
local separator=specification.separator |
| 3120 |
local quotechar=P(specification.quote) |
| 3121 |
local dquotechar=quotechar*quotechar |
| 3122 |
/specification.quote |
| 3123 |
local separator=S(separator~="" and separator or ",") |
| 3124 |
local whatever=(dquotechar+(1-quotechar))^0 |
| 3125 |
local escaped=quotechar*(numbers and (whatever/tonumber) or Cs(whatever))*quotechar |
| 3126 |
local non_escaped=C((1-quotechar-newline-separator)^1) |
| 3127 |
local field=escaped+non_escaped+Cc("") |
| 3128 |
local record=Ct(field*(separator*field)^1) |
| 3129 |
local headerline=record*Cp() |
| 3130 |
local morerecords=(newline^(specification.strict and -1 or 1)*record)^0 |
| 3131 |
local headeryes=Ct(morerecords) |
| 3132 |
local headernop=Ct(record*morerecords) |
| 3133 |
return function(data,getheader) |
| 3134 |
if getheader then |
| 3135 |
local header,position=lpegmatch(headerline,data) |
| 3136 |
local data=lpegmatch(headeryes,data,position) |
| 3137 |
return data,header |
| 3138 |
else |
| 3139 |
return lpegmatch(headernop,data) |
| 3140 |
end |
| 3141 |
end |
| 3142 |
end |
| 3143 |
local function ranger(first,last,n,action) |
| 3144 |
if not first then |
| 3145 |
elseif last==true then |
| 3146 |
for i=first,n or first do |
| 3147 |
action(i) |
| 3148 |
end |
| 3149 |
elseif last then |
| 3150 |
for i=first,last do |
| 3151 |
action(i) |
| 3152 |
end |
| 3153 |
else |
| 3154 |
action(first) |
| 3155 |
end |
| 3156 |
end |
| 3157 |
local cardinal=(lpegpatterns.hexadecimal+lpegpatterns.cardinal)/tonumber |
| 3158 |
local spacers=lpegpatterns.spacer^0 |
| 3159 |
local endofstring=lpegpatterns.endofstring |
| 3160 |
local stepper=spacers*(cardinal*(spacers*S(":-")*spacers*(cardinal+Cc(true) )+Cc(false) )*Carg(1)*Carg(2)/ranger*S(", ")^0 )^1 |
| 3161 |
local stepper=spacers*(cardinal*(spacers*S(":-")*spacers*(cardinal+(P("*")+endofstring)*Cc(true) )+Cc(false) )*Carg(1)*Carg(2)/ranger*S(", ")^0 )^1*endofstring |
| 3162 |
function parsers.stepper(str,n,action) |
| 3163 |
local ts=type(str) |
| 3164 |
if type(n)=="function" then |
| 3165 |
if ts=="number" then |
| 3166 |
n(str) |
| 3167 |
elseif ts=="table" then |
| 3168 |
for i=1,#str do |
| 3169 |
n(str[i]) |
| 3170 |
end |
| 3171 |
else |
| 3172 |
lpegmatch(stepper,str,1,false,n or print) |
| 3173 |
end |
| 3174 |
elseif ts=="string" then |
| 3175 |
lpegmatch(stepper,str,1,n,action or print) |
| 3176 |
end |
| 3177 |
end |
| 3178 |
local pattern_math=Cs((P("%")/"\\percent "+P("^")*Cc("{")*lpegpatterns.integer*Cc("}")+anything)^0) |
| 3179 |
local pattern_text=Cs((P("%")/"\\percent "+(P("^")/"\\high")*Cc("{")*lpegpatterns.integer*Cc("}")+anything)^0) |
| 3180 |
patterns.unittotex=pattern |
| 3181 |
function parsers.unittotex(str,textmode) |
| 3182 |
return lpegmatch(textmode and pattern_text or pattern_math,str) |
| 3183 |
end |
| 3184 |
local pattern=Cs((P("^")/"<sup>"*lpegpatterns.integer*Cc("</sup>")+anything)^0) |
| 3185 |
function parsers.unittoxml(str) |
| 3186 |
return lpegmatch(pattern,str) |
| 3187 |
end |
| 3188 |
local cache={} |
| 3189 |
local spaces=lpegpatterns.space^0 |
| 3190 |
local dummy=function() end |
| 3191 |
setmetatableindex(cache,function(t,k) |
| 3192 |
local separator=S(k) |
| 3193 |
local value=(1-separator)^0 |
| 3194 |
local pattern=spaces*C(value)*separator^0*Cp() |
| 3195 |
t[k]=pattern |
| 3196 |
return pattern |
| 3197 |
end) |
| 3198 |
local commalistiterator=cache[","] |
| 3199 |
function parsers.iterator(str,separator) |
| 3200 |
local n=#str |
| 3201 |
if n==0 then |
| 3202 |
return dummy |
| 3203 |
else |
| 3204 |
local pattern=separator and cache[separator] or commalistiterator |
| 3205 |
local p=1 |
| 3206 |
return function() |
| 3207 |
if p<=n then |
| 3208 |
local s,e=lpegmatch(pattern,str,p) |
| 3209 |
if e then |
| 3210 |
p=e |
| 3211 |
return s |
| 3212 |
end |
| 3213 |
end |
| 3214 |
end |
| 3215 |
end |
| 3216 |
end |
| 3217 |
local function initialize(t,name) |
| 3218 |
local source=t[name] |
| 3219 |
if source then |
| 3220 |
local result={} |
| 3221 |
for k,v in next,t[name] do |
| 3222 |
result[k]=v |
| 3223 |
end |
| 3224 |
return result |
| 3225 |
else |
| 3226 |
return {} |
| 3227 |
end |
| 3228 |
end |
| 3229 |
local function fetch(t,name) |
| 3230 |
return t[name] or {} |
| 3231 |
end |
| 3232 |
local function process(result,more) |
| 3233 |
for k,v in next,more do |
| 3234 |
result[k]=v |
| 3235 |
end |
| 3236 |
return result |
| 3237 |
end |
| 3238 |
local name=C((1-S(", "))^1) |
| 3239 |
local parser=(Carg(1)*name/initialize)*(S(", ")^1*(Carg(1)*name/fetch))^0 |
| 3240 |
local merge=Cf(parser,process) |
| 3241 |
function parsers.mergehashes(hash,list) |
| 3242 |
return lpegmatch(merge,list,1,hash) |
| 3243 |
end |
| 3244 |
function parsers.runtime(time) |
| 3245 |
if not time then |
| 3246 |
time=os.runtime() |
| 3247 |
end |
| 3248 |
local days=div(time,24*60*60) |
| 3249 |
time=mod(time,24*60*60) |
| 3250 |
local hours=div(time,60*60) |
| 3251 |
time=mod(time,60*60) |
| 3252 |
local minutes=div(time,60) |
| 3253 |
local seconds=mod(time,60) |
| 3254 |
return days,hours,minutes,seconds |
| 3255 |
end |
| 3256 |
local spacing=whitespace^0 |
| 3257 |
local apply=P("->") |
| 3258 |
local method=C((1-apply)^1) |
| 3259 |
local token=lbrace*C((1-rbrace)^1)*rbrace+C(anything^1) |
| 3260 |
local pattern=spacing*(method*spacing*apply+Carg(1))*spacing*token |
| 3261 |
function parsers.splitmethod(str,default) |
| 3262 |
if str then |
| 3263 |
return lpegmatch(pattern,str,1,default or false) |
| 3264 |
else |
| 3265 |
return default or false,"" |
| 3266 |
end |
| 3267 |
end |
| 3268 |
local p_year=lpegpatterns.digit^4/tonumber |
| 3269 |
local pattern=Cf(Ct("")*( |
| 3270 |
(Cg(Cc("year")*p_year)*S("-/")*Cg(Cc("month")*cardinal)*S("-/")*Cg(Cc("day")*cardinal) |
| 3271 |
)+(Cg(Cc("day")*cardinal)*S("-/")*Cg(Cc("month")*cardinal)*S("-/")*Cg(Cc("year")*p_year) |
| 3272 |
)+(Cg(Cc("year")*p_year)*S("-/")*Cg(Cc("month")*cardinal) |
| 3273 |
)+(Cg(Cc("month")*cardinal)*S("-/")*Cg(Cc("year")*p_year) |
| 3274 |
) |
| 3275 |
)*( |
| 3276 |
P(" ")*Cg(Cc("hour")*cardinal)*P(":")*Cg(Cc("min")*cardinal)*(P(":")*Cg(Cc("sec")*cardinal))^-1+P(-1) ) |
| 3277 |
,rawset) |
| 3278 |
lpegpatterns.splittime=pattern |
| 3279 |
function parsers.totime(str) |
| 3280 |
return lpegmatch(pattern,str) |
| 3281 |
end |
| 3282 |
|
| 3283 |
end -- closure |
| 3284 |
|
| 3285 |
do -- begin closure to overcome local limits and interference |
| 3286 |
|
| 3287 |
if not modules then modules={} end modules ['util-dim']={ |
| 3288 |
version=1.001, |
| 3289 |
comment="support for dimensions", |
| 3290 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3291 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3292 |
license="see context related readme files" |
| 3293 |
} |
| 3294 |
local format,match,gsub,type,setmetatable=string.format,string.match,string.gsub,type,setmetatable |
| 3295 |
local P,S,R,Cc,C,lpegmatch=lpeg.P,lpeg.S,lpeg.R,lpeg.Cc,lpeg.C,lpeg.match |
| 3296 |
local allocate=utilities.storage.allocate |
| 3297 |
local setmetatableindex=table.setmetatableindex |
| 3298 |
local formatters=string.formatters |
| 3299 |
local texget=tex and tex.get or function() return 65536*10*100 end |
| 3300 |
local p_stripzeros=lpeg.patterns.stripzeros |
| 3301 |
number=number or {} |
| 3302 |
local number=number |
| 3303 |
number.tonumberf=function(n) return lpegmatch(p_stripzeros,format("%.20f",n)) end |
| 3304 |
number.tonumberg=function(n) return format("%.20g",n) end |
| 3305 |
local dimenfactors=allocate { |
| 3306 |
["pt"]=1/65536, |
| 3307 |
["in"]=(100/7227)/65536, |
| 3308 |
["cm"]=(254/7227)/65536, |
| 3309 |
["mm"]=(2540/7227)/65536, |
| 3310 |
["sp"]=1, |
| 3311 |
["bp"]=(7200/7227)/65536, |
| 3312 |
["pc"]=(1/12)/65536, |
| 3313 |
["dd"]=(1157/1238)/65536, |
| 3314 |
["cc"]=(1157/14856)/65536, |
| 3315 |
["es"]=(9176/129)/65536, |
| 3316 |
["ts"]=(4588/645)/65536, |
| 3317 |
} |
| 3318 |
local f_none=formatters["%s%s"] |
| 3319 |
local f_true=formatters["%0.5F%s"] |
| 3320 |
local function numbertodimen(n,unit,fmt) |
| 3321 |
if type(n)=='string' then |
| 3322 |
return n |
| 3323 |
else |
| 3324 |
unit=unit or 'pt' |
| 3325 |
n=n*dimenfactors[unit] |
| 3326 |
if not fmt then |
| 3327 |
fmt=f_none(n,unit) |
| 3328 |
elseif fmt==true then |
| 3329 |
fmt=f_true(n,unit) |
| 3330 |
else |
| 3331 |
return formatters[fmt](n,unit) |
| 3332 |
end |
| 3333 |
end |
| 3334 |
end |
| 3335 |
number.maxdimen=1073741823 |
| 3336 |
number.todimen=numbertodimen |
| 3337 |
number.dimenfactors=dimenfactors |
| 3338 |
function number.topoints (n,fmt) return numbertodimen(n,"pt",fmt) end |
| 3339 |
function number.toinches (n,fmt) return numbertodimen(n,"in",fmt) end |
| 3340 |
function number.tocentimeters (n,fmt) return numbertodimen(n,"cm",fmt) end |
| 3341 |
function number.tomillimeters (n,fmt) return numbertodimen(n,"mm",fmt) end |
| 3342 |
function number.toscaledpoints(n) return n.."sp" end |
| 3343 |
function number.tobasepoints (n,fmt) return numbertodimen(n,"bp",fmt) end |
| 3344 |
function number.topicas (n,fmt) return numbertodimen(n "pc",fmt) end |
| 3345 |
function number.todidots (n,fmt) return numbertodimen(n,"dd",fmt) end |
| 3346 |
function number.tociceros (n,fmt) return numbertodimen(n,"cc",fmt) end |
| 3347 |
function number.toediths (n,fmt) return numbertodimen(n,"es",fmt) end |
| 3348 |
function number.totoves (n,fmt) return numbertodimen(n,"ts",fmt) end |
| 3349 |
local amount=(S("+-")^0*R("09")^0*P(".")^0*R("09")^0)+Cc("0") |
| 3350 |
local unit=R("az")^1+P("%") |
| 3351 |
local dimenpair=amount/tonumber*(unit^1/dimenfactors+Cc(1)) |
| 3352 |
lpeg.patterns.dimenpair=dimenpair |
| 3353 |
local splitter=amount/tonumber*C(unit^1) |
| 3354 |
function number.splitdimen(str) |
| 3355 |
return lpegmatch(splitter,str) |
| 3356 |
end |
| 3357 |
setmetatableindex(dimenfactors,function(t,s) |
| 3358 |
return false |
| 3359 |
end) |
| 3360 |
local stringtodimen |
| 3361 |
local amount=S("+-")^0*R("09")^0*S(".,")^0*R("09")^0 |
| 3362 |
local unit=P("pt")+P("cm")+P("mm")+P("sp")+P("bp")+P("es")+P("ts")+P("pc")+P("dd")+P("cc")+P("in") |
| 3363 |
local validdimen=amount*unit |
| 3364 |
lpeg.patterns.validdimen=validdimen |
| 3365 |
local dimensions={} |
| 3366 |
function dimensions.__add(a,b) |
| 3367 |
local ta,tb=type(a),type(b) |
| 3368 |
if ta=="string" then a=stringtodimen(a) elseif ta=="table" then a=a[1] end |
| 3369 |
if tb=="string" then b=stringtodimen(b) elseif tb=="table" then b=b[1] end |
| 3370 |
return setmetatable({ a+b },dimensions) |
| 3371 |
end |
| 3372 |
function dimensions.__sub(a,b) |
| 3373 |
local ta,tb=type(a),type(b) |
| 3374 |
if ta=="string" then a=stringtodimen(a) elseif ta=="table" then a=a[1] end |
| 3375 |
if tb=="string" then b=stringtodimen(b) elseif tb=="table" then b=b[1] end |
| 3376 |
return setmetatable({ a-b },dimensions) |
| 3377 |
end |
| 3378 |
function dimensions.__mul(a,b) |
| 3379 |
local ta,tb=type(a),type(b) |
| 3380 |
if ta=="string" then a=stringtodimen(a) elseif ta=="table" then a=a[1] end |
| 3381 |
if tb=="string" then b=stringtodimen(b) elseif tb=="table" then b=b[1] end |
| 3382 |
return setmetatable({ a*b },dimensions) |
| 3383 |
end |
| 3384 |
function dimensions.__div(a,b) |
| 3385 |
local ta,tb=type(a),type(b) |
| 3386 |
if ta=="string" then a=stringtodimen(a) elseif ta=="table" then a=a[1] end |
| 3387 |
if tb=="string" then b=stringtodimen(b) elseif tb=="table" then b=b[1] end |
| 3388 |
return setmetatable({ a/b },dimensions) |
| 3389 |
end |
| 3390 |
function dimensions.__unm(a) |
| 3391 |
local ta=type(a) |
| 3392 |
if ta=="string" then a=stringtodimen(a) elseif ta=="table" then a=a[1] end |
| 3393 |
return setmetatable({-a },dimensions) |
| 3394 |
end |
| 3395 |
function dimensions.__lt(a,b) |
| 3396 |
return a[1]<b[1] |
| 3397 |
end |
| 3398 |
function dimensions.__eq(a,b) |
| 3399 |
return a[1]==b[1] |
| 3400 |
end |
| 3401 |
function dimensions.__tostring(a) |
| 3402 |
return a[1]/65536 .."pt" |
| 3403 |
end |
| 3404 |
function dimensions.__index(tab,key) |
| 3405 |
local d=dimenfactors[key] |
| 3406 |
if not d then |
| 3407 |
error("illegal property of dimen: "..key) |
| 3408 |
d=1 |
| 3409 |
end |
| 3410 |
return 1/d |
| 3411 |
end |
| 3412 |
dimenfactors["ex"]=4/65536 |
| 3413 |
dimenfactors["em"]=10/65536 |
| 3414 |
dimenfactors["eu"]=(9176/129)/65536 |
| 3415 |
local known={} setmetatable(known,{ __mode="v" }) |
| 3416 |
function dimen(a) |
| 3417 |
if a then |
| 3418 |
local ta=type(a) |
| 3419 |
if ta=="string" then |
| 3420 |
local k=known[a] |
| 3421 |
if k then |
| 3422 |
a=k |
| 3423 |
else |
| 3424 |
local value,unit=lpegmatch(dimenpair,a) |
| 3425 |
if value and unit then |
| 3426 |
k=value/unit |
| 3427 |
else |
| 3428 |
k=0 |
| 3429 |
end |
| 3430 |
known[a]=k |
| 3431 |
a=k |
| 3432 |
end |
| 3433 |
elseif ta=="table" then |
| 3434 |
a=a[1] |
| 3435 |
end |
| 3436 |
return setmetatable({ a },dimensions) |
| 3437 |
else |
| 3438 |
return setmetatable({ 0 },dimensions) |
| 3439 |
end |
| 3440 |
end |
| 3441 |
function string.todimen(str) |
| 3442 |
local t=type(str) |
| 3443 |
if t=="number" then |
| 3444 |
return str |
| 3445 |
else |
| 3446 |
local k=known[str] |
| 3447 |
if not k then |
| 3448 |
if t=="string" then |
| 3449 |
local value,unit=lpegmatch(dimenpair,str) |
| 3450 |
if value and unit then |
| 3451 |
k=value/unit |
| 3452 |
else |
| 3453 |
k=0 |
| 3454 |
end |
| 3455 |
else |
| 3456 |
k=0 |
| 3457 |
end |
| 3458 |
known[str]=k |
| 3459 |
end |
| 3460 |
return k |
| 3461 |
end |
| 3462 |
end |
| 3463 |
stringtodimen=string.todimen |
| 3464 |
function number.toscaled(d) |
| 3465 |
return format("%0.5f",d/0x10000) |
| 3466 |
end |
| 3467 |
function number.percent(n,d) |
| 3468 |
d=d or texget("hsize") |
| 3469 |
if type(d)=="string" then |
| 3470 |
d=stringtodimen(d) |
| 3471 |
end |
| 3472 |
return (n/100)*d |
| 3473 |
end |
| 3474 |
number["%"]=number.percent |
| 3475 |
|
| 3476 |
end -- closure |
| 3477 |
|
| 3478 |
do -- begin closure to overcome local limits and interference |
| 3479 |
|
| 3480 |
if not modules then modules={} end modules ['util-jsn']={ |
| 3481 |
version=1.001, |
| 3482 |
comment="companion to m-json.mkiv", |
| 3483 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3484 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3485 |
license="see context related readme files" |
| 3486 |
} |
| 3487 |
if utilities and utilities.json then |
| 3488 |
return json |
| 3489 |
end |
| 3490 |
local P,V,R,S,C,Cc,Cs,Ct,Cf,Cg=lpeg.P,lpeg.V,lpeg.R,lpeg.S,lpeg.C,lpeg.Cc,lpeg.Cs,lpeg.Ct,lpeg.Cf,lpeg.Cg |
| 3491 |
local lpegmatch=lpeg.match |
| 3492 |
local format,gsub=string.format,string.gsub |
| 3493 |
local formatters=string.formatters |
| 3494 |
local utfchar=utf.char |
| 3495 |
local concat,sortedkeys=table.concat,table.sortedkeys |
| 3496 |
local tonumber,tostring,rawset,type,next=tonumber,tostring,rawset,type,next |
| 3497 |
local json=utilities.json or {} |
| 3498 |
utilities.json=json |
| 3499 |
do |
| 3500 |
local lbrace=P("{") |
| 3501 |
local rbrace=P("}") |
| 3502 |
local lparent=P("[") |
| 3503 |
local rparent=P("]") |
| 3504 |
local comma=P(",") |
| 3505 |
local colon=P(":") |
| 3506 |
local dquote=P('"') |
| 3507 |
local whitespace=lpeg.patterns.whitespace |
| 3508 |
local optionalws=whitespace^0 |
| 3509 |
local escapes={ |
| 3510 |
["b"]="\010", |
| 3511 |
["f"]="\014", |
| 3512 |
["n"]="\n", |
| 3513 |
["r"]="\r", |
| 3514 |
["t"]="\t", |
| 3515 |
} |
| 3516 |
local escape_un=P("\\u")/""*(C(R("09","AF","af")^-4)/function(s) |
| 3517 |
return utfchar(tonumber(s,16)) |
| 3518 |
end) |
| 3519 |
local escape_bs=P([[\]])/""*(P(1)/escapes) |
| 3520 |
local jstring=dquote*Cs((escape_un+escape_bs+(1-dquote))^0)*dquote |
| 3521 |
local jtrue=P("true")*Cc(true) |
| 3522 |
local jfalse=P("false")*Cc(false) |
| 3523 |
local jnull=P("null")*Cc(nil) |
| 3524 |
local jnumber=(1-whitespace-rparent-rbrace-comma)^1/tonumber |
| 3525 |
local key=jstring |
| 3526 |
local jsonconverter={ "value", |
| 3527 |
hash=lbrace*Cf(Ct("")*(V("pair")*(comma*V("pair"))^0+optionalws),rawset)*rbrace, |
| 3528 |
pair=Cg(optionalws*key*optionalws*colon*V("value")), |
| 3529 |
array=Ct(lparent*(V("value")*(comma*V("value"))^0+optionalws)*rparent), |
| 3530 |
value=optionalws*(jstring+V("hash")+V("array")+jtrue+jfalse+jnull+jnumber)*optionalws, |
| 3531 |
} |
| 3532 |
function json.tolua(str) |
| 3533 |
return lpegmatch(jsonconverter,str) |
| 3534 |
end |
| 3535 |
function json.load(filename) |
| 3536 |
local data=io.loaddata(filename) |
| 3537 |
if data then |
| 3538 |
return lpegmatch(jsonconverter,data) |
| 3539 |
end |
| 3540 |
end |
| 3541 |
end |
| 3542 |
do |
| 3543 |
local escaper |
| 3544 |
local f_start_hash=formatters[ '%w{' ] |
| 3545 |
local f_start_array=formatters[ '%w[' ] |
| 3546 |
local f_start_hash_new=formatters[ "\n"..'%w{' ] |
| 3547 |
local f_start_array_new=formatters[ "\n"..'%w[' ] |
| 3548 |
local f_start_hash_key=formatters[ "\n"..'%w"%s" : {' ] |
| 3549 |
local f_start_array_key=formatters[ "\n"..'%w"%s" : [' ] |
| 3550 |
local f_stop_hash=formatters[ "\n"..'%w}' ] |
| 3551 |
local f_stop_array=formatters[ "\n"..'%w]' ] |
| 3552 |
local f_key_val_seq=formatters[ "\n"..'%w"%s" : %s' ] |
| 3553 |
local f_key_val_str=formatters[ "\n"..'%w"%s" : "%s"' ] |
| 3554 |
local f_key_val_num=f_key_val_seq |
| 3555 |
local f_key_val_yes=formatters[ "\n"..'%w"%s" : true' ] |
| 3556 |
local f_key_val_nop=formatters[ "\n"..'%w"%s" : false' ] |
| 3557 |
local f_key_val_null=formatters[ "\n"..'%w"%s" : null' ] |
| 3558 |
local f_val_num=formatters[ "\n"..'%w%s' ] |
| 3559 |
local f_val_str=formatters[ "\n"..'%w"%s"' ] |
| 3560 |
local f_val_yes=formatters[ "\n"..'%wtrue' ] |
| 3561 |
local f_val_nop=formatters[ "\n"..'%wfalse' ] |
| 3562 |
local f_val_null=formatters[ "\n"..'%wnull' ] |
| 3563 |
local f_val_empty=formatters[ "\n"..'%w{ }' ] |
| 3564 |
local f_val_seq=f_val_num |
| 3565 |
local t={} |
| 3566 |
local n=0 |
| 3567 |
local function is_simple_table(tt) |
| 3568 |
local l=#tt |
| 3569 |
if l>0 then |
| 3570 |
for i=1,l do |
| 3571 |
if type(tt[i])=="table" then |
| 3572 |
return false |
| 3573 |
end |
| 3574 |
end |
| 3575 |
local nn=n |
| 3576 |
n=n+1 t[n]="[ " |
| 3577 |
for i=1,l do |
| 3578 |
if i>1 then |
| 3579 |
n=n+1 t[n]=", " |
| 3580 |
end |
| 3581 |
local v=tt[i] |
| 3582 |
local tv=type(v) |
| 3583 |
if tv=="number" then |
| 3584 |
n=n+1 t[n]=v |
| 3585 |
elseif tv=="string" then |
| 3586 |
n=n+1 t[n]='"' |
| 3587 |
n=n+1 t[n]=lpegmatch(escaper,v) or v |
| 3588 |
n=n+1 t[n]='"' |
| 3589 |
elseif tv=="boolean" then |
| 3590 |
n=n+1 t[n]=v and "true" or "false" |
| 3591 |
elseif v then |
| 3592 |
n=n+1 t[n]=tostring(v) |
| 3593 |
else |
| 3594 |
n=n+1 t[n]="null" |
| 3595 |
end |
| 3596 |
end |
| 3597 |
n=n+1 t[n]=" ]" |
| 3598 |
local s=concat(t,"",nn+1,n) |
| 3599 |
n=nn |
| 3600 |
return s |
| 3601 |
end |
| 3602 |
return false |
| 3603 |
end |
| 3604 |
local function tojsonpp(root,name,depth,level,size) |
| 3605 |
if root then |
| 3606 |
local indexed=size>0 |
| 3607 |
n=n+1 |
| 3608 |
if level==0 then |
| 3609 |
if indexed then |
| 3610 |
t[n]=f_start_array(depth) |
| 3611 |
else |
| 3612 |
t[n]=f_start_hash(depth) |
| 3613 |
end |
| 3614 |
elseif name then |
| 3615 |
if tn=="string" then |
| 3616 |
name=lpegmatch(escaper,name) or name |
| 3617 |
elseif tn~="number" then |
| 3618 |
name=tostring(name) |
| 3619 |
end |
| 3620 |
if indexed then |
| 3621 |
t[n]=f_start_array_key(depth,name) |
| 3622 |
else |
| 3623 |
t[n]=f_start_hash_key(depth,name) |
| 3624 |
end |
| 3625 |
else |
| 3626 |
if indexed then |
| 3627 |
t[n]=f_start_array_new(depth) |
| 3628 |
else |
| 3629 |
t[n]=f_start_hash_new(depth) |
| 3630 |
end |
| 3631 |
end |
| 3632 |
depth=depth+1 |
| 3633 |
if indexed then |
| 3634 |
for i=1,size do |
| 3635 |
if i>1 then |
| 3636 |
n=n+1 t[n]="," |
| 3637 |
end |
| 3638 |
local v=root[i] |
| 3639 |
local tv=type(v) |
| 3640 |
if tv=="number" then |
| 3641 |
n=n+1 t[n]=f_val_num(depth,v) |
| 3642 |
elseif tv=="string" then |
| 3643 |
v=lpegmatch(escaper,v) or v |
| 3644 |
n=n+1 t[n]=f_val_str(depth,v) |
| 3645 |
elseif tv=="table" then |
| 3646 |
if next(v) then |
| 3647 |
local st=is_simple_table(v) |
| 3648 |
if st then |
| 3649 |
n=n+1 t[n]=f_val_seq(depth,st) |
| 3650 |
else |
| 3651 |
tojsonpp(v,nil,depth,level+1,#v) |
| 3652 |
end |
| 3653 |
else |
| 3654 |
n=n+1 |
| 3655 |
t[n]=f_val_empty(depth) |
| 3656 |
end |
| 3657 |
elseif tv=="boolean" then |
| 3658 |
n=n+1 |
| 3659 |
if v then |
| 3660 |
t[n]=f_val_yes(depth,v) |
| 3661 |
else |
| 3662 |
t[n]=f_val_nop(depth,v) |
| 3663 |
end |
| 3664 |
else |
| 3665 |
n=n+1 |
| 3666 |
t[n]=f_val_null(depth) |
| 3667 |
end |
| 3668 |
end |
| 3669 |
elseif next(root) then |
| 3670 |
local sk=sortedkeys(root) |
| 3671 |
for i=1,#sk do |
| 3672 |
if i>1 then |
| 3673 |
n=n+1 t[n]="," |
| 3674 |
end |
| 3675 |
local k=sk[i] |
| 3676 |
local v=root[k] |
| 3677 |
local tv=type(v) |
| 3678 |
local tk=type(k) |
| 3679 |
if tv=="number" then |
| 3680 |
if tk=="number" then |
| 3681 |
n=n+1 t[n]=f_key_val_num(depth,k,v) |
| 3682 |
elseif tk=="string" then |
| 3683 |
k=lpegmatch(escaper,k) or k |
| 3684 |
n=n+1 t[n]=f_key_val_num(depth,k,v) |
| 3685 |
end |
| 3686 |
elseif tv=="string" then |
| 3687 |
if tk=="number" then |
| 3688 |
v=lpegmatch(escaper,v) or v |
| 3689 |
n=n+1 t[n]=f_key_val_str(depth,k,v) |
| 3690 |
elseif tk=="string" then |
| 3691 |
k=lpegmatch(escaper,k) or k |
| 3692 |
v=lpegmatch(escaper,v) or v |
| 3693 |
n=n+1 t[n]=f_key_val_str(depth,k,v) |
| 3694 |
elseif i>1 then |
| 3695 |
n=n-1 |
| 3696 |
end |
| 3697 |
elseif tv=="table" then |
| 3698 |
local l=#v |
| 3699 |
if l>0 then |
| 3700 |
local st=is_simple_table(v) |
| 3701 |
if not st then |
| 3702 |
tojsonpp(v,k,depth,level+1,l) |
| 3703 |
elseif tk=="number" then |
| 3704 |
n=n+1 t[n]=f_key_val_seq(depth,k,st) |
| 3705 |
elseif tk=="string" then |
| 3706 |
k=lpegmatch(escaper,k) or k |
| 3707 |
n=n+1 t[n]=f_key_val_seq(depth,k,st) |
| 3708 |
end |
| 3709 |
elseif next(v) then |
| 3710 |
tojsonpp(v,k,depth,level+1,0) |
| 3711 |
elseif i>1 then |
| 3712 |
n=n-1 |
| 3713 |
end |
| 3714 |
elseif tv=="boolean" then |
| 3715 |
if tk=="number" then |
| 3716 |
n=n+1 |
| 3717 |
if v then |
| 3718 |
t[n]=f_key_val_yes(depth,k) |
| 3719 |
else |
| 3720 |
t[n]=f_key_val_nop(depth,k) |
| 3721 |
end |
| 3722 |
elseif tk=="string" then |
| 3723 |
k=lpegmatch(escaper,k) or k |
| 3724 |
n=n+1 |
| 3725 |
if v then |
| 3726 |
t[n]=f_key_val_yes(depth,k) |
| 3727 |
else |
| 3728 |
t[n]=f_key_val_nop(depth,k) |
| 3729 |
end |
| 3730 |
elseif i>1 then |
| 3731 |
n=n-1 |
| 3732 |
end |
| 3733 |
else |
| 3734 |
if tk=="number" then |
| 3735 |
n=n+1 |
| 3736 |
t[n]=f_key_val_null(depth,k) |
| 3737 |
elseif tk=="string" then |
| 3738 |
k=lpegmatch(escaper,k) or k |
| 3739 |
n=n+1 |
| 3740 |
t[n]=f_key_val_null(depth,k) |
| 3741 |
elseif i>1 then |
| 3742 |
n=n-1 |
| 3743 |
end |
| 3744 |
end |
| 3745 |
end |
| 3746 |
end |
| 3747 |
n=n+1 |
| 3748 |
if indexed then |
| 3749 |
t[n]=f_stop_array(depth-1) |
| 3750 |
else |
| 3751 |
t[n]=f_stop_hash(depth-1) |
| 3752 |
end |
| 3753 |
end |
| 3754 |
end |
| 3755 |
local function tojson(value,n) |
| 3756 |
local kind=type(value) |
| 3757 |
if kind=="table" then |
| 3758 |
local done=false |
| 3759 |
local size=#value |
| 3760 |
if size==0 then |
| 3761 |
for k,v in next,value do |
| 3762 |
if done then |
| 3763 |
n=n+1;t[n]=',"' |
| 3764 |
else |
| 3765 |
n=n+1;t[n]='{"' |
| 3766 |
done=true |
| 3767 |
end |
| 3768 |
n=n+1;t[n]=lpegmatch(escaper,k) or k |
| 3769 |
n=n+1;t[n]='":' |
| 3770 |
t,n=tojson(v,n) |
| 3771 |
end |
| 3772 |
if done then |
| 3773 |
n=n+1;t[n]="}" |
| 3774 |
else |
| 3775 |
n=n+1;t[n]="{}" |
| 3776 |
end |
| 3777 |
elseif size==1 then |
| 3778 |
n=n+1;t[n]="[" |
| 3779 |
t,n=tojson(value[1],n) |
| 3780 |
n=n+1;t[n]="]" |
| 3781 |
else |
| 3782 |
for i=1,size do |
| 3783 |
if done then |
| 3784 |
n=n+1;t[n]="," |
| 3785 |
else |
| 3786 |
n=n+1;t[n]="[" |
| 3787 |
done=true |
| 3788 |
end |
| 3789 |
t,n=tojson(value[i],n) |
| 3790 |
end |
| 3791 |
n=n+1;t[n]="]" |
| 3792 |
end |
| 3793 |
elseif kind=="string" then |
| 3794 |
n=n+1;t[n]='"' |
| 3795 |
n=n+1;t[n]=lpegmatch(escaper,value) or value |
| 3796 |
n=n+1;t[n]='"' |
| 3797 |
elseif kind=="number" then |
| 3798 |
n=n+1;t[n]=value |
| 3799 |
elseif kind=="boolean" then |
| 3800 |
n=n+1;t[n]=tostring(value) |
| 3801 |
else |
| 3802 |
n=n+1;t[n]="null" |
| 3803 |
end |
| 3804 |
return t,n |
| 3805 |
end |
| 3806 |
local function jsontostring(value,pretty) |
| 3807 |
local kind=type(value) |
| 3808 |
if kind=="table" then |
| 3809 |
if not escaper then |
| 3810 |
local escapes={ |
| 3811 |
["\\"]="\\u005C", |
| 3812 |
["\""]="\\u0022", |
| 3813 |
} |
| 3814 |
for i=0,0x1F do |
| 3815 |
escapes[utfchar(i)]=format("\\u%04X",i) |
| 3816 |
end |
| 3817 |
escaper=Cs(( |
| 3818 |
(R('\0\x20')+S('\"\\'))/escapes+P(1) |
| 3819 |
)^1 ) |
| 3820 |
end |
| 3821 |
t={} |
| 3822 |
n=0 |
| 3823 |
if pretty then |
| 3824 |
tojsonpp(value,name,0,0,#value) |
| 3825 |
value=concat(t,"",1,n) |
| 3826 |
else |
| 3827 |
t,n=tojson(value,0) |
| 3828 |
value=concat(t,"",1,n) |
| 3829 |
end |
| 3830 |
t=nil |
| 3831 |
n=0 |
| 3832 |
return value |
| 3833 |
elseif kind=="string" or kind=="number" then |
| 3834 |
return lpegmatch(escaper,value) or value |
| 3835 |
else |
| 3836 |
return tostring(value) |
| 3837 |
end |
| 3838 |
end |
| 3839 |
json.tostring=jsontostring |
| 3840 |
function json.tojson(value) |
| 3841 |
return jsontostring(value,true) |
| 3842 |
end |
| 3843 |
end |
| 3844 |
|
| 3845 |
end -- closure |
| 3846 |
|
| 3847 |
do -- begin closure to overcome local limits and interference |
| 3848 |
|
| 3849 |
if not modules then modules={} end modules ['trac-inf']={ |
| 3850 |
version=1.001, |
| 3851 |
comment="companion to trac-inf.mkiv", |
| 3852 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 3853 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 3854 |
license="see context related readme files" |
| 3855 |
} |
| 3856 |
local type,tonumber,select=type,tonumber,select |
| 3857 |
local format,lower,find=string.format,string.lower,string.find |
| 3858 |
local concat=table.concat |
| 3859 |
local clock=os.gettimeofday or os.clock |
| 3860 |
local setmetatableindex=table.setmetatableindex |
| 3861 |
local serialize=table.serialize |
| 3862 |
local formatters=string.formatters |
| 3863 |
statistics=statistics or {} |
| 3864 |
local statistics=statistics |
| 3865 |
statistics.enable=true |
| 3866 |
statistics.threshold=0.01 |
| 3867 |
local statusinfo,n,registered,timers={},0,{},{} |
| 3868 |
setmetatableindex(timers,function(t,k) |
| 3869 |
local v={ timing=0,loadtime=0,offset=0 } |
| 3870 |
t[k]=v |
| 3871 |
return v |
| 3872 |
end) |
| 3873 |
local function hastiming(instance) |
| 3874 |
return instance and timers[instance] |
| 3875 |
end |
| 3876 |
local function resettiming(instance) |
| 3877 |
timers[instance or "notimer"]={ timing=0,loadtime=0,offset=0 } |
| 3878 |
end |
| 3879 |
local ticks=clock |
| 3880 |
local seconds=function(n) return n or 0 end |
| 3881 |
if os.type~="windows" then |
| 3882 |
elseif lua.getpreciseticks then |
| 3883 |
ticks=lua.getpreciseticks |
| 3884 |
seconds=lua.getpreciseseconds |
| 3885 |
elseif FFISUPPORTED then |
| 3886 |
local okay,kernel=pcall(ffi.load,"kernel32") |
| 3887 |
if kernel then |
| 3888 |
local tonumber=ffi.number or tonumber |
| 3889 |
ffi.cdef[[ |
| 3890 |
int QueryPerformanceFrequency(int64_t *lpFrequency); |
| 3891 |
int QueryPerformanceCounter(int64_t *lpPerformanceCount); |
| 3892 |
]] |
| 3893 |
local target=ffi.new("__int64[1]") |
| 3894 |
ticks=function() |
| 3895 |
if kernel.QueryPerformanceCounter(target)==1 then |
| 3896 |
return tonumber(target[0]) |
| 3897 |
else |
| 3898 |
return 0 |
| 3899 |
end |
| 3900 |
end |
| 3901 |
local target=ffi.new("__int64[1]") |
| 3902 |
seconds=function(ticks) |
| 3903 |
if kernel.QueryPerformanceFrequency(target)==1 then |
| 3904 |
return ticks/tonumber(target[0]) |
| 3905 |
else |
| 3906 |
return 0 |
| 3907 |
end |
| 3908 |
end |
| 3909 |
end |
| 3910 |
else |
| 3911 |
end |
| 3912 |
local function starttiming(instance,reset) |
| 3913 |
local timer=timers[instance or "notimer"] |
| 3914 |
local it=timer.timing |
| 3915 |
if reset then |
| 3916 |
it=0 |
| 3917 |
timer.loadtime=0 |
| 3918 |
end |
| 3919 |
if it==0 then |
| 3920 |
timer.starttime=ticks() |
| 3921 |
if not timer.loadtime then |
| 3922 |
timer.loadtime=0 |
| 3923 |
end |
| 3924 |
end |
| 3925 |
timer.timing=it+1 |
| 3926 |
end |
| 3927 |
local function stoptiming(instance) |
| 3928 |
local timer=timers[instance or "notimer"] |
| 3929 |
local it=timer.timing |
| 3930 |
if it>1 then |
| 3931 |
timer.timing=it-1 |
| 3932 |
else |
| 3933 |
local starttime=timer.starttime |
| 3934 |
if starttime and starttime>0 then |
| 3935 |
local stoptime=ticks() |
| 3936 |
local loadtime=stoptime-starttime |
| 3937 |
timer.stoptime=stoptime |
| 3938 |
timer.loadtime=timer.loadtime+loadtime |
| 3939 |
timer.timing=0 |
| 3940 |
timer.starttime=0 |
| 3941 |
end |
| 3942 |
end |
| 3943 |
end |
| 3944 |
local function benchmarktimer(instance) |
| 3945 |
local timer=timers[instance or "notimer"] |
| 3946 |
local it=timer.timing |
| 3947 |
if it>1 then |
| 3948 |
timer.timing=it-1 |
| 3949 |
else |
| 3950 |
local starttime=timer.starttime |
| 3951 |
if starttime and starttime>0 then |
| 3952 |
timer.offset=ticks()-starttime |
| 3953 |
else |
| 3954 |
timer.offset=0 |
| 3955 |
end |
| 3956 |
end |
| 3957 |
end |
| 3958 |
local function elapsed(instance) |
| 3959 |
if type(instance)=="number" then |
| 3960 |
return instance |
| 3961 |
else |
| 3962 |
local timer=timers[instance or "notimer"] |
| 3963 |
return timer and seconds(timer.loadtime-2*(timer.offset or 0)) or 0 |
| 3964 |
end |
| 3965 |
end |
| 3966 |
local function currenttime(instance) |
| 3967 |
if type(instance)=="number" then |
| 3968 |
return instance |
| 3969 |
else |
| 3970 |
local timer=timers[instance or "notimer"] |
| 3971 |
local it=timer.timing |
| 3972 |
if it>1 then |
| 3973 |
else |
| 3974 |
local starttime=timer.starttime |
| 3975 |
if starttime and starttime>0 then |
| 3976 |
return seconds(timer.loadtime+ticks()-starttime-2*(timer.offset or 0)) |
| 3977 |
end |
| 3978 |
end |
| 3979 |
return 0 |
| 3980 |
end |
| 3981 |
end |
| 3982 |
local function elapsedtime(instance) |
| 3983 |
return format("%0.3f",elapsed(instance)) |
| 3984 |
end |
| 3985 |
local function elapsedindeed(instance) |
| 3986 |
return elapsed(instance)>statistics.threshold |
| 3987 |
end |
| 3988 |
local function elapsedseconds(instance,rest) |
| 3989 |
if elapsedindeed(instance) then |
| 3990 |
return format("%0.3f seconds %s",elapsed(instance),rest or "") |
| 3991 |
end |
| 3992 |
end |
| 3993 |
statistics.hastiming=hastiming |
| 3994 |
statistics.resettiming=resettiming |
| 3995 |
statistics.starttiming=starttiming |
| 3996 |
statistics.stoptiming=stoptiming |
| 3997 |
statistics.currenttime=currenttime |
| 3998 |
statistics.elapsed=elapsed |
| 3999 |
statistics.elapsedtime=elapsedtime |
| 4000 |
statistics.elapsedindeed=elapsedindeed |
| 4001 |
statistics.elapsedseconds=elapsedseconds |
| 4002 |
statistics.benchmarktimer=benchmarktimer |
| 4003 |
function statistics.register(tag,fnc) |
| 4004 |
if statistics.enable and type(fnc)=="function" then |
| 4005 |
local rt=registered[tag] or (#statusinfo+1) |
| 4006 |
statusinfo[rt]={ tag,fnc } |
| 4007 |
registered[tag]=rt |
| 4008 |
if #tag>n then n=#tag end |
| 4009 |
end |
| 4010 |
end |
| 4011 |
local report=logs.reporter("mkiv lua stats") |
| 4012 |
function statistics.show() |
| 4013 |
if statistics.enable then |
| 4014 |
local register=statistics.register |
| 4015 |
register("used platform",function() |
| 4016 |
return format("%s, type: %s, binary subtree: %s", |
| 4017 |
os.platform or "unknown",os.type or "unknown",environment.texos or "unknown") |
| 4018 |
end) |
| 4019 |
register("used engine",function() |
| 4020 |
return format("%s version: %s, functionality level: %s, banner: %s", |
| 4021 |
LUATEXENGINE,LUATEXVERSION,LUATEXFUNCTIONALITY,lower(status.banner)) |
| 4022 |
end) |
| 4023 |
register("used hash slots",function() |
| 4024 |
return format("%s of %s + %s",status.cs_count,status.hash_size,status.hash_extra) |
| 4025 |
end) |
| 4026 |
register("callbacks",statistics.callbacks) |
| 4027 |
if JITSUPPORTED then |
| 4028 |
local jitstatus=jit.status |
| 4029 |
if jitstatus then |
| 4030 |
local jitstatus={ jitstatus() } |
| 4031 |
if jitstatus[1] then |
| 4032 |
register("luajit options",concat(jitstatus," ",2)) |
| 4033 |
end |
| 4034 |
end |
| 4035 |
end |
| 4036 |
register("lua properties",function() |
| 4037 |
local hash=2^status.luatex_hashchars |
| 4038 |
local mask=load([[τεχ = 1]]) and "utf" or "ascii" |
| 4039 |
return format("engine: %s %s, used memory: %s, hash chars: min(%i,40), symbol mask: %s (%s)", |
| 4040 |
jit and "luajit" or "lua",LUAVERSION,statistics.memused(),hash,mask,mask=="utf" and "τεχ" or "tex") |
| 4041 |
end) |
| 4042 |
register("runtime",statistics.runtime) |
| 4043 |
logs.newline() |
| 4044 |
for i=1,#statusinfo do |
| 4045 |
local s=statusinfo[i] |
| 4046 |
local r=s[2]() |
| 4047 |
if r then |
| 4048 |
report("%s: %s",s[1],r) |
| 4049 |
end |
| 4050 |
end |
| 4051 |
statistics.enable=false |
| 4052 |
end |
| 4053 |
end |
| 4054 |
function statistics.memused() |
| 4055 |
local round=math.round or math.floor |
| 4056 |
return format("%s MB, ctx: %s MB, max: %s MB", |
| 4057 |
round(collectgarbage("count")/1000), |
| 4058 |
round(status.luastate_bytes/1000000), |
| 4059 |
status.luastate_bytes_max and round(status.luastate_bytes_max/1000000) or "unknown" |
| 4060 |
) |
| 4061 |
end |
| 4062 |
starttiming(statistics) |
| 4063 |
function statistics.formatruntime(runtime) |
| 4064 |
return format("%s seconds",runtime) |
| 4065 |
end |
| 4066 |
function statistics.runtime() |
| 4067 |
stoptiming(statistics) |
| 4068 |
local runtime=lua.getruntime and lua.getruntime() or elapsedtime(statistics) |
| 4069 |
return statistics.formatruntime(runtime) |
| 4070 |
end |
| 4071 |
local report=logs.reporter("system") |
| 4072 |
function statistics.timed(action,all) |
| 4073 |
starttiming("run") |
| 4074 |
action() |
| 4075 |
stoptiming("run") |
| 4076 |
local runtime=tonumber(elapsedtime("run")) |
| 4077 |
if all then |
| 4078 |
local alltime=tonumber(lua.getruntime and lua.getruntime() or elapsedtime(statistics)) |
| 4079 |
if alltime and alltime>0 then |
| 4080 |
report("total runtime: %0.3f seconds of %0.3f seconds",runtime,alltime) |
| 4081 |
return |
| 4082 |
end |
| 4083 |
end |
| 4084 |
report("total runtime: %0.3f seconds",runtime) |
| 4085 |
end |
| 4086 |
function statistics.tracefunction(base,tag,...) |
| 4087 |
for i=1,select("#",...) do |
| 4088 |
local name=select(i,...) |
| 4089 |
local stat={} |
| 4090 |
local func=base[name] |
| 4091 |
setmetatableindex(stat,function(t,k) t[k]=0 return 0 end) |
| 4092 |
base[name]=function(n,k,v) stat[k]=stat[k]+1 return func(n,k,v) end |
| 4093 |
statistics.register(formatters["%s.%s"](tag,name),function() return serialize(stat,"calls") end) |
| 4094 |
end |
| 4095 |
end |
| 4096 |
function status.getreadstate() |
| 4097 |
return { |
| 4098 |
filename=status.filename or "?", |
| 4099 |
linenumber=status.linenumber or 0, |
| 4100 |
iocode=status.inputid or 0, |
| 4101 |
} |
| 4102 |
end |
| 4103 |
|
| 4104 |
end -- closure |
| 4105 |
|
| 4106 |
do -- begin closure to overcome local limits and interference |
| 4107 |
|
| 4108 |
if not modules then modules={} end modules ['util-lua']={ |
| 4109 |
version=1.001, |
| 4110 |
comment="companion to luat-lib.mkiv", |
| 4111 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4112 |
comment="the strip code is written by Peter Cawley", |
| 4113 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4114 |
license="see context related readme files" |
| 4115 |
} |
| 4116 |
local rep,sub,byte,dump,format=string.rep,string.sub,string.byte,string.dump,string.format |
| 4117 |
local load,loadfile,type,collectgarbage=load,loadfile,type,collectgarbage |
| 4118 |
utilities=utilities or {} |
| 4119 |
utilities.lua=utilities.lua or {} |
| 4120 |
local luautilities=utilities.lua |
| 4121 |
local report_lua=logs.reporter("system","lua") |
| 4122 |
local report_mem=logs.reporter("system","lua memory") |
| 4123 |
local tracestripping=false |
| 4124 |
local tracememory=false |
| 4125 |
luautilities.stripcode=true |
| 4126 |
luautilities.alwaysstripcode=false |
| 4127 |
luautilities.nofstrippedchunks=0 |
| 4128 |
luautilities.nofstrippedbytes=0 |
| 4129 |
local strippedchunks={} |
| 4130 |
luautilities.strippedchunks=strippedchunks |
| 4131 |
if not LUATEXENGINE then |
| 4132 |
LUATEXENGINE=status.luatex_engine and string.lower(status.luatex_engine) |
| 4133 |
JITSUPPORTED=LUATEXENGINE=="luajittex" or jit |
| 4134 |
CONTEXTLMTXMODE=CONTEXTLMTXMODE or (LUATEXENGINE=="luametatex" and 1) or 0 |
| 4135 |
end |
| 4136 |
luautilities.suffixes={ |
| 4137 |
tma="tma", |
| 4138 |
tmc=(CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and "tmd") or (jit and "tmb") or "tmc", |
| 4139 |
lua="lua", |
| 4140 |
luc=(CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and "lud") or (jit and "lub") or "luc", |
| 4141 |
lui="lui", |
| 4142 |
luv="luv", |
| 4143 |
luj="luj", |
| 4144 |
tua="tua", |
| 4145 |
tuc=(CONTEXTLMTXMODE and CONTEXTLMTXMODE>0 and "tud") or (jit and "tub") or "tuc", |
| 4146 |
} |
| 4147 |
local function register(name) |
| 4148 |
if tracestripping then |
| 4149 |
report_lua("stripped bytecode from %a",name or "unknown") |
| 4150 |
end |
| 4151 |
strippedchunks[#strippedchunks+1]=name |
| 4152 |
luautilities.nofstrippedchunks=luautilities.nofstrippedchunks+1 |
| 4153 |
end |
| 4154 |
local function stupidcompile(luafile,lucfile,strip) |
| 4155 |
local code=io.loaddata(luafile) |
| 4156 |
if code and code~="" then |
| 4157 |
code=load(code) |
| 4158 |
if code then |
| 4159 |
code=dump(code,strip and luautilities.stripcode or luautilities.alwaysstripcode) |
| 4160 |
if code and code~="" then |
| 4161 |
register(name) |
| 4162 |
io.savedata(lucfile,code) |
| 4163 |
return true,0 |
| 4164 |
end |
| 4165 |
else |
| 4166 |
report_lua("fatal error %a in file %a",1,luafile) |
| 4167 |
end |
| 4168 |
else |
| 4169 |
report_lua("fatal error %a in file %a",2,luafile) |
| 4170 |
end |
| 4171 |
return false,0 |
| 4172 |
end |
| 4173 |
function luautilities.loadedluacode(fullname,forcestrip,name,macros) |
| 4174 |
name=name or fullname |
| 4175 |
if macros then |
| 4176 |
macros=lua.macros |
| 4177 |
end |
| 4178 |
local code,message |
| 4179 |
if macros then |
| 4180 |
code,message=macros.loaded(fullname,true,false) |
| 4181 |
else |
| 4182 |
code,message=loadfile(fullname) |
| 4183 |
end |
| 4184 |
if code then |
| 4185 |
code() |
| 4186 |
else |
| 4187 |
report_lua("loading of file %a failed:\n\t%s",fullname,message or "no message") |
| 4188 |
code,message=loadfile(fullname) |
| 4189 |
end |
| 4190 |
if forcestrip and luautilities.stripcode then |
| 4191 |
if type(forcestrip)=="function" then |
| 4192 |
forcestrip=forcestrip(fullname) |
| 4193 |
end |
| 4194 |
if forcestrip or luautilities.alwaysstripcode then |
| 4195 |
register(name) |
| 4196 |
return load(dump(code,true)),0 |
| 4197 |
else |
| 4198 |
return code,0 |
| 4199 |
end |
| 4200 |
elseif luautilities.alwaysstripcode then |
| 4201 |
register(name) |
| 4202 |
return load(dump(code,true)),0 |
| 4203 |
else |
| 4204 |
return code,0 |
| 4205 |
end |
| 4206 |
end |
| 4207 |
function luautilities.strippedloadstring(code,name,forcestrip) |
| 4208 |
local code,message=load(code) |
| 4209 |
if not code then |
| 4210 |
report_lua("loading of file %a failed:\n\t%s",name,message or "no message") |
| 4211 |
end |
| 4212 |
if forcestrip and luautilities.stripcode or luautilities.alwaysstripcode then |
| 4213 |
register(name) |
| 4214 |
return load(dump(code,true)),0 |
| 4215 |
else |
| 4216 |
return code,0 |
| 4217 |
end |
| 4218 |
end |
| 4219 |
function luautilities.loadstring(code,name) |
| 4220 |
local code,message=load(code) |
| 4221 |
if not code then |
| 4222 |
report_lua("loading of file %a failed:\n\t%s",name,message or "no message") |
| 4223 |
end |
| 4224 |
return code,0 |
| 4225 |
end |
| 4226 |
function luautilities.compile(luafile,lucfile,cleanup,strip,fallback) |
| 4227 |
report_lua("compiling %a into %a",luafile,lucfile) |
| 4228 |
os.remove(lucfile) |
| 4229 |
local done=stupidcompile(luafile,lucfile,strip~=false) |
| 4230 |
if done then |
| 4231 |
report_lua("dumping %a into %a stripped",luafile,lucfile) |
| 4232 |
if cleanup==true and lfs.isfile(lucfile) and lfs.isfile(luafile) then |
| 4233 |
report_lua("removing %a",luafile) |
| 4234 |
os.remove(luafile) |
| 4235 |
end |
| 4236 |
end |
| 4237 |
return done |
| 4238 |
end |
| 4239 |
function luautilities.loadstripped(...) |
| 4240 |
local l=load(...) |
| 4241 |
if l then |
| 4242 |
return load(dump(l,true)) |
| 4243 |
end |
| 4244 |
end |
| 4245 |
local finalizers={} |
| 4246 |
setmetatable(finalizers,{ |
| 4247 |
__gc=function(t) |
| 4248 |
for i=1,#t do |
| 4249 |
pcall(t[i]) |
| 4250 |
end |
| 4251 |
end |
| 4252 |
} ) |
| 4253 |
function luautilities.registerfinalizer(f) |
| 4254 |
finalizers[#finalizers+1]=f |
| 4255 |
end |
| 4256 |
function luautilities.checkmemory(previous,threshold,trace) |
| 4257 |
local current=collectgarbage("count") |
| 4258 |
if previous then |
| 4259 |
local checked=(threshold or 64)*1024 |
| 4260 |
local delta=current-previous |
| 4261 |
if current-previous>checked then |
| 4262 |
collectgarbage("collect") |
| 4263 |
local afterwards=collectgarbage("count") |
| 4264 |
if trace or tracememory then |
| 4265 |
report_mem("previous %r MB, current %r MB, delta %r MB, threshold %r MB, afterwards %r MB", |
| 4266 |
previous/1024,current/1024,delta/1024,threshold,afterwards) |
| 4267 |
end |
| 4268 |
return afterwards |
| 4269 |
elseif trace or tracememory then |
| 4270 |
report_mem("previous %r MB, current %r MB, delta %r MB, threshold %r MB", |
| 4271 |
previous/1024,current/1024,delta/1024,threshold) |
| 4272 |
end |
| 4273 |
end |
| 4274 |
return current |
| 4275 |
end |
| 4276 |
|
| 4277 |
end -- closure |
| 4278 |
|
| 4279 |
do -- begin closure to overcome local limits and interference |
| 4280 |
|
| 4281 |
if not modules then modules={} end modules ['util-deb']={ |
| 4282 |
version=1.001, |
| 4283 |
comment="companion to luat-lib.mkiv", |
| 4284 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4285 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4286 |
license="see context related readme files" |
| 4287 |
} |
| 4288 |
local type,next,tostring,tonumber=type,next,tostring,tonumber |
| 4289 |
local format,find,sub,gsub=string.format,string.find,string.sub,string.gsub |
| 4290 |
local insert,remove,sort=table.insert,table.remove,table.sort |
| 4291 |
local setmetatableindex=table.setmetatableindex |
| 4292 |
utilities=utilities or {} |
| 4293 |
local debugger=utilities.debugger or {} |
| 4294 |
utilities.debugger=debugger |
| 4295 |
local report=logs.reporter("debugger") |
| 4296 |
local ticks=os.gettimeofday or os.clock |
| 4297 |
local seconds=function(n) return n or 0 end |
| 4298 |
local overhead=0 |
| 4299 |
local dummycalls=10*1000 |
| 4300 |
local nesting=0 |
| 4301 |
local names={} |
| 4302 |
local initialize=false |
| 4303 |
if lua.getpreciseticks then |
| 4304 |
initialize=function() |
| 4305 |
ticks=lua.getpreciseticks |
| 4306 |
seconds=lua.getpreciseseconds |
| 4307 |
initialize=false |
| 4308 |
end |
| 4309 |
elseif not (FFISUPPORTED and ffi) then |
| 4310 |
elseif os.type=="windows" then |
| 4311 |
initialize=function() |
| 4312 |
local kernel=ffilib("kernel32","system") |
| 4313 |
if kernel then |
| 4314 |
local tonumber=ffi.number or tonumber |
| 4315 |
ffi.cdef[[ |
| 4316 |
int QueryPerformanceFrequency(int64_t *lpFrequency); |
| 4317 |
int QueryPerformanceCounter(int64_t *lpPerformanceCount); |
| 4318 |
]] |
| 4319 |
local target=ffi.new("__int64[1]") |
| 4320 |
ticks=function() |
| 4321 |
if kernel.QueryPerformanceCounter(target)==1 then |
| 4322 |
return tonumber(target[0]) |
| 4323 |
else |
| 4324 |
return 0 |
| 4325 |
end |
| 4326 |
end |
| 4327 |
local target=ffi.new("__int64[1]") |
| 4328 |
seconds=function(ticks) |
| 4329 |
if kernel.QueryPerformanceFrequency(target)==1 then |
| 4330 |
return ticks/tonumber(target[0]) |
| 4331 |
else |
| 4332 |
return 0 |
| 4333 |
end |
| 4334 |
end |
| 4335 |
end |
| 4336 |
initialize=false |
| 4337 |
end |
| 4338 |
elseif os.type=="unix" then |
| 4339 |
initialize=function() |
| 4340 |
local C=ffi.C |
| 4341 |
local tonumber=ffi.number or tonumber |
| 4342 |
ffi.cdef [[ |
| 4343 |
/* what a mess */ |
| 4344 |
typedef int clk_id_t; |
| 4345 |
typedef enum { CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID } clk_id; |
| 4346 |
typedef struct timespec { long sec; long nsec; } ctx_timespec; |
| 4347 |
int clock_gettime(clk_id_t timerid, struct timespec *t); |
| 4348 |
]] |
| 4349 |
local target=ffi.new("ctx_timespec[?]",1) |
| 4350 |
local clock=C.CLOCK_PROCESS_CPUTIME_ID |
| 4351 |
ticks=function () |
| 4352 |
C.clock_gettime(clock,target) |
| 4353 |
return tonumber(target[0].sec*1000000000+target[0].nsec) |
| 4354 |
end |
| 4355 |
seconds=function(ticks) |
| 4356 |
return ticks/1000000000 |
| 4357 |
end |
| 4358 |
initialize=false |
| 4359 |
end |
| 4360 |
end |
| 4361 |
setmetatableindex(names,function(t,name) |
| 4362 |
local v=setmetatableindex(function(t,source) |
| 4363 |
local v=setmetatableindex(function(t,line) |
| 4364 |
local v={ total=0,count=0,nesting=0 } |
| 4365 |
t[line]=v |
| 4366 |
return v |
| 4367 |
end) |
| 4368 |
t[source]=v |
| 4369 |
return v |
| 4370 |
end) |
| 4371 |
t[name]=v |
| 4372 |
return v |
| 4373 |
end) |
| 4374 |
local getinfo=nil |
| 4375 |
local sethook=nil |
| 4376 |
local function hook(where) |
| 4377 |
local f=getinfo(2,"nSl") |
| 4378 |
if f then |
| 4379 |
local source=f.short_src |
| 4380 |
if not source then |
| 4381 |
return |
| 4382 |
end |
| 4383 |
local line=f.linedefined or 0 |
| 4384 |
local name=f.name |
| 4385 |
if not name then |
| 4386 |
local what=f.what |
| 4387 |
if what=="C" then |
| 4388 |
name="<anonymous>" |
| 4389 |
else |
| 4390 |
name=f.namewhat or what or "<unknown>" |
| 4391 |
end |
| 4392 |
end |
| 4393 |
local data=names[name][source][line] |
| 4394 |
if where=="call" then |
| 4395 |
local nesting=data.nesting |
| 4396 |
if nesting==0 then |
| 4397 |
data.count=data.count+1 |
| 4398 |
insert(data,ticks()) |
| 4399 |
data.nesting=1 |
| 4400 |
else |
| 4401 |
data.nesting=nesting+1 |
| 4402 |
end |
| 4403 |
elseif where=="return" then |
| 4404 |
local nesting=data.nesting |
| 4405 |
if nesting==1 then |
| 4406 |
local t=remove(data) |
| 4407 |
if t then |
| 4408 |
data.total=data.total+ticks()-t |
| 4409 |
end |
| 4410 |
data.nesting=0 |
| 4411 |
else |
| 4412 |
data.nesting=nesting-1 |
| 4413 |
end |
| 4414 |
end |
| 4415 |
end |
| 4416 |
end |
| 4417 |
function debugger.showstats(printer,threshold) |
| 4418 |
local printer=printer or report |
| 4419 |
local calls=0 |
| 4420 |
local functions=0 |
| 4421 |
local dataset={} |
| 4422 |
local length=0 |
| 4423 |
local realtime=0 |
| 4424 |
local totaltime=0 |
| 4425 |
local threshold=threshold or 0 |
| 4426 |
for name,sources in next,names do |
| 4427 |
for source,lines in next,sources do |
| 4428 |
for line,data in next,lines do |
| 4429 |
local count=data.count |
| 4430 |
if count>threshold then |
| 4431 |
if #name>length then |
| 4432 |
length=#name |
| 4433 |
end |
| 4434 |
local total=data.total |
| 4435 |
local real=total |
| 4436 |
if real>0 then |
| 4437 |
real=total-(count*overhead/dummycalls) |
| 4438 |
if real<0 then |
| 4439 |
real=0 |
| 4440 |
end |
| 4441 |
realtime=realtime+real |
| 4442 |
end |
| 4443 |
totaltime=totaltime+total |
| 4444 |
if line<0 then |
| 4445 |
line=0 |
| 4446 |
end |
| 4447 |
dataset[#dataset+1]={ real,total,count,name,source,line } |
| 4448 |
end |
| 4449 |
end |
| 4450 |
end |
| 4451 |
end |
| 4452 |
sort(dataset,function(a,b) |
| 4453 |
if a[1]==b[1] then |
| 4454 |
if a[2]==b[2] then |
| 4455 |
if a[3]==b[3] then |
| 4456 |
if a[4]==b[4] then |
| 4457 |
if a[5]==b[5] then |
| 4458 |
return a[6]<b[6] |
| 4459 |
else |
| 4460 |
return a[5]<b[5] |
| 4461 |
end |
| 4462 |
else |
| 4463 |
return a[4]<b[4] |
| 4464 |
end |
| 4465 |
else |
| 4466 |
return b[3]<a[3] |
| 4467 |
end |
| 4468 |
else |
| 4469 |
return b[2]<a[2] |
| 4470 |
end |
| 4471 |
else |
| 4472 |
return b[1]<a[1] |
| 4473 |
end |
| 4474 |
end) |
| 4475 |
if length>50 then |
| 4476 |
length=50 |
| 4477 |
end |
| 4478 |
local fmt=string.formatters["%4.9k s %3.3k %% %4.9k s %3.3k %% %8i # %-"..length.."s %4i %s"] |
| 4479 |
for i=1,#dataset do |
| 4480 |
local data=dataset[i] |
| 4481 |
local real=data[1] |
| 4482 |
local total=data[2] |
| 4483 |
local count=data[3] |
| 4484 |
local name=data[4] |
| 4485 |
local source=data[5] |
| 4486 |
local line=data[6] |
| 4487 |
calls=calls+count |
| 4488 |
functions=functions+1 |
| 4489 |
name=gsub(name,"%s+"," ") |
| 4490 |
if #name>length then |
| 4491 |
name=sub(name,1,length) |
| 4492 |
end |
| 4493 |
printer(fmt(seconds(total),100*total/totaltime,seconds(real),100*real/realtime,count,name,line,source)) |
| 4494 |
end |
| 4495 |
printer("") |
| 4496 |
printer(format("functions : %i",functions)) |
| 4497 |
printer(format("calls : %i",calls)) |
| 4498 |
printer(format("overhead : %f",seconds(overhead/1000))) |
| 4499 |
end |
| 4500 |
local function getdebug() |
| 4501 |
if sethook and getinfo then |
| 4502 |
return |
| 4503 |
end |
| 4504 |
if not debug then |
| 4505 |
local okay |
| 4506 |
okay,debug=pcall(require,"debug") |
| 4507 |
end |
| 4508 |
if type(debug)~="table" then |
| 4509 |
return |
| 4510 |
end |
| 4511 |
getinfo=debug.getinfo |
| 4512 |
sethook=debug.sethook |
| 4513 |
if type(getinfo)~="function" then |
| 4514 |
getinfo=nil |
| 4515 |
end |
| 4516 |
if type(sethook)~="function" then |
| 4517 |
sethook=nil |
| 4518 |
end |
| 4519 |
end |
| 4520 |
function debugger.savestats(filename,threshold) |
| 4521 |
local f=io.open(filename,'w') |
| 4522 |
if f then |
| 4523 |
debugger.showstats(function(str) f:write(str,"\n") end,threshold) |
| 4524 |
f:close() |
| 4525 |
end |
| 4526 |
end |
| 4527 |
function debugger.enable() |
| 4528 |
getdebug() |
| 4529 |
if sethook and getinfo and nesting==0 then |
| 4530 |
running=true |
| 4531 |
if initialize then |
| 4532 |
initialize() |
| 4533 |
end |
| 4534 |
sethook(hook,"cr") |
| 4535 |
local function dummy() end |
| 4536 |
local t=ticks() |
| 4537 |
for i=1,dummycalls do |
| 4538 |
dummy() |
| 4539 |
end |
| 4540 |
overhead=ticks()-t |
| 4541 |
end |
| 4542 |
if nesting>0 then |
| 4543 |
nesting=nesting+1 |
| 4544 |
end |
| 4545 |
end |
| 4546 |
function debugger.disable() |
| 4547 |
if nesting>0 then |
| 4548 |
nesting=nesting-1 |
| 4549 |
end |
| 4550 |
if sethook and getinfo and nesting==0 then |
| 4551 |
sethook() |
| 4552 |
end |
| 4553 |
end |
| 4554 |
local function showtraceback(rep) |
| 4555 |
getdebug() |
| 4556 |
if getinfo then |
| 4557 |
local level=2 |
| 4558 |
local reporter=rep or report |
| 4559 |
while true do |
| 4560 |
local info=getinfo(level,"Sl") |
| 4561 |
if not info then |
| 4562 |
break |
| 4563 |
elseif info.what=="C" then |
| 4564 |
reporter("%2i : %s",level-1,"C function") |
| 4565 |
else |
| 4566 |
reporter("%2i : %s : %s",level-1,info.short_src,info.currentline) |
| 4567 |
end |
| 4568 |
level=level+1 |
| 4569 |
end |
| 4570 |
end |
| 4571 |
end |
| 4572 |
debugger.showtraceback=showtraceback |
| 4573 |
if luac then |
| 4574 |
local show,dump=luac.print,string.dump |
| 4575 |
function luac.inspect(v) |
| 4576 |
if type(v)=="function" then |
| 4577 |
local ok,str=xpcall(dump,function() end,v) |
| 4578 |
if ok then |
| 4579 |
v=str |
| 4580 |
end |
| 4581 |
end |
| 4582 |
if type(v)=="string" then |
| 4583 |
show(v,true) |
| 4584 |
else |
| 4585 |
print(v) |
| 4586 |
end |
| 4587 |
end |
| 4588 |
end |
| 4589 |
|
| 4590 |
end -- closure |
| 4591 |
|
| 4592 |
do -- begin closure to overcome local limits and interference |
| 4593 |
|
| 4594 |
if not modules then modules={} end modules ['util-tpl']={ |
| 4595 |
version=1.001, |
| 4596 |
comment="companion to luat-lib.mkiv", |
| 4597 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4598 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4599 |
license="see context related readme files" |
| 4600 |
} |
| 4601 |
utilities.templates=utilities.templates or {} |
| 4602 |
local templates=utilities.templates |
| 4603 |
local trace_template=false trackers.register("templates.trace",function(v) trace_template=v end) |
| 4604 |
local report_template=logs.reporter("template") |
| 4605 |
local tostring,next=tostring,next |
| 4606 |
local format,sub,byte=string.format,string.sub,string.byte |
| 4607 |
local P,C,R,Cs,Cc,Carg,lpegmatch,lpegpatterns=lpeg.P,lpeg.C,lpeg.R,lpeg.Cs,lpeg.Cc,lpeg.Carg,lpeg.match,lpeg.patterns |
| 4608 |
local formatters=string.formatters |
| 4609 |
local replacer |
| 4610 |
local function replacekey(k,t,how,recursive) |
| 4611 |
local v=t[k] |
| 4612 |
if not v then |
| 4613 |
if trace_template then |
| 4614 |
report_template("unknown key %a",k) |
| 4615 |
end |
| 4616 |
return "" |
| 4617 |
else |
| 4618 |
v=tostring(v) |
| 4619 |
if trace_template then |
| 4620 |
report_template("setting key %a to value %a",k,v) |
| 4621 |
end |
| 4622 |
if recursive then |
| 4623 |
return lpegmatch(replacer,v,1,t,how,recursive) |
| 4624 |
else |
| 4625 |
return v |
| 4626 |
end |
| 4627 |
end |
| 4628 |
end |
| 4629 |
local sqlescape=lpeg.replacer { |
| 4630 |
{ "'","''" }, |
| 4631 |
{ "\\","\\\\" }, |
| 4632 |
{ "\r\n","\\n" }, |
| 4633 |
{ "\r","\\n" }, |
| 4634 |
} |
| 4635 |
local sqlquoted=Cs(Cc("'")*sqlescape*Cc("'")) |
| 4636 |
lpegpatterns.sqlescape=sqlescape |
| 4637 |
lpegpatterns.sqlquoted=sqlquoted |
| 4638 |
local luaescape=lpegpatterns.luaescape |
| 4639 |
local escapers={ |
| 4640 |
lua=function(s) |
| 4641 |
return lpegmatch(luaescape,s) |
| 4642 |
end, |
| 4643 |
sql=function(s) |
| 4644 |
return lpegmatch(sqlescape,s) |
| 4645 |
end, |
| 4646 |
} |
| 4647 |
local quotedescapers={ |
| 4648 |
lua=function(s) |
| 4649 |
return format("%q",s) |
| 4650 |
end, |
| 4651 |
sql=function(s) |
| 4652 |
return lpegmatch(sqlquoted,s) |
| 4653 |
end, |
| 4654 |
} |
| 4655 |
local luaescaper=escapers.lua |
| 4656 |
local quotedluaescaper=quotedescapers.lua |
| 4657 |
local function replacekeyunquoted(s,t,how,recurse) |
| 4658 |
if how==false then |
| 4659 |
return replacekey(s,t,how,recurse) |
| 4660 |
else |
| 4661 |
local escaper=how and escapers[how] or luaescaper |
| 4662 |
return escaper(replacekey(s,t,how,recurse)) |
| 4663 |
end |
| 4664 |
end |
| 4665 |
local function replacekeyquoted(s,t,how,recurse) |
| 4666 |
if how==false then |
| 4667 |
return replacekey(s,t,how,recurse) |
| 4668 |
else |
| 4669 |
local escaper=how and quotedescapers[how] or quotedluaescaper |
| 4670 |
return escaper(replacekey(s,t,how,recurse)) |
| 4671 |
end |
| 4672 |
end |
| 4673 |
local function replaceoptional(l,m,r,t,how,recurse) |
| 4674 |
local v=t[l] |
| 4675 |
return v and v~="" and lpegmatch(replacer,r,1,t,how or "lua",recurse or false) or "" |
| 4676 |
end |
| 4677 |
local function replaceformatted(l,m,r,t,how,recurse) |
| 4678 |
local v=t[r] |
| 4679 |
return v and formatters[l](v) |
| 4680 |
end |
| 4681 |
local single=P("%") |
| 4682 |
local double=P("%%") |
| 4683 |
local lquoted=P("%[") |
| 4684 |
local rquoted=P("]%") |
| 4685 |
local lquotedq=P("%(") |
| 4686 |
local rquotedq=P(")%") |
| 4687 |
local escape=double/'%%' |
| 4688 |
local nosingle=single/'' |
| 4689 |
local nodouble=double/'' |
| 4690 |
local nolquoted=lquoted/'' |
| 4691 |
local norquoted=rquoted/'' |
| 4692 |
local nolquotedq=lquotedq/'' |
| 4693 |
local norquotedq=rquotedq/'' |
| 4694 |
local nolformatted=P(":")/"%%" |
| 4695 |
local norformatted=P(":")/"" |
| 4696 |
local noloptional=P("%?")/'' |
| 4697 |
local noroptional=P("?%")/'' |
| 4698 |
local nomoptional=P(":")/'' |
| 4699 |
local args=Carg(1)*Carg(2)*Carg(3) |
| 4700 |
local key=nosingle*((C((1-nosingle)^1)*args)/replacekey)*nosingle |
| 4701 |
local quoted=nolquotedq*((C((1-norquotedq)^1)*args)/replacekeyquoted)*norquotedq |
| 4702 |
local unquoted=nolquoted*((C((1-norquoted)^1)*args)/replacekeyunquoted)*norquoted |
| 4703 |
local optional=noloptional*((C((1-nomoptional)^1)*nomoptional*C((1-noroptional)^1)*args)/replaceoptional)*noroptional |
| 4704 |
local formatted=nosingle*((Cs(nolformatted*(1-norformatted )^1)*norformatted*C((1-nosingle)^1)*args)/replaceformatted)*nosingle |
| 4705 |
local any=P(1) |
| 4706 |
replacer=Cs((unquoted+quoted+formatted+escape+optional+key+any)^0) |
| 4707 |
local function replace(str,mapping,how,recurse) |
| 4708 |
if mapping and str then |
| 4709 |
return lpegmatch(replacer,str,1,mapping,how or "lua",recurse or false) or str |
| 4710 |
else |
| 4711 |
return str |
| 4712 |
end |
| 4713 |
end |
| 4714 |
templates.replace=replace |
| 4715 |
function templates.replacer(str,how,recurse) |
| 4716 |
return function(mapping) |
| 4717 |
return lpegmatch(replacer,str,1,mapping,how or "lua",recurse or false) or str |
| 4718 |
end |
| 4719 |
end |
| 4720 |
function templates.load(filename,mapping,how,recurse) |
| 4721 |
local data=io.loaddata(filename) or "" |
| 4722 |
if mapping and next(mapping) then |
| 4723 |
return replace(data,mapping,how,recurse) |
| 4724 |
else |
| 4725 |
return data |
| 4726 |
end |
| 4727 |
end |
| 4728 |
function templates.resolve(t,mapping,how,recurse) |
| 4729 |
if not mapping then |
| 4730 |
mapping=t |
| 4731 |
end |
| 4732 |
for k,v in next,t do |
| 4733 |
t[k]=replace(v,mapping,how,recurse) |
| 4734 |
end |
| 4735 |
return t |
| 4736 |
end |
| 4737 |
|
| 4738 |
end -- closure |
| 4739 |
|
| 4740 |
do -- begin closure to overcome local limits and interference |
| 4741 |
|
| 4742 |
if not modules then modules={} end modules ['util-sta']={ |
| 4743 |
version=1.001, |
| 4744 |
comment="companion to util-ini.mkiv", |
| 4745 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4746 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4747 |
license="see context related readme files" |
| 4748 |
} |
| 4749 |
local insert,remove,fastcopy,concat=table.insert,table.remove,table.fastcopy,table.concat |
| 4750 |
local format=string.format |
| 4751 |
local select,tostring=select,tostring |
| 4752 |
local trace_stacker=false trackers.register("stacker.resolve",function(v) trace_stacker=v end) |
| 4753 |
local stacker=stacker or {} |
| 4754 |
utilities.stacker=stacker |
| 4755 |
local function start(s,t,first,last) |
| 4756 |
if s.mode=="switch" then |
| 4757 |
local n=tostring(t[last]) |
| 4758 |
if trace_stacker then |
| 4759 |
s.report("start: %s",n) |
| 4760 |
end |
| 4761 |
return n |
| 4762 |
else |
| 4763 |
local r={} |
| 4764 |
for i=first,last do |
| 4765 |
r[#r+1]=tostring(t[i]) |
| 4766 |
end |
| 4767 |
local n=concat(r," ") |
| 4768 |
if trace_stacker then |
| 4769 |
s.report("start: %s",n) |
| 4770 |
end |
| 4771 |
return n |
| 4772 |
end |
| 4773 |
end |
| 4774 |
local function stop(s,t,first,last) |
| 4775 |
if s.mode=="switch" then |
| 4776 |
local n=tostring(false) |
| 4777 |
if trace_stacker then |
| 4778 |
s.report("stop: %s",n) |
| 4779 |
end |
| 4780 |
return n |
| 4781 |
else |
| 4782 |
local r={} |
| 4783 |
for i=last,first,-1 do |
| 4784 |
r[#r+1]=tostring(false) |
| 4785 |
end |
| 4786 |
local n=concat(r," ") |
| 4787 |
if trace_stacker then |
| 4788 |
s.report("stop: %s",n) |
| 4789 |
end |
| 4790 |
return n |
| 4791 |
end |
| 4792 |
end |
| 4793 |
local function change(s,t1,first1,last1,t2,first2,last2) |
| 4794 |
if s.mode=="switch" then |
| 4795 |
local n=tostring(t2[last2]) |
| 4796 |
if trace_stacker then |
| 4797 |
s.report("change: %s",n) |
| 4798 |
end |
| 4799 |
return n |
| 4800 |
else |
| 4801 |
local r={} |
| 4802 |
for i=last1,first1,-1 do |
| 4803 |
r[#r+1]=tostring(false) |
| 4804 |
end |
| 4805 |
local n=concat(r," ") |
| 4806 |
for i=first2,last2 do |
| 4807 |
r[#r+1]=tostring(t2[i]) |
| 4808 |
end |
| 4809 |
if trace_stacker then |
| 4810 |
s.report("change: %s",n) |
| 4811 |
end |
| 4812 |
return n |
| 4813 |
end |
| 4814 |
end |
| 4815 |
function stacker.new(name) |
| 4816 |
local report=logs.reporter("stacker",name or nil) |
| 4817 |
local s |
| 4818 |
local stack={} |
| 4819 |
local list={} |
| 4820 |
local ids={} |
| 4821 |
local hash={} |
| 4822 |
local hashing=true |
| 4823 |
local function push(...) |
| 4824 |
for i=1,select("#",...) do |
| 4825 |
insert(stack,(select(i,...))) |
| 4826 |
end |
| 4827 |
if hashing then |
| 4828 |
local c=concat(stack,"|") |
| 4829 |
local n=hash[c] |
| 4830 |
if not n then |
| 4831 |
n=#list+1 |
| 4832 |
hash[c]=n |
| 4833 |
list[n]=fastcopy(stack) |
| 4834 |
end |
| 4835 |
insert(ids,n) |
| 4836 |
return n |
| 4837 |
else |
| 4838 |
local n=#list+1 |
| 4839 |
list[n]=fastcopy(stack) |
| 4840 |
insert(ids,n) |
| 4841 |
return n |
| 4842 |
end |
| 4843 |
end |
| 4844 |
local function pop() |
| 4845 |
remove(stack) |
| 4846 |
remove(ids) |
| 4847 |
return ids[#ids] or s.unset or -1 |
| 4848 |
end |
| 4849 |
local function clean() |
| 4850 |
if #stack==0 then |
| 4851 |
if trace_stacker then |
| 4852 |
s.report("%s list entries, %s stack entries",#list,#stack) |
| 4853 |
end |
| 4854 |
end |
| 4855 |
end |
| 4856 |
local tops={} |
| 4857 |
local top=nil |
| 4858 |
local switch=nil |
| 4859 |
local function resolve_reset(mode) |
| 4860 |
if #tops>0 then |
| 4861 |
report("resetting %s left-over states of %a",#tops,name) |
| 4862 |
end |
| 4863 |
tops={} |
| 4864 |
top=nil |
| 4865 |
switch=nil |
| 4866 |
end |
| 4867 |
local function resolve_begin(mode) |
| 4868 |
if mode then |
| 4869 |
switch=mode=="switch" |
| 4870 |
else |
| 4871 |
switch=s.mode=="switch" |
| 4872 |
end |
| 4873 |
top={ switch=switch } |
| 4874 |
insert(tops,top) |
| 4875 |
end |
| 4876 |
local function resolve_step(ti) |
| 4877 |
if not top then |
| 4878 |
return |
| 4879 |
end |
| 4880 |
local result=nil |
| 4881 |
local noftop=#top |
| 4882 |
if ti>0 then |
| 4883 |
local current=list[ti] |
| 4884 |
if current then |
| 4885 |
local noflist=#current |
| 4886 |
local nofsame=0 |
| 4887 |
if noflist>noftop then |
| 4888 |
for i=1,noflist do |
| 4889 |
if current[i]==top[i] then |
| 4890 |
nofsame=i |
| 4891 |
else |
| 4892 |
break |
| 4893 |
end |
| 4894 |
end |
| 4895 |
else |
| 4896 |
for i=1,noflist do |
| 4897 |
if current[i]==top[i] then |
| 4898 |
nofsame=i |
| 4899 |
else |
| 4900 |
break |
| 4901 |
end |
| 4902 |
end |
| 4903 |
end |
| 4904 |
local plus=nofsame+1 |
| 4905 |
if plus<=noftop then |
| 4906 |
if plus<=noflist then |
| 4907 |
if switch then |
| 4908 |
result=s.change(s,top,plus,noftop,current,nofsame,noflist) |
| 4909 |
else |
| 4910 |
result=s.change(s,top,plus,noftop,current,plus,noflist) |
| 4911 |
end |
| 4912 |
else |
| 4913 |
if switch then |
| 4914 |
result=s.change(s,top,plus,noftop,current,nofsame,noflist) |
| 4915 |
else |
| 4916 |
result=s.stop(s,top,plus,noftop) |
| 4917 |
end |
| 4918 |
end |
| 4919 |
elseif plus<=noflist then |
| 4920 |
if switch then |
| 4921 |
result=s.start(s,current,nofsame,noflist) |
| 4922 |
else |
| 4923 |
result=s.start(s,current,plus,noflist) |
| 4924 |
end |
| 4925 |
end |
| 4926 |
top=current |
| 4927 |
else |
| 4928 |
if 1<=noftop then |
| 4929 |
result=s.stop(s,top,1,noftop) |
| 4930 |
end |
| 4931 |
top={} |
| 4932 |
end |
| 4933 |
return result |
| 4934 |
else |
| 4935 |
if 1<=noftop then |
| 4936 |
result=s.stop(s,top,1,noftop) |
| 4937 |
end |
| 4938 |
top={} |
| 4939 |
return result |
| 4940 |
end |
| 4941 |
end |
| 4942 |
local function resolve_end() |
| 4943 |
if #tops>0 then |
| 4944 |
local result=s.stop(s,top,1,#top) |
| 4945 |
remove(tops) |
| 4946 |
top=tops[#tops] |
| 4947 |
switch=top and top.switch |
| 4948 |
return result |
| 4949 |
end |
| 4950 |
end |
| 4951 |
local function resolve(t) |
| 4952 |
resolve_begin() |
| 4953 |
for i=1,#t do |
| 4954 |
resolve_step(t[i]) |
| 4955 |
end |
| 4956 |
resolve_end() |
| 4957 |
end |
| 4958 |
s={ |
| 4959 |
name=name or "unknown", |
| 4960 |
unset=-1, |
| 4961 |
report=report, |
| 4962 |
start=start, |
| 4963 |
stop=stop, |
| 4964 |
change=change, |
| 4965 |
push=push, |
| 4966 |
pop=pop, |
| 4967 |
clean=clean, |
| 4968 |
resolve=resolve, |
| 4969 |
resolve_begin=resolve_begin, |
| 4970 |
resolve_step=resolve_step, |
| 4971 |
resolve_end=resolve_end, |
| 4972 |
resolve_reset=resolve_reset, |
| 4973 |
} |
| 4974 |
return s |
| 4975 |
end |
| 4976 |
|
| 4977 |
end -- closure |
| 4978 |
|
| 4979 |
do -- begin closure to overcome local limits and interference |
| 4980 |
|
| 4981 |
if not modules then modules={} end modules ['util-zip']={ |
| 4982 |
version=1.001, |
| 4983 |
author="Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 4984 |
copyright="PRAGMA ADE / ConTeXt Development Team", |
| 4985 |
license="see context related readme files" |
| 4986 |
} |
| 4987 |
local type,tostring,tonumber=type,tostring,tonumber |
| 4988 |
local sort,concat=table.sort,table.concat |
| 4989 |
local find,format,sub,gsub=string.find,string.format,string.sub,string.gsub |
| 4990 |
local osdate,ostime,osclock=os.date,os.time,os.clock |
| 4991 |
local ioopen=io.open |
| 4992 |
local loaddata,savedata=io.loaddata,io.savedata |
| 4993 |
local filejoin,isdir,dirname,mkdirs=file.join,lfs.isdir,file.dirname,dir.mkdirs |
| 4994 |
local suffix,suffixes=file.suffix,file.suffixes |
| 4995 |
local openfile=io.open |
| 4996 |
gzip=gzip or {} |
| 4997 |
if not zlib then |
| 4998 |
zlib=xzip |
| 4999 |
elseif not xzip then |
| 5000 |
xzip=zlib |
| 5001 |
end |
| 5002 |
local files=utilities.files |
| 5003 |
local openfile=files.open |
| 5004 |
local closefile=files.close |
| 5005 |
local getsize=files.size |
| 5006 |
local readstring=files.readstring |
| 5007 |
local readcardinal2=files.readcardinal2le |
| 5008 |
local readcardinal4=files.readcardinal4le |
| 5009 |
local setposition=files.setposition |
| 5010 |
local getposition=files.getposition |
| 5011 |
local skipbytes=files.skip |
| 5012 |
local band=bit32.band |
| 5013 |
local rshift=bit32.rshift |
| 5014 |
local lshift=bit32.lshift |
| 5015 |
local zlibdecompress=zlib.decompress |
| 5016 |
local zlibdecompresssize=zlib.decompresssize |
| 5017 |
local zlibchecksum=zlib.crc32 |
| 5018 |
if not CONTEXTLMTXMODE or CONTEXTLMTXMODE==0 then |
| 5019 |
local cs=zlibchecksum |
| 5020 |
zlibchecksum=function(str,n) return cs(n or 0,str) end |
| 5021 |
end |
| 5022 |
local decompress=function(source) return zlibdecompress (source,-15) end |
| 5023 |
local decompresssize=function(source,targetsize) return zlibdecompresssize(source,targetsize,-15) end |
| 5024 |
local calculatecrc=function(buffer,initial) return zlibchecksum (initial or 0,buffer) end |
| 5025 |
local zipfiles={} |
| 5026 |
utilities.zipfiles=zipfiles |
| 5027 |
local openzipfile,closezipfile,unzipfile,foundzipfile,getziphash,getziplist do |
| 5028 |
function openzipfile(name) |
| 5029 |
return { |
| 5030 |
name=name, |
| 5031 |
handle=openfile(name,0), |
| 5032 |
} |
| 5033 |
end |
| 5034 |
local function update(handle,data) |
| 5035 |
position=data.offset |
| 5036 |
setposition(handle,position) |
| 5037 |
local signature=readstring(handle,4) |
| 5038 |
if signature=="PK\3\4" then |
| 5039 |
local version=readcardinal2(handle) |
| 5040 |
local flag=readcardinal2(handle) |
| 5041 |
local method=readcardinal2(handle) |
| 5042 |
skipbytes(handle,4) |
| 5043 |
local crc32=readcardinal4(handle) |
| 5044 |
local compressed=readcardinal4(handle) |
| 5045 |
local uncompressed=readcardinal4(handle) |
| 5046 |
local namelength=readcardinal2(handle) |
| 5047 |
local extralength=readcardinal2(handle) |
| 5048 |
local filename=readstring(handle,namelength) |
| 5049 |
local descriptor=band(flag,8)~=0 |
| 5050 |
local encrypted=band(flag,1)~=0 |
| 5051 |
local acceptable=method==0 or method==8 |
| 5052 |
local skipped=0 |
| 5053 |
local size=0 |
| 5054 |
if encrypted then |
| 5055 |
size=readcardinal2(handle) |
| 5056 |
skipbytes(handle,size) |
| 5057 |
skipped=skipped+size+2 |
| 5058 |
skipbytes(8) |
| 5059 |
skipped=skipped+8 |
| 5060 |
size=readcardinal2(handle) |
| 5061 |
skipbytes(handle,size) |
| 5062 |
skipped=skipped+size+2 |
| 5063 |
size=readcardinal4(handle) |
| 5064 |
skipbytes(handle,size) |
| 5065 |
skipped=skipped+size+4 |
| 5066 |
size=readcardinal2(handle) |
| 5067 |
skipbytes(handle,size) |
| 5068 |
skipped=skipped+size+2 |
| 5069 |
end |
| 5070 |
if acceptable then |
| 5071 |
if filename~=data.filename then |
| 5072 |
else |
| 5073 |
position=position+30+namelength+extralength+skipped |
| 5074 |
data.position=position |
| 5075 |
return position |
| 5076 |
end |
| 5077 |
else |
| 5078 |
end |
| 5079 |
end |
| 5080 |
data.position=false |
| 5081 |
return false |
| 5082 |
end |
| 5083 |
local function collect(z) |
| 5084 |
if not z.list then |
| 5085 |
local list={} |
| 5086 |
local hash={} |
| 5087 |
local position=0 |
| 5088 |
local index=0 |
| 5089 |
local handle=z.handle |
| 5090 |
local size=getsize(handle) |
| 5091 |
for i=size-4,size-64*1024,-1 do |
| 5092 |
setposition(handle,i) |
| 5093 |
local enddirsignature=readcardinal4(handle) |
| 5094 |
if enddirsignature==0x06054B50 then |
| 5095 |
local thisdisknumber=readcardinal2(handle) |
| 5096 |
local centraldisknumber=readcardinal2(handle) |
| 5097 |
local thisnofentries=readcardinal2(handle) |
| 5098 |
local totalnofentries=readcardinal2(handle) |
| 5099 |
local centralsize=readcardinal4(handle) |
| 5100 |
local centraloffset=readcardinal4(handle) |
| 5101 |
local commentlength=readcardinal2(handle) |
| 5102 |
local comment=readstring(handle,length) |
| 5103 |
if size-i>=22 then |
| 5104 |
if thisdisknumber==centraldisknumber then |
| 5105 |
setposition(handle,centraloffset) |
| 5106 |
while true do |
| 5107 |
if readcardinal4(handle)==0x02014B50 then |
| 5108 |
skipbytes(handle,4) |
| 5109 |
local flag=readcardinal2(handle) |
| 5110 |
local method=readcardinal2(handle) |
| 5111 |
skipbytes(handle,4) |
| 5112 |
local crc32=readcardinal4(handle) |
| 5113 |
local compressed=readcardinal4(handle) |
| 5114 |
local uncompressed=readcardinal4(handle) |
| 5115 |
local namelength=readcardinal2(handle) |
| 5116 |
local extralength=readcardinal2(handle) |
| 5117 |
local commentlength=readcardinal2(handle) |
| 5118 |
skipbytes(handle,8) |
| 5119 |
local headeroffset=readcardinal4(handle) |
| 5120 |
local filename=readstring(handle,namelength) |
| 5121 |
skipbytes(handle,extralength+commentlength) |
| 5122 |
local descriptor=band(flag,8)~=0 |
| 5123 |
local encrypted=band(flag,1)~=0 |
| 5124 |
local acceptable=method==0 or method==8 |
| 5125 |
if acceptable then |
| 5126 |
index=index+1 |
| 5127 |
local data={ |
| 5128 |
filename=filename, |
| 5129 |
index=index, |
| 5130 |
position=nil, |
| 5131 |
method=method, |
| 5132 |
compressed=compressed, |
| 5133 |
uncompressed=uncompressed, |
| 5134 |
crc32=crc32, |
| 5135 |
encrypted=encrypted, |
| 5136 |
offset=headeroffset, |
| 5137 |
} |
| 5138 |
hash[filename]=data |
| 5139 |
list[index]=data |
| 5140 |
end |
| 5141 |
else |
| 5142 |
break |
| 5143 |
end |
| 5144 |
end |
| 5145 |
end |
| 5146 |
break |
| 5147 |
end |
| 5148 |
end |
| 5149 |
end |
| 5150 |
z.list=list |
| 5151 |
z.hash=hash |
| 5152 |
end |
| 5153 |
end |
| 5154 |
function getziplist(z) |
| 5155 |
local list=z.list |
| 5156 |
if not list then |
| 5157 |
collect(z) |
| 5158 |
end |
| 5159 |
return z.list |
| 5160 |
end |
| 5161 |
function getziphash(z) |
| 5162 |
local hash=z.hash |
| 5163 |
if not hash then |
| 5164 |
collect(z) |
| 5165 |
end |
| 5166 |
return z.hash |
| 5167 |
end |
| 5168 |
function foundzipfile(z,name) |
| 5169 |
return getziphash(z)[name] |
| 5170 |
end |
| 5171 |
function closezipfile(z) |
| 5172 |
local f=z.handle |
| 5173 |
if f then |
| 5174 |
closefile(f) |
| 5175 |
z.handle=nil |
| 5176 |
end |
| 5177 |
end |
| 5178 |
function unzipfile(z,filename,check) |
| 5179 |
local hash=z.hash |
| 5180 |
if not hash then |
| 5181 |
hash=zipfiles.hash(z) |
| 5182 |
end |
| 5183 |
local data=hash[filename] |
| 5184 |
if not data then |
| 5185 |
end |
| 5186 |
if data then |
| 5187 |
local handle=z.handle |
| 5188 |
local position=data.position |
| 5189 |
local compressed=data.compressed |
| 5190 |
if position==nil then |
| 5191 |
position=update(handle,data) |
| 5192 |
end |
| 5193 |
if position and compressed>0 then |
| 5194 |
setposition(handle,position) |
| 5195 |
local result=readstring(handle,compressed) |
| 5196 |
if data.method==8 then |
| 5197 |
if decompresssize then |
| 5198 |
result=decompresssize(result,data.uncompressed) |
| 5199 |
else |
| 5200 |
result=decompress(result) |
| 5201 |
end |
| 5202 |
end |
| 5203 |
if check and data.crc32~=calculatecrc(result) then |
| 5204 |
print("checksum mismatch") |
| 5205 |
return "" |
| 5206 |
end |
| 5207 |
return result |
| 5208 |
else |
| 5209 |
return "" |
| 5210 |
end |
| 5211 |
end |
| 5212 |
end |
| 5213 |
zipfiles.open=openzipfile |
| 5214 |
zipfiles.close=closezipfile |
| 5215 |
zipfiles.unzip=unzipfile |
| 5216 |
zipfiles.hash=getziphash |
| 5217 |
zipfiles.list=getziplist |
| 5218 |
zipfiles.found=foundzipfile |
| 5219 |
end |
| 5220 |
if xzip then |
| 5221 |
local writecardinal1=files.writebyte |
| 5222 |
local writecardinal2=files.writecardinal2le |
| 5223 |
local writecardinal4=files.writecardinal4le |
| 5224 |
local logwriter=logs.writer |
| 5225 |
local globpattern=dir.globpattern |
| 5226 |
local compress=xzip.compress |
| 5227 |
local checksum=xzip.crc32 |
| 5228 |
local function fromdostime(dostime,dosdate) |
| 5229 |
return ostime { |
| 5230 |
year=rshift(dosdate,9)+1980, |
| 5231 |
month=band(rshift(dosdate,5),0x0F), |
| 5232 |
day=band((dosdate ),0x1F), |
| 5233 |
hour=band(rshift(dostime,11) ), |
| 5234 |
min=band(rshift(dostime,5),0x3F), |
| 5235 |
sec=band((dostime ),0x1F), |
| 5236 |
} |
| 5237 |
end |
| 5238 |
local function todostime(time) |
| 5239 |
local t=osdate("*t",time) |
| 5240 |
return |
| 5241 |
lshift(t.year-1980,9)+lshift(t.month,5)+t.day, |
| 5242 |
lshift(t.hour,11)+lshift(t.min,5)+rshift(t.sec,1) |
| 5243 |
end |
| 5244 |
local function openzip(filename,level,comment,verbose) |
| 5245 |
local f=ioopen(filename,"wb") |
| 5246 |
if f then |
| 5247 |
return { |
| 5248 |
filename=filename, |
| 5249 |
handle=f, |
| 5250 |
list={}, |
| 5251 |
level=tonumber(level) or 3, |
| 5252 |
comment=tostring(comment), |
| 5253 |
verbose=verbose, |
| 5254 |
uncompressed=0, |
| 5255 |
compressed=0, |
| 5256 |
} |
| 5257 |
end |
| 5258 |
end |
| 5259 |
local function writezip(z,name,data,level,time) |
| 5260 |
local f=z.handle |
| 5261 |
local list=z.list |
| 5262 |
local level=tonumber(level) or z.level or 3 |
| 5263 |
local method=8 |
| 5264 |
local zipped=compress(data,level) |
| 5265 |
local checksum=checksum(data) |
| 5266 |
local verbose=z.verbose |
| 5267 |
if not zipped then |
| 5268 |
method=0 |
| 5269 |
zipped=data |
| 5270 |
end |
| 5271 |
local start=f:seek() |
| 5272 |
local compressed=#zipped |
| 5273 |
local uncompressed=#data |
| 5274 |
z.compressed=z.compressed+compressed |
| 5275 |
z.uncompressed=z.uncompressed+uncompressed |
| 5276 |
if verbose then |
| 5277 |
local pct=100*compressed/uncompressed |
| 5278 |
if pct>=100 then |
| 5279 |
logwriter(format("%10i %s",uncompressed,name)) |
| 5280 |
else |
| 5281 |
logwriter(format("%10i %02.1f %s",uncompressed,pct,name)) |
| 5282 |
end |
| 5283 |
end |
| 5284 |
f:write("\x50\x4b\x03\x04") |
| 5285 |
writecardinal2(f,0) |
| 5286 |
writecardinal2(f,0) |
| 5287 |
writecardinal2(f,method) |
| 5288 |
writecardinal2(f,0) |
| 5289 |
writecardinal2(f,0) |
| 5290 |
writecardinal4(f,checksum) |
| 5291 |
writecardinal4(f,compressed) |
| 5292 |
writecardinal4(f,uncompressed) |
| 5293 |
writecardinal2(f,#name) |
| 5294 |
writecardinal2(f,0) |
| 5295 |
f:write(name) |
| 5296 |
f:write(zipped) |
| 5297 |
list[#list+1]={ #zipped,#data,name,checksum,start,time or 0 } |
| 5298 |
end |
| 5299 |
local function closezip(z) |
| 5300 |
local f=z.handle |
| 5301 |
local list=z.list |
| 5302 |
local comment=z.comment |
| 5303 |
local verbose=z.verbose |
| 5304 |
local count=#list |
| 5305 |
local start=f:seek() |
| 5306 |
for i=1,count do |
| 5307 |
local l=list[i] |
| 5308 |
local compressed=l[1] |
| 5309 |
local uncompressed=l[2] |
| 5310 |
local name=l[3] |
| 5311 |
local checksum=l[4] |
| 5312 |
local start=l[5] |
| 5313 |
local time=l[6] |
| 5314 |
local date,time=todostime(time) |
| 5315 |
f:write('\x50\x4b\x01\x02') |
| 5316 |
writecardinal2(f,0) |
| 5317 |
writecardinal2(f,0) |
| 5318 |
writecardinal2(f,0) |
| 5319 |
writecardinal2(f,8) |
| 5320 |
writecardinal2(f,time) |
| 5321 |
writecardinal2(f,date) |
| 5322 |
writecardinal4(f,checksum) |
| 5323 |
writecardinal4(f,compressed) |
| 5324 |
writecardinal4(f,uncompressed) |
| 5325 |
writecardinal2(f,#name) |
| 5326 |
writecardinal2(f,0) |
| 5327 |
writecardinal2(f,0) |
| 5328 |
writecardinal2(f,0) |
| 5329 |
writecardinal2(f,0) |
| 5330 |
writecardinal4(f,0) |
| 5331 |
writecardinal4(f,start) |
| 5332 |
f:write(name) |
| 5333 |
end |
| 5334 |
local stop=f:seek() |
| 5335 |
local size=stop-start |
| 5336 |
f:write('\x50\x4b\x05\x06') |
| 5337 |
writecardinal2(f,0) |
| 5338 |
writecardinal2(f,0) |
| 5339 |
writecardinal2(f,count) |
| 5340 |
writecardinal2(f,count) |
| 5341 |
writecardinal4(f,size) |
| 5342 |
writecardinal4(f,start) |
| 5343 |
if type(comment)=="string" and comment~="" then |
| 5344 |
writecardinal2(f,#comment) |
| 5345 |
f:write(comment) |
| 5346 |
else |
| 5347 |
writecardinal2(f,0) |
| 5348 |
end |
| 5349 |
if verbose then |
| 5350 |
local compressed=z.compressed |
| 5351 |
local uncompressed=z.uncompressed |
| 5352 |
local filename=z.filename |
| 5353 |
local pct=100*compressed/uncompressed |
| 5354 |
logwriter("") |
| 5355 |
if pct>=100 then |
| 5356 |
logwriter(format("%10i %s",uncompressed,filename)) |
| 5357 |
else |
| 5358 |
logwriter(format("%10i %02.1f %s",uncompressed,pct,filename)) |
| 5359 |
end |
| 5360 |
end |
| 5361 |
f:close() |
| 5362 |
end |
| 5363 |
local function zipdir(zipname,path,level,verbose) |
| 5364 |
if type(zipname)=="table" then |
| 5365 |
verbose=zipname.verbose |
| 5366 |
level=zipname.level |
| 5367 |
path=zipname.path |
| 5368 |
zipname=zipname.zipname |
| 5369 |
end |
| 5370 |
if not zipname or zipname=="" then |
| 5371 |
return |
| 5372 |
end |
| 5373 |
if not path or path=="" then |
| 5374 |
path="." |
| 5375 |
end |
| 5376 |
if not isdir(path) then |
| 5377 |
return |
| 5378 |
end |
| 5379 |
path=gsub(path,"\\+","/") |
| 5380 |
path=gsub(path,"/+","/") |
| 5381 |
local list={} |
| 5382 |
local count=0 |
| 5383 |
globpattern(path,"",true,function(name,size,time) |
| 5384 |
count=count+1 |
| 5385 |
list[count]={ name,time } |
| 5386 |
end) |
| 5387 |
sort(list,function(a,b) |
| 5388 |
return a[1]<b[1] |
| 5389 |
end) |
| 5390 |
local zipf=openzip(zipname,level,comment,verbose) |
| 5391 |
if zipf then |
| 5392 |
local p=#path+2 |
| 5393 |
for i=1,count do |
| 5394 |
local li=list[i] |
| 5395 |
local name=li[1] |
| 5396 |
local time=li[2] |
| 5397 |
local data=loaddata(name) |
| 5398 |
local name=sub(name,p,#name) |
| 5399 |
writezip(zipf,name,data,level,time,verbose) |
| 5400 |
end |
| 5401 |
closezip(zipf) |
| 5402 |
end |
| 5403 |
end |
| 5404 |
local function unzipdir(zipname,path,verbose,collect,validate) |
| 5405 |
if type(zipname)=="table" then |
| 5406 |
validate=zipname.validate |
| 5407 |
collect=zipname.collect |
| 5408 |
verbose=zipname.verbose |
| 5409 |
path=zipname.path |
| 5410 |
zipname=zipname.zipname |
| 5411 |
end |
| 5412 |
if not zipname or zipname=="" then |
| 5413 |
return |
| 5414 |
end |
| 5415 |
if not path or path=="" then |
| 5416 |
path="." |
| 5417 |
end |
| 5418 |
local z=openzipfile(zipname) |
| 5419 |
if z then |
| 5420 |
local list=getziplist(z) |
| 5421 |
if list then |
| 5422 |
local total=0 |
| 5423 |
local count=#list |
| 5424 |
local step=number.idiv(count,10) |
| 5425 |
local done=0 |
| 5426 |
local steps=verbose=="steps" |
| 5427 |
local time=steps and osclock() |
| 5428 |
if collect then |
| 5429 |
collect={} |
| 5430 |
else |
| 5431 |
collect=false |
| 5432 |
end |
| 5433 |
for i=1,count do |
| 5434 |
local l=list[i] |
| 5435 |
local n=l.filename |
| 5436 |
if not validate or validate(n) then |
| 5437 |
local d=unzipfile(z,n) |
| 5438 |
if d then |
| 5439 |
local p=filejoin(path,n) |
| 5440 |
if mkdirs(dirname(p)) then |
| 5441 |
if steps then |
| 5442 |
total=total+#d |
| 5443 |
done=done+1 |
| 5444 |
if done>=step then |
| 5445 |
done=0 |
| 5446 |
logwriter(format("%4i files of %4i done, %10i bytes, %0.3f seconds",i,count,total,osclock()-time)) |
| 5447 |
end |
| 5448 |
elseif verbose then |
| 5449 |
logwriter(n) |
| 5450 |
end |
| 5451 |
savedata(p,d) |
| 5452 |
if collect then |
| 5453 |
collect[#collect+1]=p |
| 5454 |
end |
| 5455 |
end |
| 5456 |
else |
| 5457 |
logwriter(format("problem with file %s",n)) |
| 5458 |
end |
| 5459 |
else |
| 5460 |
end |
| 5461 |
end |
| 5462 |
if steps then |
| 5463 |
logwriter(format("%4i files of %4i done, %10i bytes, %0.3f seconds",count,count,total,osclock()-time)) |
| 5464 |
end |
| 5465 |
closezipfile(z) |
| 5466 |
if collect then |
| 5467 |
return collect |
| 5468 |
end |
| 5469 |
else |
| 5470 |
closezipfile(z) |
| 5471 |
end |
| 5472 |
end |
| 5473 |
end |
| 5474 |
zipfiles.zipdir=zipdir |
| 5475 |
zipfiles.unzipdir=unzipdir |
| 5476 |
end |
| 5477 |
local pattern="^\x1F\x8B\x08" |
| 5478 |
local gziplevel=3 |
| 5479 |
function gzip.suffix(filename) |
| 5480 |
local suffix,extra=suffixes(filename) |
| 5481 |
local gzipped=extra=="gz" |
| 5482 |
return suffix,gzipped |
| 5483 |
end |
| 5484 |
function gzip.compressed(s) |
| 5485 |
return s and find(s,pattern) |
| 5486 |
end |
| 5487 |
local getdecompressed |
| 5488 |
local putcompressed |
| 5489 |
if gzip.compress then |
| 5490 |
local gzipwindow=15+16 |
| 5491 |
local compress=zlib.compress |
| 5492 |
local decompress=zlib.decompress |
| 5493 |
getdecompressed=function(str) |
| 5494 |
return decompress(str,gzipwindow) |
| 5495 |
end |
| 5496 |
putcompressed=function(str,level) |
| 5497 |
return compress(str,level or gziplevel,nil,gzipwindow) |
| 5498 |
end |
| 5499 |
else |
| 5500 |
local gzipwindow=-15 |
| 5501 |
local identifier="\x1F\x8B" |
| 5502 |
local compress=zlib.compress |
| 5503 |
local decompress=zlib.decompress |
| 5504 |
local zlibchecksum=zlib.crc32 |
| 5505 |
if not CONTEXTLMTXMODE or CONTEXTLMTXMODE==0 then |
| 5506 |
local cs=zlibchecksum |
| 5507 |
zlibchecksum=function(str,n) return cs(n or 0,str) end |
| 5508 |
end |
| 5509 |
local streams=utilities.streams |
| 5510 |
local openstream=streams.openstring |
| 5511 |
local closestream=streams.close |
| 5512 |
local getposition=streams.getposition |
| 5513 |
local readbyte=streams.readbyte |
| 5514 |
local readcardinal4=streams.readcardinal4le |
| 5515 |
local readcardinal2=streams.readcardinal2le |
| 5516 |
local readstring=streams.readstring |
| 5517 |
local readcstring=streams.readcstring |
| 5518 |
local skipbytes=streams.skip |
| 5519 |
local tocardinal1=streams.tocardinal1 |
| 5520 |
local tocardinal4=streams.tocardinal4le |
| 5521 |
getdecompressed=function(str) |
| 5522 |
local s=openstream(str) |
| 5523 |
local identifier=readstring(s,2) |
| 5524 |
local method=readbyte(s,1) |
| 5525 |
local flags=readbyte(s,1) |
| 5526 |
local timestamp=readcardinal4(s) |
| 5527 |
local compression=readbyte(s,1) |
| 5528 |
local operating=readbyte(s,1) |
| 5529 |
local isjusttext=band(flags,0x01)~=0 and true or false |
| 5530 |
local extrasize=band(flags,0x04)~=0 and readcardinal2(s) or 0 |
| 5531 |
local filename=band(flags,0x08)~=0 and readcstring(s) or "" |
| 5532 |
local comment=band(flags,0x10)~=0 and readcstring(s) or "" |
| 5533 |
local checksum=band(flags,0x02)~=0 and readcardinal2(s) or 0 |
| 5534 |
local compressed=readstring(s,#str) |
| 5535 |
local data=decompress(compressed,gzipwindow) |
| 5536 |
return data |
| 5537 |
end |
| 5538 |
putcompressed=function(str,level,originalname) |
| 5539 |
return concat { |
| 5540 |
identifier, |
| 5541 |
tocardinal1(0x08), |
| 5542 |
tocardinal1(0x08), |
| 5543 |
tocardinal4(os.time()), |
| 5544 |
tocardinal1(0x02), |
| 5545 |
tocardinal1(0xFF), |
| 5546 |
(originalname or "unknownname").."\0", |
| 5547 |
compress(str,level,nil,gzipwindow), |
| 5548 |
tocardinal4(zlibchecksum(str)), |
| 5549 |
tocardinal4(#str), |
| 5550 |
} |
| 5551 |
end |
| 5552 |
end |
| 5553 |
function gzip.load(filename) |
| 5554 |
local f=openfile(filename,"rb") |
| 5555 |
if not f then |
| 5556 |
else |
| 5557 |
local data=f:read("*all") |
| 5558 |
f:close() |
| 5559 |
if data and data~="" then |
| 5560 |
if suffix(filename)=="gz" then |
| 5561 |
data=getdecompressed(data) |
| 5562 |
end |
| 5563 |
return data |
| 5564 |
end |
| 5565 |
end |
| 5566 |
end |
| 5567 |
function gzip.save(filename,data,level,originalname) |
| 5568 |
if suffix(filename)~="gz" then |
| 5569 |
filename=filename..".gz" |
| 5570 |
end |
| 5571 |
local f=openfile(filename,"wb") |
| 5572 |
if f then |
| 5573 |
data=putcompressed(data or "",level or gziplevel,originalname) |
| 5574 |
f:write(data) |
| 5575 |
f:close() |
| 5576 |
return #data |
| 5577 |
end |
| 5578 |
end |
| 5579 |
function gzip.compress(s,level) |
| 5580 |
if s and not find(s,pattern) then |
| 5581 |
if not level then |
| 5582 |
level=gziplevel |
| 5583 |
elseif level<=0 then |
| 5584 |
return s |
| 5585 |
elseif level>9 then |
| 5586 |
level=9 |
| 5587 |
end |
| 5588 |
return putcompressed(s,level or gziplevel) or s |
| 5589 |
end |
| 5590 |
end |
| 5591 |
function gzip.decompress(s) |
| 5592 |
if s and find(s,pattern) then |
| 5593 |
return getdecompressed(s) |
| 5594 |
else |
| 5595 |
return s |
| 5596 |
end |
| 5597 |
end |
| 5598 |
|
| 5599 |
end -- closure |