Skip to content

Commit 6d2f5d7

Browse files
committed
Nix.Builtins: replaceStrings: m refactor: form "replaceWithNixBug"
1 parent 68c5998 commit 6d2f5d7

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/Nix/Builtins.hs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import qualified Data.Aeson as A
3838
import Data.Align ( alignWith )
3939
import Data.Array
4040
import Data.Bits
41+
import Data.Bool ( bool )
4142
import Data.ByteString ( ByteString )
4243
import qualified Data.ByteString as B
4344
import Data.ByteString.Base16 as Base16
@@ -947,25 +948,33 @@ replaceStrings tfrom tto ts =
947948
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."
948949

949950
let
950-
-- 2021-02-18: NOTE: if there is no match - the process does not changes the context, but walks the string.
951+
-- 2021-02-18: NOTE: if there is no match - the process does not changes the context, simply slides along the string.
951952
-- So it should be more effective to pass the context as the first argument.
952953
-- And moreover, the `passOneCharNgo` passively passes the context, to context can be removed from it and inherited directly.
954+
-- Then the solution would've been elegant, but the Nix bug prevents elegant implementation.
953955
go remaining processed ctx =
954956
case maybePrefixMatch remaining of
955957
Nothing ->
956958
-- Pass the chars until match
957959
passOneCharNgo remaining processed ctx
958-
Just (matched, replacementNS, tailNS) ->
959-
-- Allowing match on "" is a bug-quirk of Nix,
960-
-- 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.
961-
--
962-
-- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
963-
-- " H e l l o w o r l d "
964-
-- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
965-
-- "iHie ioi iwioirilidi"
966-
(if matched == mempty then passOneCharNgo else go) tailNS updatedProcessed updatedCtx
960+
Just (matched, replacementNS, tailNS) -> replace
967961

968962
where
963+
replace = replaceWithNixBug tailNS updatedProcessed updatedCtx
964+
replaceWithNixBug =
965+
bool
966+
go
967+
968+
-- Allowing match on "" is a inherited bug of Nix,
969+
-- 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.
970+
--
971+
-- repl> builtins.replaceStrings ["" "e"] [" " "i"] "Hello world"
972+
-- " H e l l o w o r l d "
973+
-- repl> builtins.replaceStrings ["ll" ""] [" " "i"] "Hello world"
974+
-- "iHie ioi iwioirilidi"
975+
passOneCharNgo
976+
(matched == mempty)
977+
969978
updatedProcessed = processed <> replacement
970979
replacement = Builder.fromText $ stringIgnoreContext replacementNS
971980

0 commit comments

Comments
 (0)