#
Chapter 40
`string.ss`: String Utilities

To load: `(require (lib "string.ss"))`

`(eval-string``` `str``` [`err-display err-result`]`)` P R O C E D U R E

Reads and evaluates S-expressions from the string `str`, returning a result for each expression. Note that if `str` contains only whitespace and comments, zero values are returned, while if `str` contains two expressions, two values are returned.

If `err-display` is not `#f` (the default), then errors are caught and `err-display` is used as the error display handler. If `err-result` is specified, it must be a thunk that returns a value to be returned when an error is caught; otherwise, `#f` is returned when an error is caught.

`(expr->string``` `expr``)` P R O C E D U R E

Prints `expr` into a string and returns the string.

`(read-from-string``` `str``` [`err-display err-result`]`)` P R O C E D U R E

Reads the first S-expression from the string `str` and returns it. The `err-display` and `err-result` are as in `eval-str`.

`(read-from-string-all``` `str``` [`err-display err-result`]`)` P R O C E D U R E

Reads all S-expressions from the string `str` and returns them in a list. The `err-display` and `err-result` are as in `eval-str`.

`(regexp-match*``` `pattern string-or-input-port``` [`start-k end-k`]`)` P R O C E D U R E

Like `regexp-match` (see section 10 in PLT MzScheme: Language Manual), but the result is a list of strings corresponding to a sequence of matches of `pattern` in `string-or-input-port`. (Unlike `regexp-match`, results for parenthesized sub-patterns in `pattern` are not returned.) If `pattern` matches a zero-length string along the way, the `exn:fail` exception is raised.

If `string-or-input-port` contains no matches (in the range `start-k` to `end-k`), `null` is returned. Otherwise, each string in the resulting list is a distinct substring in `string-or-input-port` that matches `pattern`. The `end-k` argument can be `#f` to match to the end of `string-or-input-port`.

`(regexp-match/fail-without-reading``` `pattern input-port``` [`start-k end-k output-port`]`)` P R O C E D U R E

Like `regexp-match` on input ports (see section 10 in PLT MzScheme: Language Manual), except that if the match fails, no characters are read and discarded from `input-port`.

This procedure is especially useful with a `pattern` that begins with a start-of-string caret (```^`'') or with a non-`#f` `end-k`, since each limits the amount of peeking into the port.

`(regexp-match-exact?``` `pattern string-or-input-port``)` P R O C E D U R E

This procedure is like MzScheme's built-in `regexp-match` (see section 10 in PLT MzScheme: Language Manual), but the result is always `#t` or `#f`; `#t` is only returned when the entire content of `string-or-input-port` matches `pattern`.

`(regexp-match-peek-positions*``` `pattern input-port``` [`start-k end-k`]`)` P R O C E D U R E

Like `regexp-match-positions*`, but it works only on input ports, and the port is peeked instead of read for matches.

`(regexp-match-positions*``` `pattern string-or-input-port``` [`start-k end-k`]`)` P R O C E D U R E

Like `regexp-match-positions` (see section 10 in PLT MzScheme: Language Manual), but the result is a list of integer pairs corresponding to a sequence of matches of `pattern` in `string-or-input-port`. (Unlike `regexp-match-positions`, results for parenthesized sub-patterns in `pattern` are not returned.) If `pattern` matches a zero-length string along the way, the `exn:fail` exception is raised.

If `string-or-input-port` contains no matches (in the range `start-k` to `end-k`), `null` is returned. Otherwise, each position pair in the resulting list corresponds to a distinct substring in `string-or-input-port` that matches `pattern`. The `end-k` argument can be `#f` to match to the end of `string-or-input-port`.

`(regexp-quote``` `str``` [`case-sensitive?`]`)` P R O C E D U R E

Produces a string suitable for use with ``regexp`` (see section 10 in PLT MzScheme: Language Manual) to match the literal sequence of characters in `str`. If `case-sensitive?` is true, the resulting regexp matches letters in `str` case-insensitively, otherwise (and by default) it matches case-sensitively.

`(regexp-replace-quote``` `str``)` P R O C E D U R E

Produces a string suitable for use as the third argument to ``regexp-replace`` (see section 10 in PLT MzScheme: Language Manual) to insert the literal sequence of characters in `str` as a replacement. Concretely, every backslash and ampersand in `str` is protected by a quoting backslash.

`(glob->regexp``` `str``` [`hide-dots? case-sensitive?`]`)` P R O C E D U R E

Produces a regexp for a an input ``glob pattern'' in `str`. A glob pattern is one that matches ```*`'' with any string, and ```?`'' with a single character, and character ranges are the same as in regexps. In addition, the resulting regexp does not match strings that begin with a period, unless explicitly part of the glob string. The resulting regexp can be used with string file names to check the glob pattern.

If `hide-dots?` is true (the default), the resulting regexp will not match names that begin with a dot.

If `case-sensitive?` is given, it determines whether the resulting regexp is case-sensitive; otherwise the default case sensitivity depends on the system-type.

`(regexp-split``` `pattern string-or-input-port``` [`start-k end-k`]`)` P R O C E D U R E

The complement of `regexp-match*` (see above): the result is a list of sub-strings in `string-or-input-port` that are separated by matches to `pattern`; adjacent matches are separated with `""`. If `pattern` matches a zero-length string along the way, the `exn:fail` exception is raised.

If `string-or-input-port` contains no matches (in the range `start-k` to `end-k`), the result will be a list containing `string-or-input-port` (from `start-k` to `end-k`). If a match occurs at the beginning of `string-or-input-port` (at `start-k`), the resulting list will start with an empty string, and if a match occurs at the end (at `end-k`), the list will end with an empty string. The `end-k` argument can be `#f`, in which case splitting goes to the end of `string-or-input-port`.

`(string-lowercase!``` `str``)` P R O C E D U R E

Destructively changes `str` to contain only lowercase characters.

`(string-uppercase!``` `str``)` P R O C E D U R E

Destructively changes `str` to contain only uppercase characters.