-
Notifications
You must be signed in to change notification settings - Fork 5
/
Lua.yaml
1722 lines (1575 loc) · 53.8 KB
/
Lua.yaml
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
id: lua
name: Lua
subtitle: Tables, Strings, Math and Time
ordering:
- Overview
- Language
- Tables
- Strings
- Math
- Date and Time
functions:
#---------------------------------
# For Loops Overview
#---------------------------------
- category: overview
description: |
You can use **for loops** in Lua to iterate over arrays and tables, performing tasks for each element.
This example simply iterates i over the values 1 to 10
-- Iterate from 1 to 10
for i = 1, 10 do
print( i )
end
This example uses the `ipairs` function to sequentially iterate over a table. Note that ipairs only works on tables that have sequential elements beginning at 1.
-- Iterate over an array named 'arr'
arr = { 3, 2, 1 }
for i,v in ipairs(arr) do
print( "arr["..i.."] = "..v )
end
-- Prints:
-- arr[1] = 3
-- arr[2] = 2
-- arr[3] = 1
This example uses `pairs` to iterate over all the key/value pairs in a table, unlike `ipairs` the keys do not have to be integral values, and can be anything.
-- Iterate over a table named 'tab'
tab = { x = 3, y = "string", z = 1 }
for key,value in pairs(tab) do
print( "tab."..key.." = "..value )
end
-- Prints:
-- tab.y = string
-- tab.x = 3
-- tab.z = 1
group: Overview
id: for
name: For Loops Overview
related:
- ipairs
- pairs
#---------------------------------
#---------------------------------
# Conditionals Overview
#---------------------------------
- category: overview
description: |
Use **conditionals** to test for a particular circumstance and then branch your code appropriately. See the examples below.
-- Check if x is equal to 10
if x == 10 then
print( "x equals 10" )
end
-- else and elseif
if x == 10 then
print( "x is 10" )
elseif x == 5 then
print( "x is 5" )
else
print( "x is something else: "..x )
end
-- Checking multiple values
if x == 10 and y < 5 then
print( "x is 10 and y is less than 5" )
elseif x == 5 or y == 3 then
print( "x is 5 or y is 3" )
else
print( "x is "..x.." and y is "..y )
end
group: Overview
id: conditionals
name: Conditionals Overview
#---------------------------------
#---------------------------------
# While Loops
#---------------------------------
- category: overview
description: |
Use while and repeat loops to repeat code until a condition is true. While loops are used when you can't predict the number of times a loop will iterate, such as when reading input from a file or testing for a key press.
-- simple while loop. The condition is evaluated at the beginning of the loop. The loop will never get executed if the condition is false at the beginning of the loop.
while i<=5 do
print(i)
i=i+1
end
-- repeat loop. The condition is evaluated at the end of the loop, so the code always gets executed at least once
repeat
print(i)
i=i+1
until i>5
-- break statement: break exits a loop and continues program flow with the statement immediately following the end or until condition
while true do
print(i)
i=i+1
if i>5 then
break
end
end
group: Overview
id: loops
name: While Loops Overview
#---------------------------------
#---------------------------------
# ipairs()
#---------------------------------
- category: function
description: >
`ipairs` can be used to iterate over a table sequentially,
starting from the index 1 and continuing until the first integer key absent
from the table. Returns three values: an iterator function, the table,
and 0.
group: Language
id: ipairs
name: ipairs( table )
parameters:
- description: table to iterate over
name: table
related:
- for
- pairs
returns: >
Returns three values: an iterator function, the table, and 0
syntax: |
for i,v in ipairs( t ) do body end
-- This will iterate over the pairs:
-- (1,t[1]), (2,t[2]), ...
#---------------------------------
#---------------------------------
# pairs()
#---------------------------------
- category: function
description: >
`pairs` can be used to iterate over a tables key-value
pairs. Returns three values: the next function, the table t, and nil.
group: Language
id: pairs
name: pairs( table )
parameters:
- description: table to iterate over
name: table
related:
- for
- ipairs
returns: >
Returns three values: the next function, the table t, and nil
syntax: |
for k,v in pairs( t ) do body end
-- This will iterate over all key-value
-- pairs in table t
#---------------------------------
#---------------------------------
# table.concat
#---------------------------------
- category: function
description: >
Given an array where all elements are strings or numbers, returns `table[i]..sep..table[i+1] ... sep..table[j]`. The default value for `sep` is the empty string, the default for `i` is 1, and the default for `j` is the length of the table. If `i` is greater than `j`, returns the empty string.
group: Tables
id: table.concat
name: table.concat( table, sep )
parameters:
- description: table to concatenate
name: table
- description: separator string
name: sep
- description: int, starting index to concatenate from
name: i
- description: int, ending index
name: j
returns: A string of the elements in `table` concatenated with each other,
separated by `sep`
syntax: |
table.concat( table )
table.concat( table, sep )
table.concat( table, sep, i )
table.concat( table, sep, i, j )
#---------------------------------
#---------------------------------
# table.move
#---------------------------------
- category: function
description: >
Moves elements from table `a1` into table `a2`. This function performs the equivalent
to the multiple assignment: a2[t], ... = a1[f], ..., a1[e]. The default value for `a2` is `a1`. The
destination range can overlap with the source range. Index `f` must be positive.
group: Tables
id: table.move
name: table.move( a1, f, e, t, a2 )
parameters:
- description: table to move elements from
name: a1
- description: integer, starting index in table to move from
name: f
- description: integer, ending index in table to move from
name: e
- description: integer, starting index in table to move into
name: t
- description: table to move elements into (defaults to `a1`)
name: a2
syntax: |
table.move( a1, f, e, t )
table.move( a1, f, e, t, a2 )
#---------------------------------
#---------------------------------
# table.insert
#---------------------------------
- category: function
description: Inserts element `value` at position `pos` in `table`,
shifting up other elements to open space, if necessary. The default value for
`pos` is n+1, where n is the length of the table, so that a call `table.insert(t,x)`
inserts x at the end of table t.
group: Tables
id: table.insert
name: table.insert( table, pos, value )
parameters:
- description: table to insert into
name: table
- description: int, position to inset
name: pos
- description: value to insert
name: value
related:
- table.remove
syntax: |
table.insert( table, value )
table.insert( table, pos, value )
#---------------------------------
#---------------------------------
# table.remove
#---------------------------------
- category: function
description: Removes from `table` the element at position `pos`,
shifting down other elements to close the space, if necessary. Returns the value
of the removed element. The default value for `pos` is n, where n is the
length of the table, so that a call `table.remove(t)` removes the last
element of table t.
group: Tables
id: table.remove
name: table.remove( table, pos )
parameters:
- description: table to insert into
name: table
- description: int, position of value to remove
name: pos
related:
- table.insert
returns: Value of the removed element
syntax: |
table.remove( table )
table.remove( table, pos )
#---------------------------------
#---------------------------------
# table.pack
#---------------------------------
- category: function
description: Returns a new table with all parameters stored into keys 1, 2, etc.
and with a field "n" with the total number of parameters. Note that the resulting
table may not be a sequence.
group: Tables
id: table.pack
name: table.pack(...)
parameters:
- description: arguments to pack into table
name: ...
related:
- table.unpack
returns: table with all parameters packed
syntax: |
table.pack( ... )
#---------------------------------
#---------------------------------
# table.unpack
#---------------------------------
- category: function
description: Returns the elements from the given list.
group: Tables
id: table.unpack
name: table.unpack(list)
parameters:
- description: list to unpack
name: list
related:
- table.pack
returns: elements unpacked from list
syntax: |
table.unpack( list )
#---------------------------------
#---------------------------------
# table.sort
#---------------------------------
- category: function
description: >
Sorts table elements in a given order, in-place, from `table[1]`
to `table[n]`, where n is the length of the table. If `comp` is given,
then it must be a function that receives two table elements and returns true
when the first is less than the second (so that `not comp(a[i+1],a[i]`)
will be true after the sort). If `comp` is not given, then the standard
Lua operator < is used instead.
The sort algorithm is not stable;
that is, elements considered equal by the given order may have their relative
positions changed by the sort.
group: Tables
id: table.sort
name: table.sort( table )
parameters:
- description: the table to sort
name: table
- description: a function that receives two table elements and returns true when
the first is less than the second
name: comp
syntax: |
table.sort( table )
table.sort( table, comp )
#---------------------------------
#---------------------------------
# os.clock
#---------------------------------
- category: function
description: Returns an approximation of the amount in seconds of CPU time
used by the program.
group: Date and Time
id: os.clock
name: os.clock()
returns: Approximation of the amount in seconds of CPU time used by the program.
syntax: os.clock()
#---------------------------------
#---------------------------------
# os.difftime
#---------------------------------
- category: function
description: Returns the number of seconds from time `t1` to time `t2`.
In POSIX, Windows, and some other systems, this value is exactly `t2-t1`.
group: Date and Time
id: os.difftime
name: os.difftime( t2, t1 )
parameters:
- description: Ending time
name: t2
- description: Starting time
name: t1
related:
- os.time
returns: Number of seconds from time `t1` to time `t2`.
syntax: os.difftime( t2, t1 )
#---------------------------------
#---------------------------------
# os.date
#---------------------------------
- category: function
description: >
Returns a string or a table containing the date and time, formatted
according to the given string `format`.
If the `time` argument
is present, this is the time to be formatted (see the `os.time` function
for a description of this value). Otherwise, `date` formats the current
time.
If format starts with '!', then the date is formatted in
Coordinated Universal Time. After this optional character, if format is the
string '*t', then date returns a table with the following fields: year (four
digits), month (1--12), day (1--31), hour (0--23), min (0--59), sec (0--61),
wday (weekday, Sunday is 1), yday (day of the year), and isdst (daylight saving
flag, a boolean).
If `format` is not '*t', then `date`
returns the date as a string, formatted according to the same rules as the C
function `strftime`.
When called without arguments, date returns
a reasonable date and time representation that depends on the host system and
on the current locale (that is, `os.date()` is equivalent to `os.date('%c')`).
group: Date and Time
id: os.date
name: os.date( format )
parameters:
- description: String used to format the returned date
name: format
- description: If the time argument is present, this is the time to be formatted
(see the os.time function for a description of this value).
name: time
related:
- os.time
- os.setlocale
returns: A string or a table containing the date and time.
syntax: |
os.date()
os.date( format )
os.date( format, time )
#---------------------------------
#---------------------------------
# os.setlocale
#---------------------------------
- category: function
description: >
Sets the current locale of the program. `locale` is a string
specifying a locale; `category` is an optional string describing which
category to change: "all", "collate", "ctype", "monetary", "numeric", or "time";
the default category is "all". The function returns the name of the new locale,
or nil if the request cannot be honored.
If `locale` is the
empty string, the current locale is set to an implementation-defined native
locale. If `locale` is the string "C", the current locale is set to the
standard C locale.
When called with `nil` as the first argument,
this function only returns the name of the current locale for the given category.
group: Date and Time
id: os.setlocale
name: os.setlocale( locale )
parameters:
- description: String specifying a locale, can be nil or the empty string.
name: locale
- description: String specifying a category to set, can be "all", "collate", "ctype",
"monetary", "numeric", or "time"
name: category
related:
- os.date
- os.time
returns: When called with `nil` for the first argument, returns the name
of the current locale for the given category.
syntax: |
os.setlocale( locale )
os.setlocale( locale, category )
#---------------------------------
#---------------------------------
# os.time
#---------------------------------
- category: function
description: >
Returns the current time when called without arguments, or a time
representing the date and time specified by the given table. This table must
have fields `year`, `month`, and `day`, and may have fields `hour`,
`min`, `sec`, and `isdst` (for a description of these fields,
see the os.date function).
The returned value is a number, whose
meaning depends on your system. In POSIX, Windows, and some other systems, this
number counts the number of seconds since some given start time (the "epoch").
In other systems, the meaning is not specified, and the number returned by `time`
can be used only as an argument to `date` and `difftime`.
group: Date and Time
id: os.time
name: os.time()
parameters:
- description: This table must have fields `year`, `month`, and `day`,
and may have fields `hour`, `min`, `sec`, and `isdst`
name: table
related:
- os.date
- os.setlocale
- os.difftime
returns: A number, whose meaning depends on your system. In POSIX, Windows, and
some other systems, this number counts the number of seconds since some given
start time (the "epoch"). In other systems, the meaning is not specified, and
the number returned by `time` can be used only as an argument to `date`
and `difftime`.
syntax: |
os.time()
os.time( table )
#---------------------------------
#---------------------------------
# string.byte
#---------------------------------
- category: function
description: >
Returns the internal numerical codes of the characters s[i], s[i+1], ..., s[j].
The default value for `i` is 1; the default value for `j` is `i`. These indices are
corrected following the same rules of `string.sub`.
group: Strings
id: string.byte
name: string.byte( s, i, j )
parameters:
- description: string to use
name: s
- description: first index, defaults to 1
name: i
- description: last index, defaults to `i`
name: j
related:
- string.char
returns: The internal numerical codes for characters s[i] up to s[j]
syntax: |
string.byte( s )
string.byte( s, i )
string.byte( s, i, j )
#---------------------------------
#---------------------------------
# string.char
#---------------------------------
- category: function
description: >
Returns a string with length equal to the number of arguments, in which each character
has the internal numerical code equal to its corresponding argument.
group: Strings
id: string.char
name: string.char( ... )
parameters:
- description: a variable number of numerical codes
name: ...
related:
- string.byte
returns: String composed of characters equal to the numerical codes used as arguments
syntax: |
string.char( ... )
#---------------------------------
#---------------------------------
# string.dump
#---------------------------------
- category: function
description: >
Returns a string containing a binary representation (a *binary chunk*) of the given function,
so that a later `load` on this string returns a copy of the function (with new upvalues). If
`strip` is a true value, the binary representation is created without debug information about
the function (local variable names, lines, etc).
group: Strings
id: string.dump
name: string.dump( function )
parameters:
- description: a function to convert to binary string
name: function
- description: boolean, whether to strip debug information
name: strip
returns: Binary string representation of the specified function
syntax: |
string.dump( function )
string.dump( function, strip )
#---------------------------------
#---------------------------------
# string.find
#---------------------------------
- category: function
description: >
Looks for the first match of `pattern` in the string `s`.
If it finds a match, then `string.find()` returns the indices of `s`
where the occurrence starts and ends; otherwise, it returns `nil`. A third,
optional numerical argument `init` specifies where to start the search,
its default value is 1 and can be negative. A value of `true` as the fourth
optional argument `plain` turns off the pattern matching facilities so
the function performs a plain "find substring" operation, with no characters
in `pattern` being considered "magic." Note that if `plain` is given,
then `init` must be given as well.
If the pattern has captures,
then in a successful match the captured values are also returned, after the
two indices.
group: Strings
id: string.find
name: string.find( s, pattern )
parameters:
- description: string to search in
name: s
- description: pattern to look for
name: pattern
- description: starting character in string to search from, default is 1
name: init
- description: boolean, perform a plain substring search
name: plain
returns: The start and end indices of the match, or `nil` if no match. If
the pattern has captures then captured values are also returned after the indices
related:
- patterns
syntax: |
string.find( s, pattern )
string.find( s, pattern, init )
string.find( s, pattern, init, plain )
#---------------------------------
#---------------------------------
# string.format
#---------------------------------
- category: function
description: Returns a formatted version of its variable arguments following
the description given in the first argument, which must be a string. The format
string follows the same rules as the `printf` family of standard C functions.
The only difference are that the options/modifiers `*, 1, L, n, p` and
`h` are not supported and that there is an extra option `q`. The `q`
option formats a string in a form suitable to be safely read back by the Lua
interpreter, for example all double quotes, newlines, embedded zeros and backslashes
will be escaped when written.
examples:
- example: |
s = string.format("Number is %d", 5)
print( s )
-- prints "Number is 5"
group: Strings
id: string.format
name: string.format( formatstring, ... )
parameters:
- description: string defining the format
name: formatstring
returns: A formatted string
syntax: string.format( formatstring, ... )
#---------------------------------
#---------------------------------
# string.len
#---------------------------------
- category: function
description: Receives a string and returns its length. The empty string ""
has length 0.
group: Strings
id: string.len
name: string.len( s )
parameters:
- description: get the length of this string
name: s
returns: Length of string `s`
syntax: string.len( s )
#---------------------------------
#---------------------------------
# string.gmatch
#---------------------------------
- category: function
description: Returns an iterator function that, each time it is called, returns the next
captures from pattern (see **Patterns Overview**) over the string s. If pattern specifies no captures,
then the whole match is produced in each call.
group: Strings
id: string.gmatch
name: string.gmatch( s, pattern )
examples:
- example: |
-- Print each word in string
s = "hello world from Lua"
for w in string.gmatch(s,"%a+") do
print(w)
end
- example: |
-- Generate table from string
t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s,"(%w+)=(%w+)") do
t[k] = v
end
parameters:
- description: string to search
name: s
- description: pattern to match
name: pattern
related:
- patterns
- string.gsub
returns: Iterator function over matches of `pattern` within the string
syntax: |
string.gmatch( s, pattern )
#---------------------------------
#---------------------------------
# string.gsub
#---------------------------------
- category: function
description: >
Returns a copy of `s` in which all (or the first `n`, if given) occurrences of the
`pattern` (see **Patterns Overview**) have been replaced by a replacement string specified by `repl`,
which can be a string, a table, or a function. gsub also returns, as its second value,
the total number of matches that occurred. The name gsub comes from *Global SUBstitution*.
If `repl` is a string, then its value is used for replacement. The character `%` works as an escape
character: any sequence in `repl` of the form `%d`, with `d` between 1 and 9, stands for the value of
the `d`-th captured substring. The sequence `%0` stands for the whole match.
The sequence `%%` stands for a single `%`.
If `repl` is a table, then the table is queried for every match, using the first capture as the key.
If `repl` is a function, then this function is called every time a match occurs, with all captured
substrings passed as arguments, in order.
In any case, if the pattern specifies no captures, then it behaves as if the whole pattern was inside a capture.
If the value returned by the table query or by the function call is a string or a number, then it is used
as the replacement string; otherwise, if it is false or nil, then there is no replacement (that is, the
original match is kept in the string).
examples:
- example: |
x = string.gsub("hello world",
"(%w+)", "%1 %1")
group: Strings
id: string.gsub
name: string.gsub( s, pattern, repl )
parameters:
- description: string to substitute
name: s
- description: pattern to match
name: pattern
- description: string, table or function, replacement parameter
name: repl
- description: integer, number of occurrences to match (all if not specified)
name: n
related:
- patterns
- string.gmatch
returns: Returns a copy of `s` with substitutions made, as well as the number of matches
syntax: |
string.gsub( s, pattern, repl )
string.gsub( s, pattern, repl, n )
#---------------------------------
#---------------------------------
# string.lower
#---------------------------------
- category: function
description: Receives a string and returns a copy of this string with all uppercase
letters changed to lowercase. All other characters are left unchanged.
group: Strings
id: string.lower
name: string.lower( s )
parameters:
- description: get a lowercase version of this string
name: s
related:
- string.upper
returns: Lowercase version of string `s`
syntax: string.lower( s )
#---------------------------------
#---------------------------------
# string.upper
#---------------------------------
- category: function
description: Receives a string and returns a copy of this string with all lowercase
letters changed to uppercase. All other characters are left unchanged.
group: Strings
id: string.upper
name: string.upper( s )
parameters:
- description: get an uppercase version of this string
name: s
related:
- string.lower
returns: Uppercase version of string `s`
syntax: string.upper( s )
#---------------------------------
#---------------------------------
# string.match
#---------------------------------
- category: function
description: Looks for the first match of `pattern` in the string `s`.
If it finds one then `string.match()` returns the captures from the pattern,
otherwise it returns `nil`. If `pattern` specifies no captures, then
the whole match is returned. A third optional numerical argument `init`
specifies where to start the search. Its default is 1 and can be negative.
group: Strings
id: string.match
name: string.match( s, pattern )
parameters:
- description: string to search
name: s
- description: pattern to match
name: pattern
- description: starting location in string
name: init
related:
- string.find
returns: Captures from the first match of `pattern` in string `s`, or
`nil` if none were found
syntax: |
string.match( s, pattern )
string.match( s, pattern, init )
#---------------------------------
#---------------------------------
# string.rep
#---------------------------------
- category: function
description: Returns a string that is the concatenation of `n` copies
of the string `s`.
group: Strings
id: string.rep
name: string.rep( s, n )
parameters:
- description: string to replicate
name: s
- description: int, number of times to replicate the string
name: n
returns: >
`n` concatenations of string `s`
syntax: string.rep( s, n )
#---------------------------------
#---------------------------------
# string.reverse
#---------------------------------
- category: function
description: This function returns the string `s` reversed.
group: Strings
id: string.reverse
name: string.reverse( s )
parameters:
- description: string to reverse
name: s
returns: >
`s` reversed
syntax: string.reverse( s )
#---------------------------------
#---------------------------------
# string.sub
#---------------------------------
- category: function
description: Returns the substring of `s` that starts at `i` and
continues until `j`; `i` and `j` can be negative. If `j`
is absent, then it is assumed to be equal to -1 (which is the same as the string
length). In particular, the call `string.sub(s,1,j)` returns a prefix of
`s` with length `j`, and string.sub(s, -i) returns a suffix of `s`
with length `i`.
group: Strings
id: string.sub
name: string.sub( s, i, j )
parameters:
- description: find substring of this string
name: s
- description: int, starting index
name: i
- description: int, ending index
name: j
returns: Substring of string `s`
syntax: |
string.sub( s, i )
string.sub( s, i, j )
#---------------------------------
#---------------------------------
# string.pack
#---------------------------------
- category: function
description: Returns a binary string containing the values `v1`, `v2`, etc.
packed (that is, serialized in binary form) according to the format string `fmt`.
group: Strings
id: string.pack
name: string.pack( fmt, v1, v2, ... )
parameters:
- description: format string specifying binary format
name: fmt
- description: arguments to pack
name: ...
related:
- string.packsize
- string.unpack
returns: Values packed into binary string
syntax: |
string.pack( fmt, v1, v2, ... )
#---------------------------------
#---------------------------------
# string.packsize
#---------------------------------
- category: function
description: Returns the size of a string resulting from `string.pack` with the given format.
The format string cannot have the variable-length options 's' or 'z'
group: Strings
id: string.packsize
name: string.packsize( fmt )
parameters:
- description: format string specifying binary format
name: fmt
related:
- string.pack
- string.unpack
returns: Size of string packed with the given format
syntax: |
string.packsize( fmt )
#---------------------------------
#---------------------------------
# string.unpack
#---------------------------------
- category: function
description: Returns the values packed in string `s` according to the format string `fmt`.
An optional pos marks where to start reading in `s` (default is 1). After the read values,
this function also returns the index of the first unread byte in `s`.
group: Strings
id: string.pack
name: string.unpack( fmt, s )
parameters:
- description: format string specifying binary format
name: fmt
- description: string to unpack
name: s
related:
- string.pack
returns: The values packed in `s`
syntax: |
string.unpack( fmt, s )
string.unpack( fmt, s, pos )
#---------------------------------
#---------------------------------
# Patterns Overview
#---------------------------------
- category: overview
description: |
Patterns in Lua are described by regular strings, which are interpreted as patterns by the
pattern-matching functions `string.find`, `string.gmatch`, `string.gsub`, and `string.match`.
This section describes the syntax and the meaning (that is, what they match) of these strings.
**Character Class**
A character class is used to represent a set of characters. The following combinations are
allowed in describing a character class:
x: (where x is not one of the magic characters
^$()%.[]*+-?) represents the character x
itself.
.: (a dot) represents all characters.
%a: represents all letters.
%c: represents all control characters.
%d: represents all digits.
%g: represents all printable characters except space.
%l: represents all lowercase letters.
%p: represents all punctuation characters.
%s: represents all space characters.
%u: represents all uppercase letters.
%w: represents all alphanumeric characters.
%x: represents all hexadecimal digits.
%x: (where x is any non-alphanumeric character)
represents the character x.
This is the standard way to escape
the magic characters. Any non-alphanumeric
character (including all punctuations, even
the non-magical) can be preceded by a '%'
when used to represent itself in a pattern.
[set]: represents the class which is
the union of all characters in set.
A range of characters can be specified
by separating the end characters of the
range, in ascending order, with a '-'.
All classes %x described above can
also be used as components in set.
All other characters in set represent
themselves. For example, [%w_] (or [_%w])
represents all alphanumeric characters
plus the underscore, [0-7] represents
the octal digits, and [0-7%l%-] represents
the octal digits plus the lowercase letters
plus the '-' character.