From 426780c6b24145cd291a06871ef929074a564360 Mon Sep 17 00:00:00 2001 From: Matt Birkholz Date: Thu, 2 Nov 2017 09:12:30 -0700 Subject: [PATCH] x11/x11-base (->cstring): Convert 0 to the null pointer. --- src/x11/x11-base.scm | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/x11/x11-base.scm b/src/x11/x11-base.scm index 8eb177e3f..f30582fe5 100644 --- a/src/x11/x11-base.scm +++ b/src/x11/x11-base.scm @@ -70,23 +70,29 @@ Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. #t))) (define (->cstring string) - (if (bytevector? string) - (if (let ((end (bytevector-length string))) - (let loop ((i 0)) - (if (fix:< i end) - (or (fix:zero? (bytevector-u8-ref string i)) - (loop (fix:1+ i))) - #f))) - string - (error "C string not null terminated:" string)) - ;; String->iso8859-1 would be incorrect here; it does not null terminate. - (let* ((end (string-length string)) - (result (make-bytevector (fix:1+ end)))) - (do ((i 0 (fix:1+ i))) - ((not (fix:< i end)) - (bytevector-u8-set! result i #x00)) - (bytevector-u8-set! result i (char->integer (string-ref string i)))) - result))) + (cond ((and (integer? string) (zero? string)) + 0) + ((bytevector? string) + (if (let ((end (bytevector-length string))) + (let loop ((i 0)) + (if (fix:< i end) + (or (fix:zero? (bytevector-u8-ref string i)) + (loop (fix:1+ i))) + #f))) + string + (error "C string not null terminated:" string))) + ((string? string) + ;; String->iso8859-1 would be incorrect; it does not null terminate. + (let* ((end (string-length string)) + (result (make-bytevector (fix:1+ end)))) + (do ((i 0 (fix:1+ i))) + ((not (fix:< i end)) + (bytevector-u8-set! result i #x00)) + (bytevector-u8-set! result i (char->integer + (string-ref string i)))) + result)) + (else + (error:wrong-type-argument string "a string or 0" '->cstring)))) (define (x-window-set-name window name) (guarantee-xwindow window 'x-window-set-name) -- 2.25.1