Skip to content

Commit e85758f

Browse files
committed
Nix.Builtins: replaceStrings: m refactor: m upd
1 parent 84c9bee commit e85758f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Nix/Builtins.hs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ genericClosure = fromValue @(AttrSet (NValue t f m)) >=> \s ->
929929
-- 2. List of strings to replace corresponding match occurance. (arg 1 & 2 lists matched by index)
930930
-- 3. String to process
931931
-- -> returns the string with requested replacements.
932-
--
932+
--
933933
-- Example:
934934
-- builtins.replaceStrings ["ll" "e"] [" " "i"] "Hello world" == "Hi o world".
935935
replaceStrings
@@ -952,16 +952,17 @@ replaceStrings tfrom tto ts =
952952
-- So it should be more effective to pass the context as the first argument.
953953
-- And moreover, the `passOneCharNgo` passively passes the context, to context can be removed from it and inherited directly.
954954
-- Then the solution would've been elegant, but the Nix bug prevents elegant implementation.
955-
go remaining processed ctx =
955+
go input output ctx =
956956
case maybePrefixMatch of
957957
Nothing ->
958958
-- Pass the chars until match
959-
passOneCharNgo remaining processed ctx
960-
Just (matched, replacementNS, tailNS) -> replace
959+
passOneCharNgo input output ctx
960+
Just (matched, replacementNS, unprocessedInput) ->
961+
sendReplaceToOutput
961962

962963
where
963-
replace = replaceWithNixBug tailNS updatedProcessed updatedCtx
964-
replaceWithNixBug =
964+
sendReplaceToOutput = withNixBug unprocessedInput updatedOutput updatedCtx
965+
withNixBug =
965966
bool
966967
go
967968

@@ -975,18 +976,18 @@ replaceStrings tfrom tto ts =
975976
passOneCharNgo
976977
(matched == mempty)
977978

978-
updatedProcessed = processed <> replacement
979+
updatedOutput = output <> replacement
979980
replacement = Builder.fromText $ stringIgnoreContext replacementNS
980981

981-
updatedCtx = ctx <> replacementCtx
982+
updatedCtx = ctx <> replacementCtx
982983
replacementCtx = NixString.getContext replacementNS
983984

984985
where
985986
-- When prefix matched something - returns (match, replacement, reminder)
986987
maybePrefixMatch :: Maybe (Text, NixString, Text)
987-
maybePrefixMatch = formMatchReplaceNTail <$> find ((`Text.isPrefixOf` remaining) . fst) fromKeysToValsMap
988+
maybePrefixMatch = formMatchReplaceTailInfo <$> find ((`Text.isPrefixOf` input) . fst) fromKeysToValsMap
988989

989-
formMatchReplaceNTail = (\(m, r) -> (m, r, Text.drop (Text.length m) remaining))
990+
formMatchReplaceTailInfo = (\(m, r) -> (m, r, Text.drop (Text.length m) input))
990991

991992
fromKeysToValsMap = zip (fmap stringIgnoreContext fromKeys) toVals
992993

@@ -996,6 +997,8 @@ replaceStrings tfrom tto ts =
996997
(\(c, t) -> go t (output <> Builder.singleton c)) -- If there are chars - pass one char & continue
997998
(Text.uncons input) -- chip first char
998999

1000+
-- 2021-02-18: NOTE: rly?: toStrict . toLazyText
1001+
-- Maybe `text-builder`, `text-show`?
9991002
finish = makeNixString . LazyText.toStrict . Builder.toLazyText
10001003

10011004
toValue $ go (stringIgnoreContext string) mempty $ NixString.getContext string

0 commit comments

Comments
 (0)