Skip to content

Commit

Permalink
dhall-to-nix: Z-encode symbols without going through String
Browse files Browse the repository at this point in the history
We can drop the extra `any needsEncoding` check, since it should be
performant enough on its own when using `Text.concatMap` and
simplifies the code a bit.
  • Loading branch information
Profpatsch committed Feb 15, 2024
1 parent a7132df commit 34a797e
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions dhall-nix/src/Dhall/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -791,18 +791,11 @@ zEncodeSymbol = zEncodeString
-- :+ ZCzp
-- @
zEncodeString :: Text -> Text
zEncodeString cs =
-- Small check to skip the encoding if all chars don’t need encoding.
-- Otherwise we have to convert to `String` and go through Char-by-char.
if Text.any needsEncoding cs
-- This could probably be sped up somehow.
then go (Text.unpack cs)
else cs
where
go [] = []
go (c:cs') = encodeDigitChar c <> go' cs'
go' [] = []
go' (c:cs') = encodeChar c <> go' cs'
zEncodeString cs = case Text.uncons cs of
Nothing -> Text.empty
Just (c, cs') ->
encodeDigitChar c
<> Text.concatMap encodeChar cs'

-- | Whether the given characters needs to be z-encoded.
needsEncoding :: Char -> Bool
Expand Down

0 comments on commit 34a797e

Please sign in to comment.