Welcome to the CHICKEN Scheme pasting service
using the readline completion interface pasted by wasamasa on Sat Dec 10 22:45:25 2016
(define (copy-string string) (let ((data (blob->u8vector (string->blob string)))) ((foreign-lambda* (c-pointer char) ((u8vector src) (int size)) "char *dest = malloc((size + 1) * sizeof(char)); memcpy(dest, src, size * sizeof(char)); dest[size] = '\\0'; C_return(dest);") data (u8vector-length data)))) (define completion-index 0) (define completion-candidates #("foo" "bar" "baz" "💩")) ; <- utf-8 test (define-external (readline_completer ((const c-string) prefix) (int state)) (c-pointer char) (when (zero? state) (set! completion-index 0)) ; initialization (if (< completion-index (vector-length completion-candidates)) (let ((candidate (vector-ref completion-candidates completion-index))) (set! completion-index (add1 completion-index)) (copy-string candidate)) #f)) (define (enable-completer!) ((foreign-lambda* void () "rl_completion_entry_function = readline_completer;"))) (define (disable-completer!) ((foreign-lambda* void () "rl_completion_entry_function = NULL;")))
less wasteful variant pasted by wasamasa on Sat Dec 10 22:51:37 2016
(define (copy-string string)
(let ((data (string->blob string)))
((foreign-lambda* (c-pointer char) ((blob src) (int size))
"char *dest = malloc(size + 1);
memcpy(dest, src, size);
dest[size] = '\\0';
C_return(dest);")
data (blob-size data))))
even less wasteful variant added by wasamasa on Sat Dec 10 23:57:07 2016
(define copy-string
;; NOTE: returning a c-string leads to segfaults
(foreign-lambda* (c-pointer char) ((scheme-object string))
"char *src = C_c_string(string);"
"size_t size = C_SIZEOF_STRING(string);"
"char *dest = malloc(size + 1);"
"strncpy(dest, src, size);"
"dest[size] = '\\0';"
"C_return(dest);"))