Add string-to-octets
[uffi.git] / src / i18n.lisp
index 47a06db565268760cd5e6d3840c4a8683abc4932..08c16ad0b54eb962721764deaa4eea9c4a34feaa 100644 (file)
@@ -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)
+
+)
+