Skip to content

Commit 68c5998

Browse files
committed
Nix.Builtins: replaceStrings: m refactor: improve readability
1 parent 42330cd commit 68c5998

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Nix/Builtins.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -940,20 +940,21 @@ replaceStrings
940940
replaceStrings tfrom tto ts =
941941
do
942942
-- NixStrings have context - remember
943-
(fromKeys :: [NixString]) <- fromValue (Deeper tfrom)
943+
(fromKeys :: [NixString]) <- fromValue (Deeper tfrom)
944944
(toVals :: [NixString]) <- fromValue (Deeper tto)
945-
(string :: NixString ) <- fromValue ts
945+
(string :: NixString ) <- fromValue ts
946946

947947
when (length fromKeys /= length toVals) $ throwError $ ErrorCall "builtins.replaceStrings: Arguments `from`&`to` construct a key-value map, so the number of their elements must always match."
948948

949949
let
950950
-- 2021-02-18: NOTE: if there is no match - the process does not changes the context, but walks the string.
951-
-- So it should be more effective to have context as the first argument.
951+
-- So it should be more effective to pass the context as the first argument.
952+
-- And moreover, the `passOneCharNgo` passively passes the context, to context can be removed from it and inherited directly.
952953
go remaining processed ctx =
953954
case maybePrefixMatch remaining of
954955
Nothing ->
955-
-- Chip away chars until match
956-
stepOneCharNgo remaining processed ctx
956+
-- Pass the chars until match
957+
passOneCharNgo remaining processed ctx
957958
Just (matched, replacementNS, tailNS) ->
958959
-- Allowing match on "" is a bug-quirk of Nix,
959960
-- 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.
@@ -962,7 +963,7 @@ replaceStrings tfrom tto ts =
962963
-- " H e l l o w o r l d "
963964
-- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
964965
-- "iHie ioi iwioirilidi"
965-
(if matched == mempty then stepOneCharNgo else go) tailNS updatedProcessed updatedCtx
966+
(if matched == mempty then passOneCharNgo else go) tailNS updatedProcessed updatedCtx
966967

967968
where
968969
updatedProcessed = processed <> replacement
@@ -978,7 +979,7 @@ replaceStrings tfrom tto ts =
978979

979980
fromKeysToValsMap = zip (fmap stringIgnoreContext fromKeys) toVals
980981

981-
stepOneCharNgo text result =
982+
passOneCharNgo text result =
982983
maybe
983984
(finish result) -- The base case - there is no chars left to process -> finish
984985
(\(c, t) -> go t (result <> Builder.singleton c)) -- If there are chars - pass one char & continue

0 commit comments

Comments
 (0)