mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-20 06:09:18 +02:00
adding string<->utf8 conversions
This commit is contained in:
parent
7b08af0c99
commit
c3614986ef
4 changed files with 22 additions and 0 deletions
|
@ -9,6 +9,7 @@
|
||||||
make-generated-input-port make-filtered-output-port
|
make-generated-input-port make-filtered-output-port
|
||||||
make-filtered-input-port
|
make-filtered-input-port
|
||||||
open-input-bytevector open-output-bytevector get-output-bytevector
|
open-input-bytevector open-output-bytevector get-output-bytevector
|
||||||
|
string->utf8 utf8->string
|
||||||
write-u8 read-u8 peek-u8)
|
write-u8 read-u8 peek-u8)
|
||||||
(import (scheme) (chibi ast))
|
(import (scheme) (chibi ast))
|
||||||
(include-shared "io/io")
|
(include-shared "io/io")
|
||||||
|
|
|
@ -21,6 +21,9 @@
|
||||||
(c 0 (if (eqv? ch (string-ref str i)) (+ c 1) c)))
|
(c 0 (if (eqv? ch (string-ref str i)) (+ c 1) c)))
|
||||||
((>= i end) c))))
|
((>= i end) c))))
|
||||||
|
|
||||||
|
(define (utf8->string vec)
|
||||||
|
(string-copy (utf8->string! vec)))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; reading and writing
|
;; reading and writing
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,11 @@
|
||||||
(define-c sexp (get-output-bytevector "sexp_get_output_bytevector")
|
(define-c sexp (get-output-bytevector "sexp_get_output_bytevector")
|
||||||
((value ctx sexp) (value self sexp) sexp))
|
((value ctx sexp) (value self sexp) sexp))
|
||||||
|
|
||||||
|
(define-c sexp (string->utf8 "sexp_string_to_utf8")
|
||||||
|
((value ctx sexp) (value self sexp) sexp))
|
||||||
|
(define-c sexp (utf8->string! "sexp_utf8_to_string_x")
|
||||||
|
((value ctx sexp) (value self sexp) sexp))
|
||||||
|
|
||||||
(define-c sexp (write-u8 "sexp_write_u8")
|
(define-c sexp (write-u8 "sexp_write_u8")
|
||||||
((value ctx sexp) (value self sexp) sexp (default (current-output-port) sexp)))
|
((value ctx sexp) (value self sexp) sexp (default (current-output-port) sexp)))
|
||||||
(define-c sexp (read-u8 "sexp_read_u8")
|
(define-c sexp (read-u8 "sexp_read_u8")
|
||||||
|
|
|
@ -249,6 +249,19 @@ sexp sexp_get_output_bytevector (sexp ctx, sexp self, sexp port) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sexp sexp_string_to_utf8 (sexp ctx, sexp self, sexp str) {
|
||||||
|
sexp res;
|
||||||
|
sexp_assert_type(ctx, sexp_stringp, SEXP_STRING, str);
|
||||||
|
res = sexp_c_string(ctx, sexp_string_data(str), sexp_string_length(str));
|
||||||
|
return sexp_string_to_bytes(ctx, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: add validation */
|
||||||
|
sexp sexp_utf8_to_string_x (sexp ctx, sexp self, sexp vec) {
|
||||||
|
sexp_assert_type(ctx, sexp_bytesp, SEXP_BYTES, vec);
|
||||||
|
return sexp_bytes_to_string(ctx, vec);
|
||||||
|
}
|
||||||
|
|
||||||
sexp sexp_write_u8 (sexp ctx, sexp self, sexp u8, sexp out) {
|
sexp sexp_write_u8 (sexp ctx, sexp self, sexp u8, sexp out) {
|
||||||
sexp_assert_type(ctx, sexp_fixnump, SEXP_FIXNUM, u8);
|
sexp_assert_type(ctx, sexp_fixnump, SEXP_FIXNUM, u8);
|
||||||
if (sexp_unbox_fixnum(u8) < 0 || sexp_unbox_fixnum(u8) > 255)
|
if (sexp_unbox_fixnum(u8) < 0 || sexp_unbox_fixnum(u8) > 255)
|
||||||
|
|
Loading…
Add table
Reference in a new issue