Add string-to-octets
authorKevin Rosenberg <kevin@rosenberg.net>
Sun, 7 Feb 2010 19:32:15 +0000 (12:32 -0700)
committerKevin Rosenberg <kevin@rosenberg.net>
Sun, 7 Feb 2010 19:32:15 +0000 (12:32 -0700)
ChangeLog
src/i18n.lisp
src/package.lisp

index 62cc200b4e0b72ab327efc49e18a310792f5ca8d..eba11e6f37ae24f9da7060ef787a07c4bf5a9a7b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+2010-02-06 Kevin Rosenberg <kevin@rosenberg.net>
+       * src/i18n.lisp: Add new function string-to-octets
+
 2010-02-06 Kevin Rosenberg <kevin@rosenberg.net>
        * Version 1.8.1
        * src/i18n.lisp: Add new function foreign-encoded-string-octets
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)
+
+)
+
index 15b47928a0b25882f1051e9c0e484e0572156530..318e68a279e5c54a30fb8e95bfa25f8d5d447bc6 100644 (file)
@@ -86,6 +86,7 @@
    #:*default-foreign-encoding*
    #:*foreign-encodings*
    #:foreign-encoded-string-octets
+   #:string-to-octets
    ))