Next: , Previous: File Ports, Up: Port Types


27.10.2 String Ports

The following allow string ports to be opened by analogy to R4R* file port facilities:

— Scheme Procedure: call-with-output-string proc
— C Function: scm_call_with_output_string (proc)

Call the one-argument procedure proc with a newly created output port. When the procedure returns, return the string composed of the characters it has written into the port.

— Scheme Procedure: call-with-input-string str proc
— C Function: scm_call_with_input_string (str, proc)

Call the one-argument procedure proc with a newly created input port from which the contents of string str's may be read. The value yielded by the proc is returned.

— Scheme Procedure: with-output-to-string thunk

Call thunk and return its output as a string.

— Scheme Procedure: with-input-from-string string thunk

Connect an input port to string and call thunk with no arguments. During the call, current-input-port returns an input port connected to the text of string. When the thunk returns, close the port. Return the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, behavior is implementation dependent.

— Scheme Procedure: open-input-string str
— C Function: scm_open_input_string (str)

Return an input port that delivers characters from the string str. The port can be closed by close-input-port, though its storage will be reclaimed by the garbage collector if it becomes inaccessible.

— Scheme Procedure: open-output-string
— C Function: scm_open_output_string ()

Return an output port that will accumulate characters for retrieval by get-output-string. The port can be closed by the procedure close-output-port, though its storage will be reclaimed by the garbage collector if it becomes inaccessible.

— Scheme Procedure: get-output-string port
— C Function: scm_get_output_string (port)

Given an output port created by open-output-string, return a string consisting of the characters that have been output to the port so far.

A string port can be used in many procedures which accept a port but which are not dependent on implementation details of fports. E.g., seeking and truncating will work on a string port, but trying to extract the file descriptor number will fail.