From dfe3a75b3f64ac993f64c6380c442de53ec21235 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Wed, 22 Feb 2017 01:25:22 -0800 Subject: [PATCH] Add unicode support to string-replace. --- src/runtime/runtime.pkg | 3 +-- src/runtime/string.scm | 26 -------------------------- src/runtime/ustring.scm | 7 +++++++ 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index cfecd6195..becea1c84 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -1046,7 +1046,6 @@ USA. string-match-forward-ci string-pad-left string-pad-right - string-replace string-search-all string-search-backward string-search-forward @@ -1057,7 +1056,6 @@ USA. substring-match-backward-ci substring-match-forward substring-match-forward-ci - substring-replace substring-search-all substring-search-backward substring-search-forward @@ -1140,6 +1138,7 @@ USA. string-prefix-ci? string-prefix? string-ref + string-replace string-set! string-slice string-splitter diff --git a/src/runtime/string.scm b/src/runtime/string.scm index 6768eb580..80f230186 100644 --- a/src/runtime/string.scm +++ b/src/runtime/string.scm @@ -89,32 +89,6 @@ USA. (string-set! result j (string-ref string i))) result))) -;;;; Replace - -(define (string-replace string char1 char2) - (guarantee-string string 'STRING-REPLACE) - (guarantee-char char1 'STRING-REPLACE) - (guarantee-char char2 'STRING-REPLACE) - (let ((string (string-copy string))) - (%substring-replace! string 0 (string-length string) char1 char2) - string)) - -(define (substring-replace string start end char1 char2) - (guarantee-substring string start end 'SUBSTRING-REPLACE) - (guarantee-char char1 'SUBSTRING-REPLACE) - (guarantee-char char2 'SUBSTRING-REPLACE) - (let ((string (string-copy string))) - (%substring-replace! string start end char1 char2) - string)) - -(define (%substring-replace! string start end char1 char2) - (let loop ((start start)) - (let ((index (substring-find-next-char string start end char1))) - (if index - (begin - (string-set! string index char2) - (loop (fix:+ index 1))))))) - ;;;; Compare (define (string-compare string1 string2 if= if< if>) diff --git a/src/runtime/ustring.scm b/src/runtime/ustring.scm index 664454bfa..c3663c4f9 100644 --- a/src/runtime/ustring.scm +++ b/src/runtime/ustring.scm @@ -780,6 +780,13 @@ 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 (string-8-bit? string) (receive (string start end) (translate-slice string 0 (string-length string)) -- 2.25.1