Fix builtins.substring for negative lengths

This commit is contained in:
Guillaume Maudoux 2020-10-27 04:18:58 +01:00
parent 3ac720b5aa
commit 53b4db2525
1 changed files with 8 additions and 8 deletions

View File

@ -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_
@ -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)