Skip to content

Commit

Permalink
Fix builtins.substring for negative lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
layus committed Nov 18, 2020
1 parent 3ac720b commit 53b4db2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Nix/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ builtinsList = sequence
-}
, add' Normal "stringLength" (arity1 $ Text.length . principledStringIgnoreContext)
, add' Normal "sub" (arity2 ((-) @Integer))
, add' Normal "substring" (substring @e @t @f @m)
, add' Normal "substring" substring
, add Normal "tail" tail_
, add0 Normal "true" (pure $ nvConstant $ NBool True)
, add TopLevel "throw" throw_
Expand Down Expand Up @@ -668,13 +668,13 @@ splitMatches numDropped (((_, (start, len)) : captures) : mts) haystack =
thunkStr s = nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s))

substring :: forall e t f m. MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString
substring start len str = Prim $ if start < 0 --NOTE: negative values of 'len' are OK
then
throwError
$ ErrorCall
$ "builtins.substring: negative start position: "
++ show start
else pure $ principledModifyNixContents (Text.take len . Text.drop start) str
substring start len str = Prim $
if start < 0
then throwError $ ErrorCall $ "builtins.substring: negative start position: " ++ show start
else pure $ principledModifyNixContents (take . Text.drop start) str
where
--NOTE: negative values of 'len' are OK, and mean "take everything"
take = if len < 0 then id else Text.take len

attrNames
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
Expand Down

0 comments on commit 53b4db2

Please sign in to comment.