New function foreign-encoded-string-octets
[uffi.git] / src / i18n.lisp
index 78c04990beced25a3b2542bebdcba0b4c12d6f7b..47a06db565268760cd5e6d3840c4a8683abc4932 100644 (file)
@@ -57,3 +57,17 @@ encoding.")
 
 (defun implementation-foreign-encoding (normalized)
   (cdr (assoc normalized *foreign-encoding-mapping* :test 'eql)))
+
+(defun foreign-encoded-string-octets (str &optional 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
+  ;; to external-format. CLISP, like SBCL, requires conversion with external-
+  ;; format
+  (length #+(and sbcl sb-unicode)
+          (sb-ext:string-to-octets
+           str
+           :external-format (or foreign-encoding
+                                *default-foreign-encoding*
+                                :utf-8))
+          #-(and sbcl sb-unicode) str))