string-utils.scm 901 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. (define-module (string-utils)
  2. #:export (char->string
  3. string->char
  4. has-prefix?))
  5. (use-modules
  6. (ice-9 exceptions))
  7. (define char->string
  8. (λ (c)
  9. (list->string
  10. (list c))))
  11. (define string->char
  12. (λ (str)
  13. "Convert a string, which has only one single character
  14. into a character. This is useful, because some functions
  15. expect a characters as input instead of a string."
  16. (cond
  17. [(= (string-length str) 1)
  18. (car (string->list str))]
  19. [else
  20. (raise-exception
  21. (make-exception
  22. (make-non-continuable-error)
  23. (make-exception-with-message "trying to convert string of more than 1 character to char")
  24. (make-exception-with-irritants (list str))
  25. (make-exception-with-origin 'string->char)))])))
  26. (define has-prefix?
  27. (λ (str prefix)
  28. (= (string-prefix-length str prefix)
  29. (string-length prefix))))