;;; File: ;;; slams.ss - Sam's Library for Arbitrarily Munging Strings ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 1.0 of September 2003 ;;; Summary: ;;; A simple library of string manipulation routines. ;;; Contents: ;;; Exported Procedures ;;; (char-alphanumeric? ch) ;;; Determine if ch is a letter or a number. ;;; (join list-of-strings) ;;; Join all the strings in to a single string. ;;; (split list-of-strings) ;;; Split a long string into a list of words and punctuation. ;;; Local Helpers ;;; History: ;;; At end. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Exported Procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Procedure: ;;; char-alphanumeric? ;;; Parameters: ;;; ch, a character ;;; Purpose: ;;; Determines if ch is an alphabetic or numeric character. ;;; Produces: ;;; is-alphanumeric, a truth value. ;;; Preconditions: ;;; [None] ;;; Postconditions: ;;; is-alphanumeric is true (#t) is ch is a character and either ch is ;;; alphabetic or ch is numeric. ;;; is-alphanumeric is false (#f) otherwise. (define char-alphanumeric? (lambda (ch) (and (char? ch) (or (char-alphabetic? ch) (char-numeric? ch))))) ;;; Procedure: ;;; join ;;; Parameters: ;;; strings, a list of strings of the form (str1 str2 ... strn) ;;; Purpose: ;;; Joins all the strings in strings into a single string. ;;; Produces: ;;; joined, a string ;;; Preconditions: ;;; [None] ;;; Postconditions: ;;; joined is (string-append str1 str2 ... strn) (define join (lambda (strings) (apply string-append strings))) ;;; Procedure: ;;; split ;;; Parameters: ;;; str, a string ;;; Purpose: ;;; "Splits" str into a list of strings, with one word (or symbol) per string. ;;; Produces: ;;; components, a list of strings. ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; (join components) is str ;;; For each element, s, of components, either (1) s contains only ;;; letters and numbers or (2) s contains exactly one non-alphanumeric. ;;; If an element of s contains only letters and numbers, the immediate ;;; successor and predecessor contain non-alphanumerics. (define split (lambda (str) (split-helper str 0 (- (string-length str) 1) null null))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Local Procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;;; ; Step through the string, building substrings. ; str: the string ; start: the current position ; end: the last position ; chars: all accumulated letters and numbers in current word (in rev. order) ; strings: all accumulated substrings (in reverse order) (define split-helper (lambda (str pos end chars strings) (cond ; If we ((> pos end) (reverse (string-cons (list->string (reverse chars)) strings))) ((char-alphanumeric? (string-ref str pos)) (split-helper str (+ pos 1) end (cons (string-ref str pos) chars) strings)) (else (split-helper str (+ pos 1) end null (cons (string (string-ref str pos)) (string-cons (list->string (reverse chars)) strings))))))) ;;; Procedure: ;;; string-cons ;;; Parameters: ;;; str, a string ;;; strings, a list of strings ;;; Purpose: ;;; Adds str to the front of strings only if str is not the empty string. ;;; Produces: ;;; new-strings, a list of strings (define string-cons (lambda (str strings) (if (string=? str "") strings (cons str strings))))