Skip to content

Commit b545958

Browse files
authored
Merge pull request #27 from diffblue/danpoe/refactor/primitive-types-to-string
Refactor conversions from primitive types to String
2 parents ccf0abb + d47727c commit b545958

File tree

10 files changed

+118
-32
lines changed

10 files changed

+118
-32
lines changed

src/main/java/java/lang/Boolean.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public static Boolean valueOf(String s) {
176176
* @param b the boolean to be converted
177177
* @return the string representation of the specified {@code boolean}
178178
* @since 1.4
179+
*
180+
* @diffblue.fullSupport
179181
*/
180182
public static String toString(boolean b) {
181183
return b ? "true" : "false";

src/main/java/java/lang/Byte.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package java.lang;
2727

2828
import org.cprover.CProver;
29+
import org.cprover.CProverString;
2930

3031
/**
3132
*
@@ -71,9 +72,11 @@ public final class Byte extends Number implements Comparable<Byte> {
7172
* @param b the {@code byte} to be converted
7273
* @return the string representation of the specified {@code byte}
7374
* @see java.lang.Integer#toString(int)
75+
*
76+
* @diffblue.fullSupport
7477
*/
7578
public static String toString(byte b) {
76-
return Integer.toString((int)b, 10);
79+
return CProverString.toString((int)b);
7780
}
7881

7982
/**

src/main/java/java/lang/Character.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Locale;
3232

3333
import org.cprover.CProver;
34+
import org.cprover.CProverString;
3435

3536
/**
3637
* The {@code Character} class wraps a value of the primitive
@@ -4651,9 +4652,12 @@ public String toString() {
46514652
* @param c the {@code char} to be converted
46524653
* @return the string representation of the specified {@code char}
46534654
* @since 1.4
4655+
*
4656+
* @diffblue.fullSupport
46544657
*/
46554658
public static String toString(char c) {
4656-
return String.valueOf(c);
4659+
char data[] = {c};
4660+
return CProverString.ofCharArray(data, 0, 1);
46574661
}
46584662

46594663
/**

src/main/java/java/lang/Double.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,14 @@ public final class Double extends Number implements Comparable<Double> {
208208
*
209209
* @param d the {@code double} to be converted.
210210
* @return a string representation of the argument.
211-
* @diffblue.noSupport
211+
*
212+
* @diffblue.limitedSupport
213+
* The precision in the produced string may not match that of the
214+
* actual program.
212215
*/
213216
public static String toString(double d) {
214217
// return FloatingDecimal.toJavaFormatString(d);
215-
return String.valueOf(d);
218+
return CProverString.toString(d);
216219
}
217220

218221
/**

src/main/java/java/lang/Float.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import sun.misc.FloatConsts;
3232
import sun.misc.DoubleConsts;
3333
import org.cprover.CProver;
34+
import org.cprover.CProverString;
3435

3536
/**
3637
* The {@code Float} class wraps a value of primitive type
@@ -204,14 +205,13 @@ public final class Float extends Number implements Comparable<Float> {
204205
*
205206
* @param f the float to be converted.
206207
* @return a string representation of the argument.
207-
* @diffblue.noSupport
208+
*
209+
* @diffblue.limitedSupport
210+
* The precision in the produced string may not match that of the
211+
* actual program.
208212
*/
209213
public static String toString(float f) {
210-
// DIFFBLUE MODEL LIBRARY
211-
// removed for compatibility with Java 9 and newer
212-
// return FloatingDecimal.toJavaFormatString(f);
213-
CProver.notModelled();
214-
return CProver.nondetWithNullForNotModelled();
214+
return CProverString.toString(f);
215215
}
216216

217217
/**

src/main/java/java/lang/Integer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ public final class Integer extends Number implements Comparable<Integer> {
119119
* @return a string representation of the argument in the specified radix.
120120
* @see java.lang.Character#MAX_RADIX
121121
* @see java.lang.Character#MIN_RADIX
122+
*
123+
* @diffblue.fullSupport
122124
*/
123-
// DIFFBLUE MODEL LIBRARY: Handled internally by CBMC
124125
public static String toString(int i, int radix) {
125-
return CProver.nondetWithoutNullForNotModelled();
126+
return CProverString.toString(i, radix);
126127
}
127128

128129
/**
@@ -287,11 +288,11 @@ public static String toBinaryString(int i) {
287288
*
288289
* @param i an integer to be converted.
289290
* @return a string representation of the argument in base&nbsp;10.
291+
*
292+
* @diffblue.fullSupport
290293
*/
291-
// DIFFBLUE MODEL LIBRARY: Handled internally by CBMC
292294
public static String toString(int i) {
293-
// this function is handled by cbmc internally
294-
return CProver.nondetWithoutNullForNotModelled();
295+
return CProverString.toString(i);
295296
}
296297

297298
/**

src/main/java/java/lang/Long.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ public final class Long extends Number implements Comparable<Long> {
123123
* @return a string representation of the argument in the specified radix.
124124
* @see java.lang.Character#MAX_RADIX
125125
* @see java.lang.Character#MIN_RADIX
126+
*
127+
* @diffblue.fullSupport
126128
*/
127129
public static String toString(long i, int radix) {
128-
return CProver.nondetWithoutNullForNotModelled();
130+
return CProverString.toString(i, radix);
129131
}
130132

131133
/**
@@ -349,10 +351,11 @@ static String toUnsignedString0(long val, int shift) {
349351
*
350352
* @param i a {@code long} to be converted.
351353
* @return a string representation of the argument in base&nbsp;10.
354+
*
355+
* @diffblue.fullSupport
352356
*/
353357
public static String toString(long i) {
354-
// this function is handled by cbmc internally
355-
return CProver.nondetWithoutNullForNotModelled();
358+
return CProverString.toString(i);
356359
}
357360

358361
/**

src/main/java/java/lang/Short.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package java.lang;
2727

2828
import org.cprover.CProver;
29+
import org.cprover.CProverString;
2930

3031
/**
3132
* The {@code Short} class wraps a value of primitive type {@code
@@ -69,9 +70,11 @@ public final class Short extends Number implements Comparable<Short> {
6970
* @param s the {@code short} to be converted
7071
* @return the string representation of the specified {@code short}
7172
* @see java.lang.Integer#toString(int)
73+
*
74+
* @diffblue.fullSupport
7275
*/
7376
public static String toString(short s) {
74-
return Integer.toString((int)s, 10);
77+
return CProverString.toString((int)s);
7578
}
7679

7780
/**

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4238,9 +4238,7 @@ public static String copyValueOf(char data[]) {
42384238
* @diffblue.fullSupport
42394239
*/
42404240
public static String valueOf(boolean b) {
4241-
// DIFFBLUE MODEL LIBRARY This is treated internally in JBMC
4242-
return CProver.nondetWithoutNullForNotModelled();
4243-
// return b ? "true" : "false";
4241+
return b ? "true" : "false";
42444242
}
42454243

42464244
/**
@@ -4255,8 +4253,8 @@ public static String valueOf(boolean b) {
42554253
* @diffblue.untested
42564254
*/
42574255
public static String valueOf(char c) {
4258-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
4259-
return CProver.nondetWithNullForNotModelled();
4256+
char data[] = {c};
4257+
return CProverString.ofCharArray(data, 0, 1);
42604258
// char data[] = {c};
42614259
// return new String(data, true);
42624260
}
@@ -4275,8 +4273,7 @@ public static String valueOf(char c) {
42754273
* @diffblue.untested
42764274
*/
42774275
public static String valueOf(int i) {
4278-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
4279-
return CProver.nondetWithoutNullForNotModelled();
4276+
return CProverString.toString(i);
42804277
// return Integer.toString(i);
42814278
}
42824279

@@ -4294,8 +4291,7 @@ public static String valueOf(int i) {
42944291
* @diffblue.untested
42954292
*/
42964293
public static String valueOf(long l) {
4297-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
4298-
return CProver.nondetWithoutNullForNotModelled();
4294+
return CProverString.toString(l);
42994295
// return Long.toString(l);
43004296
}
43014297

@@ -4314,8 +4310,7 @@ public static String valueOf(long l) {
43144310
* actual program.
43154311
*/
43164312
public static String valueOf(float f) {
4317-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
4318-
return CProver.nondetWithoutNullForNotModelled();
4313+
return CProverString.toString(f);
43194314
// return Float.toString(f);
43204315
}
43214316

@@ -4334,9 +4329,8 @@ public static String valueOf(float f) {
43344329
* actual program.
43354330
*/
43364331
public static String valueOf(double d) {
4337-
// DIFFBLUE MODEL LIBRARY we cast the number down to float because
43384332
// string solver only knows how to convert floats to string
4339-
return valueOf(CProver.doubleToFloat(d));
4333+
return CProverString.toString(d);
43404334
// return Double.toString(d);
43414335
}
43424336

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,77 @@ public static String format(
348348
String arg4, String arg5, String arg6, String arg7, String arg8, String arg9) {
349349
return CProver.nondetWithoutNullForNotModelled();
350350
}
351+
352+
/**
353+
* Returns a {@code String} object representing the
354+
* specified integer. The argument is converted to signed decimal
355+
* representation and returned as a string.
356+
*
357+
* @param i an integer to be converted.
358+
* @return a string representation of the argument in base&nbsp;10.
359+
*/
360+
public static String toString(int i) {
361+
return CProver.nondetWithoutNullForNotModelled();
362+
}
363+
364+
/**
365+
* Returns a string representation of the first argument in the
366+
* radix specified by the second argument.
367+
*
368+
* @param i an integer to be converted to a string.
369+
* @param radix the radix to use in the string representation.
370+
* @return a string representation of the argument in the specified radix.
371+
*/
372+
public static String toString(int i, int radix) {
373+
return CProver.nondetWithoutNullForNotModelled();
374+
}
375+
376+
/**
377+
* Returns a {@code String} object representing the specified
378+
* {@code long}. The argument is converted to signed decimal
379+
* representation and returned as a string.
380+
*
381+
* @param l a {@code long} to be converted.
382+
* @return a string representation of the argument in base&nbsp;10.
383+
*/
384+
public static String toString(long l) {
385+
return CProver.nondetWithoutNullForNotModelled();
386+
}
387+
388+
/**
389+
* Returns a string representation of the first argument in the
390+
* radix specified by the second argument.
391+
*
392+
* @param l a {@code long} to be converted to a string.
393+
* @param radix the radix to use in the string representation.
394+
* @return a string representation of the argument in the specified radix.
395+
*/
396+
public static String toString(long l, int radix) {
397+
return CProver.nondetWithoutNullForNotModelled();
398+
}
399+
400+
/**
401+
* Returns a string representation of the {@code float}
402+
* argument.
403+
*
404+
* @param f the float to be converted.
405+
* @return a string representation of the argument.
406+
*/
407+
public static String toString(float f) {
408+
return CProver.nondetWithoutNullForNotModelled();
409+
}
410+
411+
/**
412+
* Returns a string representation of the {@code double}
413+
* argument.
414+
*
415+
* The double is converted to a float first as the string solver currently
416+
* can convert float to String but not double to String.
417+
*
418+
* @param d the double to be converted.
419+
* @return a string representation of the argument.
420+
*/
421+
public static String toString(double d) {
422+
return toString(CProver.doubleToFloat(d));
423+
}
351424
}

0 commit comments

Comments
 (0)