Skip to content

Commit 6a1017f

Browse files
authored
chore(dafny): Make HasSubString generic (#1549)
1 parent 48d69eb commit 6a1017f

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

StandardLibrary/src/String.dfy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module StandardLibrary.String {
77
import Wrappers
88
import opened UInt
99
import opened Sequence
10+
import opened MemoryMath
1011
export provides Int2String, Base10Int2String, HasSubString, Wrappers, UInt
1112

1213
const Base10: seq<char> := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
@@ -55,27 +56,26 @@ module StandardLibrary.String {
5556
}
5657

5758
/* Returns the index of a substring or None, if the substring is not in the string */
58-
method HasSubString(haystack: string, needle: string)
59+
method HasSubString<T(==)>(haystack: seq<T>, needle: seq<T>)
5960
returns (o: Wrappers.Option<nat>)
6061

6162
ensures o.Some? ==>
6263
&& o.value <= |haystack| - |needle| && haystack[o.value..(o.value + |needle|)] == needle
6364
&& (forall i | 0 <= i < o.value :: haystack[i..][..|needle|] != needle)
6465

65-
ensures |haystack| < |needle| || |haystack| > (UINT64_MAX_LIMIT-1) ==> o.None?
66+
ensures |haystack| < |needle| ==> o.None?
6667

6768
ensures o.None? && |needle| <= |haystack| && |haystack| <= (UINT64_MAX_LIMIT-1) ==>
6869
(forall i | 0 <= i <= (|haystack|-|needle|) :: haystack[i..][..|needle|] != needle)
6970
{
70-
if |haystack| < |needle| {
71+
SequenceIsSafeBecauseItIsInMemory(haystack);
72+
SequenceIsSafeBecauseItIsInMemory(needle);
73+
if |haystack| as uint64 < |needle| as uint64 {
7174
return Wrappers.None;
7275
}
7376

74-
// `-1` is needed because of how `limit` is calculated below
75-
expect |haystack| <= (UINT64_MAX_LIMIT-1);
76-
7777
var size : uint64 := |needle| as uint64;
78-
var limit: uint64 := |haystack| as uint64 - size + 1;
78+
var limit: uint64 := Add(|haystack| as uint64 - size, 1);
7979

8080
for index := 0 to limit
8181
invariant forall i | 0 <= i < index :: haystack[i..][..size] != needle

0 commit comments

Comments
 (0)