Skip to content

Commit 42b32df

Browse files
Merge pull request #10 from diffblue/romain/hash-code
Add a model for String.hashCode
2 parents 14e140f + 0dd01f7 commit 42b32df

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/main/java/java/lang/String.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,13 +2087,10 @@ public boolean endsWith(String suffix) {
20872087
*
20882088
* @return a hash code value for this object.
20892089
*
2090-
* @diffblue.limitedSupport
2091-
* We guarantee that two strings with different hash code have different
2092-
* content, but the exact value will not correspond to the actual one.
2090+
* @diffblue.fullSupport length of the string is limited by unwind value
20932091
*/
20942092
public int hashCode() {
2095-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
2096-
return CProver.nondetInt();
2093+
// DIFFBLUE MODEL LIBRARY
20972094
// int h = hash;
20982095
// if (h == 0 && value.length > 0) {
20992096
// char val[] = value;
@@ -2104,6 +2101,12 @@ public int hashCode() {
21042101
// hash = h;
21052102
// }
21062103
// return h;
2104+
int len = length();
2105+
int h = 0;
2106+
for (int i = 0; i < len; i++) {
2107+
h = 31 * h + CProverString.charAt(this, i);
2108+
}
2109+
return h;
21072110
}
21082111

21092112
/**

0 commit comments

Comments
 (0)