more formatting etc

svn: r5048

original commit: 680c0f419a
This commit is contained in:
Eli Barzilay 2006-12-06 21:44:21 +00:00
parent c276d448fe
commit df4458f899

View File

@ -38,30 +38,27 @@
;; string-index returns the leftmost index in string s
;; that has character c
(define string-index
(lambda (s c)
(define (string-index s c)
(let ([n (string-length s)])
(let loop ([i 0])
(cond [(>= i n) #f]
[(char=? (string-ref s i) c) i]
[else (loop (+ i 1))])))))
[else (loop (+ i 1))]))))
;; string-tokenizer breaks string s into substrings separated by character c
(define string-tokenizer
(lambda (c s)
(define (string-tokenizer c s)
(let loop ([s s])
(if (string=? s "") '()
(let ([i (string-index s c)])
(if i (cons (substring s 0 i)
(loop (substring s (+ i 1)
(string-length s))))
(list s)))))))
(list s))))))
;; Trim all spaces, except those in quoted strings.
(define re:quote-start (regexp "\""))
(define re:space (regexp "[ \t\n\r\v]"))
(define trim-all-spaces
(lambda (str)
(define (trim-all-spaces str)
;; Break out alternate quoted and unquoted parts.
;; Initial and final string are unquoted.
(let-values ([(unquoted quoted)
@ -88,34 +85,29 @@
(list clean)
(list* clean
(car quoted)
(loop (cdr unquoted) (cdr quoted))))))))))
(loop (cdr unquoted) (cdr quoted)))))))))
;; Only trims left and right spaces:
(define trim-spaces
(lambda (str)
(trim-right (trim-left str))))
(define (trim-spaces str)
(trim-right (trim-left str)))
(define re:left-spaces (regexp "^[ \t\r\n\v]+"))
(define trim-left
(lambda (str)
(regexp-replace re:left-spaces str "")))
(define (trim-left str)
(regexp-replace re:left-spaces str ""))
(define re:right-spaces (regexp "[ \t\r\n\v]+$"))
(define trim-right
(lambda (str)
(regexp-replace re:right-spaces str "")))
(define (trim-right str)
(regexp-replace re:right-spaces str ""))
(define re:comments (regexp "^[^\"]*(\"[^\"]*\")*[^\"]*(\\(.*\\))"))
(define trim-comments
(lambda (str)
(define (trim-comments str)
(let ([positions (regexp-match-positions re:comments str)])
(if positions
(string-append (substring str 0 (caaddr positions))
(substring str (cdaddr positions) (string-length str)))
str))))
str)))
(define lowercase
(lambda (str)
(define (lowercase str)
(let loop ([out ""] [rest str] [size (string-length str)])
(cond [(zero? size) out]
[else
@ -123,7 +115,7 @@
(char-downcase
(string-ref rest 0))))
(substring rest 1 size)
(sub1 size))]))))
(sub1 size))])))
(define warning
void