diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2013-11-12 22:56:13 +0100 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2013-11-13 00:29:05 +0100 |
| commit | 56b943de6e61f41d6ebd2dfa65ff886cdfd83759 (patch) | |
| tree | 3669027822242116653430944897e0a27ca58f55 | |
| parent | be0f611208b4e6d730991267f69f9effe703e32f (diff) | |
utils: Add 'string-replace-substring'.
* guix/utils.scm (string-replace-substring): New procedure. Based on
code by Mark H. Weaver.
* tests/utils.scm ("string-replace-substring"): New test.
| -rw-r--r-- | guix/utils.scm | 24 | ||||
| -rw-r--r-- | tests/utils.scm | 8 |
2 files changed, 32 insertions, 0 deletions
diff --git a/guix/utils.scm b/guix/utils.scm index 1f3c0c8ad66..b730340edac 100644 --- a/guix/utils.scm +++ b/guix/utils.scm | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org> | 2 | ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org> | ||
| 3 | ;;; | 4 | ;;; |
| 4 | ;;; This file is part of GNU Guix. | 5 | ;;; This file is part of GNU Guix. |
| 5 | ;;; | 6 | ;;; |
| @@ -62,6 +63,7 @@ | |||
| 62 | guile-version>? | 63 | guile-version>? |
| 63 | package-name->name+version | 64 | package-name->name+version |
| 64 | string-tokenize* | 65 | string-tokenize* |
| 66 | string-replace-substring | ||
| 65 | file-extension | 67 | file-extension |
| 66 | file-sans-extension | 68 | file-sans-extension |
| 67 | call-with-temporary-output-file | 69 | call-with-temporary-output-file |
| @@ -387,6 +389,28 @@ like `string-tokenize', but SEPARATOR is a string." | |||
| 387 | (else | 389 | (else |
| 388 | (reverse (cons string result)))))) | 390 | (reverse (cons string result)))))) |
| 389 | 391 | ||
| 392 | (define* (string-replace-substring str substr replacement | ||
| 393 | #:optional | ||
| 394 | (start 0) | ||
| 395 | (end (string-length str))) | ||
| 396 | "Replace all occurrences of SUBSTR in the START--END range of STR by | ||
| 397 | REPLACEMENT." | ||
| 398 | (match (string-length substr) | ||
| 399 | (0 | ||
| 400 | (error "string-replace-substring: empty substring")) | ||
| 401 | (substr-length | ||
| 402 | (let loop ((start start) | ||
| 403 | (pieces (list (substring str 0 start)))) | ||
| 404 | (match (string-contains str substr start end) | ||
| 405 | (#f | ||
| 406 | (string-concatenate-reverse | ||
| 407 | (cons (substring str start) pieces))) | ||
| 408 | (index | ||
| 409 | (loop (+ index substr-length) | ||
| 410 | (cons* replacement | ||
| 411 | (substring str start index) | ||
| 412 | pieces)))))))) | ||
| 413 | |||
| 390 | (define (call-with-temporary-output-file proc) | 414 | (define (call-with-temporary-output-file proc) |
| 391 | "Call PROC with a name of a temporary file and open output port to that | 415 | "Call PROC with a name of a temporary file and open output port to that |
| 392 | file; close the file and delete it when leaving the dynamic extent of this | 416 | file; close the file and delete it when leaving the dynamic extent of this |
diff --git a/tests/utils.scm b/tests/utils.scm index 4f6ecc514de..017d9170fa7 100644 --- a/tests/utils.scm +++ b/tests/utils.scm | |||
| @@ -82,6 +82,14 @@ | |||
| 82 | (string-tokenize* "foo!bar!" "!") | 82 | (string-tokenize* "foo!bar!" "!") |
| 83 | (string-tokenize* "foo+-+bar+-+baz" "+-+"))) | 83 | (string-tokenize* "foo+-+bar+-+baz" "+-+"))) |
| 84 | 84 | ||
| 85 | (test-equal "string-replace-substring" | ||
| 86 | '("foo BAR! baz" | ||
| 87 | "/gnu/store/chbouib" | ||
| 88 | "") | ||
| 89 | (list (string-replace-substring "foo bar baz" "bar" "BAR!") | ||
| 90 | (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/") | ||
| 91 | (string-replace-substring "" "foo" "bar"))) | ||
| 92 | |||
| 85 | (test-equal "fold2, 1 list" | 93 | (test-equal "fold2, 1 list" |
| 86 | (list (reverse (iota 5)) | 94 | (list (reverse (iota 5)) |
| 87 | (map - (reverse (iota 5)))) | 95 | (map - (reverse (iota 5)))) |
