Skip to content

Commit 2cd441a

Browse files
authored
[doc] Fix #1300 with-input-to-string and with-output-to-string macros (#1554)
Document macros `with-input-to-string` and `with-output-to-string` Co-authored-by: @cgay
1 parent 6e308d8 commit 2cd441a

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

documentation/library-reference/source/io/streams.rst

+144
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,67 @@ most libraries that provide stream classes provide a macro. For example,
329329
// close(stream) is called automatically
330330
end
331331
332+
with-output-to-string / with-input-from-string macros
333+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
334+
335+
The macro :macro:`with-output-to-string` provides a convenient way of
336+
returning a :drm:`<string>` after performing a series of operations on a
337+
:class:`<stream>`, and then close the stream.
338+
339+
Instead of writing this code:
340+
341+
.. code-block:: dylan
342+
343+
let stream = make(<string-stream>, direction: #"output");
344+
write-element(stream, 'O');
345+
write-element(stream, 'k');
346+
contents(stream);
347+
348+
we can write:
349+
350+
.. code-block:: dylan
351+
352+
with-output-to-string (stream)
353+
write-element(stream, 'O');
354+
write-element(stream, 'k');
355+
end;
356+
357+
The symmetric macro :macro:`with-input-from-string` creates a
358+
:class:`<string-stream>` with direction `#"input"`, provides an
359+
opportunity to perform operations and closes the stream.
360+
361+
For instance:
362+
363+
.. code-block:: dylan
364+
365+
with-input-from-string (stream = "Foobar")
366+
for (i from 0,
367+
char = read-element(stream, on-end-of-stream: #f)
368+
then read-element(stream, on-end-of-stream: #f),
369+
while: char)
370+
format-out("%d: %c\n", i, char)
371+
end;
372+
end;
373+
374+
// 0: F
375+
// 1: o
376+
// 2: o
377+
// 3: b
378+
// 4: a
379+
// 5: r
380+
381+
will replace:
382+
383+
.. code-block:: dylan
384+
385+
let s = make(<string-stream>, contents: "Foobar", direction: #"input");
386+
for (i from 0,
387+
char = read-element(s, on-end-of-stream: #f)
388+
then read-element(s, on-end-of-stream: #f),
389+
while: char)
390+
format-out("%d: %c\n", i, char)
391+
end;
392+
close(s);
332393
333394
Locking streams
334395
^^^^^^^^^^^^^^^
@@ -2779,6 +2840,89 @@ are exported from the *streams* module.
27792840
* :class:`<indenting-stream>`
27802841
* :gf:`indent`
27812842

2843+
.. macro:: with-input-from-string
2844+
:statement:
2845+
2846+
Creates an input stream, to perform operations on it and closes the
2847+
stream.
2848+
2849+
2850+
:macrocall:
2851+
.. parsed-literal::
2852+
2853+
with-input-from-string (stream-name = string)
2854+
body
2855+
end
2856+
2857+
.. parsed-literal::
2858+
2859+
with-input-from-string (stream-name = string, class-name)
2860+
body
2861+
end
2862+
2863+
:parameter stream-name: A Dylan variable-name *bnf*.
2864+
:parameter string-expression: A Dylan string expression *bnf*.
2865+
:parameter classname: A Dylan class.
2866+
:parameter body: A Dylan body *bnf*.
2867+
2868+
:description:
2869+
2870+
In the first use, it creates a :class:`<string-stream>` with
2871+
direction `#"input"` that is associated with a variable name. In
2872+
the second use, it allows to specify a class for the stream type.
2873+
2874+
:example:
2875+
2876+
.. code-block:: dylan
2877+
2878+
with-input-from-string (stream = "Foobar")
2879+
for (i from 0,
2880+
char = read-element(stream, on-end-of-stream: #f)
2881+
then read-element(stream, on-end-of-stream: #f),
2882+
while: char)
2883+
format-out("%d: %c\n", i, char)
2884+
end;
2885+
end;
2886+
2887+
:see-also:
2888+
:macro:`with-output-to-string`
2889+
2890+
.. macro:: with-output-to-string
2891+
:statement:
2892+
2893+
Provides a convenient way of returning a :drm:`<string>` after
2894+
performing a series of operations on a :drm:`<stream>`, and then
2895+
close the stream.
2896+
2897+
:macrocall:
2898+
.. parsed-literal::
2899+
2900+
with-output-to-string (`stream-name`)
2901+
`body`
2902+
end => `string`
2903+
2904+
:parameter stream-name: A Dylan variable-name *bnf*.
2905+
:parameter body: A Dylan body *bnf*.
2906+
:value string: Instance of :drm:`<string>`.
2907+
2908+
:description:
2909+
2910+
Creates a :class:`<string-stream>` with direction `#"output"` and
2911+
provides an opportunity to perform operations and close the
2912+
stream.
2913+
2914+
:example:
2915+
2916+
.. code-block:: dylan
2917+
2918+
with-output-string (stream)
2919+
print-document(document, stream)
2920+
end;
2921+
2922+
:see-also:
2923+
:macro:`with-input-from-string`
2924+
2925+
27822926
.. macro:: with-stream-locked
27832927
:statement:
27842928

0 commit comments

Comments
 (0)