1
0
Fork 0
cl-sites/novaspec.org/cl/f_with-output-to-string.html
2025-02-05 18:52:26 +01:00

506 lines
No EOL
10 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>with-output-to-string | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_with-output-to-string.html"
><LINK REL="next" HREF="v_debug-io.html" TYPE="text/html" TITLE="*debug-io*, *error-output*, *query-io*, *standard-input*, *standard-output*, *trace-output*"
><LINK REL="prev" HREF="f_with-input-from-string.html" TYPE="text/html" TITLE="with-input-from-string"
><LINK REL="up" HREF="21_2_Streams_Dictionary.html" TYPE="text/html" TITLE="21.2 Streams Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="21_Streams.html"
>21. Streams</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="21_2_Streams_Dictionary.html"
>21.2 Streams Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_with-output-to-string.html"
>with-output-to-string</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_with-input-from-string.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="v_debug-io.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="with-output-to-string"
></SPAN
><B
>with-output-to-string</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Macro</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><DIV CLASS="DefmacWithValuesNewline"
><DIV
><B
>with-output-to-string</B
> <SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmssi"
>var</SPAN
> <SPAN CLASS="cmtt"
>&amp;optional</SPAN
> <SPAN CLASS="cmssi"
>string-form</SPAN
> <SPAN CLASS="cmtt"
>&amp;key</SPAN
> <SPAN CLASS="cmssi"
>element-type</SPAN
><SPAN CLASS="cmr"
>)</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>declaration</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>form</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> </DIV
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>result</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
></DIV
></DIV
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>var</VAR
> &#8212; a <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>string-form</VAR
> &#8212; a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>; if <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
>, evaluated to produce <VAR CLASS="param"
>string</VAR
>. </P
><P CLASS="j"
><VAR CLASS="param"
>string</VAR
> &#8212; a <A HREF="26_1_Glossary.html#string"
><EM CLASS="term"
>string</EM
></A
> that has a <A HREF="26_1_Glossary.html#fill_pointer"
><EM CLASS="term"
>fill pointer</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>element-type</VAR
> &#8212; a <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
>; evaluated. The default is <A HREF="t_character.html" CLASS="typeref"
><B
>character</B
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>declaration</VAR
> &#8212; a <A HREF="m_declare.html" CLASS="misc"
><B
>declare</B
></A
> <A HREF="26_1_Glossary.html#expression"
><EM CLASS="term"
>expression</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>forms</VAR
> &#8212; an <A HREF="26_1_Glossary.html#implicit_progn"
><EM CLASS="term"
>implicit progn</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>results</VAR
> &#8212; If a <VAR CLASS="param"
>string-form</VAR
> is not supplied or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, a <A HREF="26_1_Glossary.html#string"
><EM CLASS="term"
>string</EM
></A
>; otherwise, the <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> returned by the <VAR CLASS="param"
>forms</VAR
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_with-output-to-string.html" CLASS="macref"
><B
>with-output-to-string</B
></A
> creates a character <A HREF="26_1_Glossary.html#output"
><EM CLASS="term"
>output</EM
></A
> <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
>, performs a series of operations that may send results to this <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
>, and then closes the <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
>. </P
><P CLASS="j"
>The <VAR CLASS="param"
>element-type</VAR
> names the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of the elements of the <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
>; a <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
> is constructed of the most specialized <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> that can accommodate elements of the given <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>. </P
><P CLASS="j"
>The body is executed as an <A HREF="26_1_Glossary.html#implicit_progn"
><EM CLASS="term"
>implicit progn</EM
></A
> with <VAR CLASS="param"
>var</VAR
> bound to an <A HREF="26_1_Glossary.html#output"
><EM CLASS="term"
>output</EM
></A
> <A HREF="26_1_Glossary.html#string_stream"
><EM CLASS="term"
>string stream</EM
></A
>. All output to that <A HREF="26_1_Glossary.html#string_stream"
><EM CLASS="term"
>string stream</EM
></A
> is saved in a <A HREF="26_1_Glossary.html#string"
><EM CLASS="term"
>string</EM
></A
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>string</VAR
> is supplied, <VAR CLASS="param"
>element-type</VAR
> is ignored, and the output is incrementally appended to <VAR CLASS="param"
>string</VAR
> as if by use of <A HREF="f_vector-push.html" CLASS="funref"
><B
>vector-push-extend</B
></A
>. </P
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#output"
><EM CLASS="term"
>output</EM
></A
> <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
> is automatically closed on exit from <A HREF="f_with-output-to-string.html" CLASS="macref"
><B
>with-output-to-string</B
></A
>, no matter whether the exit is normal or abnormal. The <A HREF="26_1_Glossary.html#output"
><EM CLASS="term"
>output</EM
></A
> <A HREF="26_1_Glossary.html#string_stream"
><EM CLASS="term"
>string stream</EM
></A
> to which the <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <VAR CLASS="param"
>var</VAR
> is <A HREF="26_1_Glossary.html#bound"
><EM CLASS="term"
>bound</EM
></A
> has <A HREF="26_1_Glossary.html#dynamic_extent"
><EM CLASS="term"
>dynamic extent</EM
></A
>; its <A HREF="26_1_Glossary.html#extent"
><EM CLASS="term"
>extent</EM
></A
> ends when the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is exited. </P
><P CLASS="j"
>If no <VAR CLASS="param"
>string</VAR
> is provided, then <A HREF="f_with-output-to-string.html" CLASS="macref"
><B
>with-output-to-string</B
></A
> produces a <A HREF="26_1_Glossary.html#stream"
><EM CLASS="term"
>stream</EM
></A
> that accepts characters and returns a <A HREF="26_1_Glossary.html#string"
><EM CLASS="term"
>string</EM
></A
> of the indicated <VAR CLASS="param"
>element-type</VAR
>. If <VAR CLASS="param"
>string</VAR
> is provided, <A HREF="f_with-output-to-string.html" CLASS="macref"
><B
>with-output-to-string</B
></A
> returns the results of evaluating the last <VAR CLASS="param"
>form</VAR
>. </P
><P CLASS="j"
>The consequences are undefined if an attempt is made to <A HREF="26_1_Glossary.html#assign"
><EM CLASS="term"
>assign</EM
></A
> the <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <VAR CLASS="param"
>var</VAR
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(setq fstr (make-array '(0) :element-type 'base-char
:fill-pointer 0 :adjustable t)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ""
(with-output-to-string (s fstr)
(format s "here's some output")
(input-stream-p s)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
fstr <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "here's some output"</PRE
></DD
><DT
><B
>Side Effects</B
></DT
><DD
><P CLASS="j"
>The <VAR CLASS="param"
>string</VAR
> is modified.</P
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>The consequences are undefined if destructive modifications are performed directly on the <VAR CLASS="param"
>string</VAR
> during the <A HREF="26_1_Glossary.html#dynamic_extent"
><EM CLASS="term"
>dynamic extent</EM
></A
> of the call.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_make-string-output-stream.html" CLASS="funref"
><B
>make-string-output-stream</B
></A
>, <A HREF="f_vector-push.html" CLASS="funref"
><B
>vector-push-extend</B
></A
>, <A HREF="3_6_Traversal_Rules_and_Side_Effects.html#sec_3_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.6</SPAN
> <SPAN CLASS="cmr"
>(Traversal</SPAN
> <SPAN CLASS="cmr"
>Rules</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Side</SPAN
> <SPAN CLASS="cmr"
>Effects)</SPAN
></A
></P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_with-input-from-string.html" CLASS="prev"
>&#8592;</A
><A HREF="v_debug-io.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>