fork(1) download
  1. ;; String chomp that copies behavior from Ruby programming language.
  2.  
  3. (define (delete-match-right s pat)
  4. (string-drop-right s (string-suffix-length s pat)))
  5.  
  6. (define (chomp-chars s lc)
  7. (if (null? lc)
  8. s
  9. (chomp-chars (delete-match-right s (string (car lc))) (cdr lc))))
  10.  
  11. (define (chomp-trailing-nl s)
  12. (let ((n (string-suffix-length s "\r\n")))
  13. (if (zero? n)
  14. s
  15. (chomp-trailing-nl (string-drop-right s n)))))
  16.  
  17. (define* (chomp s #:optional (sep '(#\newline #\return)))
  18. (cond
  19. ((list? sep)
  20. (chomp-chars s sep))
  21. ((string-null? sep)
  22. (chomp-trailing-nl s))
  23. (else
  24. (delete-match-right s sep))))
  25.  
  26. ;; Show.
  27.  
  28. (format #t "~S~%" (chomp "hello")) ;=> "hello"
  29. (format #t "~S~%" (chomp "hello\n")) ;=> "hello"
  30. (format #t "~S~%" (chomp "hello\r\n")) ;=> "hello"
  31. (format #t "~S~%" (chomp "hello\n\r")) ;=> "hello\n"
  32. (format #t "~S~%" (chomp "hello\r")) ;=> "hello"
  33. (format #t "~S~%" (chomp "hello \n there")) ;=> "hello \n there"
  34. (format #t "~S~%" (chomp "hello" "llo")) ;=> "he"
  35. (format #t "~S~%" (chomp "hello\r\n\r\n\n" "")) ;=> "hello"
  36. (format #t "~S~%" (chomp "hello\r\n\r\r\n" "")) ;=> "hello\r\n\r"
Success #stdin #stdout 0.01s 11016KB
stdin
Standard input is empty
stdout
"hello"
"hello"
"hello"
"hello\n"
"hello"
"hello \n there"
"he"
"hello"
"hello\r\n\r"