fork(2) download
  1. ; trailing comments
  2.  
  3. (define (tc? str) ; true when a trailing comment is found, else false
  4. (let loop ((xs (string->list str)) (inquotes #f)) ; initialize loop
  5. (cond ((null? xs) #f) ; reached end-of-line without comment
  6. ((and inquotes (char=? (car xs) #\")) ; end of quote
  7. (loop (cdr xs) #f)) ; initialize for next quoted string
  8. ((and inquotes (char=? (car xs) #\\) (pair? (cdr xs))) ; escape
  9. (loop (cddr xs) #t)) ; skip backslash and escaped character
  10. (inquotes (loop (cdr xs) #t)) ; continue quote
  11. ((char=? (car xs) #\") (loop (cdr xs) #t)) ; start quote
  12. ((char=? (car xs) #\#) #t) ; found trailing comment
  13. (else (loop (cdr xs) #f))))) ; next character
  14.  
  15. (display (tc? "x = \"# This is fine\"")) (newline)
  16. (display (tc? "\"This \\# is fine too\"")) (newline)
  17. (display (tc? "x = \"\" # This is not\"")) (newline)
Success #stdin #stdout 0s 7992KB
stdin
Standard input is empty
stdout
#f
#f
#t