chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] low level strings


From: Thomas Chust
Subject: Re: [Chicken-users] low level strings
Date: Tue, 06 Sep 2005 17:50:21 -0000
User-agent: Opera M2/8.02 (MacPPC, build 2148)

Am 06.09.2005, 15:56 Uhr, schrieb Michael Benfield <address@hidden>:
Is it possible within Scheme code to get pointers to Scheme strings that can be passed to C? I don't want to just pass it as a c-string; I need [...]

Just pass the string as a scheme-object and manipulate it by hand then.

Example:

$ cat >my-string-ref.scm
(define my-string-ref
  (foreign-lambda* scheme-object ((scheme-object str) (int idx))
#<<EOD
if (C_stringp(str) != C_SCHEME_TRUE || idx < 0 || idx > C_header_size(str))
    return C_SCHEME_FALSE;
  else
    return C_make_character(C_c_string(str)[idx]);
EOD
))

(define-macro (show-eval expr)
  `(begin
     (display ";; ") (write ',expr) (newline)
     (write ,expr) (newline)))

(define my-string "Hello world!")

(show-eval (my-string-ref 'my-string 0))
(show-eval (my-string-ref my-string -42))
(show-eval (my-string-ref my-string +42))
(show-eval (my-string-ref my-string +06))

$ csc -O2 -d0 my-string-ref.scm -o my-string-ref
$ ./my-string-ref
;; (my-string-ref (quote my-string) 0)
#f
;; (my-string-ref my-string -42)
#f
;; (my-string-ref my-string 42)
#f
;; (my-string-ref my-string 6)
#\w




reply via email to

[Prev in Thread] Current Thread [Next in Thread]