Skip to content

Commit ac1f7d1

Browse files
committed
Fix builtins.substring for negative lengths
1 parent 1c72f5c commit ac1f7d1

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Nix/Builtins.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ builtinsList = sequence
269269
-}
270270
, add' Normal "stringLength" (arity1 $ Text.length . principledStringIgnoreContext)
271271
, add' Normal "sub" (arity2 ((-) @Integer))
272-
, add' Normal "substring" (substring @e @t @f @m)
272+
, add' Normal "substring" substring
273273
, add Normal "tail" tail_
274274
, add0 Normal "true" (pure $ nvConstant $ NBool True)
275275
, add TopLevel "throw" throw_
@@ -668,13 +668,13 @@ splitMatches numDropped (((_, (start, len)) : captures) : mts) haystack =
668668
thunkStr s = nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s))
669669

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

679679
attrNames
680680
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)

0 commit comments

Comments
 (0)