Skip to content

Commit 4aa0e99

Browse files
committed
Nix.Builtins: replaceStrings: m refactor: m upd to readability
1 parent 1ddcebc commit 4aa0e99

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/Nix/Builtins.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -956,23 +956,23 @@ replaceStrings tfrom tto ts =
956956
case maybePrefixMatch of
957957
Nothing ->
958958
-- Passively pass the chars
959-
realPassOneChar
959+
passOneChar
960960

961961
Just (matched, replacementNS, unprocessedInput) ->
962-
sendReplaceToOutput
962+
replace
963963

964964
where
965-
sendReplaceToOutput = sendReplaceToOutputWithNixBug unprocessedInput updatedOutput
966-
sendReplaceToOutputWithNixBug =
965+
replace = replaceWithNixBug unprocessedInput updatedOutput
966+
replaceWithNixBug =
967967
bool
968+
(go updatedCtx) -- tail recursion
968969
-- Allowing match on "" is a inherited bug of Nix,
969970
-- when "" is checked - it always matches. And so - when it checks - it always insers a replacement, and then process simply passesthrough the char that was under match.
970971
--
971972
-- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
972973
-- " H e l l o w o r l d "
973974
-- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
974975
-- "iHie ioi iwioirilidi"
975-
(go updatedCtx) -- true tail recursion
976976
bugPassOneChar -- augmented recursion
977977
isNixBugCase
978978

@@ -991,7 +991,7 @@ replaceStrings tfrom tto ts =
991991
bugPassOneChar input output =
992992
maybe
993993
(finish updatedCtx output) -- The base case - there is no chars left to process -> finish
994-
(\(c, i) -> go updatedCtx i (output <> Builder.singleton c)) -- If there are chars - pass one char & continue
994+
(\(c, i) -> go updatedCtx i (output <> Builder.singleton c)) -- If there are chars - pass one char & continue
995995
(Text.uncons input) -- chip first char
996996
where
997997
-- When prefix matched something - returns (match, replacement, reminder)
@@ -1003,7 +1003,7 @@ replaceStrings tfrom tto ts =
10031003
fromKeysToValsMap = zip (fmap stringIgnoreContext fromKeys) toVals
10041004

10051005
-- Not passing args => It is constant that gets embedded into `go` => It is simple `go` tail recursion
1006-
realPassOneChar =
1006+
passOneChar =
10071007
maybe
10081008
(finish ctx output) -- The base case - there is no chars left to process -> finish
10091009
(\(c, i) -> go ctx i (output <> Builder.singleton c)) -- If there are chars - pass one char & continue

0 commit comments

Comments
 (0)