(defun lookup-foreign-encoding (normalized)
(cdr (assoc normalized *foreign-encoding-mapping* :test 'eql)))
-(defmacro string-to-octets (str &key encoding)
+(defmacro string-to-octets (str &key encoding null-terminate)
(declare (ignorable encoding))
#-(or allegro lispworks openmcl sbcl)
(map-into (make-array (length str) :element-type '(unsigned-byte 8))
#+allegro
(let ((fe (gensym "FE-"))
(ife (gensym "IFE-"))
- (s (gensym "STR-")))
+ (s (gensym "STR-"))
+ (nt (gensym "NT-")))
`(let* ((,fe (or ,encoding *default-foreign-encoding*))
(,ife (when ,fe (lookup-foreign-encoding ,fe)))
- (,s ,str))
+ (,s ,str)
+ (,nt ,null-terminate))
(values
(if ,ife
- (excl:string-to-octets ,s :external-format ,ife :null-terminate nil)
- (excl:string-to-octets ,s :null-terminate nil)))))
+ (excl:string-to-octets ,s :external-format ,ife :null-terminate ,nt)
+ (excl:string-to-octets ,s :null-terminate ,nt)))))
#+(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 (length str) :element-type '(unsigned-byte 8))
- #'char-code str)
+ (let ((len (gensym "LEN-"))
+ (out (gensym "OUT-")))
+ `(let ((,len (length ,str)))
+ (if (,null-terminate)
+ (progn
+ (let ((,out (map-into (make-array (1+ ,len) :element-type '(unsigned-byte 8))
+ #'char-code ,str)))
+ (setf (char ,out ,len) 0)
+ ,out))
+ (map-into (make-array len :element-type '(unsigned-byte 8))
+ #'char-code str))))
#+sbcl
(let ((fe (gensym "FE-"))
(ife (gensym "IFE-"))
- (s (gensym "STR-")))
+ (s (gensym "STR-"))
+ (nt (gensym "NT-")))
`(let* ((,fe (or ,encoding *default-foreign-encoding*))
(,ife (when ,fe (lookup-foreign-encoding ,fe)))
- (,s ,str))
+ (,s ,str)
+ (,nt ,null-terminate))
(if ,ife
- (sb-ext:string-to-octets ,s :external-format ,ife)
- (sb-ext:string-to-octets ,s))))
+ (sb-ext:string-to-octets ,s :external-format ,ife :null-terminate ,nt)
+ (sb-ext:string-to-octets ,s :null-terminate ,nt))))
)
(uffi:string-to-octets "abc")
#(97 98 99))
-;; Below is UTF-8 encoded, 27 octets / 20 lisp characters
(deftest :i18n/sto/4
+ (uffi:string-to-octets "abc" :null-terminate t)
+ #(97 98 99 0))
+
+;; Below is UTF-8 encoded, 27 octets / 20 lisp characters
+(deftest :i18n/sto/5
(uffi:string-to-octets "Iñtërnâtiônàlizætiøn" :encoding :utf-8)
#(73 195 177 116 195 171 114 110 195 162 116 105 195 180 110 195 160 108 105 122 195 166 116 105 195 184 110))
-(deftest :i18n/sto/5
+(deftest :i18n/sto/6
+ (uffi:string-to-octets "Iñtërnâtiônàlizætiøn" :encoding :utf-8 :null-terminate t)
+ #(73 195 177 116 195 171 114 110 195 162 116 105 195 180 110 195 160 108 105 122 195 166 116 105 195 184 110 0))
+
+(deftest :i18n/lsto/1
(length (uffi:string-to-octets "Iñtërnâtiônàlizætiøn" :encoding :utf-8))
27)
+(deftest :i18n/lsto/2
+ (length (uffi:string-to-octets "Iñtërnâtiônàlizætiøn" :encoding :utf-8 :null-terminate t))
+ 28)
+
(deftest :i18n/feoc/1
(uffi:foreign-encoded-octet-count "")
0)