Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] Fix #1300. Document with-input-to-string and with-output-to-string macros #1554

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions documentation/library-reference/source/io/streams.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,67 @@ most libraries that provide stream classes provide a macro. For example,
// close(stream) is called automatically
end

with-output-to-string / with-input-from-string macros
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The macro :macro:`with-output-to-string` provides a convenient way of
returning a :class:`<string>` after performing a series of operations on a
:class:`<stream>`, and then close the stream.

Instead of writing this code:

.. code-block:: dylan

let stream = make(<string-stream>, direction: #"output");
write-element(stream, 'O');
write-element(stream, 'k');
contents(stream);

we can write:

.. code-block:: dylan

with-output-to-string (stream)
write-element(stream, 'O');
write-element(stream, 'k');
end;

The symmetric macro :macro:`with-input-to-string` creates a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"to" should be "from" here.

:class:`<string-stream>` with direction `#"input"`, provides an
opportunity to perform operations and closes the stream.

For instance:

.. code-block:: dylan

with-input-from-string (stream = "Foobar")
for (i from 0,
char = read-element(stream, on-end-of-stream: #f)
then read-element(stream, on-end-of-stream: #f),
while: char)
format-out("%d: %c\n", i, char)
end;
end;

// 0: F
// 1: o
// 2: o
// 3: b
// 4: a
// 5: r

will replace:

.. code-block:: dylan

let s = make(<string-stream>, contents: "Foobar", direction: #"input");
for (i from 0,
char = read-element(s, on-end-of-stream: #f)
then read-element(s, on-end-of-stream: #f),
while: char)
format-out("%d: %c\n", i, char)
end;
close(s);

Locking streams
^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -2779,6 +2840,90 @@ are exported from the *streams* module.
* :class:`<indenting-stream>`
* :gf:`indent`

.. macro:: with-input-from-string
:statement:

Creates an input stream, to perform operations on it and closes the
stream.


:macrocall:
.. parsed-literal::

with-input-from-string (name = string)
body
end

.. parsed-literal::

with-input-from-string (name = string, classname)
body
end

:parameter stream-name: A Dylan variable-name *bnf*.
:parameter string-expression: A Dylan string expression *bnf*.
:parameter classname: A Dylan class.
:parameter body: A Dylan body *bnf*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of consistency issues:

  • The examples use "name" but the parameter is stream-name.
  • classname should use a hyphen to match stream-name and Dylan naming style in general.


:description:

In the first use, it creates a :class:`<string-stream>` with
direction `#"input"` that is associated with a variable name. In
the second use, it allows to specify a class for the stream type.

:example:

.. code-block:: dylan

with-input-from-string (stream = "Foobar")
for (i from 0,
char = read-element(stream, on-end-of-stream: #f)
then read-element(stream, on-end-of-stream: #f),
while: char)
format-out("%d: %c\n", i, char)
end;
end;

:see-also:
:macro:`with-output-to-string`

.. macro:: with-output-to-string
:statement:

Provides a convenient way of returning a :class:`<string>` after
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For names that are exported from the dylan module (i.e., defined in the DRM) use :drm: instead of :class:. (There are a few other places where this needs to be fixed.)

performing a series of operations on a :class:`<stream>`, and then
close the stream.

:macrocall:
.. parsed-literal::

with-output-to-string (`stream-name`)
`body`
end => `string`

:parameter stream-name: A Dylan variable-name *bnf*.
:parameter body: A Dylan body *bnf*.
:value string: Instance of :class:`<string>`.
:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra colon?


:description:

Creates a :class:`<string-stream>` with direction `#"output"` and
provides an opportunity to perform operations and close the
stream.

:example:

.. code-block:: dylan

with-output-string (stream)
print-document(document, stream)
end;

:see-also:
:macro:`with-input-from-string`


.. macro:: with-stream-locked
:statement:

Expand Down
Loading