Add unicode support to string-replace.
authorChris Hanson <org/chris-hanson/cph>
Wed, 22 Feb 2017 09:25:22 +0000 (01:25 -0800)
committerChris Hanson <org/chris-hanson/cph>
Wed, 22 Feb 2017 09:25:22 +0000 (01:25 -0800)
src/runtime/runtime.pkg
src/runtime/string.scm
src/runtime/ustring.scm

index cfecd61954aebef7159d614b830ca14dd40e0e20..becea1c84f614023ea77a74e32546282b2ca34ff 100644 (file)
@@ -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
index 6768eb58081e6b38653909539bef56cf1c10bbaa..80f230186697d5a1f664b088fb0bf0068826524f 100644 (file)
@@ -89,32 +89,6 @@ USA.
        (string-set! result j (string-ref string i)))
       result)))
 \f
-;;;; 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)))))))
-\f
 ;;;; Compare
 
 (define (string-compare string1 string2 if= if< if>)
index 664454bfa466082dbb09ce55d205871386516ba8..c3663c4f942331bc7a19d32cdc4951b9aaf5fddc 100644 (file)
@@ -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))
 \f
 (define (string-8-bit? string)
   (receive (string start end) (translate-slice string 0 (string-length string))