Guile Library

(string transform)

Overview

Module (string transform) provides functions for modifying strings beyond that which is provided in the guile core and (srfi srfi-13).

Usage

escape-special-chars a b c
[Function]

Returns a copy of str with all given special characters preceded by the given escape-char.

special-chars can either be a single character, or a string consisting of all the special characters.

;; make a string regexp-safe...
 (escape-special-chars "***(Example String)***"  
                      "[]()/*." 
                      #\\)
=> "\\*\\*\\*\\(Example String\\)\\*\\*\\*"

;; also can escape a singe char...
 (escape-special-chars "richardt@vzavenue.net"
                      #\@
                      #\@)
=> "richardt@@vzavenue.net"
transform-string _ _ _ [_] [_]
[Function]

Uses match? against each character in str, and performs a replacement on each character for which matches are found.

match? may either be a function, a character, a string, or #t. If match? is a function, then it takes a single character as input, and should return #t for matches. match? is a character, it is compared to each string character using char=?. If match? is a string, then any character in that string will be considered a match. #t will cause every character to be a match.

If replace is a function, it is called with the matched character as an argument, and the returned value is sent to the output string via display. If replace is anything else, it is sent through the output string via display.

Note that te replacement for the matched characters does not need to be a single character. That is what differentiates this function from string-map, and what makes it useful for applications such as converting #\& to "&" in web page text. Some other functions in this module are just wrappers around common uses of transform-string. Transformations not possible with this function should probably be done with regular expressions.

If start and end are given, they control which portion of the string undergoes transformation. The entire input string is still output, though. So, if start is 5, then the first five characters of str will still appear in the returned string.

; these two are equivalent...
 (transform-string str #\space #\-) ; change all spaces to -'s
 (transform-string str (lambda (c) (char=? #\space c)) #\-)
expand-tabs _ [_]
[Function]

Returns a copy of str with all tabs expanded to spaces. tab-size defaults to 8.

Assuming tab size of 8, this is equivalent to:

 (transform-string str #\tab "        ")
center-string _ [_] [_] [_]
[Function]

Returns a copy of str centered in a field of width characters. Any needed padding is done by character chr, which defaults to #\space. If rchr is provided, then the padding to the right will use it instead. See the examples below. left and rchr on the right. The default width is 80. The default lchr and rchr is #\space. The string is never truncated.

 (center-string "Richard Todd" 24)
=> "      Richard Todd      "

 (center-string " Richard Todd " 24 #\=)
=> "===== Richard Todd ====="

 (center-string " Richard Todd " 24 #\< #\>)
=> "<<<<< Richard Todd >>>>>"
left-justify-string _ [_] [_]
[Function]

left-justify-string str [width chr]. Returns a copy of str padded with chr such that it is left justified in a field of width characters. The default width is 80. Unlike string-pad from srfi-13, the string is never truncated.

right-justify-string _ [_] [_]
[Function]

Returns a copy of str padded with chr such that it is right justified in a field of width characters. The default width is 80. The default chr is #\space. Unlike string-pad from srfi-13, the string is never truncated.

collapse-repeated-chars _ [_] [_]
[Function]

Returns a copy of str with all repeated instances of chr collapsed down to at most num instances. The default value for chr is #\space, and the default value for num is 1.

 (collapse-repeated-chars "H  e  l  l  o")
=> "H e l l o"
 (collapse-repeated-chars "H--e--l--l--o" #\-)
=> "H-e-l-l-o"
 (collapse-repeated-chars "H-e--l---l----o" #\- 2)
=> "H-e--l--l--o"