From 517a1245ac8e89d0cd18684eee9266f4975aadcb Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Wed, 22 Feb 2017 02:05:16 -0800 Subject: [PATCH] Move reverse-string into ustring. --- src/runtime/runtime.pkg | 3 +-- src/runtime/string.scm | 26 +++++--------------------- src/runtime/ustring.scm | 10 +++++++++- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index 547d9dfd8..934888ded 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -1036,8 +1036,6 @@ USA. guarantee-substring guarantee-substring-end-index guarantee-substring-start-index - reverse-string - reverse-substring string-pad-left string-pad-right string-search-all @@ -1092,6 +1090,7 @@ USA. (substring string-copy) list->string make-string + reverse-string string string* string->list diff --git a/src/runtime/string.scm b/src/runtime/string.scm index b2a6bac1a..cde00ab1b 100644 --- a/src/runtime/string.scm +++ b/src/runtime/string.scm @@ -55,7 +55,7 @@ USA. vector-8b-find-previous-char-ci (vector-8b-ref 2) (vector-8b-set! 3)) - + ;;;; Basic Operations (define (make-legacy-string k #!optional char) @@ -71,23 +71,6 @@ USA. (if (default-object? ascii) ascii (integer->char ascii)))) - -(define (reverse-string string) - (guarantee-string string 'REVERSE-STRING) - (%reverse-substring string 0 (string-length string))) - -(define (reverse-substring string start end) - (guarantee-substring string start end 'REVERSE-SUBSTRING) - (%reverse-substring string start end)) - -(define (%reverse-substring string start end) - (let ((n (fix:- end start))) - (let ((result (make-legacy-string n))) - (do ((i start (fix:+ i 1)) - (j (fix:- n 1) (fix:- j 1))) - ((fix:= i end)) - (string-set! result j (string-ref string i))) - result))) ;;;; Trim @@ -313,7 +296,7 @@ USA. (define (%bm-substring-search-backward text tstart tend pattern pstart pend) (let ((m (fix:- pend pstart)) (pend-1 (fix:- pend 1)) - (rpattern (reverse-substring pattern pstart pend))) + (rpattern (reverse-string (string-slice pattern pstart pend)))) (let ((tstart+m (fix:+ tstart m)) (lambda* (compute-last-occurrence-function rpattern 0 m)) (gamma @@ -369,8 +352,9 @@ USA. (define (compute-good-suffix-function pattern pstart pend gamma0) (let ((m (fix:- pend pstart))) (let ((pi - (compute-prefix-function (reverse-substring pattern pstart pend) - 0 m)) + (compute-prefix-function + (reverse-string (string-slice pattern pstart pend)) + 0 m)) (gamma (make-vector m gamma0)) (m-1 (fix:- m 1))) (do ((l 0 (fix:+ l 1))) diff --git a/src/runtime/ustring.scm b/src/runtime/ustring.scm index 1f34cc266..b8c2590e3 100644 --- a/src/runtime/ustring.scm +++ b/src/runtime/ustring.scm @@ -830,13 +830,21 @@ USA. (define (burst-string string delimiter allow-runs?) ((string-splitter delimiter allow-runs?) string)) - + (define (string-replace string char1 char2) (guarantee bitless-char? char1 'string-replace) (guarantee bitless-char? char2 'string-replace) (string-map (lambda (char) (if (char=? char char1) char2 char)) string)) + +(define (reverse-string string) + (let ((builder (string-builder))) + (do ((i (fix:- (string-length string) 1) + (fix:- i 1))) + ((not (fix:>= i 0))) + (builder (string-ref string i))) + (builder))) (define (string-8-bit? string) (receive (string start end) (translate-slice string 0 (string-length string)) -- 2.25.1