From f24eef289c7a1657251b705556e583bcb6cd89c2 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sat, 24 Jan 2015 12:46:44 +0900 Subject: [PATCH] Optimizing string-offset->index. --- include/chibi/sexp.h | 1 + lib/init-7.scm | 4 +--- opcodes.c | 1 + sexp.c | 9 +++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/chibi/sexp.h b/include/chibi/sexp.h index 922890ab..949aa17d 100755 --- a/include/chibi/sexp.h +++ b/include/chibi/sexp.h @@ -1436,6 +1436,7 @@ SEXP_API char* sexp_string_utf8_prev (unsigned char *p); SEXP_API sexp sexp_string_utf8_ref (sexp ctx, sexp str, sexp i); SEXP_API sexp sexp_string_utf8_index_ref (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp i); SEXP_API sexp sexp_string_index_to_offset (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp index); +SEXP_API sexp sexp_string_offset_to_index (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp offset); SEXP_API sexp sexp_utf8_substring_op (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp start, sexp end); SEXP_API void sexp_utf8_encode_char (unsigned char* p, int len, int c); SEXP_API int sexp_write_utf8_char (sexp ctx, int c, sexp out); diff --git a/lib/init-7.scm b/lib/init-7.scm index 56685d72..3a63da9a 100644 --- a/lib/init-7.scm +++ b/lib/init-7.scm @@ -1239,9 +1239,7 @@ (cond-expand (full-unicode - (define string-cursor-end string-size) - (define (string-offset->index str off) - (string-length (substring-cursor str 0 off)))) + (define string-cursor-end string-size)) (else (define (string-index->offset str i) i) (define (string-offset->index str off) off) diff --git a/opcodes.c b/opcodes.c index 928fde48..57e770b2 100644 --- a/opcodes.c +++ b/opcodes.c @@ -213,6 +213,7 @@ _FN1(_I(SEXP_NUMBER), _I(SEXP_NUMBER), "ceiling", 0, sexp_ceiling), _FN2(_I(SEXP_NUMBER), _I(SEXP_NUMBER), _I(SEXP_NUMBER), "expt", 0, sexp_expt_op), #if SEXP_USE_UTF8_STRINGS _FN2(_I(SEXP_FIXNUM), _I(SEXP_STRING), _I(SEXP_FIXNUM), "string-index->offset", 0, sexp_string_index_to_offset), +_FN2(_I(SEXP_FIXNUM), _I(SEXP_STRING), _I(SEXP_FIXNUM), "string-offset->index", 0, sexp_string_offset_to_index), _FN2(_I(SEXP_CHAR), _I(SEXP_STRING), _I(SEXP_FIXNUM), "string-ref", 0, sexp_string_utf8_index_ref), #if SEXP_USE_MUTABLE_STRINGS _FN3(SEXP_VOID, _I(SEXP_STRING), _I(SEXP_FIXNUM), _I(SEXP_CHAR), "string-set!", 0, sexp_string_utf8_index_set), diff --git a/sexp.c b/sexp.c index e0ae8fec..f3cbb505 100644 --- a/sexp.c +++ b/sexp.c @@ -1009,6 +1009,15 @@ sexp sexp_string_index_to_offset (sexp ctx, sexp self, sexp_sint_t n, sexp str, return sexp_make_fixnum(j); } +sexp sexp_string_offset_to_index (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp offset) { + sexp_sint_t off = sexp_unbox_fixnum(offset); + sexp_assert_type(ctx, sexp_stringp, SEXP_STRING, str); + sexp_assert_type(ctx, sexp_fixnump, SEXP_FIXNUM, offset); + if (off < 0 || off > sexp_string_size(str)) + return sexp_user_exception(ctx, self, "string-offset->index: offset out of range", offset); + return sexp_make_fixnum(sexp_string_utf8_length((unsigned char*)sexp_string_data(str), off)); +} + #endif sexp sexp_make_string_op (sexp ctx, sexp self, sexp_sint_t n, sexp len, sexp ch)