Skip to content

Commit f42ccdf

Browse files
Add and use CProverString.ofCharArray
This adds a function for converting char arrays to strings and use it for a String constructor.
1 parent c86885c commit f42ccdf

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,21 @@ public String(char value[]) {
216216
* characters outside the bounds of the {@code value} array
217217
*
218218
* @diffblue.limitedSupport
219-
* Does not throw exceptions.
220219
* @diffblue.untested
221220
*/
222221
public String(char value[], int offset, int count) {
223-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
224-
// if (offset < 0) {
225-
// throw new StringIndexOutOfBoundsException(offset);
226-
// }
227-
// if (count < 0) {
228-
// throw new StringIndexOutOfBoundsException(count);
229-
// }
230-
// // Note: offset or count might be near -1>>>1.
231-
// if (offset > value.length - count) {
232-
// throw new StringIndexOutOfBoundsException(offset + count);
233-
// }
222+
if (offset < 0) {
223+
throw new StringIndexOutOfBoundsException(offset);
224+
}
225+
if (count < 0) {
226+
throw new StringIndexOutOfBoundsException(count);
227+
}
228+
// Note: offset or count might be near -1>>>1.
229+
if (offset > value.length - count) {
230+
throw new StringIndexOutOfBoundsException(offset + count);
231+
}
232+
// DIFFBLUE MODEL LIBRARY Use CProverString function instead of array copy
233+
this(CProverString.ofCharArray(value, offset, count));
234234
// this.value = Arrays.copyOfRange(value, offset, offset+count);
235235
}
236236

src/main/java/org/cprover/CProverString.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,26 @@ public static StringBuffer insert(
388388
return CProver.nondetWithoutNullForNotModelled();
389389
}
390390

391+
/**
392+
* Converts an array of characters to a string. This uses a loop and
393+
* therefore in test-generation the {@code count} will be limited by the
394+
* unwind parameter.
395+
* This assumes {@code value} is not null, its length is greater or equal to
396+
* {@code offset + count} and {@code offset} and {@code count} are positive.
397+
*
398+
* @param value Array of characters which is the source to copy from
399+
* @param offset Index in {@code value} of the first character to copy
400+
* @param count Number of characters to copy
401+
* @return The created String
402+
*/
403+
public static String ofCharArray(char value[], int offset, int count) {
404+
org.cprover.CProver.assume(value != null);
405+
org.cprover.CProver.assume(value.length - count >= offset
406+
&& offset >= 0 && count >= 0);
407+
StringBuilder builder = new StringBuilder();
408+
for(int i = 0; i < count; i++) {
409+
builder.append(value[offset + i]);
410+
}
411+
return builder.toString();
412+
}
391413
}

0 commit comments

Comments
 (0)