unformat->proc

Name

unformat->proc -- Constructs an "unformat" procedure

Type: function

Synopsis

unformat->proc( format-string, proc);

Arguments

format-string

A format string.

anywhere?

An optional <boolean>. Default value is #f.

Return Values

proc

An instance of <function>.

Description

This function constructs a procedure which will do the inverse of format when applied to a string.

However, the format-string can only contain the following format specifiers, and no modifiers are currently supported:

specifiermeaning
~sconsume as much input as possible and interpret it using read.
~aconsume as much input as possible, returning it as a string.
~dconsume a sequence of digit and dot characters and interpret it using string->number.

The way this function (usually) works is by using the format string to build a regular expression, using the regex binding facility to extract substrings corresponding to the format specifiers in format-string. The function returned by this function invokes the regular expression matcher on it's argument, and, if it matches, maps the appropriate interpretation procedures (e.g., string->number) over the substrings.

If the returned function does not find a match, it returns no values.

If the format string contains no format characters, then an error is signaled by unformat->proc, since there will be no way to distinguish success from failure cases of invocations of proc.

If anywhere? is #t, then the returned procedure will try to find something that matches anywhere inside the argument string. In addition, it will return two additional values (the first two), which are the start and ending offset in it's given string of the substring that matched. Otherwise, by default, the returned procedure will require an exact match between the format string and it's argument string.

(define p (unformat->proc "foo(~d,~a)"))

p a <function>

(p "blah")

(p "foo(10,hello)") 10 "hello"

As usual, you can use bind to capture the multiple values from proc.