adding string<->utf8 conversions

This commit is contained in:
Alex Shinn 2011-10-04 22:15:58 +09:00
parent 7b08af0c99
commit c3614986ef
4 changed files with 22 additions and 0 deletions

View file

@ -9,6 +9,7 @@
make-generated-input-port make-filtered-output-port
make-filtered-input-port
open-input-bytevector open-output-bytevector get-output-bytevector
string->utf8 utf8->string
write-u8 read-u8 peek-u8)
(import (scheme) (chibi ast))
(include-shared "io/io")

View file

@ -21,6 +21,9 @@
(c 0 (if (eqv? ch (string-ref str i)) (+ c 1) c)))
((>= i end) c))))
(define (utf8->string vec)
(string-copy (utf8->string! vec)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; reading and writing

View file

@ -33,6 +33,11 @@
(define-c sexp (get-output-bytevector "sexp_get_output_bytevector")
((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")
((value ctx sexp) (value self sexp) sexp (default (current-output-port) sexp)))
(define-c sexp (read-u8 "sexp_read_u8")

View file

@ -249,6 +249,19 @@ sexp sexp_get_output_bytevector (sexp ctx, sexp self, sexp port) {
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_assert_type(ctx, sexp_fixnump, SEXP_FIXNUM, u8);
if (sexp_unbox_fixnum(u8) < 0 || sexp_unbox_fixnum(u8) > 255)