From 3044928889785c0160fd021a51fbf86ad691a3a9 Mon Sep 17 00:00:00 2001 From: Kevin Rosenberg Date: Sun, 7 Feb 2010 12:32:15 -0700 Subject: [PATCH] Add string-to-octets --- ChangeLog | 3 +++ src/i18n.lisp | 25 ++++++++++++++++++++++++- src/package.lisp | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 62cc200..eba11e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2010-02-06 Kevin Rosenberg + * src/i18n.lisp: Add new function string-to-octets + 2010-02-06 Kevin Rosenberg * Version 1.8.1 * src/i18n.lisp: Add new function foreign-encoded-string-octets diff --git a/src/i18n.lisp b/src/i18n.lisp index 47a06db..08c16ad 100644 --- a/src/i18n.lisp +++ b/src/i18n.lisp @@ -58,7 +58,7 @@ encoding.") (defun implementation-foreign-encoding (normalized) (cdr (assoc normalized *foreign-encoding-mapping* :test 'eql))) -(defun foreign-encoded-string-octets (str &optional foreign-encoding) +(defun foreign-encoded-string-octets (str &key foreign-encoding) "Returns the octets required to represent the string when passed to a ~ foreign function." ;; AllegroCL, CCL, and Lispworks give correct value without converting @@ -71,3 +71,26 @@ foreign function." *default-foreign-encoding* :utf-8)) #-(and sbcl sb-unicode) str)) + +(defun string-to-octets (str &key foreign-encoding) + "Converts a Lisp string to a vector of octets." + #-(or allegro lispworks openmcl sbcl) + (declare (ignore foreign-encoding)) + #-(or allegro lispworks openmcl sbcl) + (map-into (make-array len :element-type '(unsigned-byte 8)) + #'char-code str) + + #+allegro + (excl:string-to-native str :external-format foreign-encoding :null-terminate nil) + + #+(or lispworks openmcl) + ;; simply reading each char-code from the LENGTH of string handles multibyte characters + ;; just fine in testing LW 6.0 and CCL 1.4 + (map-into (make-array len :element-type '(unsigned-byte 8)) + #'char-code str) + + #+sbcl + (sb-ext:string-to-native str :external-format foreign-encoding) + +) + diff --git a/src/package.lisp b/src/package.lisp index 15b4792..318e68a 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -86,6 +86,7 @@ #:*default-foreign-encoding* #:*foreign-encodings* #:foreign-encoded-string-octets + #:string-to-octets )) -- 2.34.1