@@ -953,46 +953,12 @@ replaceStrings tfrom tto ts =
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.
955955 go ctx input output =
956- case maybePrefixMatch of
957- Nothing ->
956+ maybe
958957 -- Passively pass the chars
959- passOneChar
960-
961- Just (matched, replacementNS, unprocessedInput) ->
962- replace
963-
964- where
965- replace = replaceWithNixBug unprocessedInput updatedOutput
966- replaceWithNixBug =
967- bool
968- (go updatedCtx) -- tail recursion
969- -- Allowing match on "" is a inherited bug of Nix,
970- -- 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.
971- --
972- -- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
973- -- " H e l l o w o r l d "
974- -- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
975- -- "iHie ioi iwioirilidi"
976- bugPassOneChar -- augmented recursion
977- isNixBugCase
978-
979- isNixBugCase = matched == mempty
980-
981- updatedOutput = output <> replacement
982- replacement = Builder. fromText $ stringIgnoreContext replacementNS
983-
984- updatedCtx = ctx <> replacementCtx
985- replacementCtx = NixString. getContext replacementNS
986-
987- -- The bug modifies the content, so need to pass modified args => bug demands `pass` to be a real function =>
988- -- `go` calls `pass` function
989- -- `pass` calls `go` function
990- -- In other words, bug creates a case of mutual recusion.
991- bugPassOneChar input output =
992- maybe
993- (finish updatedCtx output) -- The base case - there is no chars left to process -> finish
994- (\ (c, i) -> go updatedCtx i (output <> Builder. singleton c)) -- If there are chars - pass one char & continue
995- (Text. uncons input) -- chip first char
958+ passOneChar
959+ replace
960+ maybePrefixMatch
961+
996962 where
997963 -- When prefix matched something - returns (match, replacement, reminder)
998964 maybePrefixMatch :: Maybe (Text , NixString , Text )
@@ -1009,11 +975,43 @@ replaceStrings tfrom tto ts =
1009975 (\ (c, i) -> go ctx i (output <> Builder. singleton c)) -- If there are chars - pass one char & continue
1010976 (Text. uncons input) -- chip first char
1011977
1012-
1013978 -- 2021-02-18: NOTE: rly?: toStrict . toLazyText
1014979 -- Maybe `text-builder`, `text-show`?
1015980 finish ctx output = makeNixString (LazyText. toStrict $ Builder. toLazyText output) ctx
1016981
982+ replace (matched, replacementNS, unprocessedInput) = replaceWithNixBug unprocessedInput updatedOutput
983+
984+ where
985+ replaceWithNixBug =
986+ bool
987+ (go updatedCtx) -- tail recursion
988+ -- Allowing match on "" is a inherited bug of Nix,
989+ -- 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.
990+ --
991+ -- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
992+ -- " H e l l o w o r l d "
993+ -- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
994+ -- "iHie ioi iwioirilidi"
995+ bugPassOneChar -- augmented recursion
996+ isNixBugCase
997+
998+ isNixBugCase = matched == mempty
999+
1000+ updatedOutput = output <> replacement
1001+ replacement = Builder. fromText $ stringIgnoreContext replacementNS
1002+
1003+ updatedCtx = ctx <> replacementCtx
1004+ replacementCtx = NixString. getContext replacementNS
1005+
1006+ -- The bug modifies the content => bug demands `pass` to be a real function =>
1007+ -- `go` calls `pass` function && `pass` calls `go` function
1008+ -- => mutual recusion case, so placed separately.
1009+ bugPassOneChar input output =
1010+ maybe
1011+ (finish updatedCtx output) -- The base case - there is no chars left to process -> finish
1012+ (\ (c, i) -> go updatedCtx i (output <> Builder. singleton c)) -- If there are chars - pass one char & continue
1013+ (Text. uncons input) -- chip first char
1014+
10171015 toValue $ go (NixString. getContext string) (stringIgnoreContext string) mempty
10181016
10191017removeAttrs
0 commit comments