mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-09 19:13:26 +03:00
lib/strings: init replaceString
This commit is contained in:
parent
8753210c68
commit
b5720e2b3f
3 changed files with 47 additions and 0 deletions
|
@ -309,6 +309,7 @@ let
|
||||||
stringLength
|
stringLength
|
||||||
substring
|
substring
|
||||||
isString
|
isString
|
||||||
|
replaceString
|
||||||
replaceStrings
|
replaceStrings
|
||||||
intersperse
|
intersperse
|
||||||
concatStringsSep
|
concatStringsSep
|
||||||
|
|
|
@ -332,6 +332,41 @@ rec {
|
||||||
*/
|
*/
|
||||||
concatLines = concatMapStrings (s: s + "\n");
|
concatLines = concatMapStrings (s: s + "\n");
|
||||||
|
|
||||||
|
/**
|
||||||
|
Given string `s`, replace every occurrence of the string `from` with the string `to`.
|
||||||
|
|
||||||
|
# Inputs
|
||||||
|
|
||||||
|
`from`
|
||||||
|
: The string to be replaced
|
||||||
|
|
||||||
|
`to`
|
||||||
|
: The string to replace with
|
||||||
|
|
||||||
|
`s`
|
||||||
|
: The original string where replacements will be made
|
||||||
|
|
||||||
|
# Type
|
||||||
|
|
||||||
|
```
|
||||||
|
replaceString :: string -> string -> string -> string
|
||||||
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
:::{.example}
|
||||||
|
## `lib.strings.replaceString` usage example
|
||||||
|
|
||||||
|
```nix
|
||||||
|
replaceString "world" "Nix" "Hello, world!"
|
||||||
|
=> "Hello, Nix!"
|
||||||
|
replaceString "." "_" "v1.2.3"
|
||||||
|
=> "v1_2_3"
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
*/
|
||||||
|
replaceString = from: to: replaceStrings [ from ] [ to ];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Repeat a string `n` times,
|
Repeat a string `n` times,
|
||||||
and concatenate the parts into a new string.
|
and concatenate the parts into a new string.
|
||||||
|
|
|
@ -91,6 +91,7 @@ let
|
||||||
range
|
range
|
||||||
recursiveUpdateUntil
|
recursiveUpdateUntil
|
||||||
removePrefix
|
removePrefix
|
||||||
|
replaceString
|
||||||
replicate
|
replicate
|
||||||
runTests
|
runTests
|
||||||
setFunctionArgs
|
setFunctionArgs
|
||||||
|
@ -497,6 +498,11 @@ runTests {
|
||||||
expected = "/usr/include:/usr/local/include";
|
expected = "/usr/include:/usr/local/include";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testReplaceStringString = {
|
||||||
|
expr = strings.replaceString "." "_" "v1.2.3";
|
||||||
|
expected = "v1_2_3";
|
||||||
|
};
|
||||||
|
|
||||||
testReplicateString = {
|
testReplicateString = {
|
||||||
expr = strings.replicate 5 "hello";
|
expr = strings.replicate 5 "hello";
|
||||||
expected = "hellohellohellohellohello";
|
expected = "hellohellohellohellohello";
|
||||||
|
@ -1728,6 +1734,11 @@ runTests {
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testReplaceString = {
|
||||||
|
expr = replaceString "world" "Nix" "Hello, world!";
|
||||||
|
expected = "Hello, Nix!";
|
||||||
|
};
|
||||||
|
|
||||||
testReplicate = {
|
testReplicate = {
|
||||||
expr = replicate 3 "a";
|
expr = replicate 3 "a";
|
||||||
expected = [
|
expected = [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue