Merge pull request #249206 from tweag/removePrefix-optimise

Optimise `lib.strings.removePrefix`
This commit is contained in:
Maximilian Bosch 2023-08-15 14:46:59 +02:00 committed by GitHub
commit 9f605b9807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -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);

View File

@ -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/";