76 lines
8.7 KiB
HTML
76 lines
8.7 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Macro WITH-OUTPUT-TO-STRING</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="m_w_in_f.htm">
|
|
<LINK REL=UP HREF="c_stream.htm">
|
|
<LINK REL=NEXT HREF="v_debug_.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="m_w_in_f.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_stream.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="v_debug_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="with-output-to-string"><I>Macro</I> <B>WITH-OUTPUT-TO-STRING</B></A> <P>
|
|
<P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>with-output-to-string</B> <I>(var <TT>&optional</TT> string-form <TT>&key</TT> element-type) <I>declaration</I><B>*</B> <I>form</I><B>*</B></I><P> => <I><I>result</I><B>*</B></I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>var</I>---a <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A>. <P>
|
|
<I>string-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>; if <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, evaluated to produce <I>string</I>. <P>
|
|
<I>string</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A> that has a <A REL=DEFINITION HREF="26_glo_f.htm#fill_pointer"><I>fill pointer</I></A>. <P>
|
|
<I>element-type</I>---a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>; evaluated. The default is <A REL=DEFINITION HREF="t_ch.htm#character"><B>character</B></A>. <P>
|
|
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
|
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
|
<I>results</I>---If a <I>string-form</I> is not supplied or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A>; otherwise, the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <I>forms</I>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#with-output-to-string"><B>with-output-to-string</B></A> creates a character <A REL=DEFINITION HREF="26_glo_o.htm#output"><I>output</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>, performs a series of operations that may send results to this <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>, and then closes the <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>. <P>
|
|
The <I>element-type</I> names the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> of the elements of the <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>; a <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A> is constructed of the most specialized <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> that can accommodate elements of the given <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>. <P>
|
|
The body is executed as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A> with <I>var</I> bound to an <A REL=DEFINITION HREF="26_glo_o.htm#output"><I>output</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#string_stream"><I>string stream</I></A>. All output to that <A REL=DEFINITION HREF="26_glo_s.htm#string_stream"><I>string stream</I></A> is saved in a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A>. <P>
|
|
If <I>string</I> is supplied, <I>element-type</I> is ignored, and the output is incrementally appended to <I>string</I> as if by use of <A REL=DEFINITION HREF="f_vec_ps.htm#vector-push-extend"><B>vector-push-extend</B></A>. <P>
|
|
The <A REL=DEFINITION HREF="26_glo_o.htm#output"><I>output</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A> is automatically closed on exit from <B>with-output-from-string</B>, no matter whether the exit is normal or abnormal. The <A REL=DEFINITION HREF="26_glo_o.htm#output"><I>output</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#string_stream"><I>string stream</I></A> to which the <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <I>var</I> is <A REL=DEFINITION HREF="26_glo_b.htm#bound"><I>bound</I></A> has <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_extent"><I>dynamic extent</I></A>; its <A REL=DEFINITION HREF="26_glo_e.htm#extent"><I>extent</I></A> ends when the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is exited. <P>
|
|
If no <I>string</I> is provided, then <B>with-output-from-string</B> produces a <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A> that accepts characters and returns a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A> of the indicated <I>element-type</I>. If <I>string</I> is provided, <A REL=DEFINITION HREF="#with-output-to-string"><B>with-output-to-string</B></A> returns the results of evaluating the last <I>form</I>. <P>
|
|
The consequences are undefined if an attempt is made to <A REL=DEFINITION HREF="26_glo_a.htm#assign"><I>assign</I></A> the <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <I>var</I>. <P>
|
|
<P><B>Examples:</B><P>
|
|
|
|
<PRE>
|
|
(setq fstr (make-array '(0) :element-type 'base-char
|
|
:fill-pointer 0 :adjustable t)) => ""
|
|
(with-output-to-string (s fstr)
|
|
(format s "here's some output")
|
|
(input-stream-p s)) => <A REL=DEFINITION HREF="26_glo_f.htm#false">false</A>
|
|
fstr => "here's some output"
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Side Effects:</B><P>
|
|
<P>
|
|
The <I>string</I> is modified. <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
The consequences are undefined if destructive modifications are performed directly on the <I>string</I> during the <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_extent"><I>dynamic extent</I></A> of the call. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="f_mk_s_2.htm#make-string-output-stream"><B>make-string-output-stream</B></A>, <A REL=DEFINITION HREF="f_vec_ps.htm#vector-push-extend"><B>vector-push-extend</B></A>, <A REL=CHILD HREF="03_f.htm">Section 3.6 (Traversal Rules and Side Effects)</A> <P>
|
|
<P><B>Notes:</B> None.
|
|
<P>
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss240.htm">MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE</A><LI> <A REL=CHILD HREF="../Issues/iss330.htm">STRING-OUTPUT-STREAM-BASHING:UNDEFINED</A><LI> <A REL=CHILD HREF="../Issues/iss362.htm">WITH-OPEN-FILE-SETQ:EXPLICITLY-VAGUE</A><LI> <A REL=CHILD HREF="../Issues/iss363.htm">WITH-OPEN-FILE-STREAM-EXTENT:DYNAMIC-EXTENT</A><LI> <A REL=CHILD HREF="../Issues/iss364.htm">WITH-OUTPUT-TO-STRING-APPEND-STYLE:VECTOR-PUSH-EXTEND</A><LI> <A REL=CHILD HREF="../Issues/iss327.htm">STREAM-ACCESS:ADD-TYPES-ACCESSORS</A><LI> <A REL=CHILD HREF="../Issues/iss035.htm">CHARACTER-PROPOSAL:2-3-6</A><LI> <A REL=CHILD HREF="../Issues/iss040.htm">CHARACTER-PROPOSAL:2-5-6</A><LI> <A REL=CHILD HREF="../Issues/iss097.htm">DECLS-AND-DOC</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|