-
Notifications
You must be signed in to change notification settings - Fork 5
/
strings.dylan
763 lines (618 loc) · 22.5 KB
/
strings.dylan
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
Module: %strings
// TODO(cgay): Anything related to unicode.
// eXpression IF. (Not that the built-in IF isn't an expression.)
define macro xif
{ xif(?test:expression, ?true:expression, ?false:expression) }
=> { if (?test) ?true else ?false end }
{ xif(?test:expression, ?true:expression) }
=> { if (?test) ?true end }
end;
define constant <string-or-char> = type-union(<string>, <character>);
//// Character-type predicates that apply to characters or strings.
define sealed generic alphabetic?
(string-or-character :: <string-or-char>, #key) => (alphabetic? :: <boolean>);
define method alphabetic?
(char :: <character>, #key) => (b :: <boolean>)
let code = as(<integer>, char);
uppercase-code?(code) | lowercase-code?(code)
end;
define inline method alphabetic?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(alphabetic?, string, start, epos)
end method alphabetic?;
define sealed generic alphanumeric?
(string-or-character :: <string-or-char>, #key) => (alphanumeric? :: <boolean>);
define method alphanumeric?
(char :: <character>, #key) => (b :: <boolean>)
let code = as(<integer>, char);
uppercase-code?(code) | lowercase-code?(code) | decimal-digit?(char)
end;
define method alphanumeric?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(alphanumeric?, string, start, epos)
end;
define sealed generic graphic?
(string-or-character :: <string-or-char>, #key) => (graphic? :: <boolean>);
define method graphic?
(char :: <character>, #key) => (b :: <boolean>)
let code = as(<integer>, char);
as(<integer>, ' ') <= code & code <= as(<integer>, '~')
end;
define method graphic?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(graphic?, string, start, epos)
end;
define sealed generic printable?
(string-or-character :: <string-or-char>, #key) => (printable? :: <boolean>);
define method printable?
(char :: <character>, #key) => (b :: <boolean>)
graphic?(char) | whitespace?(char)
end;
define method printable?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(printable?, string, start, epos)
end;
define sealed generic control?
(string-or-character :: <string-or-char>, #key) => (control? :: <boolean>);
define method control?
(char :: <character>, #key) => (b :: <boolean>)
~ printable?(char)
end;
define method control?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(control?, string, start, epos)
end;
define sealed generic whitespace?
(string-or-character :: <string-or-char>, #key) => (whitespace? :: <boolean>);
define method whitespace?
(char :: <character>, #key) => (b :: <boolean>)
select (char)
// Space, tab, newline, formfeed, carriage return
' ', '\t', '\n', '\f', '\r' => #t;
otherwise => #f;
end select
end;
define method whitespace?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(whitespace?, string, start, epos)
end;
define sealed generic uppercase?
(char :: <character>) => (uppercase? :: <boolean>);
define inline method uppercase?
(char :: <character>) => (b :: <boolean>)
uppercase-code?(as(<integer>, char))
end;
define sealed generic lowercase?
(char :: <character>) => (lowercase? :: <boolean>);
define inline method lowercase?
(char :: <character>) => (b :: <boolean>)
lowercase-code?(as(<integer>, char))
end;
define sealed generic octal-digit?
(string-or-character :: <string-or-char>, #key) => (octal? :: <boolean>);
define method octal-digit?
(char :: <character>, #key) => (b :: <boolean>)
let code = as(<integer>, char);
as(<integer>, '0') <= code & code <= as(<integer>, '7')
end;
define method octal-digit?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(octal-digit?, string, start, epos)
end;
define sealed generic decimal-digit?
(string-or-character :: <string-or-char>, #key) => (decimal? :: <boolean>);
define method decimal-digit?
(char :: <character>, #key) => (b :: <boolean>)
let code = as(<integer>, char);
as(<integer>, '0') <= code & code <= as(<integer>, '9')
end;
define method decimal-digit?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(decimal-digit?, string, start, epos)
end;
define sealed generic hexadecimal-digit?
(string-or-character :: <string-or-char>, #key) => (hexadecimal? :: <boolean>);
define method hexadecimal-digit?
(char :: <character>, #key) => (b :: <boolean>)
let code = as(<integer>, as-lowercase(char));
(as(<integer>, '0') <= code & code <= as(<integer>, '9')) |
(as(<integer>, 'a') <= code & code <= as(<integer>, 'f'))
end;
define method hexadecimal-digit?
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (b :: <boolean>)
%every?(hexadecimal-digit?, string, start, epos)
end;
//// Character comparisons
// Note that the one-of result type causes the compiler to insert a
// check-type(result, <integer>) in some places. Might want to type
// these as <integer> instead.
define inline function char-compare
(char1 :: <character>, char2 :: <character>)
=> (result :: one-of(-1, 0, 1))
let c1 :: <integer> = as(<integer>, char1);
let c2 :: <integer> = as(<integer>, char2);
xif(c1 == c2, 0, xif(c1 < c2, -1, 1))
end;
define inline function char-compare-ic
(char1 :: <character>, char2 :: <character>) => (result :: one-of(-1, 0, 1))
let c1 :: <integer> = as(<integer>, as-lowercase(char1));
let c2 :: <integer> = as(<integer>, as-lowercase(char2));
xif(c1 == c2, 0, xif(c1 < c2, -1, 1))
end;
define inline function char-equal-ic?
(char1 :: <character>, char2 :: <character>) => (eq? :: <boolean>)
as-lowercase(char1) == as-lowercase(char2)
end;
//// String comparisons
// Compare string1 and string2, returning -1 if string1 < string2, 0
// if string1 = string2, and 1 if string1 > string2.
define sealed generic string-compare
(string1 :: <string>, string2 :: <string>,
#key start1, end1, start2, end2, test)
=> (result :: one-of(-1, 0, 1));
define method string-compare
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size,
test :: <function> = char-compare)
=> (result :: one-of(-1, 0, 1))
%string-compare(string1, string2, start1, end1, start2, end2, test)
end;
define sealed generic %string-compare
(string1 :: <string>, string2 :: <string>,
start1 :: <integer>, end1 :: <integer>,
start2 :: <integer>, end2 :: <integer>,
test :: <function>)
=> (result :: one-of(-1, 0, 1));
// A macro to inline specific comparator functions for the fast path,
// namely char-compare and char-compare-ic.
define macro %string-compare-body
{ %string-compare-body(?test:name,
?string1:expression, ?string2:expression,
?start1:expression, ?end1:expression,
?start2:expression, ?end2:expression)
} => {
iterate loop (i1 :: <integer> = ?start1, i2 :: <integer> = ?start2)
case
i1 = ?end1 =>
xif(i2 = ?end2, 0, -1);
i2 = ?end2 =>
1;
otherwise =>
let result :: <integer> = ?test(?string1[i1], ?string2[i2]);
xif(result == 0,
loop(i1 + 1, i2 + 1),
result);
end case
end iterate
}
end macro %string-compare-body;
define method %string-compare
(string1 :: <string>, string2 :: <string>,
start1 :: <integer>, end1 :: <integer>,
start2 :: <integer>, end2 :: <integer>,
test :: <function>)
=> (result :: one-of(-1, 0, 1))
range-check(string1, string1.size, start1, end1);
range-check(string2, string2.size, start2, end2);
%string-compare-body(test, string1, string2, start1, end1, start2, end2)
end method %string-compare;
define method %string-compare
(string1 :: <byte-string>, string2 :: <byte-string>,
start1 :: <integer>, end1 :: <integer>,
start2 :: <integer>, end2 :: <integer>,
test :: <function>)
=> (result :: one-of(-1, 0, 1))
range-check(string1, string1.size, start1, end1);
range-check(string2, string2.size, start2, end2);
select (test)
char-compare =>
%string-compare-body(char-compare,
string1, string2, start1, end1, start2, end2);
char-compare-ic =>
%string-compare-body(char-compare-ic,
string1, string2, start1, end1, start2, end2);
otherwise =>
%string-compare-body(test, string1, string2, start1, end1, start2, end2);
end select
end method %string-compare;
define sealed generic string-equal?
(string1 :: <string>, string2 :: <string>, #key start1, end1, start2, end2, test)
=> (equal? :: <boolean>);
define method string-equal?
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size,
test :: <function> = char-compare)
=> (equal? :: <boolean>)
(end2 - start2) == (end1 - start1)
& (%string-compare(string1, string2, start1, end1, start2, end2, test) == 0)
end method string-equal?;
// Is string1 < string2?
define sealed generic string-less?
(string1 :: <string>, string2 :: <string>, #key start1, end1, start2, end2, test)
=> (less? :: <boolean>);
define method string-less?
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size,
test :: <function> = char-compare)
=> (less? :: <boolean>)
%string-compare(string1, string2, start1, end1, start2, end2, test) == -1
end method string-less?;
// Is string1 > string2?
define sealed generic string-greater?
(string1 :: <string>, string2 :: <string>, #key start1, end1, start2, end2, test)
=> (greater? :: <boolean>);
define method string-greater?
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size,
test :: <function> = char-compare)
=> (greater? :: <boolean>)
%string-compare(string1, string2, start1, end1, start2, end2, test) == 1
end method string-greater?;
define sealed generic string-equal-ic?
(string1 :: <string>, string2 :: <string>, #key start1, end1, start2, end2)
=> (equal? :: <boolean>);
define method string-equal-ic?
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size)
=> (equal? :: <boolean>)
(end2 - start2) == (end1 - start1)
& (%string-compare(string1, string2, start1, end1, start2, end2, char-compare-ic) == 0)
end method string-equal-ic?;
// Is string1 < string2, ignoring case?
define sealed generic string-less-ic?
(string1 :: <string>, string2 :: <string>, #key start1, end1, start2, end2)
=> (less? :: <boolean>);
define method string-less-ic?
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size)
=> (less? :: <boolean>)
%string-compare(string1, string2, start1, end1, start2, end2, char-compare-ic) == -1
end method string-less-ic?;
// Is string1 > string2, ignoring case?
define sealed generic string-greater-ic?
(string1 :: <string>, string2 :: <string>, #key start1, end1, start2, end2)
=> (greater? :: <boolean>);
define method string-greater-ic?
(string1 :: <string>, string2 :: <string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
start2 :: <integer> = 0, end2 :: <integer> = string2.size)
=> (greater? :: <boolean>)
%string-compare(string1, string2, start1, end1, start2, end2, char-compare-ic) = 1
end method string-greater-ic?;
// Does string start with pattern? (Is pattern a prefix of string?)
define sealed generic starts-with?
(string :: <string>, pattern :: <string>, #key test)
=> (starts-with? :: <boolean>);
define method starts-with?
(string :: <string>, pattern :: <string>, #key test :: <function> = char-compare)
=> (starts-with? :: <boolean>)
let plen :: <integer> = pattern.size;
if (plen <= string.size)
%string-compare(string, pattern, 0, plen, 0, plen, test) = 0
end
end method starts-with?;
// Does string end with pattern? (Is pattern a suffix of string?)
define sealed generic ends-with?
(string :: <string>, pattern :: <string>, #key test)
=> (ends-with? :: <boolean>);
define method ends-with?
(string :: <string>, pattern :: <string>, #key test :: <function> = char-compare)
=> (ends-with? :: <boolean>)
let slen :: <integer> = string.size;
let plen :: <integer> = pattern.size;
if (plen <= slen)
%string-compare(string, pattern, slen - plen, slen, 0, plen, test) = 0
end
end method ends-with?;
//// Case conversions
define sealed generic lowercase
(string-or-character :: <string-or-char>, #key)
=> (new-string-or-character :: <string-or-char>);
define method lowercase
(char :: <character>, #key) => (new-char :: <character>)
as-lowercase(char)
end;
define method lowercase
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (new-string :: <string>)
lowercase!(copy-sequence(string, start: start, end: epos))
end method lowercase;
define sealed generic lowercase!
(string-or-character :: <string-or-char>, #key)
=> (string-or-character :: <string-or-char>);
define method lowercase!
(char :: <character>, #key) => (new-char :: <character>)
as-lowercase(char)
end;
define method lowercase!
(string :: <string>,
#key start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (string :: <string>)
range-check(string, string.size, start, epos);
for (i from start below epos)
string[i] := lowercase(string[i])
end;
string
end method lowercase!;
define sealed generic uppercase
(string-or-character :: <string-or-char>, #key)
=> (new-string-or-character :: <string-or-char>);
define method uppercase
(char :: <character>, #key) => (new-char :: <character>)
as-uppercase(char)
end;
define method uppercase
(string :: <string>,
#key start :: <integer> = 0, end: epos :: <integer> = string.size)
=> (new-string :: <string>)
uppercase!(copy-sequence(string, start: start, end: epos))
end method uppercase;
define sealed generic uppercase!
(string-or-character :: <string-or-char>, #key)
=> (string-or-character :: <string-or-char>);
define method uppercase!
(char :: <character>, #key) => (new-char :: <character>)
as-uppercase(char)
end;
define method uppercase!
(string :: <string>,
#key start :: <integer> = 0, end: epos :: <integer> = string.size)
=> (string :: <string>)
range-check(string, string.size, start, epos);
for (i from start below epos)
string[i] := uppercase(string[i])
end;
string
end method uppercase!;
//// String padding (or alignment)
define sealed generic pad
(string :: <string>, width :: <integer>, #key fill)
=> (new-string :: <string>);
define method pad
(string :: <string>, width :: <integer>, #key fill :: <character> = ' ')
=> (new-string :: <string>)
let slen = string.size;
if (slen >= width)
string
else
let start = floor/(width - slen, 2); // start of "string" in "new"
let new = make(<string>, size: width);
for (i :: <integer> from 0 below start)
new[i] := fill;
end;
for (c in string,
i :: <integer> from start)
new[i] := c;
end;
for (i :: <integer> from start + slen below width)
new[i] := fill;
end;
new
end
end method pad;
// Return a string of size 'width', padded on the right with 'fill' characters.
define sealed generic pad-right
(string :: <string>, width :: <integer>, #key fill)
=> (new-string :: <string>);
define method pad-right
(string :: <string>, width :: <integer>, #key fill :: <character> = ' ')
=> (new-string :: <string>)
let slen :: <integer> = string.size;
if (slen >= width)
string
else
let new = make(<string>, size: width);
for (c in string,
i :: <integer> from 0)
new[i] := c;
end;
for (i :: <integer> from slen below width)
new[i] := fill;
end;
new
end
end method pad-right;
// Return a string of size 'width', padded on the left with 'fill' characters.
define sealed generic pad-left
(string :: <string>, width :: <integer>, #key fill)
=> (new-string :: <string>);
define method pad-left
(string :: <string>, width :: <integer>, #key fill :: <character> = ' ')
=> (new-string :: <string>)
let slen :: <integer> = string.size;
if (slen >= width)
string
else
let new = make(<string>, size: width);
for (i :: <integer> from 0 below width - slen)
new[i] := fill;
end;
for (i :: <integer> from width - slen,
c in string)
new[i] := c;
end;
new
end
end method pad-left;
//// Stripping (or trimming)
define sealed generic strip
(string :: <string>, #key test, start, end: epos) => (new-string :: <string>);
define method strip
(string :: <string>,
#key test :: <function> = whitespace?,
start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (new-string :: <string>)
let untest = complement(test);
let left = find-any(string, untest, start: start, end: epos);
if (~left)
""
else
// If left is not #f then right will also not be #f.
let right :: <integer>
= find-any(string, untest, start: start, end: epos, from-end?: #t);
if (left == 0 & right == (string.size - 1))
string
else
copy-sequence(string, start: left, end: right + 1)
end
end if
end method strip;
define sealed generic strip-left
(string :: <string>, #key test, start, end: epos) => (new-string :: <string>);
define method strip-left
(string :: <string>,
#key test :: <function> = whitespace?,
start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (new-string :: <string>)
let left :: <integer>
= find-any(string, complement(test), start: start, end: epos) | epos;
if (left == 0 & epos == string.size)
string
else
copy-sequence(string, start: left, end: epos)
end
end method strip-left;
define sealed generic strip-right
(string :: <string>, #key test, start, end: epos) => (new-string :: <string>);
define method strip-right
(string :: <string>,
#key test :: <function> = whitespace?,
start :: <integer> = 0,
end: epos :: <integer> = string.size)
=> (new-string :: <string>)
let right = find-any(string, complement(test), from-end?: #t, start: start, end: epos);
case
~right
=> "";
start == 0 & right == (string.size - 1)
=> string;
otherwise
=> copy-sequence(string, start: start, end: right + 1)
end
end method strip-right;
//// Split lines
define function split-lines
(string :: <string>, #key remove-if-empty? :: <boolean>)
=> (lines :: <sequence>)
let epos :: <integer> = string.size;
reverse!(iterate loop (bpos :: <integer> = 0, parts :: <list> = #())
let (sep-start, sep-end)
= iterate find-eol (i = bpos) // Recognize CR alone, CRLF, or LF alone.
if (i < epos)
select (string[i])
'\r' =>
let i2 = i + 1;
let char2 = (i2 < epos) & string[i2];
values(i, (char2 = '\n' & i + 2) | i2);
'\n' =>
values(i, i + 1);
otherwise =>
find-eol(i + 1);
end select
end if
end iterate;
let part = copy-sequence(string, start: bpos, end: sep-start | epos);
case
~sep-start =>
xif(remove-if-empty? & empty?(part),
parts,
pair(part, parts));
sep-end = epos => // trailing EOL
pair(part, parts);
remove-if-empty? & empty?(part) =>
loop(sep-end, parts);
otherwise =>
loop(sep-end, pair(part, parts));
end
end)
end function split-lines;
//// Utilities
define inline function uppercase-code?
(code :: <integer>) => (true? :: <boolean>)
as(<integer>, 'A') <= code & code <= as(<integer>, 'Z')
end;
define inline function lowercase-code?
(code :: <integer>) => (true? :: <boolean>)
as(<integer>, 'a') <= code & code <= as(<integer>, 'z')
end;
define inline function range-check
(sequence :: <sequence>, _size :: <integer>, _start :: <integer>, _end :: <integer>) => ()
when (_start < 0 | _start > _size)
element-range-error(sequence, _start)
end;
when (_end < 0 | _end > _size)
element-range-error(sequence, _end)
end
end;
// Because every? doesn't have start/end parameters.
define inline function %every?
(test :: <function>, string :: <string>, bpos :: <integer>, epos :: <integer>)
iterate loop (pos :: <integer> = bpos)
xif(pos < epos,
xif(test(string[pos]), loop(pos + 1), #f),
#t)
end
end function %every?;
// Find the index of the first char in `string` that is a member of `charset`.
define generic find-any (string :: <string>, test :: <function>,
#key start, end: epos, from-end?)
=> (index :: false-or(<integer>));
define method find-any (string :: <string>, test :: <function>,
#key start: bpos :: <integer> = 0,
end: epos :: <integer> = string.size,
from-end? :: <boolean>)
=> (index :: false-or(<integer>))
block (return)
if (from-end?)
for (i from epos - 1 to bpos by -1)
if (test(string[i]))
return(i);
end;
end;
else
for (i from bpos below epos)
if (test(string[i]))
return(i);
end;
end;
end;
end
end method;