From 9db190eb31d4adc412d50bc03574951f9a1f9dae Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 14 Jan 2011 12:47:10 +0000 Subject: * builtins.substring: if "start" is beyond the end of the string, return the empty string. --- src/libexpr/primops.cc | 2 +- tests/lang/eval-okay-substring.exp | 2 +- tests/lang/eval-okay-substring.nix | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index a4812de06519..3e955ea3fe09 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -965,7 +965,7 @@ static void prim_substring(EvalState & state, Value * * args, Value & v) if (start < 0) throw EvalError("negative start position in `substring'"); - mkString(v, string(s, start, len), context); + mkString(v, start >= s.size() ? "" : string(s, start, len), context); } diff --git a/tests/lang/eval-okay-substring.exp b/tests/lang/eval-okay-substring.exp index d936b7e96f94..6aace04b0f57 100644 --- a/tests/lang/eval-okay-substring.exp +++ b/tests/lang/eval-okay-substring.exp @@ -1 +1 @@ -"ooxfoobarybarzobaabb" +"ooxfoobarybarzobaabbc" diff --git a/tests/lang/eval-okay-substring.nix b/tests/lang/eval-okay-substring.nix index 184d72580c51..424af00d9b3b 100644 --- a/tests/lang/eval-okay-substring.nix +++ b/tests/lang/eval-okay-substring.nix @@ -17,3 +17,5 @@ substring 1 2 s + substring 3 0 s + "b" + substring 3 1 s ++ "c" ++ substring 5 10 "perl" -- cgit 1.4.1