Skip to content

Commit 42330cd

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

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/Nix/Builtins.hs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -940,50 +940,53 @@ replaceStrings
940940
replaceStrings tfrom tto ts =
941941
do
942942
-- NixStrings have context - remember
943-
(nsFromKeys :: [NixString]) <- fromValue (Deeper tfrom)
944-
(nsToVals :: [NixString]) <- fromValue (Deeper tto)
945-
(ns :: NixString ) <- fromValue ts
943+
(fromKeys :: [NixString]) <- fromValue (Deeper tfrom)
944+
(toVals :: [NixString]) <- fromValue (Deeper tto)
945+
(string :: NixString ) <- fromValue ts
946946

947-
when (length nsFromKeys /= length nsToVals) $ throwError $ ErrorCall "builtins.replaceStrings: Arguments `from`&`to` construct a key-value map, so the number of their elements must always match."
947+
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
950-
go remainder processed ctx =
951-
case maybePrefixMatch remainder of
950+
-- 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.
952+
go remaining processed ctx =
953+
case maybePrefixMatch remaining of
952954
Nothing ->
953955
-- Chip away chars until match
954-
stepOneCharNgo remainder processed ctx
955-
Just (matched, replacementNS, rest) ->
956+
stepOneCharNgo remaining processed ctx
957+
Just (matched, replacementNS, tailNS) ->
956958
-- Allowing match on "" is a bug-quirk of Nix,
957959
-- 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.
958960
--
959961
-- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
960962
-- " H e l l o w o r l d "
961963
-- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
962964
-- "iHie ioi iwioirilidi"
963-
(if matched == mempty then stepOneCharNgo else go) rest updProcessed updCtx
965+
(if matched == mempty then stepOneCharNgo else go) tailNS updatedProcessed updatedCtx
964966

965967
where
968+
updatedProcessed = processed <> replacement
966969
replacement = Builder.fromText $ stringIgnoreContext replacementNS
967-
updProcessed = processed <> replacement
968970

971+
updatedCtx = ctx <> replacementCtx
969972
replacementCtx = NixString.getContext replacementNS
970-
updCtx = ctx <> replacementCtx
971973

972974
where
975+
-- When prefix matched something - returns (match, replacement, reminder)
976+
maybePrefixMatch :: Text -> Maybe (Text, NixString, Text)
977+
maybePrefixMatch src = (\(m, r) -> (m, r, Text.drop (Text.length m) src)) <$> find ((`Text.isPrefixOf` src) . fst) fromKeysToValsMap
978+
979+
fromKeysToValsMap = zip (fmap stringIgnoreContext fromKeys) toVals
980+
973981
stepOneCharNgo text result =
974982
maybe
975983
(finish result) -- The base case - there is no chars left to process -> finish
976-
(\(c, t) -> go t (result <> Builder.singleton c))
977-
(Text.uncons text) -- chip one char
984+
(\(c, t) -> go t (result <> Builder.singleton c)) -- If there are chars - pass one char & continue
985+
(Text.uncons text) -- chip first char
978986

979987
finish = makeNixString . LazyText.toStrict . Builder.toLazyText
980988

981-
-- When prefix matched something - returns (match, replacement, reminder)
982-
maybePrefixMatch src = (\(m, r) -> (m, r, Text.drop (Text.length m) src)) <$> find ((`Text.isPrefixOf` src) . fst) matchReplaceMap
983-
984-
matchReplaceMap = zip (fmap stringIgnoreContext nsFromKeys) nsToVals
985-
986-
toValue $ go (stringIgnoreContext ns) mempty $ NixString.getContext ns
989+
toValue $ go (stringIgnoreContext string) mempty $ NixString.getContext string
987990

988991
removeAttrs
989992
:: forall e t f m

0 commit comments

Comments
 (0)