-
Notifications
You must be signed in to change notification settings - Fork 239
/
pl.utils.html
1604 lines (1294 loc) · 48.6 KB
/
pl.utils.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<title>Penlight Documentation</title>
<link rel="stylesheet" href="../ldoc_fixed.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
<!-- Menu -->
<div id="navigation">
<br/>
<h1>Penlight</h1>
<ul>
<li><a href="https://github.com/lunarmodules/Penlight">GitHub Project</a></li>
<li><a href="../index.html">Documentation</a></li>
</ul>
<h2>Contents</h2>
<ul>
<li><a href="#Functions">Functions</a></li>
<li><a href="#Tables">Tables</a></li>
<li><a href="#Error_handling">Error handling </a></li>
<li><a href="#File_handling">File handling </a></li>
<li><a href="#OS_functions">OS functions </a></li>
<li><a href="#String_functions">String functions </a></li>
<li><a href="#Functional">Functional </a></li>
<li><a href="#Deprecation">Deprecation </a></li>
</ul>
<h2>Libraries</h2>
<ul class="nowrap">
<li><a href="../libraries/pl.html">pl</a></li>
<li><a href="../libraries/pl.app.html">pl.app</a></li>
<li><a href="../libraries/pl.array2d.html">pl.array2d</a></li>
<li><a href="../libraries/pl.class.html">pl.class</a></li>
<li><a href="../libraries/pl.compat.html">pl.compat</a></li>
<li><a href="../libraries/pl.comprehension.html">pl.comprehension</a></li>
<li><a href="../libraries/pl.config.html">pl.config</a></li>
<li><a href="../libraries/pl.data.html">pl.data</a></li>
<li><a href="../libraries/pl.dir.html">pl.dir</a></li>
<li><a href="../libraries/pl.file.html">pl.file</a></li>
<li><a href="../libraries/pl.func.html">pl.func</a></li>
<li><a href="../libraries/pl.import_into.html">pl.import_into</a></li>
<li><a href="../libraries/pl.input.html">pl.input</a></li>
<li><a href="../libraries/pl.lapp.html">pl.lapp</a></li>
<li><a href="../libraries/pl.lexer.html">pl.lexer</a></li>
<li><a href="../libraries/pl.luabalanced.html">pl.luabalanced</a></li>
<li><a href="../libraries/pl.operator.html">pl.operator</a></li>
<li><a href="../libraries/pl.path.html">pl.path</a></li>
<li><a href="../libraries/pl.permute.html">pl.permute</a></li>
<li><a href="../libraries/pl.pretty.html">pl.pretty</a></li>
<li><a href="../libraries/pl.seq.html">pl.seq</a></li>
<li><a href="../libraries/pl.sip.html">pl.sip</a></li>
<li><a href="../libraries/pl.strict.html">pl.strict</a></li>
<li><a href="../libraries/pl.stringio.html">pl.stringio</a></li>
<li><a href="../libraries/pl.stringx.html">pl.stringx</a></li>
<li><a href="../libraries/pl.tablex.html">pl.tablex</a></li>
<li><a href="../libraries/pl.template.html">pl.template</a></li>
<li><a href="../libraries/pl.test.html">pl.test</a></li>
<li><a href="../libraries/pl.text.html">pl.text</a></li>
<li><a href="../libraries/pl.types.html">pl.types</a></li>
<li><a href="../libraries/pl.url.html">pl.url</a></li>
<li><strong>pl.utils</strong></li>
<li><a href="../libraries/pl.xml.html">pl.xml</a></li>
</ul>
<h2>Classes</h2>
<ul class="nowrap">
<li><a href="../classes/pl.Date.html">pl.Date</a></li>
<li><a href="../classes/pl.List.html">pl.List</a></li>
<li><a href="../classes/pl.Map.html">pl.Map</a></li>
<li><a href="../classes/pl.MultiMap.html">pl.MultiMap</a></li>
<li><a href="../classes/pl.OrderedMap.html">pl.OrderedMap</a></li>
<li><a href="../classes/pl.Set.html">pl.Set</a></li>
</ul>
<h2>Manual</h2>
<ul class="nowrap">
<li><a href="../manual/01-introduction.md.html">Introduction</a></li>
<li><a href="../manual/02-arrays.md.html">Tables and Arrays</a></li>
<li><a href="../manual/03-strings.md.html">Strings. Higher-level operations on strings.</a></li>
<li><a href="../manual/04-paths.md.html">Paths and Directories</a></li>
<li><a href="../manual/05-dates.md.html">Date and Time</a></li>
<li><a href="../manual/06-data.md.html">Data</a></li>
<li><a href="../manual/07-functional.md.html">Functional Programming</a></li>
<li><a href="../manual/08-additional.md.html">Additional Libraries</a></li>
<li><a href="../manual/09-discussion.md.html">Technical Choices</a></li>
</ul>
<h2>Examples</h2>
<ul class="nowrap">
<li><a href="../examples/seesubst.lua.html">seesubst.lua</a></li>
<li><a href="../examples/sipscan.lua.html">sipscan.lua</a></li>
<li><a href="../examples/symbols.lua.html">symbols.lua</a></li>
<li><a href="../examples/test-cmp.lua.html">test-cmp.lua</a></li>
<li><a href="../examples/test-data.lua.html">test-data.lua</a></li>
<li><a href="../examples/test-listcallbacks.lua.html">test-listcallbacks.lua</a></li>
<li><a href="../examples/test-pretty.lua.html">test-pretty.lua</a></li>
<li><a href="../examples/test-symbols.lua.html">test-symbols.lua</a></li>
<li><a href="../examples/testclone.lua.html">testclone.lua</a></li>
<li><a href="../examples/testconfig.lua.html">testconfig.lua</a></li>
<li><a href="../examples/testglobal.lua.html">testglobal.lua</a></li>
<li><a href="../examples/testinputfields.lua.html">testinputfields.lua</a></li>
<li><a href="../examples/testinputfields2.lua.html">testinputfields2.lua</a></li>
<li><a href="../examples/testxml.lua.html">testxml.lua</a></li>
<li><a href="../examples/which.lua.html">which.lua</a></li>
</ul>
</div>
<div id="content">
<h1>Module <code>pl.utils</code></h1>
<p>Generally useful routines.</p>
<p> See <a href="../manual/01-introduction.md.html#Generally_useful_functions_">the Guide</a>.</p>
<p> Dependencies: <a href="../libraries/pl.compat.html#">pl.compat</a>, all exported fields and functions from
<a href="../libraries/pl.compat.html#">pl.compat</a> are also available in this module.</p>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#pack">pack (...)</a></td>
<td class="summary">pack an argument list into a table.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#unpack">unpack (t[, i[, j]])</a></td>
<td class="summary">unpack a table and return its contents.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#printf">printf (fmt, ...)</a></td>
<td class="summary">print an arbitrary number of arguments using a format.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#fprintf">fprintf (f, fmt, ...)</a></td>
<td class="summary">write an arbitrary number of arguments to a file using a format.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#import">import (t, T)</a></td>
<td class="summary">take a table and 'inject' it into the local namespace.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#choose">choose (cond, value1, value2)</a></td>
<td class="summary">return either of two values, depending on a condition.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#array_tostring">array_tostring (t[, temp[, tostr]])</a></td>
<td class="summary">convert an array of values to strings.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#is_type">is_type (obj, tp)</a></td>
<td class="summary">is the object of the specified type?</td>
</tr>
<tr>
<td class="name" nowrap><a href="#npairs">npairs (t[, i_start=1[, i_end=t.n or #t[, step=1]]])</a></td>
<td class="summary">an iterator with indices, similar to <a href="https://www.lua.org/manual/5.1/manual.html#pdf-ipairs">ipairs</a>, but with a range.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#kpairs">kpairs (t)</a></td>
<td class="summary">an iterator over all non-integer keys (inverse of <a href="https://www.lua.org/manual/5.1/manual.html#pdf-ipairs">ipairs</a>).</td>
</tr>
</table>
<h2><a href="#Tables">Tables</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#patterns">patterns</a></td>
<td class="summary">Some standard patterns</td>
</tr>
<tr>
<td class="name" nowrap><a href="#stdmt">stdmt</a></td>
<td class="summary">Standard meta-tables as used by other Penlight modules</td>
</tr>
</table>
<h2><a href="#Error_handling">Error handling </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#assert_arg">assert_arg (n, val, tp, verify, msg, lev)</a></td>
<td class="summary">assert that the given argument is in fact of the correct type.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#enum">enum (...)</a></td>
<td class="summary">creates an Enum or constants lookup table for improved error handling.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#function_arg">function_arg (idx, f, msg)</a></td>
<td class="summary">process a function argument.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#assert_string">assert_string (n, val)</a></td>
<td class="summary">assert the common case that the argument is a string.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#on_error">on_error (mode)</a></td>
<td class="summary">control the error strategy used by Penlight.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#raise">raise (err)</a></td>
<td class="summary">used by Penlight functions to return errors.</td>
</tr>
</table>
<h2><a href="#File_handling">File handling </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#readfile">readfile (filename, is_bin)</a></td>
<td class="summary">return the contents of a file as a string</td>
</tr>
<tr>
<td class="name" nowrap><a href="#writefile">writefile (filename, str, is_bin)</a></td>
<td class="summary">write a string to a file</td>
</tr>
<tr>
<td class="name" nowrap><a href="#readlines">readlines (filename)</a></td>
<td class="summary">return the contents of a file as a list of lines</td>
</tr>
</table>
<h2><a href="#OS_functions">OS functions </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#executeex">executeex (cmd, bin)</a></td>
<td class="summary">execute a shell command and return the output.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#quote_arg">quote_arg (argument)</a></td>
<td class="summary">Quote and escape an argument of a command.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#quit">quit ([code], msg, ...)</a></td>
<td class="summary">error out of this program gracefully.</td>
</tr>
</table>
<h2><a href="#String_functions">String functions </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#escape">escape (s)</a></td>
<td class="summary">escape any Lua 'magic' characters in a string</td>
</tr>
<tr>
<td class="name" nowrap><a href="#split">split (s, re, plain, n)</a></td>
<td class="summary">split a string into a list of strings separated by a delimiter.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#splitv">splitv (s, re, plain, n)</a></td>
<td class="summary">split a string into a number of return values.</td>
</tr>
</table>
<h2><a href="#Functional">Functional </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#memoize">memoize (func)</a></td>
<td class="summary">'memoize' a function (cache returned value for next call).</td>
</tr>
<tr>
<td class="name" nowrap><a href="#add_function_factory">add_function_factory (mt, fun)</a></td>
<td class="summary">associate a function factory with a type.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#string_lambda">string_lambda (lf)</a></td>
<td class="summary">an anonymous function as a string.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#bind1">bind1 (fn, p)</a></td>
<td class="summary">bind the first argument of the function to a value.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#bind2">bind2 (fn, p)</a></td>
<td class="summary">bind the second argument of the function to a value.</td>
</tr>
</table>
<h2><a href="#Deprecation">Deprecation </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#set_deprecation_func">set_deprecation_func (func)</a></td>
<td class="summary">Sets a deprecation warning function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#raise_deprecation">raise_deprecation (opts)</a></td>
<td class="summary">raises a deprecation warning.</td>
</tr>
</table>
<br/>
<br/>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
<dl class="function">
<dt>
<a name = "pack"></a>
<strong>pack (...)</strong>
</dt>
<dd>
pack an argument list into a table.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
any arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
a table with field <code>n</code> set to the length
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../libraries/pl.compat.html#table.pack">compat.pack</a></li>
<li><a href="../libraries/pl.utils.html#npairs">utils.npairs</a></li>
<li><a href="../libraries/pl.utils.html#unpack">utils.unpack</a></li>
</ul>
</dd>
<dt>
<a name = "unpack"></a>
<strong>unpack (t[, i[, j]])</strong>
</dt>
<dd>
unpack a table and return its contents. </p>
<p> NOTE: this implementation differs from the Lua implementation in the way
that this one DOES honor the <code>n</code> field in the table <code>t</code>, such that it is 'nil-safe'.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
table to unpack
</li>
<li><span class="parameter">i</span>
index from which to start unpacking, defaults to 1
(<em>optional</em>)
</li>
<li><span class="parameter">j</span>
index of the last element to unpack, defaults to <code>t.n</code> or else <code>#t</code>
(<em>optional</em>)
</li>
</ul>
<h3>Returns:</h3>
<ol>
multiple return values from the table
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../libraries/pl.compat.html#table.unpack">compat.unpack</a></li>
<li><a href="../libraries/pl.utils.html#pack">utils.pack</a></li>
<li><a href="../libraries/pl.utils.html#npairs">utils.npairs</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> t = <span class="global">table</span>.pack(<span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="number">4</span>)
<span class="keyword">local</span> a, b, c, d = <span class="global">table</span>.<span class="global">unpack</span>(t) <span class="comment">-- this <a href="../libraries/pl.utils.html#unpack">unpack</a> is NOT nil-safe, so d == nil
</span>
<span class="keyword">local</span> a, b, c, d = utils.<span class="global">unpack</span>(t) <span class="comment">-- this is nil-safe, so d == 4</span></pre>
</ul>
</dd>
<dt>
<a name = "printf"></a>
<strong>printf (fmt, ...)</strong>
</dt>
<dd>
print an arbitrary number of arguments using a format.
Output will be sent to <code>stdout</code>.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fmt</span>
The format (see <a href="https://www.lua.org/manual/5.1/manual.html#pdf-string.format">string.format</a>)
</li>
<li><span class="parameter">...</span>
Extra arguments for format
</li>
</ul>
</dd>
<dt>
<a name = "fprintf"></a>
<strong>fprintf (f, fmt, ...)</strong>
</dt>
<dd>
write an arbitrary number of arguments to a file using a format.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">f</span>
File handle to write to.
</li>
<li><span class="parameter">fmt</span>
The format (see <a href="https://www.lua.org/manual/5.1/manual.html#pdf-string.format">string.format</a>).
</li>
<li><span class="parameter">...</span>
Extra arguments for format
</li>
</ul>
</dd>
<dt>
<a name = "import"></a>
<strong>import (t, T)</strong>
</dt>
<dd>
take a table and 'inject' it into the local namespace.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
The table (table), or module name (string), defaults to this <a href="../libraries/pl.utils.html">utils</a> module table
</li>
<li><span class="parameter">T</span>
An optional destination table (defaults to callers environment)
</li>
</ul>
</dd>
<dt>
<a name = "choose"></a>
<strong>choose (cond, value1, value2)</strong>
</dt>
<dd>
return either of two values, depending on a condition.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">cond</span>
A condition
</li>
<li><span class="parameter">value1</span>
Value returned if cond is truthy
</li>
<li><span class="parameter">value2</span>
Value returned if cond is falsy
</li>
</ul>
</dd>
<dt>
<a name = "array_tostring"></a>
<strong>array_tostring (t[, temp[, tostr]])</strong>
</dt>
<dd>
convert an array of values to strings.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
a list-like table
</li>
<li><span class="parameter">temp</span>
(table) buffer to use, otherwise allocate
(<em>optional</em>)
</li>
<li><span class="parameter">tostr</span>
custom tostring function, called with (value,index). Defaults to <a href="https://www.lua.org/manual/5.1/manual.html#pdf-tostring">tostring</a>.
(<em>optional</em>)
</li>
</ul>
<h3>Returns:</h3>
<ol>
the converted buffer
</ol>
</dd>
<dt>
<a name = "is_type"></a>
<strong>is_type (obj, tp)</strong>
</dt>
<dd>
is the object of the specified type?
If the type is a string, then use type, otherwise compare with metatable
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">obj</span>
An object to check
</li>
<li><span class="parameter">tp</span>
String of what type it should be
</li>
</ul>
<h3>Returns:</h3>
<ol>
boolean
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example">utils.is_type(<span class="string">"hello world"</span>, <span class="string">"string"</span>) <span class="comment">--> true
</span><span class="comment">-- or check metatable
</span><span class="keyword">local</span> my_mt = {}
<span class="keyword">local</span> my_obj = <span class="global">setmetatable</span>(my_obj, my_mt)
utils.is_type(my_obj, my_mt) <span class="comment">--> true</span></pre>
</ul>
</dd>
<dt>
<a name = "npairs"></a>
<strong>npairs (t[, i_start=1[, i_end=t.n or #t[, step=1]]])</strong>
</dt>
<dd>
an iterator with indices, similar to <a href="https://www.lua.org/manual/5.1/manual.html#pdf-ipairs">ipairs</a>, but with a range.
This is a nil-safe index based iterator that will return <code>nil</code> when there
is a hole in a list. To be safe ensure that table <code>t.n</code> contains the length.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.5">table</a></span>
the table to iterate over
</li>
<li><span class="parameter">i_start</span>
<span class="types"><span class="type">integer</span></span>
start index
(<em>default</em> 1)
</li>
<li><span class="parameter">i_end</span>
<span class="types"><span class="type">integer</span></span>
end index
(<em>default</em> t.n or #t)
</li>
<li><span class="parameter">step</span>
<span class="types"><span class="type">integer</span></span>
step size
(<em>default</em> 1)
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">integer</span></span>
index</li>
<li>
<span class="types"><span class="type">any</span></span>
value at index (which can be <code>nil</code>!)</li>
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../libraries/pl.utils.html#pack">utils.pack</a></li>
<li><a href="../libraries/pl.utils.html#unpack">utils.unpack</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> t = utils.pack(<span class="keyword">nil</span>, <span class="number">123</span>, <span class="keyword">nil</span>) <span class="comment">-- adds an <code>n</code> field when packing
</span>
<span class="keyword">for</span> i, v <span class="keyword">in</span> utils.npairs(t, <span class="number">2</span>) <span class="keyword">do</span> <span class="comment">-- start at index 2
</span> t[i] = <span class="global">tostring</span>(t[i])
<span class="keyword">end</span>
<span class="comment">-- t = { n = 3, [2] = "123", [3] = "nil" }</span></pre>
</ul>
</dd>
<dt>
<a name = "kpairs"></a>
<strong>kpairs (t)</strong>
</dt>
<dd>
an iterator over all non-integer keys (inverse of <a href="https://www.lua.org/manual/5.1/manual.html#pdf-ipairs">ipairs</a>).
It will skip any key that is an integer number, so negative indices or an
array with holes will not return those either (so it returns slightly less than
'the inverse of <a href="https://www.lua.org/manual/5.1/manual.html#pdf-ipairs">ipairs</a>').</p>
<p> This uses <a href="https://www.lua.org/manual/5.1/manual.html#pdf-pairs">pairs</a> under the hood, so any value that is iterable using <a href="https://www.lua.org/manual/5.1/manual.html#pdf-pairs">pairs</a>
will work with this function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.5">table</a></span>
the table to iterate over
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">key</span></span>
</li>
<li>
<span class="types"><span class="type">value</span></span>
</li>
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> t = {
<span class="string">"hello"</span>,
<span class="string">"world"</span>,
hello = <span class="string">"hallo"</span>,
world = <span class="string">"Welt"</span>,
}
<span class="keyword">for</span> k, v <span class="keyword">in</span> utils.kpairs(t) <span class="keyword">do</span>
<span class="global">print</span>(<span class="string">"German: "</span>, v)
<span class="keyword">end</span>
<span class="comment">-- output;
</span><span class="comment">-- German: hallo
</span><span class="comment">-- German: Welt</span></pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Tables"></a>Tables</h2>
<dl class="function">
<dt>
<a name = "patterns"></a>
<strong>patterns</strong>
</dt>
<dd>
Some standard patterns
<h3>Fields:</h3>
<ul>
<li><span class="parameter">FLOAT</span>
floating point number
</li>
<li><span class="parameter">INTEGER</span>
integer number
</li>
<li><span class="parameter">IDEN</span>
identifier
</li>
<li><span class="parameter">FILE</span>
file
</li>
</ul>
</dd>
<dt>
<a name = "stdmt"></a>
<strong>stdmt</strong>
</dt>
<dd>
Standard meta-tables as used by other Penlight modules
<h3>Fields:</h3>
<ul>
<li><span class="parameter">List</span>
the List metatable
</li>
<li><span class="parameter">Map</span>
the Map metatable
</li>
<li><span class="parameter">Set</span>
the Set metatable
</li>
<li><span class="parameter">MultiMap</span>
the MultiMap metatable
</li>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Error_handling"></a>Error handling </h2>
<dl class="function">
<dt>
<a name = "assert_arg"></a>
<strong>assert_arg (n, val, tp, verify, msg, lev)</strong>
</dt>
<dd>
assert that the given argument is in fact of the correct type.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">n</span>
argument index
</li>
<li><span class="parameter">val</span>
the value
</li>
<li><span class="parameter">tp</span>
the type
</li>
<li><span class="parameter">verify</span>
an optional verification function
</li>
<li><span class="parameter">msg</span>
an optional custom message
</li>
<li><span class="parameter">lev</span>
optional stack position for trace, default 2
</li>
</ul>
<h3>Returns:</h3>
<ol>
the validated value
</ol>
<h3>Raises:</h3>
if <code>val</code> is not the correct type
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> param1 = assert_arg(<span class="number">1</span>,<span class="string">"hello"</span>,<span class="string">'table'</span>) <span class="comment">--> error: argument 1 expected a 'table', got a 'string'
</span><span class="keyword">local</span> param4 = assert_arg(<span class="number">4</span>,<span class="string">'!@#$%^&*'</span>,<span class="string">'string'</span>,path.isdir,<span class="string">'not a directory'</span>)
<span class="comment">--> error: argument 4: '!@#$%^&*' not a directory</span></pre>
</ul>
</dd>
<dt>
<a name = "enum"></a>
<strong>enum (...)</strong>
</dt>
<dd>
creates an Enum or constants lookup table for improved error handling.
This helps prevent magic strings in code by throwing errors for accessing
non-existing values, and/or converting strings/identifiers to other values.</p>
<p> Calling on the object does the same, but returns a soft error; <code>nil + err</code>, if
the call is succesful (the key exists), it will return the value.</p>
<p> When calling with varargs or an array the values will be equal to the keys.
The enum object is read-only.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.5">table</a> or <span class="type">vararg</span></span>
the input for the Enum. If varargs or an array then the
values in the Enum will be equal to the names (must be strings), if a hash-table
then values remain (any type), and the keys must be strings.
</li>
</ul>
<h3>Returns:</h3>
<ol>
Enum object (read-only table/object)
</ol>
<h3>Usage:</h3>
<ul>
<li><pre class="example"><span class="comment">-- Enum access at runtime
</span><span class="keyword">local</span> obj = {}
obj.MOVEMENT = utils.enum(<span class="string">"FORWARD"</span>, <span class="string">"REVERSE"</span>, <span class="string">"LEFT"</span>, <span class="string">"RIGHT"</span>)
<span class="keyword">if</span> current_movement == obj.MOVEMENT.FORWARD <span class="keyword">then</span>
<span class="comment">-- do something
</span>
<span class="keyword">elseif</span> current_movement == obj.MOVEMENT.REVERES <span class="keyword">then</span>
<span class="comment">-- throws error due to typo 'REVERES', so a silent mistake becomes a hard error
</span> <span class="comment">-- "'REVERES' is not a valid value (expected one of: 'FORWARD', 'REVERSE', 'LEFT', 'RIGHT')"
</span>
<span class="keyword">end</span></pre></li>
<li><pre class="example"><span class="comment">-- standardized error codes
</span><span class="keyword">local</span> obj = {
ERR = utils.enum {
NOT_FOUND = <span class="string">"the item was not found"</span>,
OUT_OF_BOUNDS = <span class="string">"the index is outside the allowed range"</span>
},
some_method = <span class="keyword">function</span>(self)
<span class="keyword">return</span> self.ERR.OUT_OF_BOUNDS
<span class="keyword">end</span>,
}
<span class="keyword">local</span> result, err = obj:some_method()
<span class="keyword">if</span> <span class="keyword">not</span> result <span class="keyword">then</span>
<span class="keyword">if</span> err == obj.ERR.NOT_FOUND <span class="keyword">then</span>
<span class="comment">-- check on error code, not magic strings
</span>
<span class="keyword">else</span>
<span class="comment">-- return the error description, contained in the constant
</span> <span class="keyword">return</span> <span class="keyword">nil</span>, <span class="string">"error: "</span>..err <span class="comment">-- "error: the index is outside the allowed range"
</span> <span class="keyword">end</span>
<span class="keyword">end</span></pre></li>
<li><pre class="example"><span class="comment">-- validating/converting user-input
</span><span class="keyword">local</span> color = <span class="string">"purple"</span>
<span class="keyword">local</span> ansi_colors = utils.enum {
black = <span class="number">30</span>,
red = <span class="number">31</span>,
green = <span class="number">32</span>,
}
<span class="keyword">local</span> color_code, err = ansi_colors(color) <span class="comment">-- calling on the object, returns the value from the enum
</span><span class="keyword">if</span> <span class="keyword">not</span> color_code <span class="keyword">then</span>
<span class="global">print</span>(<span class="string">"bad 'color', "</span> .. err)
<span class="comment">-- "bad 'color', 'purple' is not a valid value (expected one of: 'black', 'red', 'green')"
</span> <span class="global">os</span>.exit(<span class="number">1</span>)
<span class="keyword">end</span></pre></li>
</ul>
</dd>
<dt>
<a name = "function_arg"></a>
<strong>function_arg (idx, f, msg)</strong>
</dt>
<dd>
process a function argument.
This is used throughout Penlight and defines what is meant by a function:
Something that is callable, or an operator string as defined by <code>pl.operator</code>,
such as '>' or '#'. If a function factory has been registered for the type, it will
be called to get the function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">idx</span>
argument index
</li>
<li><span class="parameter">f</span>
a function, operator string, or callable object
</li>
<li><span class="parameter">msg</span>
optional error message
</li>
</ul>
<h3>Returns:</h3>
<ol>
a callable
</ol>
<h3>Raises:</h3>
if idx is not a number or if f is not callable
</dd>
<dt>
<a name = "assert_string"></a>
<strong>assert_string (n, val)</strong>
</dt>
<dd>
assert the common case that the argument is a string.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">n</span>
argument index
</li>
<li><span class="parameter">val</span>
a value that must be a string
</li>
</ul>
<h3>Returns:</h3>
<ol>
the validated value
</ol>
<h3>Raises:</h3>
val must be a string
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> val = <span class="number">42</span>
<span class="keyword">local</span> param2 = utils.assert_string(<span class="number">2</span>, val) <span class="comment">--> error: argument 2 expected a 'string', got a 'number'</span></pre>
</ul>
</dd>
<dt>
<a name = "on_error"></a>
<strong>on_error (mode)</strong>
</dt>
<dd>
<p>control the error strategy used by Penlight.
This is a global setting that controls how <a href="../libraries/pl.utils.html#raise">utils.raise</a> behaves:</p>
<ul>
<li>'default': return <code>nil + error</code> (this is the default)</li>
<li>'error': throw a Lua error</li>
<li>'quit': exit the program</li>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">mode</span>
either 'default', 'quit' or 'error'
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#raise">utils.raise</a>
</ul>
</dd>
<dt>
<a name = "raise"></a>
<strong>raise (err)</strong>
</dt>
<dd>
used by Penlight functions to return errors. Its global behaviour is controlled
by <a href="../libraries/pl.utils.html#on_error">utils.on_error</a>.
To use this function you MUST use it in conjunction with <code>return</code>, since it might
return <code>nil + error</code>.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">err</span>
the error string.
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#on_error">utils.on_error</a>
</ul>