-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathbuiltin_function_manager.go
2814 lines (2512 loc) · 91.9 KB
/
builtin_function_manager.go
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
// ================================================================
// Adding a new builtin function:
// * New entry in makeBuiltinFunctionLookupTable
// * Implement the function in mlrval_functions.go
//
// Note: Miller-DSL functions, e.g. sec2gmt, are implemented by Go functions
// with names like BIF_sec2gmt not Sec2GMT. This is an intentional departure
// from Go naming conventions: it makes it easier to mentally pair up
// Miller-DSL functions with their Go implementations. Please preserve this
// naming convention.
// ================================================================
package cst
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/johnkerl/miller/v6/pkg/bifs"
"github.com/johnkerl/miller/v6/pkg/colorizer"
"github.com/johnkerl/miller/v6/pkg/lib"
)
type TFunctionClass string
const (
FUNC_CLASS_ARITHMETIC TFunctionClass = "arithmetic"
FUNC_CLASS_MATH TFunctionClass = "math"
FUNC_CLASS_STATS TFunctionClass = "stats"
FUNC_CLASS_BOOLEAN TFunctionClass = "boolean"
FUNC_CLASS_STRING TFunctionClass = "string"
FUNC_CLASS_HASHING TFunctionClass = "hashing"
FUNC_CLASS_CONVERSION TFunctionClass = "conversion"
FUNC_CLASS_TYPING TFunctionClass = "typing"
FUNC_CLASS_COLLECTIONS TFunctionClass = "collections"
FUNC_CLASS_HOFS TFunctionClass = "higher-order-functions"
FUNC_CLASS_SYSTEM TFunctionClass = "system"
FUNC_CLASS_TIME TFunctionClass = "time"
)
// ================================================================
type BuiltinFunctionInfo struct {
name string
class TFunctionClass
// For source-code storage, these have newlines in them. For any presentation to the user, they must be
// formatted using the JoinHelp() method which joins newlines. This is crucial for rendering of
// help-strings for manual page, webdocs, etc wherein we must let the user's resizing of the terminal
// window or browser determine -- at their choosing -- where lines wrap.
help string
examples []string
hasMultipleArities bool
minimumVariadicArity int
maximumVariadicArity int // 0 means no max
zaryFunc bifs.ZaryFunc
unaryFunc bifs.UnaryFunc
binaryFunc bifs.BinaryFunc
ternaryFunc bifs.TernaryFunc
variadicFunc bifs.VariadicFunc
unaryFuncWithContext bifs.UnaryFuncWithContext // asserting_{typename}
regexCaptureBinaryFunc bifs.RegexCaptureBinaryFunc // =~ and !=~
binaryFuncWithState BinaryFuncWithState // select, apply, reduce
ternaryFuncWithState TernaryFuncWithState // fold
variadicFuncWithState VariadicFuncWithState // sort
}
// ================================================================
func isLetter(c byte) bool {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
}
func startsWithLetter(s string) bool {
if len(s) < 1 {
return false
} else {
return isLetter(s[0])
}
}
func makeBuiltinFunctionLookupTable() []BuiltinFunctionInfo {
lookupTable := []BuiltinFunctionInfo{
// ----------------------------------------------------------------
// FUNC_CLASS_ARITHMETIC
{
name: "+",
class: FUNC_CLASS_ARITHMETIC,
help: `Addition as binary operator; unary plus operator.`,
unaryFunc: bifs.BIF_plus_unary,
binaryFunc: bifs.BIF_plus_binary,
hasMultipleArities: true,
},
{
name: "-",
class: FUNC_CLASS_ARITHMETIC,
help: `Subtraction as binary operator; unary negation operator.`,
unaryFunc: bifs.BIF_minus_unary,
binaryFunc: bifs.BIF_minus_binary,
hasMultipleArities: true,
},
{
name: "*",
class: FUNC_CLASS_ARITHMETIC,
help: `Multiplication, with integer*integer overflow to float.`,
binaryFunc: bifs.BIF_times,
},
{
name: "/",
class: FUNC_CLASS_ARITHMETIC,
help: `Division. Integer / integer is integer when exact, else floating-point: e.g. 6/3 is 2 but 6/4 is 1.5.`,
binaryFunc: bifs.BIF_divide,
},
{
name: "//",
class: FUNC_CLASS_ARITHMETIC,
help: `Pythonic integer division, rounding toward negative.`,
binaryFunc: bifs.BIF_int_divide,
},
{
name: "**",
class: FUNC_CLASS_ARITHMETIC,
help: `Exponentiation. Same as pow, but as an infix operator.`,
binaryFunc: bifs.BIF_pow,
},
{
name: "pow",
class: FUNC_CLASS_ARITHMETIC,
help: `Exponentiation. Same as **, but as a function.`,
binaryFunc: bifs.BIF_pow,
},
{
name: ".+",
class: FUNC_CLASS_ARITHMETIC,
help: `Addition, with integer-to-integer overflow.`,
binaryFunc: bifs.BIF_dot_plus,
},
{
name: ".-",
class: FUNC_CLASS_ARITHMETIC,
help: `Subtraction, with integer-to-integer overflow.`,
binaryFunc: bifs.BIF_dot_minus,
},
{
name: ".*",
class: FUNC_CLASS_ARITHMETIC,
help: `Multiplication, with integer-to-integer overflow.`,
binaryFunc: bifs.BIF_dot_times,
},
{
name: "./",
class: FUNC_CLASS_ARITHMETIC,
help: `Integer division, rounding toward zero.`,
binaryFunc: bifs.BIF_dot_divide,
},
{
name: "%",
class: FUNC_CLASS_ARITHMETIC,
help: `Remainder; never negative-valued (pythonic).`,
binaryFunc: bifs.BIF_modulus,
},
{
name: "~",
class: FUNC_CLASS_ARITHMETIC,
help: `Bitwise NOT. Beware '$y=~$x' since =~ is the regex-match operator: try '$y = ~$x'.`,
unaryFunc: bifs.BIF_bitwise_not,
},
{
name: "&",
class: FUNC_CLASS_ARITHMETIC,
help: `Bitwise AND.`,
binaryFunc: bifs.BIF_bitwise_and,
},
{
name: "|",
class: FUNC_CLASS_ARITHMETIC,
help: `Bitwise OR.`,
binaryFunc: bifs.BIF_bitwise_or,
},
{
name: "^",
help: `Bitwise XOR.`,
class: FUNC_CLASS_ARITHMETIC,
binaryFunc: bifs.BIF_bitwise_xor,
},
{
name: "<<",
class: FUNC_CLASS_ARITHMETIC,
help: `Bitwise left-shift.`,
binaryFunc: bifs.BIF_left_shift,
},
{
name: ">>",
class: FUNC_CLASS_ARITHMETIC,
help: `Bitwise signed right-shift.`,
binaryFunc: bifs.BIF_signed_right_shift,
},
{
name: ">>>",
class: FUNC_CLASS_ARITHMETIC,
help: `Bitwise unsigned right-shift.`,
binaryFunc: bifs.BIF_unsigned_right_shift,
},
{
name: "bitcount",
class: FUNC_CLASS_ARITHMETIC,
help: "Count of 1-bits.",
unaryFunc: bifs.BIF_bitcount,
},
{
name: "madd",
class: FUNC_CLASS_ARITHMETIC,
help: `a + b mod m (integers)`,
ternaryFunc: bifs.BIF_mod_add,
},
{
name: "msub",
class: FUNC_CLASS_ARITHMETIC,
help: `a - b mod m (integers)`,
ternaryFunc: bifs.BIF_mod_sub,
},
{
name: "mmul",
class: FUNC_CLASS_ARITHMETIC,
help: `a * b mod m (integers)`,
ternaryFunc: bifs.BIF_mod_mul,
},
{
name: "mexp",
class: FUNC_CLASS_ARITHMETIC,
help: `a ** b mod m (integers)`,
ternaryFunc: bifs.BIF_mod_exp,
},
// ----------------------------------------------------------------
// FUNC_CLASS_BOOLEAN
{
name: "!",
class: FUNC_CLASS_BOOLEAN,
help: `Logical negation.`,
unaryFunc: bifs.BIF_logical_NOT,
},
{
name: "==",
class: FUNC_CLASS_BOOLEAN,
help: `String/numeric equality. Mixing number and string results in string compare.`,
binaryFunc: bifs.BIF_equals,
},
{
name: "!=",
class: FUNC_CLASS_BOOLEAN,
help: `String/numeric inequality. Mixing number and string results in string compare.`,
binaryFunc: bifs.BIF_not_equals,
},
{
name: ">",
help: `String/numeric greater-than. Mixing number and string results in string compare.`,
class: FUNC_CLASS_BOOLEAN,
binaryFunc: bifs.BIF_greater_than,
},
{
name: ">=",
help: `String/numeric greater-than-or-equals. Mixing number and string results in string compare.`,
class: FUNC_CLASS_BOOLEAN,
binaryFunc: bifs.BIF_greater_than_or_equals,
},
{
name: "<=>",
help: `Comparator, nominally for sorting. Given a <=> b, returns <0, 0, >0 as a < b, a == b, or a > b, respectively.`,
class: FUNC_CLASS_BOOLEAN,
binaryFunc: bifs.BIF_cmp,
},
{
name: "<",
class: FUNC_CLASS_BOOLEAN,
help: `String/numeric less-than. Mixing number and string results in string compare.`,
binaryFunc: bifs.BIF_less_than,
},
{
name: "<=",
class: FUNC_CLASS_BOOLEAN,
help: `String/numeric less-than-or-equals. Mixing number and string results in string compare.`,
binaryFunc: bifs.BIF_less_than_or_equals,
},
{
name: "=~",
class: FUNC_CLASS_BOOLEAN,
help: `String (left-hand side) matches regex (right-hand side), e.g.
'$name =~ "^a.*b$"'.
Capture groups \1 through \9 are matched from (...) in the right-hand side, and can be
used within subsequent DSL statements. See also "Regular expressions" at ` + lib.DOC_URL + `.`,
examples: []string{
`With if-statement: if ($url =~ "http.*com") { ... }`,
`Without if-statement: given $line = "index ab09 file", and $line =~ "([a-z][a-z])([0-9][0-9])", then $label = "[\1:\2]", $label is "[ab:09]"`,
},
regexCaptureBinaryFunc: bifs.BIF_string_matches_regexp,
},
{
name: "!=~",
class: FUNC_CLASS_BOOLEAN,
help: `String (left-hand side) does not match regex (right-hand side), e.g. '$name !=~ "^a.*b$"'.`,
regexCaptureBinaryFunc: bifs.BIF_string_does_not_match_regexp,
},
{
name: "strmatch",
class: FUNC_CLASS_STRING,
help: `Boolean yes/no for whether the stringable first argument matches the regular-expression second argument. No regex captures are provided; please see ` + "`strmatch`.",
examples: []string{
`strmatch("a", "abc") is false`,
`strmatch("abc", "a") is true`,
`strmatch("abc", "a[a-z]c") is true`,
`strmatch("abc", "(a).(c)") is true`,
`strmatch(12345, "34") is true`,
},
binaryFunc: bifs.BIF_strmatch,
},
{
name: "strmatchx",
class: FUNC_CLASS_STRING,
help: `Extended information for whether the stringable first argument matches the regular-expression second argument. Regex captures are provided in the return-value map; \1, \2, etc. are not set, in contrast to the ` + "`=~` operator. As well, while the `=~` operator limits matches to \\1 through \\9, an arbitrary number are supported here.",
examples: []string{
`strmatchx("a", "abc") returns:`,
` {`,
` "matched": false`,
` }`,
`strmatchx("abc", "a") returns:`,
` {`,
` "matched": true,`,
` "full_capture": "a",`,
` "full_start": 1,`,
` "full_end": 1`,
` }`,
`strmatchx("[zy:3458]", "([a-z]+):([0-9]+)") returns:`,
` {`,
` "matched": true,`,
` "full_capture": "zy:3458",`,
` "full_start": 2,`,
` "full_end": 8,`,
` "captures": ["zy", "3458"],`,
` "starts": [2, 5],`,
` "ends": [3, 8]`,
` }`,
},
binaryFunc: bifs.BIF_strmatchx,
},
{
name: "&&",
class: FUNC_CLASS_BOOLEAN,
help: `Logical AND.`,
binaryFunc: BinaryShortCircuitPlaceholder,
},
{
name: "||",
class: FUNC_CLASS_BOOLEAN,
help: `Logical OR.`,
binaryFunc: BinaryShortCircuitPlaceholder,
},
{
name: "^^",
class: FUNC_CLASS_BOOLEAN,
help: `Logical XOR.`,
binaryFunc: bifs.BIF_logical_XOR,
},
{
name: "??",
class: FUNC_CLASS_BOOLEAN,
help: `Absent-coalesce operator. $a ?? 1 evaluates to 1 if $a isn't defined in the current record.`,
binaryFunc: BinaryShortCircuitPlaceholder,
},
{
name: "???",
class: FUNC_CLASS_BOOLEAN,
help: `Absent/empty-coalesce operator. $a ??? 1 evaluates to 1 if $a isn't defined in the current record, or has empty value.`,
binaryFunc: BinaryShortCircuitPlaceholder,
},
{
name: "?:",
class: FUNC_CLASS_BOOLEAN,
help: `Standard ternary operator.`,
ternaryFunc: TernaryShortCircuitPlaceholder,
},
// ----------------------------------------------------------------
// FUNC_CLASS_STRING
{
name: ".",
class: FUNC_CLASS_STRING,
help: `String concatenation. Non-strings are coerced, so you can do '"ax".98' etc.`,
binaryFunc: bifs.BIF_dot,
},
{
name: "capitalize",
class: FUNC_CLASS_STRING,
help: "Convert string's first character to uppercase.",
unaryFunc: bifs.BIF_capitalize,
},
{
name: "clean_whitespace",
class: FUNC_CLASS_STRING,
help: "Same as collapse_whitespace and strip, followed by type inference.",
unaryFunc: bifs.BIF_clean_whitespace,
},
{
name: "collapse_whitespace",
class: FUNC_CLASS_STRING,
help: "Strip repeated whitespace from string.",
unaryFunc: bifs.BIF_collapse_whitespace,
},
{
name: "lstrip",
class: FUNC_CLASS_STRING,
help: "Strip leading whitespace from string.",
unaryFunc: bifs.BIF_lstrip,
},
{
name: "regextract",
class: FUNC_CLASS_STRING,
help: `Extracts a substring (the first, if there are multiple matches), matching a
regular expression, from the input. Does not use capture groups; see also the =~ operator which does.`,
binaryFunc: bifs.BIF_regextract,
examples: []string{
`regextract("index ab09 file", "[a-z][a-z][0-9][0-9]") gives "ab09"`,
`regextract("index a999 file", "[a-z][a-z][0-9][0-9]") gives (absent), which will result in an assignment not happening.`,
},
},
{
name: "regextract_or_else",
class: FUNC_CLASS_STRING,
help: `Like regextract but the third argument is the return value in case the input string (first
argument) doesn't match the pattern (second argument).`,
ternaryFunc: bifs.BIF_regextract_or_else,
examples: []string{
`regextract_or_else("index ab09 file", "[a-z][a-z][0-9][0-9]", "nonesuch") gives "ab09"`,
`regextract_or_else("index a999 file", "[a-z][a-z][0-9][0-9]", "nonesuch") gives "nonesuch"`,
},
},
{
name: "rstrip",
class: FUNC_CLASS_STRING,
help: "Strip trailing whitespace from string.",
unaryFunc: bifs.BIF_rstrip,
},
{
name: "strip",
class: FUNC_CLASS_STRING,
help: "Strip leading and trailing whitespace from string.",
unaryFunc: bifs.BIF_strip,
},
{
name: "strlen",
class: FUNC_CLASS_STRING,
help: "String length.",
unaryFunc: bifs.BIF_strlen,
},
{
name: "ssub",
class: FUNC_CLASS_STRING,
help: `Like sub but does no regexing. No characters are special.`,
ternaryFunc: bifs.BIF_ssub,
examples: []string{
`ssub("abc.def", ".", "X") gives "abcXdef"`,
},
},
{
name: "gssub",
class: FUNC_CLASS_STRING,
help: `Like gsub but does no regexing. No characters are special.`,
ternaryFunc: bifs.BIF_gssub,
examples: []string{
`gssub("ab.d.fg", ".", "X") gives "abXdXfg"`,
},
},
{
name: "sub",
class: FUNC_CLASS_STRING,
help: `'$name = sub($name, "old", "new")': replace once (first match, if there are multiple matches),
with support for regular expressions. Capture groups \1 through \9 in the new part are matched from (...) in
the old part, and must be used within the same call to sub -- they don't persist for subsequent DSL
statements. See also =~ and regextract. See also "Regular expressions" at ` + lib.DOC_URL + `.`,
ternaryFunc: bifs.BIF_sub,
examples: []string{
`sub("ababab", "ab", "XY") gives "XYabab"`,
`sub("abc.def", ".", "X") gives "Xbc.def"`,
`sub("abc.def", "\.", "X") gives "abcXdef"`,
`sub("abcdefg", "[ce]", "X") gives "abXdefg"`,
`sub("prefix4529:suffix8567", "suffix([0-9]+)", "name\1") gives "prefix4529:name8567"`,
},
},
{
name: "gsub",
class: FUNC_CLASS_STRING,
help: `'$name = gsub($name, "old", "new")': replace all, with support for regular expressions.
Capture groups \1 through \9 in the new part are matched from (...) in the old part, and must be
used within the same call to gsub -- they don't persist for subsequent DSL statements. See also
=~ and regextract. See also "Regular expressions" at ` + lib.DOC_URL + `.`,
ternaryFunc: bifs.BIF_gsub,
examples: []string{
`gsub("ababab", "ab", "XY") gives "XYXYXY"`,
`gsub("abc.def", ".", "X") gives "XXXXXXX"`,
`gsub("abc.def", "\.", "X") gives "abcXdef"`,
`gsub("abcdefg", "[ce]", "X") gives "abXdXfg"`,
`gsub("prefix4529:suffix8567", "(....ix)([0-9]+)", "[\1 : \2]") gives "[prefix : 4529]:[suffix : 8567]"`,
},
},
{
name: "substr0",
class: FUNC_CLASS_STRING,
help: `substr0(s,m,n) gives substring of s from 0-up position m to n inclusive.
Negative indices -len .. -1 alias to 0 .. len-1. See also substr and substr1.`,
ternaryFunc: bifs.BIF_substr_0_up,
},
{
name: "substr1",
class: FUNC_CLASS_STRING,
help: `substr1(s,m,n) gives substring of s from 1-up position m to n inclusive.
Negative indices -len .. -1 alias to 1 .. len. See also substr and substr0.`,
ternaryFunc: bifs.BIF_substr_1_up,
},
{
name: "substr",
class: FUNC_CLASS_STRING,
help: `substr is an alias for substr0. See also substr1. Miller is generally 1-up with all
array and string indices, but, this is a backward-compatibility issue with Miller 5 and below.
Arrays are new in Miller 6; the substr function is older.`,
ternaryFunc: bifs.BIF_substr_0_up,
},
{
name: "index",
class: FUNC_CLASS_STRING,
help: `Returns the index (1-based) of the second argument within the first. Returns -1 if the second argument isn't a substring of the first. Stringifies non-string inputs. Uses UTF-8 encoding to count characters, not bytes.`,
binaryFunc: bifs.BIF_index,
examples: []string{
`index("abcde", "e") gives 5`,
`index("abcde", "x") gives -1`,
`index(12345, 34) gives 3`,
`index("forêt", "t") gives 5`,
},
},
{
name: "contains",
class: FUNC_CLASS_STRING,
help: `Returns true if the first argument contains the second as a substring. This is like saying ` + "`index(arg1, arg2) >= 0`" + `but with less keystroking.`,
binaryFunc: bifs.BIF_contains,
examples: []string{
`contains("abcde", "e") gives true`,
`contains("abcde", "x") gives false`,
`contains(12345, 34) gives true`,
`contains("forêt", "ê") gives true`,
},
},
{
name: "tolower",
class: FUNC_CLASS_STRING,
help: "Convert string to lowercase.",
unaryFunc: bifs.BIF_tolower,
},
{
name: "toupper",
class: FUNC_CLASS_STRING,
help: "Convert string to uppercase.",
unaryFunc: bifs.BIF_toupper,
},
{
name: "truncate",
class: FUNC_CLASS_STRING,
help: `Truncates string first argument to max length of int second argument.`,
binaryFunc: bifs.BIF_truncate,
},
{
name: "leftpad",
class: FUNC_CLASS_STRING,
help: `Left-pads first argument to at most the specified length (second, integer argument) using specified pad value (third, string argument). If the first argument is not a string, it will be stringified first.`,
ternaryFunc: bifs.BIF_leftpad,
examples: []string{
`leftpad("abcdefg", 10 , "*") gives "***abcdefg".`,
`leftpad("abcdefg", 10 , "XY") gives "XYabcdefg".`,
`leftpad("1234567", 10 , "0") gives "0001234567".`,
},
},
{
name: "rightpad",
class: FUNC_CLASS_STRING,
help: `Right-pads first argument to at most the specified length (second, integer argument) using specified pad value (third, string argument). If the first argument is not a string, it will be stringified first.`,
ternaryFunc: bifs.BIF_rightpad,
examples: []string{
`rightpad("abcdefg", 10 , "*") gives "abcdefg***".`,
`rightpad("abcdefg", 10 , "XY") gives "abcdefgXY".`,
`rightpad("1234567", 10 , "0") gives "1234567000".`,
},
},
{
name: "format",
class: FUNC_CLASS_STRING,
help: `Using first argument as format string, interpolate remaining arguments in place of
each "{}" in the format string. Too-few arguments are treated as the empty string; too-many arguments are discarded.`,
examples: []string{
`format("{}:{}:{}", 1,2) gives "1:2:".`,
`format("{}:{}:{}", 1,2,3) gives "1:2:3".`,
`format("{}:{}:{}", 1,2,3,4) gives "1:2:3".`,
},
variadicFunc: bifs.BIF_format,
},
{
name: "unformat",
class: FUNC_CLASS_STRING,
help: `Using first argument as format string, unpacks second argument into an array of matches,
with type-inference. On non-match, returns error -- use is_error() to check.`,
examples: []string{
`unformat("{}:{}:{}", "1:2:3") gives [1, 2, 3].`,
`unformat("{}h{}m{}s", "3h47m22s") gives [3, 47, 22].`,
`is_error(unformat("{}h{}m{}s", "3:47:22")) gives true.`,
},
binaryFunc: bifs.BIF_unformat,
},
{
name: "unformatx",
class: FUNC_CLASS_STRING,
help: `Same as unformat, but without type-inference.`,
examples: []string{
`unformatx("{}:{}:{}", "1:2:3") gives ["1", "2", "3"].`,
`unformatx("{}h{}m{}s", "3h47m22s") gives ["3", "47", "22"].`,
`is_error(unformatx("{}h{}m{}s", "3:47:22")) gives true.`,
},
binaryFunc: bifs.BIF_unformatx,
},
{
name: "latin1_to_utf8",
class: FUNC_CLASS_STRING,
help: `Tries to convert Latin-1-encoded string to UTF-8-encoded string.
If argument is array or map, recurses into it.`,
examples: []string{
`$y = latin1_to_utf8($x)`,
`$* = latin1_to_utf8($*)`,
},
unaryFunc: bifs.BIF_latin1_to_utf8,
},
{
name: "utf8_to_latin1",
class: FUNC_CLASS_STRING,
help: `Tries to convert UTF-8-encoded string to Latin-1-encoded string.
If argument is array or map, recurses into it.`,
examples: []string{
`$y = utf8_to_latin1($x)`,
`$* = utf8_to_latin1($*)`,
},
unaryFunc: bifs.BIF_utf8_to_latin1,
},
// ----------------------------------------------------------------
// FUNC_CLASS_HASHING
{
name: "md5",
class: FUNC_CLASS_HASHING,
help: `MD5 hash.`,
unaryFunc: bifs.BIF_md5,
},
{
name: "sha1",
class: FUNC_CLASS_HASHING,
help: `SHA1 hash.`,
unaryFunc: bifs.BIF_sha1,
},
{
name: "sha256",
class: FUNC_CLASS_HASHING,
help: `SHA256 hash.`,
unaryFunc: bifs.BIF_sha256,
},
{
name: "sha512",
class: FUNC_CLASS_HASHING,
help: `SHA512 hash.`,
unaryFunc: bifs.BIF_sha512,
},
// ----------------------------------------------------------------
// FUNC_CLASS_MATH
{
name: "abs",
class: FUNC_CLASS_MATH,
help: "Absolute value.",
unaryFunc: bifs.BIF_abs,
},
{
name: "acos",
class: FUNC_CLASS_MATH,
help: "Inverse trigonometric cosine.",
unaryFunc: bifs.BIF_acos,
},
{
name: "acosh",
class: FUNC_CLASS_MATH,
help: "Inverse hyperbolic cosine.",
unaryFunc: bifs.BIF_acosh,
},
{
name: "asin",
class: FUNC_CLASS_MATH,
help: "Inverse trigonometric sine.",
unaryFunc: bifs.BIF_asin,
},
{
name: "asinh",
class: FUNC_CLASS_MATH,
help: "Inverse hyperbolic sine.",
unaryFunc: bifs.BIF_asinh,
},
{
name: "atan",
class: FUNC_CLASS_MATH,
help: "One-argument arctangent.",
unaryFunc: bifs.BIF_atan,
},
{
name: "atan2",
class: FUNC_CLASS_MATH,
help: "Two-argument arctangent.",
binaryFunc: bifs.BIF_atan2,
},
{
name: "atanh",
class: FUNC_CLASS_MATH,
help: "Inverse hyperbolic tangent.",
unaryFunc: bifs.BIF_atanh,
},
{
name: "cbrt",
class: FUNC_CLASS_MATH,
help: "Cube root.",
unaryFunc: bifs.BIF_cbrt,
},
{
name: "ceil",
class: FUNC_CLASS_MATH,
help: "Ceiling: nearest integer at or above.",
unaryFunc: bifs.BIF_ceil,
},
{
name: "cos",
class: FUNC_CLASS_MATH,
help: "Trigonometric cosine.",
unaryFunc: bifs.BIF_cos,
},
{
name: "cosh",
class: FUNC_CLASS_MATH,
help: "Hyperbolic cosine.",
unaryFunc: bifs.BIF_cosh,
},
{
name: "erf",
class: FUNC_CLASS_MATH,
help: "Error function.",
unaryFunc: bifs.BIF_erf,
},
{
name: "erfc",
class: FUNC_CLASS_MATH,
help: "Complementary error function.",
unaryFunc: bifs.BIF_erfc,
},
{
name: "exp",
class: FUNC_CLASS_MATH,
help: "Exponential function e**x.",
unaryFunc: bifs.BIF_exp,
},
{
name: "expm1",
class: FUNC_CLASS_MATH,
help: "e**x - 1.",
unaryFunc: bifs.BIF_expm1,
},
{
name: "floor",
class: FUNC_CLASS_MATH,
help: "Floor: nearest integer at or below.",
unaryFunc: bifs.BIF_floor,
},
{
name: "invqnorm",
class: FUNC_CLASS_MATH,
help: `Inverse of normal cumulative distribution function. Note that invqorm(urand())
is normally distributed.`,
unaryFunc: bifs.BIF_invqnorm,
},
{
name: "log",
class: FUNC_CLASS_MATH,
help: "Natural (base-e) logarithm.",
unaryFunc: bifs.BIF_log,
},
{
name: "log10",
class: FUNC_CLASS_MATH,
help: "Base-10 logarithm.",
unaryFunc: bifs.BIF_log10,
},
{
name: "log1p",
class: FUNC_CLASS_MATH,
help: "log(1-x).",
unaryFunc: bifs.BIF_log1p,
},
{
name: "logifit",
class: FUNC_CLASS_MATH,
help: `Given m and b from logistic regression, compute fit: $yhat=logifit($x,$m,$b).`,
ternaryFunc: bifs.BIF_logifit,
},
{
name: "max",
class: FUNC_CLASS_MATH,
help: `Max of n numbers; null loses. The min and max functions also recurse into arrays and maps, so they can be used to get min/max stats on array/map values.`,
variadicFunc: bifs.BIF_max_variadic,
},
{
name: "min",
class: FUNC_CLASS_MATH,
help: `Min of n numbers; null loses. The min and max functions also recurse into arrays and maps, so they can be used to get min/max stats on array/map values.`,
variadicFunc: bifs.BIF_min_variadic,
},
{
name: "qnorm",
class: FUNC_CLASS_MATH,
help: `Normal cumulative distribution function.`,
unaryFunc: bifs.BIF_qnorm,
},
{
name: "round",
class: FUNC_CLASS_MATH,
help: "Round to nearest integer.",
unaryFunc: bifs.BIF_round,
},
{
name: "sgn",
class: FUNC_CLASS_MATH,
help: `+1, 0, -1 for positive, zero, negative input respectively.`,
unaryFunc: bifs.BIF_sgn,
},
{
name: "sin",
class: FUNC_CLASS_MATH,
help: "Trigonometric sine.",
unaryFunc: bifs.BIF_sin,
},
{
name: "sinh",
class: FUNC_CLASS_MATH,
help: "Hyperbolic sine.",
unaryFunc: bifs.BIF_sinh,
},
{
name: "sqrt",
class: FUNC_CLASS_MATH,
help: "Square root.",
unaryFunc: bifs.BIF_sqrt,
},
{
name: "tan",
class: FUNC_CLASS_MATH,
help: "Trigonometric tangent.",
unaryFunc: bifs.BIF_tan,
},
{
name: "tanh",
class: FUNC_CLASS_MATH,
help: "Hyperbolic tangent.",
unaryFunc: bifs.BIF_tanh,
},
{
name: "roundm",
class: FUNC_CLASS_MATH,
help: `Round to nearest multiple of m: roundm($x,$m) is the same as round($x/$m)*$m.`,
binaryFunc: bifs.BIF_roundm,
},
{
name: "urand",
class: FUNC_CLASS_MATH,
help: `Floating-point numbers uniformly distributed on the unit interval.`,
examples: []string{
"Int-valued example: '$n=floor(20+urand()*11)'.",
},
zaryFunc: bifs.BIF_urand,
},
{
name: "urandint",
class: FUNC_CLASS_MATH,
help: `Integer uniformly distributed between inclusive integer endpoints.`,
binaryFunc: bifs.BIF_urandint,
},
{
name: "urandrange",
class: FUNC_CLASS_MATH,
help: `Floating-point numbers uniformly distributed on the interval [a, b).`,