iconv.scm 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; Encoding and decoding byte representations of strings
  2. ;; Copyright (C) 2013, 2021 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (ice-9 iconv)
  18. #:use-module (rnrs bytevectors)
  19. #:use-module (ice-9 binary-ports)
  20. #:use-module ((ice-9 rdelim) #:select (read-string))
  21. #:export (string->bytevector
  22. bytevector->string
  23. call-with-encoded-output-string))
  24. (define* (call-with-encoded-output-string encoding proc
  25. #:optional
  26. (conversion-strategy 'error))
  27. "Call PROC on a fresh port. Encode the resulting string as a
  28. bytevector according to ENCODING, and return the bytevector."
  29. (if (and (string-ci=? encoding "utf-8")
  30. (eq? conversion-strategy 'error))
  31. ;; I don't know why, but this appears to be faster; at least for
  32. ;; serving examples/debug-sxml.scm (1464 reqs/s versus 850
  33. ;; reqs/s).
  34. (string->utf8 (call-with-output-string proc))
  35. (call-with-output-bytevector
  36. (lambda (port)
  37. (set-port-encoding! port encoding)
  38. (if conversion-strategy
  39. (set-port-conversion-strategy! port conversion-strategy))
  40. (proc port)))))
  41. ;; TODO: Provide C implementations that call scm_from_stringn and
  42. ;; friends?
  43. (define* (string->bytevector str encoding
  44. #:optional (conversion-strategy 'error))
  45. "Encode STRING according to ENCODING, which should be a string naming
  46. a character encoding, like \"utf-8\"."
  47. (if (and (string-ci=? encoding "utf-8")
  48. (eq? conversion-strategy 'error))
  49. (string->utf8 str)
  50. (call-with-encoded-output-string
  51. encoding
  52. (lambda (port)
  53. (display str port))
  54. conversion-strategy)))
  55. (define* (bytevector->string bv encoding
  56. #:optional (conversion-strategy 'error))
  57. "Decode the string represented by BV. The bytes in the bytevector
  58. will be interpreted according to ENCODING, which should be a string
  59. naming a character encoding, like \"utf-8\"."
  60. (if (and (string-ci=? encoding "utf-8")
  61. (eq? conversion-strategy 'error))
  62. (utf8->string bv)
  63. (let ((p (open-bytevector-input-port bv)))
  64. (set-port-encoding! p encoding)
  65. (if conversion-strategy
  66. (set-port-conversion-strategy! p conversion-strategy))
  67. (let ((res (read-string p)))
  68. (close-port p)
  69. (if (eof-object? res)
  70. ""
  71. res)))))