mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 11:50:28 +02:00
New function string-replace-substring in (ice-9 string-fun)
By A. Wingo in https://lists.gnu.org/archive/html/guile-devel/2014-03/msg00058.html. * module/ice-9/string-fun.scm (string-replace-substring): As stated. * doc/ref/api-data.texi: Document the new function. * test-suite/tests/strings.test: Test.
This commit is contained in:
parent
ddad8ae05a
commit
6cfee36543
3 changed files with 60 additions and 2 deletions
|
@ -4227,6 +4227,22 @@ a predicate, if it is a character, it is tested for equality and if it
|
||||||
is a character set, it is tested for membership.
|
is a character set, it is tested for membership.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
The following additional functions are available in the module @code{(ice-9 string-fun)}. They can be used with:
|
||||||
|
|
||||||
|
@example
|
||||||
|
(use-modules (ice-9 string-fun))
|
||||||
|
@end example
|
||||||
|
|
||||||
|
@deffn {Scheme Procedure} string-replace-substring str substring replacement
|
||||||
|
Return a new string where every instance of @var{substring} in string
|
||||||
|
@var{str} has been replaced by @var{replacement}. For example:
|
||||||
|
|
||||||
|
@lisp
|
||||||
|
(string-replace-substring "a ring of strings" "ring" "rut")
|
||||||
|
@result{} "a rut of struts"
|
||||||
|
@end lisp
|
||||||
|
@end deffn
|
||||||
|
|
||||||
@node Representing Strings as Bytes
|
@node Representing Strings as Bytes
|
||||||
@subsubsection Representing Strings as Bytes
|
@subsubsection Representing Strings as Bytes
|
||||||
|
|
||||||
|
|
|
@ -25,13 +25,17 @@
|
||||||
separate-fields-discarding-char separate-fields-after-char
|
separate-fields-discarding-char separate-fields-after-char
|
||||||
separate-fields-before-char string-prefix-predicate string-prefix=?
|
separate-fields-before-char string-prefix-predicate string-prefix=?
|
||||||
sans-surrounding-whitespace sans-trailing-whitespace
|
sans-surrounding-whitespace sans-trailing-whitespace
|
||||||
sans-leading-whitespace sans-final-newline has-trailing-newline?))
|
sans-leading-whitespace sans-final-newline has-trailing-newline?
|
||||||
|
string-replace-substring))
|
||||||
|
|
||||||
;;;;
|
;;;;
|
||||||
;;;
|
;;;
|
||||||
;;; Various string funcitons, particularly those that take
|
;;; Various string funcitons, particularly those that take
|
||||||
;;; advantage of the "shared substring" capability.
|
;;; advantage of the "shared substring" capability.
|
||||||
|
;;; FIXME Document these functions in Miscellaneous String Operations::
|
||||||
|
;;; in doc/ref/api-data.texi.
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
|
|
||||||
;;; {String Fun: Dividing Strings Into Fields}
|
;;; {String Fun: Dividing Strings Into Fields}
|
||||||
;;;
|
;;;
|
||||||
|
@ -278,3 +282,34 @@
|
||||||
;;; (fail parts)
|
;;; (fail parts)
|
||||||
;;; (apply return parts))))
|
;;; (apply return parts))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
;;; {String Fun: string-replace-substring}
|
||||||
|
;;;
|
||||||
|
|
||||||
|
;; string-replace-substring By A. Wingo in
|
||||||
|
;; https://lists.gnu.org/archive/html/guile-devel/2014-03/msg00058.html
|
||||||
|
;; also in string-replace-substring guix:guix/utils.scm.
|
||||||
|
|
||||||
|
(define (string-replace-substring str substring replacement)
|
||||||
|
"Return a new string where every instance of @var{substring} in string
|
||||||
|
@var{str} has been replaced by @var{replacement}. For example:
|
||||||
|
|
||||||
|
@lisp
|
||||||
|
(string-replace-substring \"a ring of strings\" \"ring\" \"rut\")
|
||||||
|
@result{} \"a rut of struts\"
|
||||||
|
@end lisp
|
||||||
|
"
|
||||||
|
(let ((sublen (string-length substring)))
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda ()
|
||||||
|
(let lp ((start 0))
|
||||||
|
(cond
|
||||||
|
((string-contains str substring start)
|
||||||
|
=> (lambda (end)
|
||||||
|
(display (substring/shared str start end))
|
||||||
|
(display replacement)
|
||||||
|
(lp (+ end sublen))))
|
||||||
|
(else
|
||||||
|
(display (substring/shared str start)))))))))
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,8 @@
|
||||||
|
|
||||||
(define-module (test-strings)
|
(define-module (test-strings)
|
||||||
#:use-module ((system base compile) #:select (compile))
|
#:use-module ((system base compile) #:select (compile))
|
||||||
#:use-module (test-suite lib))
|
#:use-module (test-suite lib)
|
||||||
|
#:use-module (ice-9 string-fun))
|
||||||
|
|
||||||
(define exception:read-only-string
|
(define exception:read-only-string
|
||||||
(cons 'misc-error "^string is read-only"))
|
(cons 'misc-error "^string is read-only"))
|
||||||
|
@ -693,3 +694,9 @@
|
||||||
(string-upcase! (substring/shared str2 1 4))
|
(string-upcase! (substring/shared str2 1 4))
|
||||||
(and (string=? str1 "foofoofoo")
|
(and (string=? str1 "foofoofoo")
|
||||||
(string=? str2 "oFOOf")))))
|
(string=? str2 "oFOOf")))))
|
||||||
|
|
||||||
|
(with-test-prefix "miscellaneous string functions"
|
||||||
|
|
||||||
|
(pass-if "string-replace-substring"
|
||||||
|
(string=? (string-replace-substring "a ring of strings" "ring" "rut")
|
||||||
|
"a rut of struts")))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue