unroff-website/www/doc/m-11.html

232 lines
7.2 KiB
HTML

<html>
<head>
<!-- This file has been generated by unroff 1.0, 03/21/96 19:29:46. -->
<!-- Do not edit! -->
<link rev="made" href="mailto:net@informatik.uni-bremen.de">
<title>unroff Programmer's Manual, section 11.</title>
</head><body>
<h2><a name="section11">11.</a>&#160;<tt> </tt>String Functions
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>Most of the string handling primitives described in this section
could as well have been implemented in Scheme based on the standard
Scheme string primitives.<tt> </tt>
They are provided as built-in primitives by
<i>unroff
</i>mainly as optimizations or because writing them as Scheme
procedures would have been significantly more cumbersome.<tt> </tt>
All the string functions return new strings, that is, they
do not modify their arguments.<tt> </tt>
<h2>
(concat <i>.</i> <i>args</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt><i>concat
</i>can be called with any number of Scheme strings, symbols, and
characters.<tt> </tt>
The primitive concatenates its arguments and returns the result
as a string.<tt> </tt>
<h2>
(spread)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>This primitive is identical to
<i>concat</i>,
except that it delimits its arguments by a space character.<tt> </tt>
For example, the event procedure for a macro that just
returns a line consisting of its arguments could be define like this:
<dl><dt><dd>
<pre>
(defmacro 'X
(lambda (X . words)
(parse (apply spread words) #\newline)))
</pre>
</dl>
<h2>
(repeat-string <i>num</i> <i>string</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>Returns a string consisting of the string argument
<i>string
</i>repeated
<i>num
</i>times.<tt> </tt>
<h2>
(string-prune-left <i>string</i> <i>prefix</i> <i>fail</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>This primitive checks whether
<i>string
</i>starts with the given string prefix, and if so, returns the rest of
<i>string
</i>beginning at the first character position after the initial prefix.<tt> </tt>
If the strings do not match,
<i>fail
</i>is returned (which may an arbitrary object).<tt> </tt>
Example:
<dl><dt><dd>
<pre>
(string-prune-left "+foo" "+" #f) =&gt; "foo"
(string-prune-left "gulp" "+" #f) =&gt; #f
</pre>
</dl>
<h2>
(string-prune-right <i>string</i> <i>suffix</i> <i>fail</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>This primitive is identical to
<i>string-prune-left</i>,
except that it checks for a suffix rather than a prefix,
that is, whether
<i>string
</i>ends with
<i>suffix</i>.<tt> </tt>
<h2>
(string-compose <i>string1</i> <i>string2</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>If the argument
<i>string2
</i>begins with a plus sign,
<i>string-compose
</i>returns the concatenation of
<i>string1
</i>and
<i>string2
</i>with the initial plus sign stripped.<tt> </tt>
If
<i>string2
</i>begins with a minus sign,
it returns a string consisting of
<i>string1
</i>with all characters occurring in
<i>string2
</i>removed.<tt> </tt>
Otherwise,
<i>string-compose
</i>just returns
<i>string2</i>.<tt> </tt>
This primitive is used for the implementation of the option type
<i>dynstring</i>.<tt> </tt>
<h2>
(parse-pair <i>string</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>If
<i>string
</i>consists of two parts separated and enclosed by an arbitrary delimiter
character,
<i>parse-pair
</i>returns a cons cell holding the two substrings.<tt> </tt>
Otherwise, it returns #f.<tt> </tt>
Example:
<dl><dt><dd>
<pre>
(parse-pair "'foo'bar'") =&gt; ("foo" . "bar")
(parse-pair "hello") =&gt; #f
</pre>
</dl>
<h2>
(parse-triple <i>string</i>)
</h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>This primitive is identical to
<i>parse-pair</i>,
except that it breaks up a three-part string rather than a
two-part string and returns an improper list whose car, cadr,
and cddr consist of the three substrings<a href="m-notes.html#footnote5">[note 5]</a>
.<tt> </tt>
<i>parse-pair
</i>and
<i>parse-triple
</i>are useful mainly for parsing the arguments to troff requests such
as ``.if'' and ``.tl''.<tt> </tt>
<h2>
<a name=".substitute">(substitute <i>string</i> <i>.</i> <i>args</i>)</a></h2>
<p>
&#160;<tt> </tt>&#160;<tt> </tt>&#160;<tt> </tt>This primitive returns a copy of
<i>string
</i>in which each sequence of a percent sign, a
<i>substitution specifier</i>,
and another percent sign is replaced by another string according
to the specifier.<tt> </tt>
Two adjacent percent signs are replaced by a single percent sign.<tt> </tt>
The following list describes all substitution specifiers together
with their respective replacements.<tt> </tt>
<dl>
<dt><b>macros</b><dd>
The name of the troff macro package whose macros are recognized,
that is, the argument to the option
<b>-m
</b>(or the empty string if none was specified).<tt> </tt>
<dt><b>format</b><dd>
The output format, that is, the argument to the option
<b>-f
</b>(or the default output format if the option was omitted).<tt> </tt>
<dt><b>directory</b><dd>
The name of the library directory from which
<i>unroff
</i>loads its Scheme files.<tt> </tt>
<dt><b>progname</b><dd>
The name of the running program (this is used as a prefix in
error messages and warning messages).<tt> </tt>
<dt><b>filepos</b><dd>
A space character followed by the target of the current input
stream, a colon, the number of the last input line read from
the stream, and another colon.<tt> </tt>
If the current input stream is bound to #f, the empty string
is substituted.<tt> </tt>
This specifier is useful for displaying error messages or warning messages.<tt> </tt>
<dt><b>tmpname</b><dd>
A file name that can be used for a temporary file.<tt> </tt>
Each use of this specifier creates a new, unique file name.<tt> </tt>
<dt><b>version</b><dd>
The program's major and minor version numbers separated by a period.<tt> </tt>
<dt><b>weekday</b><dd>
The abbreviated weekday name.<tt> </tt>
<dt><b>weekday+</b><dd>
The full weekday name.<tt> </tt>
<dt><b>weekdaynum</b><dd>
The weekday (0-6, Sunday is 0).<tt> </tt>
<dt><b>monthname</b><dd>
The abbreviated month name.<tt> </tt>
<dt><b>monthname+</b><dd>
The full monthname.<tt> </tt>
<dt><b>day</b><dd>
The day of the month (01-31).<tt> </tt>
<dt><b>month</b><dd>
The month (01-12).<tt> </tt>
<dt><b>year</b><dd>
The year.<tt> </tt>
<dt><b>date</b><dd>
The date (in the local environment's representation).<tt> </tt>
<dt><b>time</b><dd>
The time (in the local environment's representation).<tt> </tt>
<dt>a positive number <i>n</i><dd>
The
<i>n</i>th
additional argument in the call to the
<i>substitute
</i>primitive, which must be a string.<tt> </tt>
<dt>a <i>string</i><dd>
<i>string
</i>is interpreted as the name of an environment variable,
and the value of this variable is substituted (or the empty
string if the environment variable is undefined).<tt> </tt>
</dl>
<p>
Examples:
<dl><dt><dd>
<pre>
(substitute "%date% %HOME%") =&gt; "04/09/95 /home/kbs/net"
(substitute "%progname%:%filepos% %1%" "hello")
=&gt; "unroff: manual.ms:21: hello"
(load (substitute "%directory%/scm/%format%/m%macros%.scm"))
</pre>
</dl>
<p><hr>
Markup created by <em>unroff</em> 1.0,&#160;<tt> </tt>&#160;<tt> </tt>March 21, 1996,&#160;<tt> </tt>&#160;<tt> </tt>net@informatik.uni-bremen.de</body>
</html>