forked from scheme-requests-for-implementation/srfi-130
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrfi-130.html
1643 lines (1423 loc) · 74 KB
/
srfi-130.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'>
<!-- 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, list processing, SRFI" />
<link rev=made href="mailto:[email protected]" />
<title>SRFI 130: Cursor-based string library</title>
<!-- 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>
</head>
<body>
<!--========================================================================-->
<H1>Title</H1>
<div class=title-text>Cursor-based string library</div>
<!--========================================================================-->
<H1>Author</H1>
John Cowan
<H1>Status</H1>
<p>This SRFI is currently in <em>draft</em> status. Here is <a href="http://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+130+at+srfi+dotschemers+dot+org">srfi-130@<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="http://srfi-email.schemers.org/srfi-130">archive</a>.</p>
<ul>
<li>Received: 2015/12/2</li>
<li>60-day deadline: 2016/2/1</li>
<li>Draft #1 published: 2015/12/3</li>
<li>Draft #2 published: 2015/12/13</li>
<li>Draft #3 published: 2016/3/28</li>
<li>Draft #4 published: 2016/4/12</li>
<li>Draft #5 published: 2016/5/14</li>
<li>Draft #6 published: 2016/5/14</li>
<li>Draft #7 published: 2016/5/22</li>
<li>Draft #8 published: 2016/5/23 (code changes only)</li>
</ul>
<h1>Table of contents</H1>
<!-- A bug in netscape (?) keeps the first link in this UL from being active.
==== So the Abstract link be dead. 99/8/22 -Olin
-->
<ul id=toc-table>
<li><a href="#Abstract">Abstract</a>
<li><a href="#ProcedureIndex">Procedure index</a>
<li><a href="#Rationale">Rationale</a>
<li><a href="#Specification">Specification</a>
<ul>
<li><a href="#StringCursors">String cursors</a>
<li><a href="#CallingPredicates">Calling predicates</a>
<li><a href="#SharedStorage">Shared storage</a>
<li><a href="#NamingConventions">Naming conventions</a>
<li><a href="#Notation">Notation</a>
<li><a href="#Procedures">Procedures</a>
<ul>
<li><a href="#CursorOperations">Cursor operations</a>
<li><a href="#Predicates">Predicates</a>
<li><a href="#Constructors">Constructors</a>
<li><a href="#Conversion">Conversion</a>
<li><a href="#Selection">Selection</a>
<li><a href="#PrefixesSuffixes">Prefixes & suffixes</a>
<li><a href="#Searching">Searching</a>
<li><a href="#TheWholeString">The whole string</a>
</ul>
<li><a href="#SampleImp">Sample implementation</a>
<li><a href="#Acknowledgements">Acknowledgements</a>
<li><a href="#Links">References & Links</a>
<li><a href="#Copyright">Copyright</a>
</ul>
<!--========================================================================-->
<h1><a name="Abstract">Abstract</a></H1>
<p>
<abbr title="Revised<sup>5</sup> Report on Scheme"><a href="#R5RS">R5RS</a></abbr>
Scheme has an impoverished set of string-processing utilities, which is a
problem for authors of portable code. Although
<abbr title="Revised<sup>7</sup> Report on Scheme"><a href="#R7RS">R7RS</a></abbr>
provides some extensions and improvements, it is still very incomplete.
This <abbr title="Scheme Request for
Implementation">SRFI</abbr> proposes a coherent and comprehensive set of
string-processing procedures; it is accompanied by a portable sample implementation
of the spec.
<p>This SRFI is derived from SRFI 13. The biggest difference is that it
allows subsequences of strings to be specified by
<em>cursors</em> as well as the traditional string indexes.
In addition, it omits the comparison, case-mapping, and mutation operations
of SRFI 13, as well as all procedures already present in
<abbr title="Revised<sup>7</sup> Report on Scheme"><a href="#R7RS">R7RS</a></abbr>.
<!--========================================================================-->
<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> Cursor operations
<dd class=proc-index>
<pre class=proc-index>
<a href="#string-cursor-p">string-cursor?</a>
<a href="#string-cursor-start">string-cursor-start</a> <a href="#string-cursor-end">string-cursor-end</a>
<a href="#string-cursor-next">string-cursor-next</a> <a href="#string-cursor-prev">string-cursor-prev</a>
<a href="#string-cursor-forward">string-cursor-forward</a> <a href="#string-cursor-back">string-cursor-back</a>
<a href="#string-cursor-eq">string-cursor=?</a>
<a href="#string-cursor-lt">string-cursor<?</a> <a href="#string-cursor-gt">string-cursor>?</a>
<a href="#string-cursor-le">string-cursor<=?</a> <a href="#string-cursor-ge">string-cursor>=?</a>
<a href="#string-cursor-diff">string-cursor-diff</a>
<a href="#string-cursor2index">string-cursor->index</a> <a href="#string-index2cursor">string-index->cursor</a>
</pre>
<dt class=proc-index> Predicates
<dd class=proc-index>
<pre class=proc-index>
<a href="#string-null-p">string-null?</a>
<a href="#string-every">string-every</a> <a href="#string-any">string-any</a>
</pre>
<dt class=proc-index> Constructors
<dd class=proc-index>
<pre class=proc-index>
<a href="#string-tabulate">string-tabulate</a>
<a href="#string-unfold">string-unfold</a> <a href="#string-unfold-right">string-unfold-right</a>
</pre>
<dt class=proc-index> Conversion
<dd class=proc-index>
<pre class=proc-index>
<a href="#string2list-w-cursors">string->list/cursors</a> <a href="#string2vector-w-cursors">string->vector/cursors</a>
<a href="#reverse-list2string">reverse-list->string</a> <a href="#string-join">string-join</a>
</pre>
<dt class=proc-index> Selection
<dd class=proc-index>
<pre class=proc-index>
<a href="#string-ref-w-cursor">string-ref/cursor</a>
<a href="#substring-w-cursors">substring/cursors</a> <a href="#string-copy-w-cursors">string-copy/cursors</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>
<dt class=proc-index>Prefixes & suffixes
<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>
<dt class=proc-index>Searching
<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>
</pre>
<dt class=proc-index>The whole string
<dd class=proc-index>
<pre class=proc-index>
<a href="#string-reverse">string-reverse</a>
<a href="#string-concatenate">string-concatenate</a> <a href="#string-concatenate-reverse">string-concatenate-reverse</a>
<a href="#string-fold">string-fold</a> <a href="#string-fold-right">string-fold-right</a>
<a href="#string-for-each-cursor">string-for-each-cursor</a>
<a href="#string-replicate">string-replicate</a> <a href="#string-count">string-count</a>
<a href="#string-replace">string-replace</a> <a href="#string-split">string-split</a>
<a href="#string-filter">string-filter</a> <a href="#string-remove">string-remove</a>
</pre>
</dl>
</div>
<!--========================================================================-->
<h1><a name="Rationale">Rationale</a></h1>
<p>
This SRFI defines a rich set of operations for
manipulating strings. These are frequently useful for scripting and other
text-manipulation applications. The library's design was influenced by the
string libraries found in MIT Scheme, Gambit, RScheme, MzScheme, SLIB, Common
Lisp, Bigloo, Guile, Chez, APL, Java, and the SML standard basis.
All functionality is available in substring and full-string forms.
<p>
When SRFI 13 was defined in 1999, it was intended to provide efficient string operations
on both whole strings and substrings. At that time, it was normal for strings to be
sequences of 8-bit characters, or in a few cases, characters of other fixed lengths.
Consequently, many of the SRFI 13 procedures accept optional exact integer
arguments for each of the string arguments, indexing the beginning and the end
of the substring(s) to be operated on.</p>
<p>
Unfortunately for this design, Unicode has become much more widely used, and it is now
fairly common for implementations to store strings internally as UTF-8 or UTF-16 code
unit sequences, which means that indexing operations are potentially O(n) rather than O(1).
Using opaque cursors makes it possible to iterate much more efficiently through such strings
compared to incrementing or decrementing indexes; however, for backward compatibility,
the procedures defined in this SRFI accept either cursors or indexes.
The results returned are always cursors: the use of indexes is
preserved mainly for the sake of existing code and for implementer
convenience.</p>
<p>
The operations provided here are entirely independent of the character repertoire supported
by the implementation. In particular, this means that the comparison and case conversion
procedures of SRFI 13 are excluded. There is also no provision for
<a class="ext-link" href="http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-2.html#node_idx_54">R6RS normalization procedures</a>
or for a <code>string->integer</code> procedure that was proposed for SRFI 13 but not included. These may appear in future SRFIs.
Furthermore, string mutation can be
extremely expensive if the storage used for the string needs to be expanded, particularly
if the implementation does not use an indirect pointer to it (as in Chicken),
so this SRFI does not provide for it.
The low-level procedures of SRFI 13 are specific to the sample implementation, and have been removed
to make other implementations simpler and easier.</p>
<p>Many SRFI 13 procedures accept either a predicate, a single character, or a
<a href="http://srfi.schemers.org/srfi-14/srfi-14.html">SRFI 14</a> character set.
In this SRFI, only support for predicates is required, though implementations may also support the
other two alternatives. In that case, a single character is interpreted as a predicate which returns true if its
argument is the same (in the sense of <code>eqv?</code>) to that character; a character set is interpreted
as a predicate which returns true if its argument belongs to that character set.
In SRFI 13, character sets are inherently more efficient than predicates
<a class="ext-link" href="http://srfi.schemers.org/srfi-13/mail-archive/msg00052.html">
because testing them is fast and free of side effects</a>,
though how fast character sets actually are if they support full Unicode is very implementation-dependent.
The only procedure that absolutely requires character set support, <code>string-tokenize</code>,
has been replaced here by the more usual <code>string-split</code> procedure provided by Perl,
Python, Java, JavaScript, and other languages.
</p>
<p>The search procedures in SRFI 13 return either an index or <code>#f</code> if the search fails.
Their counterparts in this SRFI return cursors. Left-to-right searches return a cursor representing
the leftmost matching character, or the post-end cursor if there is no match; right-to-left searches
return a cursor representing the <i>successor</i> of the rightmost matching character, or the
start cursor if there is no match. This convention was devised by Alan Watson
and implemented in Chibi Scheme.</p>
<p>
In short, this SRFI is intended to help move the practice of Scheme programming
away from mutable strings, string indexes, and SRFI 13, while largely maintaining
backward compatibility. It does not require any particular run-time efficiencies from its procedures.
<!--========================================================================-->
<h1><a name="Specification">Specification</a></h1>
<!--========================================================================-->
<h2><a name="StringCursors">String cursors</a></h2>
<p>
While indexes are exact integers
ranging from 0 to the length of the string they refer to, cursors are opaque objects
that point into strings.
However, they are not required to belong to a disjoint type, as long as they are either disjoint
from indexes or identical to indexes. For example, they may be negative exact integers representing
indexes into a byte array underlying the string. It is also possible to implement cursors as a record type
or an implementation-specific primitive type.
Additionally, in implementations where no provision has been made for cursors, or there is no benefit in
implementing them separately because strings are in fact arrays of fixed-length characters, it is useful
to allow indexes and cursors to be the same thing.
(Cursors must also be disjoint from <code>#f</code>.)
<p>It is an error to make any use of a cursor referring to a string after
the string, or any string that shares storage with it, has been mutated by a procedure like <code>string-set!</code>,
<code>string-copy!</code>, or <code>string-fill!</code>.
<p>
Given a string of length <em>n</em>, there are <em>n</em> + 1 valid cursors
that refer to it: one for each character in the string,
and one for the position just after the last character, known as the "post-end cursor".
The cursor for the first (or zeroth) position in the string is known as the "start cursor".
The post-end cursor is provided because
when creating a string from cursors the second cursor argument is exclusive.
It is an error if a cursor argument is not one of the valid cursors for the string argument.
The index analogue of the post-end cursor is <em>n</em>.
</p>
<h2><a name="CallingPredicates">Calling predicates</a></h2>
<p>
All predicates passed to procedures defined in this SRFI may be called in any order
and any number of times, except as otherwise noted. This is not the case in SRFI 13.
<!--========================================================================-->
<h2><a name="SharedStorage">Shared storage</a></h2>
<p>
Some Scheme implementations, <em>e.g.</em> Guile, provide ways to construct
substrings that share storage with other strings. SRFI 130 provides only
minimal support for such shared substrings. The following SRFI 130 procedures
are allowed to return
a result which shares storage with one or more of their string arguments:
<pre>
substring/cursors
string-take string-take-right
string-drop string-drop-right
string-pad string-pad-right
string-trim string-trim-right string-trim-both
string-split string-filter string-remove
</pre>
<p>In particular, if the result is the same (in the sense of <code>string=?</code>)
as any of the arguments, <i>any</i> implementation of the above procedures may return
the string argument
without copying it. Other procedures such as
<code>string-copy/cursors</code>, as well as all the
<abbr title="Revised<sup>7</sup> Report on Scheme"><a href="#R7RS">R7RS</a></abbr> procedures,
are not permitted to return shared results.
If a shared value is returned, it may be mutable or immutable.
<!--========================================================================-->
<h2><a name="NamingConventions">Naming conventions</a></h2>
<p>
The procedures of this SRFI follow
a consistent naming scheme, and are consistent with the conventions
developed in SRFI 1. The names are composed of smaller lexemes
in a regular way that exposes the structure and relationships between the
procedures. This should help the programmer to recall or reconstitute the name
of the desired procedure. In
particular, the order of common parameters is consistent across the
different procedures.
<p>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.
This is a general convention that has been established in other SRFIs;
the value of a convention is proportional to the extent of its use.
</ul>
<!--========================================================================-->
<h2><a name="Notation">Notation</a></h2>
<p>
In the following procedure specifications:
<ul>
<li><p> An <var>s</var> parameter is a string.
<li><p> A <var>char</var> parameter is a character.
<li><p> <var>Start</var> and <var>end</var> parameters are half-open string
cursors or indexes specifying
a substring within a string parameter, and typically restrict a procedure's
action to the indicated substring. When omitted, they default
to 0 and the length of the string, respectively; or from another
point of view, they default to the start cursor
and the post-end cursor, respectively. For indexes, it
must be the case that 0 <= <var>start</var> <= <var>end</var>
<= <code>(string-length <var>s</var>)</code>, for
the corresponding parameter <var>s</var> when <var>start</var>
and <var>end</var> are indexes, and the corresponding relationship
must hold when they are cursors. It is an error unless <var>start</var>
and <var>end</var> are both cursors or both indexes.
<li><p> A <var>pred</var> parameter is a unary character predicate procedure, returning
a true/false value when applied to a character. It is an error if a <var>pred</var>
is not pure and functional.
<li><p> A <var>cursor</var> parameter is either a cursor or an exact non-negative integer
specifying an index into a string.
<li><p> <var>Len</var> and <var>nchars</var> parameters are exact non-negative integers specifying a
length of a string or some number of characters.
<li><p> An <var>obj</var> parameter may be any value at all.
</ul>
<p class=continue>
Passing values to procedures with these parameters that do not satisfy these
types is an error.
<p>
Parameters given in square brackets are optional. Unless otherwise noted in the
text describing the procedure, any prefix of these optional parameters 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>
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 parameters,
and return two values, a boolean and an integer.
<p>
A parameter 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>
takes two required parameters
(<var>doc</var> and <var>dict<sub>1</sub></var>)
and zero or more optional parameters (<var>dict<sub>2</sub> ...</var>).
<p>
If a procedure's return value is said to be "unspecified," this means that the procedure
returns a single arbitrary value. Such a procedure is not even
required to be consistent from call to call.
<!--========================================================================-->
<h2><a name="Procedures">Procedures</a></h2>
<!--========================================================================-->
<h3><a name="CursorOperations">Cursor operations</a></h3>
<p>These procedures are mostly taken from Chibi Scheme.
<dl>
<!--
==== string-cursor?
============================================================================-->
<dt class=proc-def1>
<a name="string-cursor-p"></a>
<code class=proc-def>string-cursor?</code><var> obj → boolean</var>
<dd class=proc-def>
Returns <code>#t</code> if <var>obj</var> can be a string cursor,
and <code>#f</code> otherwise. In implementations where cursors
and indexes are the same thing, <code>#t</code> is returned on
any cursor or index; where they are disjoint, <code>#t</code> is
returned on cursors, <code>#f</code> on indexes. If <var>obj</var>
is neither a cursor nor an index, <code>string-cursor?</code> will
always return <code>#f</code>.
</dd>
<dl>
<!--
==== string-cursor-start string-cursor-end
============================================================================-->
<dt class=proc-def1>
<a name="string-cursor-start"></a>
<a name="string-cursor-end"></a>
<code class=proc-def>string-cursor-start</code><var> s → cursor</var>
<dt class=proc-defn><code class=proc-def>string-cursor-end</code><var> s → cursor</var>
<dd class=proc-def>
Returns the start/post-end cursor of <var>s</var> respectively.
</dd>
<!--
==== string-cursor-next string-cursor-prev
============================================================================-->
<dt class=proc-def1>
<a name="string-cursor-next"></a>
<a name="string-cursor-prev"></a>
<code class=proc-def>string-cursor-next</code><var> s cursor → cursor</var>
<dt class=proc-defn><code class=proc-def>string-cursor-prev</code><var> s cursor → cursor</var>
<dd class=proc-def>
Returns the cursor into <var>s</var> following/preceding <var>cursor</var>.
If <var>cursor</var> is an index, returns one more/less than <var>cursor</var>.
It is an error if <var>cursor</var> is the post-end/start cursor of <var>s</var>.
</dd>
<!--
==== string-cursor-forward string-cursor-back
============================================================================-->
<dt class=proc-def1>
<a name="string-cursor-forward"></a>
<a name="string-cursor-back"></a>
<code class=proc-def>string-cursor-forward</code><var> s cursor nchars → cursor</var>
<dt class=proc-defn><code class=proc-def>string-cursor-back</code><var> s cursor nchars → cursor</var>
<dd class=proc-def>
Returns the cursor into <var>s</var> which follows/precedes <var>cursor</var>
by <var>nchars</var> characters.
If <var>cursor</var> is an index, returns <var>nchars</var> more/less than <var>cursor</var>.
It is an error if the result would be an invalid cursor or index.
</dd>
<!--
==== string-cursor comparisons
============================================================================-->
<dt class=proc-def1>
<a name="string-cursor-eq"></a>
<a name="string-cursor-lt"></a>
<a name="string-cursor-gt"></a>
<a name="string-cursor-le"></a>
<a name="string-cursor-ge"></a>
<code class=proc-def>string-cursor=?</code><var> cursor<sub>1</sub> cursor<sub>2</sub> → boolean</var>
<dt class=proc-defi><code class=proc-def>string-cursor<?</code><var> cursor<sub>1</sub> cursor<sub>2</sub> → boolean</var>
<dt class=proc-defi><code class=proc-def>string-cursor>?</code><var> cursor<sub>1</sub> cursor<sub>2</sub> → boolean</var>
<dt class=proc-defi><code class=proc-def>string-cursor<=?</code><var> cursor<sub>1</sub> cursor<sub>2</sub> → boolean</var>
<dt class=proc-defn><code class=proc-def>string-cursor>=?</code><var> cursor<sub>1</sub> cursor<sub>2</sub> → boolean</var>
<dd class=proc-def>
Compares two cursors or two indexes pointing into the same string.
<!--
==== string-cursor-diff
============================================================================-->
<dt class=proc-def>
<a name="string-cursor-diff"></a>
<code class=proc-def>string-cursor-diff</code><var> s start end → nchars</var>
<dd class=proc-def>
Returns the number of characters between <var>start</var> and <var>end</var>
in string <var>s</var>.
Note that the result is always non-negative if <var>start</var>
and <var>end</var> are a valid start-end pair.
</dd>
<!--
==== string-cursor->index string-index->cursor
============================================================================-->
<dt class=proc-def1>
<a name="string-cursor2index"></a>
<a name="string-index2cursor"></a>
<code class=proc-def>string-cursor->index</code><var> s cursor → index</var>
<dt class=proc-defn><code class=proc-def>string-index->cursor</code><var> s index → cursor</var>
<dd class=proc-def>
Converts a cursor/index into <var>s</var> into the corresponding
index/cursor. If the argument is already an index/cursor, it is
returned unchanged.
</dd>
</dl>
<!--========================================================================-->
<h3><a name="Predicates">Predicates</a></h3>
<dl>
<!--
==== string-null?
============================================================================-->
<dt class=proc-def>
<a name="string-null-p"></a>
<code class=proc-def>string-null?</code><var> s → boolean</var>
<dd class=proc-def>
Is <var>s</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 s [start end] → value</var>
<dt class=proc-defn><code class=proc-def>string-any</code><var> pred s [start end] → value</var>
<dd class=proc-def>
Checks to see if every/any character in <var>s</var> satisfies <var>pred</var>
proceeding from left (index <var>start</var>) to right (index <var>end</var>).
The predicate is "witness-generating":
<ul>
<li> If <code>string-any</code> returns true, the returned true value is the one produced
by the application of the predicate.
<li> If <code>string-every</code> returns true, the returned true value is the one
produced by the final application of the predicate to <var>s</var>[<var>end</var>-1].
If <code>string-every</code> is applied to an empty sequence of characters,
it simply returns <code>#t</code>.
</ul>
<p>
The names of these procedures do not end with a question mark — this is to
indicate that they do not return a simple boolean
(<code>#t</code> or <code>#f</code>), but a general value.
</dl>
<!--========================================================================-->
<h3><a name="Constructors">Constructors</a></h3>
<dl>
<!--
==== string-tabulate
============================================================================-->
<dt class=proc-def>
<a name="string-tabulate"></a>
<code class=proc-def>string-tabulate</code><var> proc len → string</var>
<dd class=proc-def>
<var>Proc</var> is an integer → char procedure. Construct a string of size <var>len</var>
by applying <var>proc</var> to each value from 0 (inclusive) to <var>len</var> (exclusive) to produce the corresponding string
element. The order in which <var>proc</var> is applied to the indexes is not
specified.
<p>Note that the order of arguments is not the same as SRFI 1's
<code>list-tabulate</code>, but is the same as tabulation functions
in other SRFIs. When this discrepancy was discovered in SRFI 13,
it was too late to change SRFI 1.
<!--
==== 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>
<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> <var>Stop?</var> tells us when to stop — when it returns true when applied to one
of these seed values.
<li> <var>Mapper</var> maps each seed value to the corresponding character
in the result string. These chars are assembled into the
string in a left-to-right order.
<li> <var>Base</var> is the optional initial/leftmost portion of the constructed string;
it defaults to the empty string "".
<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>.
</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:
<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>, <var>kons</var>, and <var>knil</var> satisfying
<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>
then
<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>s</var>)) = <var>s</var>.
</pre>
The final string constructed does not share storage with either <var>base</var>
or the value produced by <var>make-final</var>.
<p>
This combinator sometimes is called an "anamorphism."
<p>
Note: implementations should take care that runtime stack limits do not
cause overflow when constructing large (<em>e.g.</em>, megabyte) strings with
<code>string-unfold</code>.
<!--
==== 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>
<dd class=proc-def>
This is a fundamental constructor for strings. It is equivalent to <code>string-unfold</code>,
except that the results of <var>mapper</var> are assembled into the
string in a 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.
</dl>
<h3><a name="Conversion">Conversion</a></h3>
<dl>
<!--
==== string->list/cursors string->vector/cursors
============================================================================-->
<dt class=proc-def1>
<a name="string2list-w-cursors"></a>
<a name="string2vector-w-cursors"></a>
<code class=proc-def>string->list/cursors</code><var> s [start end] → char-list</var>
<dt class=proc-defn><code class=proc-def>string->vector/cursors</code><var> s [start end] → char-vector</var>
<dd class=proc-def>
<code>string->list/cursors</code> and <code>string->vector/cursors</code> return
a newly allocated list or vector of the characters
that make up the given string. They differ from the
<abbr title="Revised<sup>7</sup> Report on Scheme"><a href="#R7RS">R7RS</a></abbr> procedures
<code>string->list</code> and <code>string->vector</code>
by accepting either cursors or indexes.
<!--
==== 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>
<dd class=proc-def>
An efficient implementation of <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 epilog of string-processing loops
that accumulate an answer in a reverse-order list. (See also
<code>string-concatenate-reverse</code> for the "chunked" variant.)
<!--
==== string-join
============================================================================-->
<dt class=proc-def>
<a name="string-join"></a>
<code class=proc-def>string-join</code><var> string-list [delimiter grammar] → string</var>
<dd class=proc-def>
This procedure is a simple unparser —- it pastes strings together using
the delimiter string.
<p>
The <var>grammar</var> argument is a symbol that determines how the delimiter is
used, and defaults to <code>'infix</code>.
<ul>
<li> <code>'infix</code> means an infix or separator grammar:
insert the delimiter
between list elements. An empty list will produce an empty string —
note, however, that parsing an empty string with an infix or separator
grammar is ambiguous. Is it an empty list, or a list of one element,
the empty string?
<li> <code>'strict-infix</code> means the same as <code>'infix</code>,
but will signal an error if given an empty list.
<li> <code>'suffix</code> means a suffix or terminator grammar:
insert the delimiter
after every list element. This grammar has no ambiguities.
<li> <code>'prefix</code> means a prefix grammar: insert the delimiter
before every list element. This grammar has no ambiguities.
</ul>
The delimiter is the string used to delimit elements; it defaults to
a single space " ".
<pre class=code-example>
(string-join '("foo" "bar" "baz") ":") => "foo:bar:baz"
(string-join '("foo" "bar" "baz") ":" 'suffix) => "foo:bar:baz:"
;; Infix grammar is ambiguous wrt empty list vs. empty string,
(string-join '() ":") => ""
(string-join '("") ":") => ""
;; but suffix & prefix grammars are not.
(string-join '() ":" 'suffix) => ""
(string-join '("") ":" 'suffix) => ":"
</pre>
</dl>
<!--========================================================================-->
<h3><a name="Selection">Selection</a></h3>
<dl>
<!--
==== string-ref/cursor
============================================================================-->
<dt class=proc-def>
<a name="string-ref-w-cursor"></a>
<code class=proc-def>string-ref/cursor</code><var> s cursor → char</var>
<dd class=proc-def>
Returns character <var>s[i]</var> using a valid cursor or index of <var>s</var>.
It differs from the
<abbr title="Revised<sup>7</sup> Report on Scheme"><a href="#R7RS">R7RS</a></abbr> procedure <code>string-ref</code>
by accepting either a cursor or an index.
<!--
==== string-copy substring/cursors
============================================================================-->
<dt class=proc-def1>
<a name="substring-w-cursors"></a>
<a name="string-copy-w-cursors"></a>
<code class=proc-def>substring/cursors</code><var> s start end → string</var>
<dt class=proc-defn><code class="proc-def">string-copy/cursors</code><var> s [start end] → string</var>
<dd class=proc-def>
These procedures return a string whose contents are the characters of <var>s</var>
beginning with index <var>start</var> (inclusive) and ending with index <var>end</var>
(exclusive).
If <code>substring/cursors</code> produces the entire string, it may return either
<var>s</var> or a copy of <var>s</var>; in some implementations, proper substrings may share
memory with <var>s</var>.
However, <code>string-copy/cursors</code> always returns a newly allocated string.
They differ from the
<abbr title="Revised<sup>7</sup> Report on Scheme"><a href="#R7RS">R7RS</a></abbr> procedures
<code>substring</code> and <code>string-copy</code>
by accepting either cursors or indexes.
<!--
==== 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> s nchars → string</var>
<dt class=proc-defi><code class=proc-def>string-drop</code><var> s nchars → string</var>
<dt class=proc-defi><code class=proc-def>string-take-right</code><var> s nchars → string</var>
<dt class=proc-defn><code class=proc-def>string-drop-right</code><var> s nchars → string</var>
<dd class=proc-def>
<code>string-take</code> returns the first <var>nchars</var> of <var>s</var>;
<code>string-drop</code> returns all but the first <var>nchars</var> of <var>s</var>.
<code>string-take-right</code> returns the last <var>nchars</var> of <var>s</var>;
<code>string-drop-right</code> returns all but the last <var>nchars</var> of <var>s</var>.
If these procedures produce the entire string, they may return either
<var>s</var> or a copy of <var>s</var>; in some implementations, proper substrings may share
memory with <var>s</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>
It is an error to take or drop more characters than are in the string:
<pre class=code-example>
(string-take "foo" 37) => <em>error</em>
</pre>
<!--
==== 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> s len [char start end] → string</var>
<dt class=proc-defn><code class=proc-def>string-pad-right</code><var> s len [char start end] → string</var>
<dd class=proc-def>
Build a string of length <var>len</var> comprised of <var>s</var> padded on the left (right)
by as many occurrences of the character <var>char</var> as needed. If <var>s</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>.
<p>
If <var>len</var> <= <var>end</var>-<var>start</var>, the returned value is allowed to share storage
with <var>s</var>, or be exactly <var>s</var> (if <var>len</var> = <var>end</var>-<var>start</var>).
<pre class=code-example>
(string-pad "325" 5) => " 325"
(string-pad "71325" 5) => "71325"
(string-pad "8871325" 5) => "71325"
</pre>
<!--
==== string-trim string-trim-right string-trim-both
============================================================================-->
<dt class=proc-def1>
<a name="string-trim"></a>
<a name="string-trim-right"></a>
<a name="string-trim-both"></a>
<code class=proc-def>string-trim </code><var> s [pred start end] → string</var>
<dt class=proc-defi><code class=proc-def>string-trim-right</code><var> s [pred start end] → string</var>
<dt class=proc-defi><code class=proc-def>string-trim-both </code><var> s [pred start end] → string</var>
<dd class=proc-defn>
Trim <var>s</var> by skipping over all characters on the left / on the right /
on both sides that satisfy the second parameter <var>pred</var>:
<var>pred</var> defaults to <code>char-whitespace?</code>.
<p>
If no trimming occurs, these functions may return either <var>s</var> or a copy of <var>s</var>;
in some implementations, proper substrings may share memory with <var>s</var>.
<pre class=code-example>
(string-trim-both " The outlook wasn't brilliant, \n\r")
=> "The outlook wasn't brilliant,"
</pre>
</dl>