-
Notifications
You must be signed in to change notification settings - Fork 2
/
srfi-152.html
2136 lines (1875 loc) · 80.1 KB
/
srfi-152.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html public '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/REC-html4/strict.dtd'>
<!-- HTML skeleton (including style hackery) copied from srfi-130.html -->
<!-- Can I have bangs, plusses, or slashes in #tags? Spaces?
Yes: plus, bang, star No: space Yes: slash, question, ampersand
You can't put sharp in a path, so anything goes, really.
Nonetheless, some of these confuse Netscape, so I'll avoid them.
-->
<!--========================================================================-->
<html>
<head>
<meta name="keywords" content="Scheme, programming language, texts, texts, Unicode, SRFI" />
<link rel="author" href="mailto:[email protected]" />
<title>SRFI 152: String Library (reduced)</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/srfi.css" type="text/css" />
<link href="/favicon.png" rel="icon" sizes="192x192" type="image/png" />
<!-- Should have a media=all to get, for example, printing to work.
== But my Netscape will completely ignore the tag if I do that.
-->
<style type="text/css">
/* A little general layout hackery for headers & the title. */
body { margin-left: +7%;
font-family: "Helvetica", sans-serif;
}
/* Netscape workaround: */
td, th { font-family: "Helvetica", sans-serif; }
code, pre { font-family: "courier new", "courier"; }
div.inset { margin-left: +5%; }
h1 { margin-left: -5%; }
h1, h2 { clear: both; }
h1, h2, h3, h4, h5, h6 { color: blue }
div.title-text { font-size: large; font-weight: bold; }
h3 { margin-top: 2em; margin-bottom: 0em }
/* "Continue" class marks text that isn't really the start
** of a new paragraph — e.g., continuing a para after a
** code sample.
*/
p.continue { text-indent: 0em; margin-top: 0em}
div.indent { margin-left: 2em; } /* General indentation */
pre.code-example { margin-left: 2em; } /* Indent code examples. */
/* This stuff is for definition lists of defined procedures.
** A proc-def1 is used when you want a stack of procs to go
** with one dd body. In this case, make the first
** proc a proc-def1, following ones proc-defi's, and the last one
** a proc-defn.
**
** Unfortunately, Netscape has huge bugs with respect to style
** sheets and dl list rendering. We have to set truly random
** values here to get the rendering to come out. The proper values
** are in the following style sheet, for Internet Explorer.
** In the following settings, the *comments* say what the
** setting *really* causes Netscape to do.
**
** Ugh. Professional coders sacrifice their self-respect,
** that others may live.
*/
/* m-t ignored; m-b sets top margin space. */
dt.proc-def1 { margin-top: 0ex; margin-bottom: 3ex; }
dt.proc-defi { margin-top: 0ex; margin-bottom: 0ex; }
dt.proc-defn { margin-top: 0ex; margin-bottom: 0ex; }
/* m-t works weird depending on whether or not the last line
** of the previous entry was a pre. Set to zero.
*/
dt.proc-def { margin-top: 0ex; margin-bottom: 3ex; }
/* m-b sets space between dd & dt; m-t ignored. */
dd.proc-def { margin-bottom: 0.5ex; margin-top: 0ex; }
/* Boldface the name of a procedure when it's being defined. */
code.proc-def { font-weight: bold; font-size: 110%}
/* For the index of procedures.
** Same hackery as for dt.proc-def, above.
*/
/* m-b sets space between dd & dt; m-t ignored. */
dd.proc-index { margin-bottom: 0ex; margin-top: 0ex; }
/* What the fuck? */
pre.proc-index { margin-top: -2ex; }
/* Pull the table of contents back flush with the margin.
** Both NS & IE screw this up in different ways.
*/
#toc-table { margin-top: -2ex; margin-left: -5%; }
/* R5RS proc names are in italic; extended R5RS names
** in italic boldface.
*/
span.r5rs-proc { font-weight: bold; }
span.r5rs-procx { font-style: italic; font-weight: bold; }
/* Spread out bibliographic lists. */
/* More Netscape-specific lossage; see the following stylesheet
** for the proper values (used by IE).
*/
dt.biblio { margin-bottom: 3ex; }
/* Links to draft copies (e.g., not at the official SRFI site)
** are colored in red, so people will use them during the
** development process and kill them when the document's done.
*/
a.draft { color: red; }
</style>
<style type="text/css" media="all">
/* Nastiness: Here, I'm using a bug to work around a bug.
** Netscape rendering bugs mean you need bogus <dt> and <dd>
** margin settings — settings which screw up IE's proper rendering.
** Fortunately, Netscape has *another* bug: it will ignore this
** media=all style sheet. So I am placing the (proper) IE values
** here. Perhaps, one day, when these rendering bugs are fixed,
** this gross hackery can be removed.
*/
dt.proc-def1 { margin-top: 3ex; margin-bottom: 0ex; }
dt.proc-defi { margin-top: 0ex; margin-bottom: 0ex; }
dt.proc-defn { margin-top: 0ex; margin-bottom: 0.5ex; }
dt.proc-def { margin-top: 3ex; margin-bottom: 0.5ex; }
pre { margin-top: 1ex; }
dd.proc-def { margin-bottom: 2ex; margin-top: 0.5ex; }
/* For the index of procedures.
** Same hackery as for dt.proc-def, above.
*/
dd.proc-index { margin-top: 0ex; }
pre.proc-index { margin-top: 0ex; }
/* Spread out bibliographic lists. */
dt.biblio { margin-top: 3ex; margin-bottom: 0ex; }
dd.biblio { margin-bottom: 1ex; }
</style>
<style type="text/css" media="all">
/* Added by Will Clinger so lists don't look so crowded. */
ul li { margin-top: 2pt; margin-bottom: 2pt; }
</style>
</head>
<body>
<!--========================================================================-->
<H1>Title</H1>
<div class="title-string">String Library (reduced)</div>
<!--========================================================================-->
<H1>Author</H1>
John Cowan
<h1>Status</h1>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+152+at+srfi+dotschemers+dot+org">srfi-152@<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="http://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-152">archive</a>.</p>
<ul>
<li>Received: 2017-06-02</li>
<li>Draft #1 published: 2017-06-03</li>
<li>Draft #2 published: 2017-06-09</li>
<li>Draft #3 published: 2017-06-22</li>
<li>Draft #4 published: 2017-06-25</li>
<li>Draft #5 published: 2017-07-17</li>
<li>Draft #6 published: 2017-09-11</li>
<li>Draft #7 published: 2017-09-26</li>
<li>Finalized: 2017-10-04</li>
<li>Revised to fix errata:
<ul>
<li>2020/3/4 (Add note about accumulation of strings in <code>string-unfold-right</code>.)</li>
<li>2024-09-02 (Revised to fix missing argument in <a href="#string-fold">examples</a> of <code>string-fold</code> and <code>string-fold-right</code>.)</li></ul></li>
</ul>
<h1>Table of contents</h1>
<ul id="toc-table">
<li><a href="#Abstract">Abstract</a></li>
<li><a href="#ProcedureIndex">Procedure index</a></li>
<li><a href="#Rationale">Rationale</a></li>
<li><a href="#Specification">Specification</a>
<ul>
<li><a href="#Notation">Notation</a></li>
<li><a href="#Predicates">Predicates</a></li>
<li><a href="#Constructors">Constructors</a></li>
<li><a href="#Conversion">Conversion</a></li>
<li><a href="#Selection">Selection</a></li>
<li><a href="#Replacement">Replacement</a></li>
<li><a href="#Comparison">Comparison</a></li>
<li><a href="#PrefixesSuffixes">Prefixes and suffixes</a></li>
<li><a href="#Searching">Searching</a></li>
<li><a href="#Concatenation">Concatenation</a></li>
<li><a href="#FoldMap">Fold and map and friends</a></li>
<li><a href="#ReplicationSplitting">Replication and splitting </a></li>
<li><a href="#InputOutput">Input-output </a></li>
<li><a href="#Mutation">Mutation </a></li>
</ul>
<li><a href="#SampleImp">Sample implementations</a></li>
<li><a href="#Acknowledgements">Acknowledgements</a></li>
<li><a href="#Copyright">Copyright</a></li>
</ul>
<!--========================================================================-->
<h1><a name="Abstract">Abstract</a></H1>
<p>Scheme has an impoverished set of string-processing utilities, which
is a problem for authors of portable code. This SRFI proposes a coherent
and comprehensive set of string-processing procedures. It is a reduced
version of SRFI 13 that has been aligned with SRFI 135, Immutable Texts.
Unlike SRFI 13, it has been made consistent with the R5RS, R6RS, and
R7RS-small string procedures. </p>
<!--========================================================================-->
<h1><a name="ProcedureIndex">Procedure Index</a></h1>
<p>
Here is a list of the procedures provided by this SRFI:
<div class="indent">
<dl>
<dt class="proc-index"> Predicates</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-p">string?</a>
<a href="#string-null-p">string-null?</a>
<a href="#string-every">string-every</a> <a href="#string-any">string-any</a>
</pre>
</dd>
<dt class="proc-index"> Constructors</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#make-string">make-string</a> <a href="#string">string</a>
<a href="#string-tabulate">string-tabulate</a>
<a href="#string-unfold">string-unfold</a> <a href="#string-unfold-right">string-unfold-right</a>
</pre>
</dd>
<dt class="proc-index"> Conversion</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string2vector">string->vector</a> <a href="#string2list">string->list</a>
<a href="#vector2string">vector->string</a> <a href="#list2string">list->string</a>
<a href="#reverse-list2string">reverse-list->string</a>
</pre>
</dd>
<dt class="proc-index"> Selection</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-length">string-length</a>
<a href="#string-ref">string-ref</a>
<a href="#substring">substring</a>
<a href="#string-copy">string-copy</a>
<a href="#string-take">string-take</a> <a href="#string-take-right">string-take-right</a>
<a href="#string-drop">string-drop</a> <a href="#string-drop-right">string-drop-right</a>
<a href="#string-pad">string-pad</a> <a href="#string-pad-right">string-pad-right</a>
<a href="#string-trim">string-trim</a> <a href="#string-trim-right">string-trim-right</a> <a href="#string-trim-both">string-trim-both</a>
</pre>
</dd>
<dt class="proc-index"> Replacement</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-replace">string-replace</a>
</pre>
</dd>
<dt class="proc-index"> Comparison</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-equal-p">string=?</a> <a href="#string-ci-equal-p">string-ci=?</a>
<a href="#string-less-p">string<?</a> <a href="#string-ci-less-p">string-ci<?</a>
<a href="#string-greater-p">string>?</a> <a href="#string-ci-greater-p">string-ci>?</a>
<a href="#string-leq-p">string<=?</a> <a href="#string-ci-leq-p">string-ci<=?</a>
<a href="#string-geq-p">string>=?</a> <a href="#string-ci-geq-p">string-ci>=?</a>
</pre>
</dd>
<dt class="proc-index">Prefixes and suffixes</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-prefix-length">string-prefix-length</a> <a href="#string-suffix-length">string-suffix-length</a>
<a href="#string-prefix-p">string-prefix?</a> <a href="#string-suffix-p">string-suffix?</a>
</pre>
</dd>
<dt class="proc-index">Searching</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-index">string-index</a> <a href="#string-index-right">string-index-right</a>
<a href="#string-skip">string-skip</a> <a href="#string-skip-right">string-skip-right</a>
<a href="#string-contains">string-contains</a> <a href="#string-contains-right">string-contains-right</a>
<a href="#string-take-while">string-take-while</a> <a href="#string-take-while-right">string-take-while-right</a>
<a href="#string-drop-while">string-drop-while</a> <a href="#string-drop-while-right">string-drop-while-right</a>
<a href="#string-break">string-break</a> <a href="#string-span">string-span</a>
</pre>
</dd>
<dt class="proc-index"> Concatenation</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-append">string-append</a> <a href="#string-concatenate">string-concatenate</a> <a href="#string-concatenate-reverse">string-concatenate-reverse</a>
<a href="#string-join">string-join</a>
</pre>
</dd>
<dt class="proc-index">Fold and map and friends</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-fold">string-fold</a> <a href="#string-fold-right">string-fold-right</a>
<a href="#string-map">string-map</a> <a href="#string-for-each">string-for-each</a>
<!-- <a href="#string-map-index">string-map-index</a> <a href="#string-for-each-index">string-for-each-index</a>
--><a href="#string-count">string-count</a>
<a href="#string-filter">string-filter</a> <a href="#string-remove">string-remove</a>
</pre>
</dd>
<dt class="proc-index">Replication and splitting</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-replicate">string-replicate</a>
<a href="#string-segment">string-segment</a> <a href="#string-split">string-split</a>
</pre>
</dd>
<dt class="proc-index">Input-output</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#read-string">read-string</a> <a href="#write-string">write-string</a>
</pre>
</dd>
<dt class="proc-index">Mutation</dt>
<dd class="proc-index">
<pre class="proc-index">
<a href="#string-set!">string-set!</a> <a href="#string-fill!">string-fill!</a> <a href="#string-copy!">string-copy!</a>
</pre>
</dd>
</dl>
</div>
<!--========================================================================-->
<h1><a name="Rationale">Rationale</a></h1>
<p>
This SRFI is based upon
<a href="https://srfi.schemers.org/srfi-130/srfi-130.html">SRFI 130</a>,
copying much of its structure
and wording, but eliminating the concept of cursors.
However, it is textually derived from
<a href="https://srfi.schemers.org/srfi-135/srfi-135.html">SRFI 135</a>,
in order to gain access to the editorial improvements made to the text of
that SRFI, which was itself based on SRFI 130.
Ultimately the origin of all these SRFIs is
<a href="https://srfi.schemers.org/srfi-13/srfi-13.html">SRFI 13</a>.
</p>
<p>This SRFI omits the following bells, whistles, and gongs of SRFI 13:</p>
<ul><li>the Knuth-Morris-Pratt string search algorithm (still used internally by the sample implementation but not exposed)</li>
<li>case-insensitive operations, other than the ones in R7RS-small</li>
<li>titlecase operations</li>
<li>direct comparison of substrings</li>
<li>mutation procedures, other than the ones in R7RS-small</li>
<li>string reversal (even more pointless in a Unicode age)</li>
<li>characters and SRFI 14 character sets as alternatives to predicates</li>
</ul>
In addition, this SRFI includes
the <code>string-segment</code>
and <code>string-split</code> procedures from other sources.
For completeness, <code>string-take-while</code>, <code>string-drop-while</code>, <code>string-take-while-right</code>, and <code>string-drop-while-right</code> are also provided.
<p>
There are no performance guarantees for any of the procedures in this SRFI.
</p>
<p>
The Scheme programming language does not expose the internal
representation of strings.
Some implementations of Scheme use UTF-32 or a similar encoding,
which makes <code>string-length</code>, <code>string-ref</code>,
and <code>string-set!</code> run in O(1) time.
Some implementations use UTF-16 or UTF-8, which save space
at the expense of making <code>string-ref</code> take
time proportional to the length of a string.
Others allow only 256 characters, typically the Latin-1 repertoire.
</p>
<p>
Although Scheme's string data type allows portable code to use strings
independently of their internal representation, the variation
in performance between implementations has created a problem
for programs that use long strings.
In some systems, long strings are inefficient with respect to space;
in other systems, long strings are inefficient with respect to time.
Consequently, this SRFI suggests that Scheme's mutable
strings be used only for relatively short sequences of
characters, while using the immutable texts defined by
<a href="https://srfi.schemers.org/srfi-135/srfi-135.html">SRFI 135</a>
for long sequences of characters.
</p>
<!--========================================================================-->
<h1><a name="Specification">Specification</a></h1>
<p>
Procedures present in R5RS, R6RS, and R7RS-small are marked (R5RS).
Procedures present in R5RS and R6RS but with additional arguments in R7RS-small are marked (R5RS+).
Procedures present in R6RS and R7RS-small are marked (R6-R7RS).
Procedures present in R6RS only are marked (R6RS).
Procedures present in R7RS-small only are marked (R7RS-small).
</p>
<p>
Except as noted, the results returned from the procedures of this
SRFI <i>must</i> be newly allocated strings.
This is a change from the definition of SRFIs 13 and 130,
though most Schemes do not support sharable strings in any case.
However, the empty string need not be newly allocated.
<p>
The procedures of this SRFI follow
a consistent naming scheme, and are consistent with the conventions
developed in SRFI 1 and used in SRFI 13, SRFI 130, and SRFI 135.
In particular,
procedures that have left/right directional variants
use no suffix to specify left-to-right operation,
<code>-right</code> to specify
right-to-left operation, and <code>-both</code> to specify both.
One discrepancy between SRFI 1 and other SRFIs is in the tabulate
procedure: SRFI 1's <code>list-tabulate</code> takes the length argument
first, before the procedure, whereas all string SRFIs put the procedure
first, in line with mapping and folding operations.
</p>
<p>
The order of common arguments is consistent across the
different procedures.
In particular, all procedures place the main string to be operated on first,
with the exception of the mapping and folding procedures, which are consistent
with R7RS-small and SRFI 1.
</p>
<p>
If a procedure's return value is said to be "unspecified," the
procedure returns a single result whose value is unconstrained
and might even vary from call to call.
</p>
<!--========================================================================-->
<!--========================================================================-->
<h2><a name="Notation">Notation</a></h2>
<p>
In the following procedure specifications:
<ul>
<li>A <var>string</var> argument is a string.</li>
<li>A <var>char</var> argument is a character.</li>
<li>An <var>idx</var> argument is an exact non-negative integer
specifying a valid character index into a string.
The valid character indexes of a string <var>string</var>
of length <var>n</var> are the exact integers <var>idx</var> satisfying
0 <= <var>idx</var> < <var>n</var>.
</li>
<li>A <var>k</var> argument or result is a <em>position</em>:
an exact non-negative
integer that is either a valid character index for one of the
string arguments or is the length of a string argument.
</li>
<li><var>start</var> and <var>end</var> arguments are positions
specifying
a half-open interval of indexes for a substring.
When omitted, <var>start</var> defaults to 0 and <var>end</var>
to the length of the corresponding <var>string</var> argument.
It is an error unless
0 <= <var>start</var> <= <var>end</var>
<= <code>(string-length <var>string</var>)</code>;
the sample implementations detect that error and raise an exception.
</li>
<li>A <var>len</var> or <var>nchars</var> argument is an exact
non-negative integer specifying some number of characters,
usually the length of a string.</li>
<li>A <var>pred</var> argument is a unary character predicate,
taking a character as its one argument and returning a value
that will be interpreted as true or false.
Unless noted otherwise, as with <code>string-every</code> and
<code>string-any</code>,
all predicates passed to procedures specified in this SRFI may be
called in any order and any number of times.
It is an error if <var>pred</var> has side effects or
does not behave functionally (returning the same result whenever
it is called with the same character);
the sample implementation does not detect those errors.
</li>
<li>An <var>obj</var> argument may be any value at all.</li>
</ul>
<p class="continue">
It is an error to pass values that violate the specification above.
</p>
<p>
Arguments given in square brackets are optional. Unless otherwise noted in the
string describing the procedure, any prefix of these optional arguments may
be supplied, from zero arguments to the full list. When a procedure returns
multiple values, this is shown by listing the return values in square
brackets as well. So, for example, the procedure with signature
<pre class="code-example">
halts? <var>f [x init-store]</var> → <var>[boolean integer]</var>
</pre>
<p>
would take one (<var>f</var>), two (<var>f</var>, <var>x</var>)
or three (<var>f</var>, <var>x</var>, <var>init-store</var>) input arguments,
and return two values, a boolean and an integer.
</p>
<p>
An argument followed by "<code>...</code>" means zero or more elements.
So the procedure with the signature
<pre class="code-example">
sum-squares <var>x ... </var> → <var>number</var>
</pre>
takes zero or more arguments (<var>x ...</var>),
while the procedure with signature
<pre class="code-example">
spell-check <var>doc dict<sub>1</sub> dict<sub>2</sub> ...</var> → <var>string-list</var>
</pre>
<p>takes two required arguments
(<var>doc</var> and <var>dict<sub>1</sub></var>)
and zero or more optional arguments (<var>dict<sub>2</sub> ...</var>).
</p>
<!--========================================================================-->
<h2><a name="Procedures">Procedures</a></h2>
<!--========================================================================-->
<h3><a name="Predicates">Predicates</a></h3>
<dl>
<!--
==== string?
============================================================================-->
<dt class="proc-def">
<a name="string-p"></a>
<code class="proc-def"> string?</code><var> obj → boolean</var> (R5RS)
</dt>
<dd class="proc-def">
Is <var>obj</var> a string?
</dd>
<!--
==== string-null?
============================================================================-->
<dt class="proc-def">
<a name="string-null-p"></a>
<code class="proc-def">string-null?</code><var> string → boolean</var>
</dt>
<dd class="proc-def">
Is <var>string</var> the empty string?
</dd>
<!--
==== string-every string-any
============================================================================-->
<dt class="proc-def1">
<a name="string-every"></a>
<a name="string-any"></a>
<code class="proc-def">string-every</code><var> pred string [start end] → value</var>
</dt>
<dt class="proc-defn">
<code class="proc-def">string-any </code><var> pred string [start end] → value</var>
</dt>
<dd class="proc-def">
<p>
Checks to see if every/any character in <var>string</var>
satisfies <var>pred</var>,
proceeding from left (index <var>start</var>)
to right (index <var>end</var>).
These procedures are short-circuiting:
if <var>pred</var> returns false, <code>string-every</code>
does not call <var>pred</var> on subsequent characters;
if <var>pred</var> returns true, <code>string-any</code>
does not call <var>pred</var> on subsequent characters;
Both procedures are "witness-generating":
</p>
<ul>
<li> If <code>string-every</code> is given an empty interval
(with <var>start</var> = <var>end</var>),
it returns <code>#t</code>.</li>
<li> If <code>string-every</code> returns true for a non-empty
interval (with <var>start</var> < <var>end</var>),
the returned true value is the one returned by the final call to the
predicate on
<code>(string-ref (string-copy <var>string</var>) (- <var>end</var> 1))</code>.</li>
<li> If <code>string-any</code> returns true,
the returned true value is the one returned by the predicate.</li>
</ul>
<p>
<i>Note:</i>
The names of these procedures do not end with a question mark.
This indicates a general value is returned instead of a simple boolean
(<code>#t</code> or <code>#f</code>).
</p>
</dd>
</dl>
<!--========================================================================-->
<h3><a name="Constructors">Constructors</a></h3>
<dl>
<!--
==== make-string
============================================================================-->
<dt class="proc-def">
<a name="make-string"></a>
<code class="proc-def"> make-string</code><var> len char → string</var> (R5RS)
</dt>
<dd class="proc-def">
Returns a string of the given length filled with the given character.
</dd>
<!--
==== string
============================================================================-->
<dt class="proc-def">
<a name="string"></a>
<code class="proc-def"> string</code><var> char ... → string</var> (R5RS)
</dt>
<dd class="proc-def">
Returns a string consisting of the given characters.
</dd>
<!--
==== string-tabulate
============================================================================-->
<dt class="proc-def">
<a name="string-tabulate"></a>
<code class="proc-def">string-tabulate</code><var> proc len → string</var>
</dt>
<dd class="proc-def">
<var>Proc</var> is a procedure that accepts an exact integer
as its argument and returns a character.
Constructs a string of size <var>len</var> by calling <var>proc</var>
on each value from 0 (inclusive) to <var>len</var> (exclusive)
to produce the corresponding element of the string.
The order in which <var>proc</var> is called on those indexes is not
specified.
<p>
<i>Rationale:</i>
Although <code>string-unfold</code> is more general,
<code>string-tabulate</code> is likely to run faster
for the common special case it implements.
</p>
</dd>
<!--
==== string-unfold
============================================================================-->
<dt class="proc-def">
<a name="string-unfold"></a>
<code class="proc-def">string-unfold</code><var> stop? mapper successor seed [base make-final] → string</var>
</dt>
<dd class="proc-def">
This is a fundamental constructor for strings.
<ul>
<li> <var>successor</var> is used to generate a series of "seed"
values from the initial seed:
<div class="inset">
<var>seed</var>, (<var>successor</var> <var>seed</var>),
(<var>successor<sup>2</sup></var> <var>seed</var>),
(<var>successor<sup>3</sup></var> <var>seed</var>), ...
</div>
</li>
<li> <var>stop?</var> tells us when to stop — when it returns
true when applied to one of these seed values.</li>
<li> <var>mapper</var> maps each seed value to the corresponding character(s)
in the result string, which are assembled into that string in left-to-right
order.
It is an error for <var>mapper</var> to return anything
other than a character or string.</li>
<li> <var>base</var> is the optional initial/leftmost portion of
the constructed string, which defaults to the empty string <code>""</code>.
It is an error if <var>base</var> is anything other than a character
or string.</li>
<li> <var>make-final</var> is applied to the terminal seed value
(on which <var>stop?</var> returns
true) to produce the final/rightmost portion of the constructed string.
It defaults to <code>(lambda (x) "")</code>.
It is an error for <var>make-final</var> to return anything other
than a character or string.</li>
</ul>
<p>
<code>string-unfold</code> is a fairly powerful string constructor.
You can use it to
convert a list to a string, read a port into a string, reverse a string,
copy a string, and so forth. Examples:
</p>
<pre class="code-example">
(port->string p) = (string-unfold eof-object?
values
(lambda (x) (read-char p))
(read-char p))
(list->string lis) = (string-unfold null? car cdr lis)
(string-tabulate f size) = (string-unfold (lambda (i) (= i size)) f add1 0)
</pre>
<p>
To map <var>f</var> over a list <var>lis</var>, producing a string:
<pre class="code-example">
(string-unfold null? (compose f car) cdr lis)
</pre>
<p>
Interested functional programmers may enjoy noting that
<code>string-fold-right</code>
and <code>string-unfold</code> are in some sense inverses.
That is, given operations
<var>knull?</var>, <var>kar</var><var>, kdr</var>, and <var>kons</var>,
and a value <var>knil</var> satisfying
</p>
<pre class="code-example">
(<var>kons</var> (<var>kar</var> x) (<var>kdr</var> x)) = x and (<var>knull?</var> <var>knil</var>) = #t
</pre>
<p>
then
</p>
<pre class="code-example">
(string-fold-right <var>kons</var> <var>knil</var> (string-unfold <var>knull?</var> <var>kar</var> <var>kdr</var> <var>x</var>)) = <var>x</var>
</pre>
and
<pre class="code-example">
(string-unfold <var>knull?</var> <var>kar</var> <var>kdr</var> (string-fold-right <var>kons</var> <var>knil</var> <var>string</var>)) = <var>string</var>.
</pre>
<p>
This combinator pattern is sometimes called an "anamorphism."
</p>
<p>
<i>Note:</i> Implementations should not allow the size of strings created
by <code>string-unfold</code> to be limited by limits on stack size.
</p>
</dd>
<!--
==== string-unfold-right
============================================================================-->
<dt class="proc-def">
<a name="string-unfold-right"></a>
<code class="proc-def">string-unfold-right</code><var> stop? mapper successor seed [base make-final] → string</var>
</dt>
<dd class="proc-def">
This is a fundamental constructor for strings.
It is the same as <code>string-unfold</code>
except the results of <var>mapper</var> are assembled into the
string in right-to-left order,
<var>base</var> is the optional rightmost portion
of the constructed string, and <var>make-final</var>
produces the leftmost portion of the constructed string.
If <var>mapper</var> returns a string, the string
is prepended to the constructed string (without reversal).
<pre class="code-example">
(string-unfold-right (lambda (n) (< n (char->integer #\A)))
(lambda (n) (char-downcase (integer->char n)))
(lambda (n) (- n 1))
(char->integer #\Z)
#\space
(lambda (n) " The English alphabet: "))
=> " The English alphabet: abcdefghijklmnopqrstuvwxyz "
(string-unfold-right null?
(lambda (x) (string #\[ (car x) #\]))
cdr
'(#\a #\b #\c))
=> "[c][b][a]"
</pre>
</dd>
</dl>
<!--========================================================================-->
<h3><a name="Conversion">Conversion</a></h3>
<dl>
<!--
==== string->vector string->list
============================================================================-->
<dt class="proc-def1">
<a name="string2vector"></a>
<a name="string2list"></a>
</dt>
<dt class="proc-defn">
<code class="proc-def">string->vector</code><var> string [start end] → char-vector</var> (R7RS-small)
</dt>
<dt class="proc-defn">
<code class="proc-def">string->list </code><var> string [start end] → char-list</var> (R5RS+)
</dt>
<dd class="proc-def">
These procedures
return a newly allocated (unless empty) vector or list
of the characters that make up the given substring.
</dd>
<!--
==== vector->string list->string
============================================================================-->
<dt class="proc-def1">
<a name="vector2string"></a>
<a name="list2string"></a>
<code class="proc-def">vector->string</code><var> char-vector [start end] → string</var> (R7RS-small)
</dt>
<dt class="proc-defn">
<code class="proc-def"> list->string </code><var> char-list → string</var> (R5RS)
</dt>
<dd class="proc-def">
These procedures return a string containing the characters of the given
(sub)vector or list.
The behavior of the string will not be affected by subsequent mutation
of the given vector or list.
</dd>
<!--
==== reverse-list->string
============================================================================-->
<dt class="proc-def">
<a name="reverse-list2string"></a>
<code class="proc-def">reverse-list->string</code><var> char-list → string</var>
</dt>
<dd class="proc-def">
Semantically equivalent to <code>(compose list->string reverse)</code>:
<pre class="code-example">
(reverse-list->string '(#\a #\B #\c)) → "cBa"
</pre>
This is a common idiom in the epilogue of string-processing loops
that accumulate their result using a list in reverse order.
(See also
<code>string-concatenate-reverse</code> for the "chunked" variant.)
<!--========================================================================-->
<h3><a name="Selection">Selection</a></h3>
<dl>
<!--
==== string-length
============================================================================-->
<dt class="proc-def">
<a name="string-length"></a>
<code class="proc-def">string-length</code><var> string → len</var> (R5RS)
</dt>
<dd class="proc-def">
Returns the number of characters within the given string.
</dd>
<!--
==== string-ref
============================================================================-->
<dt class="proc-def">
<a name="string-ref"></a>
<code class="proc-def">string-ref</code><var> string idx → char</var> (R5RS)
</dt>
<dd class="proc-def">
Returns character <var>string[idx]</var>, using 0-origin indexing.
</dd>
<!--
==== substring string-copy
============================================================================-->
<dt class="proc-def1">
<a name="substring"></a>
<a name="string-copy"></a>
<code class="proc-def">substring </code><var> string start end → string</var> (R5RS)
</dt>
<dt class="proc-defn">
<code class="proc-def">string-copy </code><var> string [start end] → string</var> (R5RS+)
</dt>
<dd class="proc-def">
These procedures return a string containing the characters of
<var>string</var> beginning with index <var>start</var>
(inclusive) and ending with index <var>end</var> (exclusive).
The only difference is that <code>substring</code> requires all three arguments,
whereas <code>string-copy</code> requires only one.
</dd>
<!--
==== string-take string-drop string-take-right string-drop-right
============================================================================-->
<dt class="proc-def1">
<a name="string-take"></a>
<a name="string-drop"></a>
<a name="string-take-right"></a>
<a name="string-drop-right"></a>
<code class="proc-def">string-take </code><var> string nchars → string</var>
</dt>
<dt class="proc-defi">
<code class="proc-def">string-drop </code><var> string nchars → string</var>
</dt>
<dt class="proc-defi">
<code class="proc-def">string-take-right</code><var> string nchars → string</var>
</dt>
<dt class="proc-defn">
<code class="proc-def">string-drop-right</code><var> string nchars → string</var>
</dt>
<dd class="proc-def">
<code>string-take</code> returns a string containing the first
<var>nchars</var> of <var>string</var>;
<code>string-drop</code> returns a string containing all but the
first <var>nchars</var> of <var>string</var>.
<code>string-take-right</code> returns a string containing the
last <var>nchars</var> of <var>string</var>;
<code>string-drop-right</code> returns a string containing all
but the last <var>nchars</var> of <var>string</var>.
<pre class="code-example">
(string-take "Pete Szilagyi" 6) => "Pete S"
(string-drop "Pete Szilagyi" 6) => "zilagyi"
(string-take-right "Beta rules" 5) => "rules"
(string-drop-right "Beta rules" 5) => "Beta "
</pre>
<p>
It is an error to take or drop more characters than are in the string:
</p>
<pre class="code-example">
(string-take "foo" 37) => <em>error</em>
</pre>
</dd>
<!--
==== string-pad string-pad-right
============================================================================-->
<dt class="proc-def1">
<a name="string-pad"></a>
<a name="string-pad-right"></a>
<code class="proc-def">string-pad </code><var> string len [char start end] → string</var>
</dt>
<dt class="proc-defn">
<code class="proc-def">string-pad-right</code><var> string len [char start end] → string</var>
</dt>
<dd class="proc-def">
Returns a string of length <var>len</var> comprised of the characters
drawn from the given subrange of <var>string</var>,
padded on the left (right)
by as many occurrences of the character <var>char</var> as needed.
If <var>string</var> has more
than <var>len</var> chars, it is truncated on the left (right)
to length <var>len</var>.
<var>char</var> defaults to <code>#\space</code>.
<pre class="code-example">
(string-pad "325" 5) => " 325"