mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-09-29 15:43:00 +00:00
Merge pull request #249206 from tweag/removePrefix-optimise
Optimise `lib.strings.removePrefix`
This commit is contained in:
commit
9f605b9807
@ -629,10 +629,10 @@ rec {
|
||||
This behavior is deprecated and will throw an error in the future.''
|
||||
(let
|
||||
preLen = stringLength prefix;
|
||||
sLen = stringLength str;
|
||||
in
|
||||
if substring 0 preLen str == prefix then
|
||||
substring preLen (sLen - preLen) str
|
||||
# -1 will take the string until the end
|
||||
substring preLen (-1) str
|
||||
else
|
||||
str);
|
||||
|
||||
|
@ -349,6 +349,27 @@ runTests {
|
||||
expected = true;
|
||||
};
|
||||
|
||||
testRemovePrefixExample1 = {
|
||||
expr = removePrefix "foo." "foo.bar.baz";
|
||||
expected = "bar.baz";
|
||||
};
|
||||
testRemovePrefixExample2 = {
|
||||
expr = removePrefix "xxx" "foo.bar.baz";
|
||||
expected = "foo.bar.baz";
|
||||
};
|
||||
testRemovePrefixEmptyPrefix = {
|
||||
expr = removePrefix "" "foo";
|
||||
expected = "foo";
|
||||
};
|
||||
testRemovePrefixEmptyString = {
|
||||
expr = removePrefix "foo" "";
|
||||
expected = "";
|
||||
};
|
||||
testRemovePrefixEmptyBoth = {
|
||||
expr = removePrefix "" "";
|
||||
expected = "";
|
||||
};
|
||||
|
||||
testNormalizePath = {
|
||||
expr = strings.normalizePath "//a/b//c////d/";
|
||||
expected = "/a/b/c/d/";
|
||||
|
Loading…
Reference in New Issue
Block a user