Skip to content

Commit 7485ca4

Browse files
committed
Nix.Builtins: replaceStrings: m refactor: final refactor & comments
1 parent 40516d6 commit 7485ca4

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

src/Nix/Builtins.hs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,10 @@ genericClosure = fromValue @(AttrSet (NValue t f m)) >=> \s ->
924924
fmap (t :) <$> go op (ts <> ys) (S.insert (WValue k') ks)
925925

926926
-- | Takes:
927-
-- 1. List of expressions to replace.
928-
-- (finds the occurances of them)
929-
-- 2. List of expressions to replace corresponding occurance. (arg 1 & 2 lists matched by index)
930-
-- 3. Expression to process
931-
-- -> returns the expression with requested replacements.
927+
-- 1. List of strings to match.
928+
-- 2. List of strings to replace corresponding match occurance. (arg 1 & 2 lists matched by index)
929+
-- 3. String to process
930+
-- -> returns the string with requested replacements.
932931
--
933932
-- Example:
934933
-- builtins.replaceStrings ["ll" "e"] [" " "i"] "Hello world" == "Hi o world".
@@ -940,57 +939,51 @@ replaceStrings
940939
-> m (NValue t f m)
941940
replaceStrings tfrom tto ts =
942941
do
943-
-- `ns*` goes for NixString here, which are with context - remember
944-
(nsListMatch :: [NixString]) <- fromValue (Deeper tfrom)
945-
(nsListReplace :: [NixString]) <- fromValue (Deeper tto)
946-
(ns :: NixString ) <- fromValue ts
942+
-- NixStrings have context - remember
943+
(nsFromKeys :: [NixString]) <- fromValue (Deeper tfrom)
944+
(nsToVals :: [NixString]) <- fromValue (Deeper tto)
945+
(ns :: NixString ) <- fromValue ts
947946

948-
when (length nsListMatch /= length nsListReplace)
949-
$ throwError $ ErrorCall "builtins.replaceStrings: Arguments `from`&`to` are lists `from` what replace `to` what, so the number of their inhabitanting elements must always match."
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."
950948

951949
let
952-
go remainder processedAccum ctx =
953-
case maybePrefixMatches nsListMatch remainder of
950+
go remainder processed ctx =
951+
case maybePrefixMatch remainder of
954952
Nothing ->
955-
process remainder processedAccum ctx
953+
-- Chip away chars until match
954+
stepOneCharNgo remainder processed ctx
956955
Just (matched, replacementNS, rest) ->
957956
-- Allowing match on "" is a bug-quirk of Nix,
958-
-- when "" is checked - it always matches. And so - if there is no previous matches the "" is replaced with " " and the process simply passesthrough the next char.
957+
-- 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.
959958
--
960959
-- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
961960
-- " H e l l o w o r l d "
962961
-- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
963962
-- "iHie ioi iwioirilidi"
964-
(if matched == mempty then process else go) rest updProcessedAccum newCtx
963+
(if matched == mempty then stepOneCharNgo else go) rest updProcessed updCtx
965964

966965
where
967-
newReplacement = Builder.fromText $ stringIgnoreContext replacementNS
968-
updProcessedAccum = processedAccum <> newReplacement
969-
additionalCtx = NixString.getContext replacementNS
970-
newCtx = ctx <> additionalCtx
966+
replacement = Builder.fromText $ stringIgnoreContext replacementNS
967+
updProcessed = processed <> replacement
968+
969+
replacementCtx = NixString.getContext replacementNS
970+
updCtx = ctx <> replacementCtx
971971

972972
where
973-
process text result =
974-
maybe -- chip one char from text
975-
(finish result)
973+
stepOneCharNgo text result =
974+
maybe
975+
(finish result) -- The base case - there is no chars left to process -> finish
976976
(\(c, t) -> go t (result <> Builder.singleton c))
977-
(Text.uncons text)
977+
(Text.uncons text) -- chip one char
978978

979979
finish = makeNixString . LazyText.toStrict . Builder.toLazyText
980980

981-
maybePrefixMatches nsListMatch src =
982-
do -- monadic context handles Maybe result here, aka if Nothing returned
983-
(from, to) <- find ((`Text.isPrefixOf` src) . fst) matchReplaceMap
984-
let rest = Text.drop (Text.length from) src
985-
pure (from, to, rest)
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
986983

987-
where
988-
textListMatch = fmap stringIgnoreContext nsListMatch
989-
matchReplaceMap = zip textListMatch nsListReplace
984+
matchReplaceMap = zip (fmap stringIgnoreContext nsFromKeys) nsToVals
990985

991-
toValue
992-
$ go (stringIgnoreContext ns) mempty
993-
$ NixString.getContext ns
986+
toValue $ go (stringIgnoreContext ns) mempty $ NixString.getContext ns
994987

995988
removeAttrs
996989
:: forall e t f m

0 commit comments

Comments
 (0)