zuzu-rust 0.4.0

Rust implementation of ZuzuScript
Documentation
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
=encoding utf8

=head1 NAME

std/path/zz/functions - Function definitions for ZZPath expressions.

=head1 IMPLEMENTATION SUPPORT

This module is supported by all implementations of ZuzuScript.

=head1 DESCRIPTION

This module extends the base ZPath function table with
ZuzuScript-flavoured path expression helpers.

=head1 EXPORTS

=head2 Constants

=over

=item C<STANDARD_FUNCTION_NAMES>

Type: C<Array>. Names of inherited ZPath functions available to ZZPath.

=item C<STANDARD_FUNCTIONS>

Type: C<Array>. Complete ZZPath function definition table.

=back

=head2 Functions

=over

=item C<< first_arg_node(name, ev, ctx, args) >>

Parameters: function name, evaluator, context, and argument AST nodes.
Returns: C<Node> or C<null>. Resolves the first argument or current
context node.

=item C<< first_number_arg(name, ev, ctx, args) >>, C<< nth_number_arg(name, ev, ctx, args, i) >>

Parameters: function name, evaluator, context, arguments, and optional
index. Returns: C<Number>. Resolves an argument as a number.

=item C<< first_string_arg(name, ev, ctx, args) >>, C<< nth_string_arg(name, ev, ctx, args, i) >>

Parameters: function name, evaluator, context, arguments, and optional
index. Returns: C<String>. Resolves an argument as a string.

=item C<< nth_value_arg(name, ev, ctx, args, i) >>

Parameters: function name, evaluator, context, arguments, and index.
Returns: value. Resolves an argument as a primitive value.

=item C<< nth_array_arg(name, ev, ctx, args, i) >>

Parameters: function name, evaluator, context, arguments, and index.
Returns: C<Array>. Resolves an argument as an array value.

=item C<< number_args(ev, ctx, args) >>

Parameters: evaluator, context, and argument AST nodes. Returns:
C<Array>. Resolves all arguments as numbers.

=item C<< first_arg_value(name, ev, ctx, args) >>

Parameters: function name, evaluator, context, and arguments. Returns:
value. Resolves the first argument as a primitive value.

=item C<< z_function(spelling) >>

Parameters: C<spelling> is a base ZPath function name. Returns:
C<Function>. Returns the inherited function implementation.

=item C<< string_index_of(funk, ev, ast, ctx, args) >>

Parameters: standard ZPath function callback arguments. Returns:
C<Array>. Implements string C<index-of>.

=item C<< string_rindex(funk, ev, ast, ctx, args) >>

Parameters: standard ZPath function callback arguments. Returns:
C<Array>. Implements string reverse-index lookup.

=item C<< defined_function(funk, ev, ast, ctx, args) >>

Parameters: standard ZPath function callback arguments. Returns:
C<Array>. Implements defined-value testing.

=item C<< empty_function(funk, ev, ast, ctx, args) >>

Parameters: standard ZPath function callback arguments. Returns:
C<Array>. Implements empty-value testing.

=back

=head1 COPYRIGHT AND LICENCE

B<< std/path/zz/functions >> is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under
the terms of either the Artistic License 1.0 or the GNU General Public
License version 2.

=cut

from std/path/z/functions import Func, STANDARD_FUNCTIONS as Z_STANDARD_FUNCTIONS;


const STANDARD_FUNCTION_NAMES := [
	"true",
	"false",
	"null",
	"tag",
	"url",
	"local-name",
	"key",
	"value",
	"index",
	"count",
	"is-first",
	"is-last",
	"next",
	"prev",
];

function first_arg_node ( name, ev, ctx, args ) {
	if ( args.length() = 0 ) {
		return ctx.nodeset.get( 0, null );
	}
	if ( args.length() = 1 ) {
		const got := ev.eval_expr( args[0], ev.nested_ctx( ctx ) );
		return got.get( 0, null );
	}
	die `Too many arguments for ${name}()`;
}

function first_number_arg ( name, ev, ctx, args ) {
	const node := first_arg_node( name, ev, ctx, args );
	return ev.to_number(node) ?: 0;
}

function nth_number_arg ( name, ev, ctx, args, i ) {
	die `Not enough arguments for ${name}()` if args.length() <= i;
	const got := ev.eval_expr( args[i], ev.nested_ctx( ctx ) );
	return ev.to_number( got.get( 0, null ) ) ?: 0;
}

function nth_string_arg ( name, ev, ctx, args, i ) {
	die `Not enough arguments for ${name}()` if args.length() <= i;
	const got := ev.eval_expr( args[i], ev.nested_ctx( ctx ) );
	const value := ev.to_string( got.get( 0, null ) );
	return value ≡ null ? "" : "" _ value;
}

function nth_value_arg ( name, ev, ctx, args, i ) {
	die `Not enough arguments for ${name}()` if args.length() <= i;
	const got := ev.eval_expr( args[i], ev.nested_ctx( ctx ) );
	const node := got.get( 0, null );
	return node ≡ null ? null : node.primitive_value();
}

function nth_array_arg ( name, ev, ctx, args, i ) {
	die `Not enough arguments for ${name}()` if args.length() <= i;
	const got := ev.eval_expr( args[i], ev.nested_ctx( ctx ) );
	if ( got.length() = 1 ) {
		const one := got[0].primitive_value();
		return one if one instanceof Array;
	}
	return got.map( fn node → node.primitive_value() );
}

function number_args ( ev, ctx, args ) {
	let nums := [];
	if ( args.length() = 0 ) {
		for ( let node in ctx.nodeset ) {
			const value := ev.to_number(node);
			nums.push(value) if value instanceof Number;
		}
		return nums;
	}
	for ( let arg in args ) {
		const got := ev.eval_expr( arg, ev.nested_ctx( ctx ) );
		for ( let node in got ) {
			const value := ev.to_number(node);
			nums.push(value) if value instanceof Number;
		}
	}
	return nums;
}

function first_string_arg ( name, ev, ctx, args ) {
	const node := first_arg_node( name, ev, ctx, args );
	const value := ev.to_string(node);
	return value ≡ null ? "" : "" _ value;
}

function first_arg_value ( name, ev, ctx, args ) {
	const node := first_arg_node( name, ev, ctx, args );
	return node ≡ null ? null : node.primitive_value();
}

function z_function ( spelling ) {
	const func := Z_STANDARD_FUNCTIONS.first( fn f → f.has_name(spelling) );
	die `std/path/z/functions is missing ${spelling}()` if func ≡ null;
	return func;
}

function string_index_of ( funk, ev, ast, ctx, args ) {
	from std/string import index;
	die "Too many arguments for index-of()" if args.length() > 3;
	if ( args.length() = 2 ) {
		return funk.wrap( index(
			nth_string_arg( "index-of", ev, ctx, args, 0 ),
			nth_string_arg( "index-of", ev, ctx, args, 1 ),
		) );
	}
	return funk.wrap( index(
		nth_string_arg( "index-of", ev, ctx, args, 0 ),
		nth_string_arg( "index-of", ev, ctx, args, 1 ),
		nth_number_arg( "index-of", ev, ctx, args, 2 ),
	) );
}

function string_rindex ( funk, ev, ast, ctx, args ) {
	from std/string import rindex;
	const name := funk.get_spelling;
	die `Too many arguments for ${name}()` if args.length() > 3;
	if ( args.length() = 2 ) {
		return funk.wrap( rindex(
			nth_string_arg( name, ev, ctx, args, 0 ),
			nth_string_arg( name, ev, ctx, args, 1 ),
		) );
	}
	return funk.wrap( rindex(
		nth_string_arg( name, ev, ctx, args, 0 ),
		nth_string_arg( name, ev, ctx, args, 1 ),
		nth_number_arg( name, ev, ctx, args, 2 ),
	) );
}

function defined_function ( funk, ev, ast, ctx, args ) {
	const node := first_arg_node( "defined", ev, ctx, args );
	return funk.wrap( node ≢ null and node.primitive_value() ≢ null );
}

function empty_function ( funk, ev, ast, ctx, args ) {
	const value := first_arg_value( "empty", ev, ctx, args );
	return funk.wrap( true ) if value ≡ null;

	if (
		value instanceof Array or
		value instanceof Bag or
		value instanceof Set or
		value instanceof Dict or
		value instanceof PairList
	) {
		return funk.wrap( value.length() = 0 );
	}

	die `empty() expects a Collection or null, got ${typeof value}`;
}

// Start with a base of functions inherited from ZPath:
const STANDARD_FUNCTIONS := STANDARD_FUNCTION_NAMES.map( fn n → z_function(n) );

// Add equivalents for ZuzuScript word-like unary operators:
STANDARD_FUNCTIONS.push(
	new Func(
		spelling: "abs",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( abs first_number_arg( "abs", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "floor",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( floor first_number_arg( "floor", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "ceil",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( ceil first_number_arg( "ceil", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "round",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( round first_number_arg( "round", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "int",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( int first_number_arg( "int", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "sqrt",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( sqrt first_number_arg( "sqrt", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "uc",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( uc first_string_arg( "uc", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "lc",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( lc first_string_arg( "lc", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "length",
		f: function ( funk, ev, ast, ctx, args ) {
			return funk.wrap( length first_string_arg( "length", ev, ctx, args ) );
		},
	),
	new Func(
		spelling: "not",
		f: function ( funk, ev, ast, ctx, args ) {
			const node := first_arg_node( "not", ev, ctx, args );
			return funk.wrap( not ev.truthy(node) );
		},
	),
	new Func(
		spelling: "typeof",
		f: function ( funk, ev, ast, ctx, args ) {
			const node := first_arg_node( "typeof", ev, ctx, args );
			const value := node ≡ null ? null : node.primitive_value();
			return funk.wrap( typeof value );
		},
	),
	new Func(
		spelling: "defined",
		f: defined_function,
	),
	new Func(
		spelling: "empty",
		f: empty_function,
	),
);

// Functions from std/internals:
STANDARD_FUNCTIONS.push(
	new Func(
		spelling: "string",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/internals import to_String;
			return funk.wrap( to_String( first_arg_value(
				"string",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "number",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/internals import to_Number;
			return funk.wrap( to_Number( first_arg_value(
				"number",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "boolean",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/internals import to_Boolean;
			return funk.wrap( to_Boolean( first_arg_value(
				"boolean",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "regexp",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/internals import to_Regexp;
			return funk.wrap( to_Regexp( first_arg_value(
				"regexp",
				ev,
				ctx,
				args,
			) ) );
		},
	),
);

// Functions from std/math:
STANDARD_FUNCTIONS.push(
	new Func(
		spelling: "sum",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/math import Math;
			return funk.wrap( Math.sum( number_args( ev, ctx, args ) ) );
		},
	),
	new Func(
		spelling: "min",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/math import Math;
			return funk.wrap( Math.min( number_args( ev, ctx, args ) ) );
		},
	),
	new Func(
		spelling: "max",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/math import Math;
			return funk.wrap( Math.max( number_args( ev, ctx, args ) ) );
		},
	),
	new Func(
		spelling: "clamp",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/math import Math;
			die "Too many arguments for clamp()" if args.length() > 3;
			return funk.wrap( Math.clamp(
				nth_number_arg( "clamp", ev, ctx, args, 0 ),
				nth_number_arg( "clamp", ev, ctx, args, 1 ),
				nth_number_arg( "clamp", ev, ctx, args, 2 ),
			) );
		},
	),
);

// Functions from std/string:
STANDARD_FUNCTIONS.push(
	new Func(
		spelling: "substr",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import substr;
			die "Too many arguments for substr()" if args.length() > 3;
			if ( args.length() = 2 ) {
				return funk.wrap( substr(
					nth_string_arg( "substr", ev, ctx, args, 0 ),
					nth_number_arg( "substr", ev, ctx, args, 1 ),
				) );
			}
			return funk.wrap( substr(
				nth_string_arg( "substr", ev, ctx, args, 0 ),
				nth_number_arg( "substr", ev, ctx, args, 1 ),
				nth_number_arg( "substr", ev, ctx, args, 2 ),
			) );
		},
	),
	new Func(
		spelling: "index-of",
		f: string_index_of,
	),
	new Func(
		spelling: "rindex",
		f: string_rindex,
	),
	new Func(
		spelling: "last-index-of",
		f: string_rindex,
	),
	new Func(
		spelling: "contains",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import contains;
			die "Too many arguments for contains()" if args.length() > 2;
			return funk.wrap( contains(
				nth_string_arg( "contains", ev, ctx, args, 0 ),
				nth_string_arg( "contains", ev, ctx, args, 1 ),
			) );
		},
	),
	new Func(
		spelling: "chr",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import chr;
			die "Too many arguments for chr()" if args.length() > 1;
			return funk.wrap( chr( nth_number_arg(
				"chr",
				ev,
				ctx,
				args,
				0,
			) ) );
		},
	),
	new Func(
		spelling: "ord",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import ord;
			die "Too many arguments for ord()" if args.length() > 2;
			if ( args.length() = 1 ) {
				return funk.wrap( ord( nth_string_arg(
					"ord",
					ev,
					ctx,
					args,
					0,
				) ) );
			}
			return funk.wrap( ord(
				nth_string_arg( "ord", ev, ctx, args, 0 ),
				nth_number_arg( "ord", ev, ctx, args, 1 ),
			) );
		},
	),
	new Func(
		spelling: "replace",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import replace;
			die "Too many arguments for replace()" if args.length() > 4;
			if ( args.length() = 3 ) {
				return funk.wrap( replace(
					nth_string_arg( "replace", ev, ctx, args, 0 ),
					nth_value_arg( "replace", ev, ctx, args, 1 ),
					nth_string_arg( "replace", ev, ctx, args, 2 ),
				) );
			}
			return funk.wrap( replace(
				nth_string_arg( "replace", ev, ctx, args, 0 ),
				nth_value_arg( "replace", ev, ctx, args, 1 ),
				nth_string_arg( "replace", ev, ctx, args, 2 ),
				nth_string_arg( "replace", ev, ctx, args, 3 ),
			) );
		},
	),
	new Func(
		spelling: "search",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import search;
			die "Too many arguments for search()" if args.length() > 3;
			if ( args.length() = 2 ) {
				return funk.wrap( search(
					nth_string_arg( "search", ev, ctx, args, 0 ),
					nth_value_arg( "search", ev, ctx, args, 1 ),
				) );
			}
			return funk.wrap( search(
				nth_string_arg( "search", ev, ctx, args, 0 ),
				nth_value_arg( "search", ev, ctx, args, 1 ),
				nth_string_arg( "search", ev, ctx, args, 2 ),
			) );
		},
	),
	new Func(
		spelling: "starts_with",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import starts_with;
			die "Too many arguments for starts_with()" if args.length() > 2;
			return funk.wrap( starts_with(
				nth_string_arg( "starts_with", ev, ctx, args, 0 ),
				nth_string_arg( "starts_with", ev, ctx, args, 1 ),
			) );
		},
	),
	new Func(
		spelling: "ends_with",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import ends_with;
			die "Too many arguments for ends_with()" if args.length() > 2;
			return funk.wrap( ends_with(
				nth_string_arg( "ends_with", ev, ctx, args, 0 ),
				nth_string_arg( "ends_with", ev, ctx, args, 1 ),
			) );
		},
	),
	new Func(
		spelling: "matches",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import matches;
			die "Too many arguments for matches()" if args.length() > 3;
			if ( args.length() = 2 ) {
				return funk.wrap( matches(
					nth_string_arg( "matches", ev, ctx, args, 0 ),
					nth_value_arg( "matches", ev, ctx, args, 1 ),
				) );
			}
			return funk.wrap( matches(
				nth_string_arg( "matches", ev, ctx, args, 0 ),
				nth_value_arg( "matches", ev, ctx, args, 1 ),
				nth_string_arg( "matches", ev, ctx, args, 2 ),
			) );
		},
	),
	new Func(
		spelling: "pattern_to_regexp",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import pattern_to_regexp;
			die "Too many arguments for pattern_to_regexp()"
				if args.length() > 2;
			if ( args.length() = 1 ) {
				return funk.wrap( pattern_to_regexp(
					nth_string_arg( "pattern_to_regexp", ev, ctx, args, 0 ),
				) );
			}
			return funk.wrap( pattern_to_regexp(
				nth_string_arg( "pattern_to_regexp", ev, ctx, args, 0 ),
				nth_value_arg( "pattern_to_regexp", ev, ctx, args, 1 ),
			) );
		},
	),
	new Func(
		spelling: "quotemeta",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import quotemeta;
			die "Too many arguments for quotemeta()" if args.length() > 1;
			return funk.wrap( quotemeta(
				nth_string_arg( "quotemeta", ev, ctx, args, 0 ),
			) );
		},
	),
	new Func(
		spelling: "sprint",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import sprint;
			die "Not enough arguments for sprint()" if args.length() = 0;
			die "Too many arguments for sprint()" if args.length() > 8;

			const fmt := nth_string_arg( "sprint", ev, ctx, args, 0 );
			if ( args.length() = 1 ) {
				return funk.wrap( sprint(fmt) );
			}
			if ( args.length() = 2 ) {
				return funk.wrap( sprint(
					fmt,
					nth_value_arg( "sprint", ev, ctx, args, 1 ),
				) );
			}
			if ( args.length() = 3 ) {
				return funk.wrap( sprint(
					fmt,
					nth_value_arg( "sprint", ev, ctx, args, 1 ),
					nth_value_arg( "sprint", ev, ctx, args, 2 ),
				) );
			}
			if ( args.length() = 4 ) {
				return funk.wrap( sprint(
					fmt,
					nth_value_arg( "sprint", ev, ctx, args, 1 ),
					nth_value_arg( "sprint", ev, ctx, args, 2 ),
					nth_value_arg( "sprint", ev, ctx, args, 3 ),
				) );
			}
			if ( args.length() = 5 ) {
				return funk.wrap( sprint(
					fmt,
					nth_value_arg( "sprint", ev, ctx, args, 1 ),
					nth_value_arg( "sprint", ev, ctx, args, 2 ),
					nth_value_arg( "sprint", ev, ctx, args, 3 ),
					nth_value_arg( "sprint", ev, ctx, args, 4 ),
				) );
			}
			if ( args.length() = 6 ) {
				return funk.wrap( sprint(
					fmt,
					nth_value_arg( "sprint", ev, ctx, args, 1 ),
					nth_value_arg( "sprint", ev, ctx, args, 2 ),
					nth_value_arg( "sprint", ev, ctx, args, 3 ),
					nth_value_arg( "sprint", ev, ctx, args, 4 ),
					nth_value_arg( "sprint", ev, ctx, args, 5 ),
				) );
			}
			if ( args.length() = 7 ) {
				return funk.wrap( sprint(
					fmt,
					nth_value_arg( "sprint", ev, ctx, args, 1 ),
					nth_value_arg( "sprint", ev, ctx, args, 2 ),
					nth_value_arg( "sprint", ev, ctx, args, 3 ),
					nth_value_arg( "sprint", ev, ctx, args, 4 ),
					nth_value_arg( "sprint", ev, ctx, args, 5 ),
					nth_value_arg( "sprint", ev, ctx, args, 6 ),
				) );
			}
			return funk.wrap( sprint(
				fmt,
				nth_value_arg( "sprint", ev, ctx, args, 1 ),
				nth_value_arg( "sprint", ev, ctx, args, 2 ),
				nth_value_arg( "sprint", ev, ctx, args, 3 ),
				nth_value_arg( "sprint", ev, ctx, args, 4 ),
				nth_value_arg( "sprint", ev, ctx, args, 5 ),
				nth_value_arg( "sprint", ev, ctx, args, 6 ),
				nth_value_arg( "sprint", ev, ctx, args, 7 ),
			) );
		},
	),
	new Func(
		spelling: "split",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import split;
			die "Too many arguments for split()" if args.length() > 3;
			if ( args.length() = 2 ) {
				return funk.wrap( split(
					nth_string_arg( "split", ev, ctx, args, 0 ),
					nth_value_arg( "split", ev, ctx, args, 1 ),
				) );
			}
			return funk.wrap( split(
				nth_string_arg( "split", ev, ctx, args, 0 ),
				nth_value_arg( "split", ev, ctx, args, 1 ),
				nth_number_arg( "split", ev, ctx, args, 2 ),
			) );
		},
	),
	new Func(
		spelling: "join",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import join;
			die "Too many arguments for join()" if args.length() > 2;
			return funk.wrap( join(
				nth_string_arg( "join", ev, ctx, args, 0 ),
				nth_array_arg( "join", ev, ctx, args, 1 ),
			) );
		},
	),
	new Func(
		spelling: "trim",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import trim;
			die "Too many arguments for trim()" if args.length() > 1;
			return funk.wrap( trim( first_string_arg(
				"trim",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "pad",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import pad;
			die "Too many arguments for pad()" if args.length() > 4;
			if ( args.length() = 2 ) {
				return funk.wrap( pad(
					nth_string_arg( "pad", ev, ctx, args, 0 ),
					nth_number_arg( "pad", ev, ctx, args, 1 ),
				) );
			}
			if ( args.length() = 3 ) {
				return funk.wrap( pad(
					nth_string_arg( "pad", ev, ctx, args, 0 ),
					nth_number_arg( "pad", ev, ctx, args, 1 ),
					nth_string_arg( "pad", ev, ctx, args, 2 ),
				) );
			}
			return funk.wrap( pad(
				nth_string_arg( "pad", ev, ctx, args, 0 ),
				nth_number_arg( "pad", ev, ctx, args, 1 ),
				nth_string_arg( "pad", ev, ctx, args, 2 ),
				nth_string_arg( "pad", ev, ctx, args, 3 ),
			) );
		},
	),
	new Func(
		spelling: "chomp",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import chomp;
			die "Too many arguments for chomp()" if args.length() > 1;
			return funk.wrap( chomp( first_string_arg(
				"chomp",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "title",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import title;
			die "Too many arguments for title()" if args.length() > 1;
			return funk.wrap( title( first_string_arg(
				"title",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "snake",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import snake;
			die "Too many arguments for snake()" if args.length() > 1;
			return funk.wrap( snake( first_string_arg(
				"snake",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "kebab",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import kebab;
			die "Too many arguments for kebab()" if args.length() > 1;
			return funk.wrap( kebab( first_string_arg(
				"kebab",
				ev,
				ctx,
				args,
			) ) );
		},
	),
	new Func(
		spelling: "camel",
		f: function ( funk, ev, ast, ctx, args ) {
			from std/string import camel;
			die "Too many arguments for camel()" if args.length() > 1;
			return funk.wrap( camel( first_string_arg(
				"camel",
				ev,
				ctx,
				args,
			) ) );
		},
	),
);