Skip to content

Commit f5fc8db

Browse files
committed
Nix.Builtins: replaceStrings: comment a bit, readability
1 parent 9fb1df4 commit f5fc8db

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/Nix/Builtins.hs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -929,47 +929,53 @@ replaceStrings
929929
-> NValue t f m
930930
-> NValue t f m
931931
-> m (NValue t f m)
932+
-- `ns*` goes for NixString here, which are with context - remember
932933
replaceStrings tfrom tto ts = fromValue (Deeper tfrom) >>= \(nsFrom :: [NixString]) ->
933934
fromValue (Deeper tto) >>= \(nsTo :: [NixString]) ->
934935
fromValue ts >>= \(ns :: NixString) -> do
935-
let from = fmap stringIgnoreContext nsFrom
936936
when (length nsFrom /= length nsTo)
937937
$ throwError
938938
$ ErrorCall
939939
$ "'from' and 'to' arguments to 'replaceStrings'"
940940
<> " have different lengths"
941941
let
942-
lookupPrefix s = do
942+
943+
from = fmap stringIgnoreContext nsFrom
944+
945+
lookupPrefix s = do -- monadic context handles Maybe result here, aka if Nothing returned
943946
(prefix, replacement) <- find ((`Text.isPrefixOf` s) . fst)
944947
$ zip from nsTo
945948
let rest = Text.drop (Text.length prefix) s
946949
pure (prefix, replacement, rest)
950+
947951
finish b =
948952
makeNixString (LazyText.toStrict $ Builder.toLazyText b)
953+
949954
go orig result ctx = case lookupPrefix orig of
950955
Nothing -> case Text.uncons orig of
951956
Nothing -> finish result ctx
952957
Just (h, t) -> go t (result <> Builder.singleton h) ctx
953958
Just (prefix, replacementNS, rest) ->
954-
let replacement = stringIgnoreContext replacementNS
955-
newCtx = NixString.getContext replacementNS
956-
in case prefix of
957-
"" -> case Text.uncons rest of
958-
Nothing -> finish
959+
case prefix of
960+
"" -> case Text.uncons rest of
961+
Nothing -> finish
962+
(result <> Builder.fromText replacement)
963+
(ctx <> newCtx)
964+
Just (h, t) -> go
965+
t
966+
(mconcat
967+
[ result
968+
, Builder.fromText replacement
969+
, Builder.singleton h
970+
]
971+
)
972+
(ctx <> newCtx)
973+
_ -> go rest
959974
(result <> Builder.fromText replacement)
960975
(ctx <> newCtx)
961-
Just (h, t) -> go
962-
t
963-
(mconcat
964-
[ result
965-
, Builder.fromText replacement
966-
, Builder.singleton h
967-
]
968-
)
969-
(ctx <> newCtx)
970-
_ -> go rest
971-
(result <> Builder.fromText replacement)
972-
(ctx <> newCtx)
976+
where
977+
replacement = stringIgnoreContext replacementNS
978+
newCtx = NixString.getContext replacementNS
973979
toValue
974980
$ go (stringIgnoreContext ns) mempty
975981
$ NixString.getContext ns

0 commit comments

Comments
 (0)