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 "stringLength" (arity1 $ Text.length . principledStringIgnoreContext)
, add' Normal "sub" (arity2 ((-) @Integer)) , add' Normal "sub" (arity2 ((-) @Integer))
, add' Normal "substring" (substring @e @t @f @m) , add' Normal "substring" substring
, add Normal "tail" tail_ , add Normal "tail" tail_
, add0 Normal "true" (pure $ nvConstant $ NBool True) , add0 Normal "true" (pure $ nvConstant $ NBool True)
, add TopLevel "throw" throw_ , add TopLevel "throw" throw_
@ -668,13 +668,13 @@ splitMatches numDropped (((_, (start, len)) : captures) : mts) haystack =
thunkStr s = nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s)) thunkStr s = nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s))
substring :: forall e t f m. MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString 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 substring start len str = Prim $
then if start < 0
throwError then throwError $ ErrorCall $ "builtins.substring: negative start position: " ++ show start
$ ErrorCall else pure $ principledModifyNixContents (take . Text.drop start) str
$ "builtins.substring: negative start position: " where
++ show start --NOTE: negative values of 'len' are OK, and mean "take everything"
else pure $ principledModifyNixContents (Text.take len . Text.drop start) str take = if len < 0 then id else Text.take len
attrNames attrNames
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m) :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)