Skip to content

Commit 14e140f

Browse files
Merge pull request #22 from diffblue/romain/intern
Add a model for String.intern
2 parents 0e6413d + 1d879b0 commit 14e140f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,6 +4271,18 @@ public static String valueOf(double d) {
42714271
// return Double.toString(d);
42724272
}
42734273

4274+
/**
4275+
* Pool of strings used by the {@code String}.intern model.
4276+
*/
4277+
static String[] cproverInternPool = null;
4278+
4279+
/**
4280+
* Number of elements stored in the pool for {@code String.intern} pool.
4281+
* This can be smaller than {@code cproverInternPool.length} which
4282+
* represents the capacity of the array and is fixed for each execution.
4283+
*/
4284+
static int cproverInternPoolSize = 0;
4285+
42744286
/**
42754287
* Returns a canonical representation for the string object.
42764288
* <p>
@@ -4294,11 +4306,29 @@ public static String valueOf(double d) {
42944306
* @return a string that has the same contents as this string, but is
42954307
* guaranteed to be from a pool of unique strings.
42964308
*
4297-
* @diffblue.noSupport
4309+
* @diffblue.limitedSupport literal strings and string-valued constant
4310+
* expressions are not interned.
42984311
*/
4312+
// DIFFBLUE MODEL LIBRARY
42994313
// public native String intern();
43004314
public String intern() {
4301-
CProver.notModelled();
4302-
return CProver.nondetWithoutNullForNotModelled();
4315+
// Initialize the pool if needed
4316+
if (cproverInternPool == null) {
4317+
int capacity = CProver.nondetInt();
4318+
CProver.assume(capacity > 0);
4319+
cproverInternPool = new String[capacity];
4320+
cproverInternPool[0] = this;
4321+
return this;
4322+
}
4323+
// Look for an entry in the pool equal to `this`
4324+
for (int i = 0; i < cproverInternPoolSize; ++i) {
4325+
if (CProverString.equals(cproverInternPool[i], this)) {
4326+
return cproverInternPool[i];
4327+
}
4328+
}
4329+
// Add `this` to the pool
4330+
CProver.assume(cproverInternPool.length > cproverInternPoolSize);
4331+
cproverInternPool[cproverInternPoolSize++] = this;
4332+
return this;
43034333
}
43044334
}

0 commit comments

Comments
 (0)