lib/strings: init replaceString (#411547)
This commit is contained in:
@@ -309,6 +309,7 @@ let
|
||||
stringLength
|
||||
substring
|
||||
isString
|
||||
replaceString
|
||||
replaceStrings
|
||||
intersperse
|
||||
concatStringsSep
|
||||
|
||||
+2
-2
@@ -18,7 +18,7 @@ let
|
||||
concatStrings
|
||||
escape
|
||||
head
|
||||
replaceStrings
|
||||
replaceString
|
||||
;
|
||||
|
||||
mkPrimitive = t: v: {
|
||||
@@ -451,7 +451,7 @@ rec {
|
||||
mkString =
|
||||
v:
|
||||
let
|
||||
sanitize = s: replaceStrings [ "\n" ] [ "\\n" ] (escape [ "'" "\\" ] s);
|
||||
sanitize = s: replaceString "\n" "\\n" (escape [ "'" "\\" ] s);
|
||||
in
|
||||
mkPrimitive type.string v
|
||||
// {
|
||||
|
||||
+36
-1
@@ -332,6 +332,41 @@ rec {
|
||||
*/
|
||||
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,
|
||||
and concatenate the parts into a new string.
|
||||
@@ -1138,7 +1173,7 @@ rec {
|
||||
string = toString arg;
|
||||
in
|
||||
if match "[[:alnum:],._+:@%/-]+" string == null then
|
||||
"'${replaceStrings [ "'" ] [ "'\\''" ] string}'"
|
||||
"'${replaceString "'" "'\\''" string}'"
|
||||
else
|
||||
string;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ let
|
||||
optionalAttrs
|
||||
optionalString
|
||||
removeSuffix
|
||||
replaceStrings
|
||||
replaceString
|
||||
toUpper
|
||||
;
|
||||
|
||||
@@ -522,7 +522,7 @@ let
|
||||
#
|
||||
# https://github.com/rust-lang/cargo/pull/9169
|
||||
# https://github.com/rust-lang/cargo/issues/8285#issuecomment-634202431
|
||||
cargoEnvVarTarget = replaceStrings [ "-" ] [ "_" ] (toUpper final.rust.cargoShortTarget);
|
||||
cargoEnvVarTarget = replaceString "-" "_" (toUpper final.rust.cargoShortTarget);
|
||||
|
||||
# True if the target is no_std
|
||||
# https://github.com/rust-lang/rust/blob/2e44c17c12cec45b6a682b1e53a04ac5b5fcc9d2/src/bootstrap/config.rs#L415-L421
|
||||
|
||||
@@ -91,6 +91,7 @@ let
|
||||
range
|
||||
recursiveUpdateUntil
|
||||
removePrefix
|
||||
replaceString
|
||||
replicate
|
||||
runTests
|
||||
setFunctionArgs
|
||||
@@ -497,6 +498,11 @@ runTests {
|
||||
expected = "/usr/include:/usr/local/include";
|
||||
};
|
||||
|
||||
testReplaceStringString = {
|
||||
expr = strings.replaceString "." "_" "v1.2.3";
|
||||
expected = "v1_2_3";
|
||||
};
|
||||
|
||||
testReplicateString = {
|
||||
expr = strings.replicate 5 "hello";
|
||||
expected = "hellohellohellohellohello";
|
||||
@@ -1728,6 +1734,11 @@ runTests {
|
||||
];
|
||||
};
|
||||
|
||||
testReplaceString = {
|
||||
expr = replaceString "world" "Nix" "Hello, world!";
|
||||
expected = "Hello, Nix!";
|
||||
};
|
||||
|
||||
testReplicate = {
|
||||
expr = replicate 3 "a";
|
||||
expected = [
|
||||
|
||||
Reference in New Issue
Block a user