;; String chomp that copies behavior from Ruby. (1.01)
(define (string-drop-suffix s pat)
(string-drop-right s (string-suffix-length s pat)))
(define (chomp-newline s)
(string-fold (lambda (c s)
(string-drop-suffix s (string c)))
s "\n\r"))
(define (chomp-all-newlines s)
(let ((n (string-suffix-length s "\r\n")))
(if (zero? n)
s
(chomp-all-newlines (string-drop-right s n)))))
(define* (chomp s #:optional (sep '()))
(cond
((null? sep)
(chomp-newline s))
((string-null? sep)
(chomp-all-newlines s))
(else
(string-drop-suffix s sep))))
;; Show.
(format #t "~S~%" (chomp "hello")) ;=> "hello"
(format #t "~S~%" (chomp "hello\n")) ;=> "hello"
(format #t "~S~%" (chomp "hello\r\n")) ;=> "hello"
(format #t "~S~%" (chomp "hello\n\r")) ;=> "hello\n"
(format #t "~S~%" (chomp "hello\r")) ;=> "hello"
(format #t "~S~%" (chomp "hello \n there")) ;=> "hello \n there"
(format #t "~S~%" (chomp "hello" "llo")) ;=> "he"
(format #t "~S~%" (chomp "hello\r\n\r\n\n" "")) ;=> "hello"
(format #t "~S~%" (chomp "hello\r\n\r\r\n" "")) ;=> "hello\r\n\r"