Skip to content

Commit fcb68b9

Browse files
Add a model of String.format
This uses CProverString.format which is builtin in the string solver.
1 parent 27b9c01 commit fcb68b9

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3750,6 +3750,34 @@ public char[] toCharArray() {
37503750
return result;
37513751
}
37523752

3753+
/**
3754+
* Helper function for the {@code format} function.
3755+
* Serialize potential String.format argument to a String.
3756+
* The returned String is never null.
3757+
*/
3758+
static String cproverFormatArgument(Object obj)
3759+
{
3760+
if(obj == null)
3761+
return "";
3762+
if(obj instanceof String)
3763+
return (String)obj;
3764+
if(obj instanceof Integer)
3765+
return Integer.toHexString((Integer)obj);
3766+
if(obj instanceof Character)
3767+
return String.valueOf(((Character) obj).charValue());
3768+
if(obj instanceof Byte)
3769+
return Integer.toHexString(((Byte) obj).intValue());
3770+
if(obj instanceof Long)
3771+
return Long.toHexString((Long) obj);
3772+
if(obj instanceof Boolean)
3773+
return ((Boolean) obj) ? "true" : "false";
3774+
if(obj instanceof Float)
3775+
return Integer.toHexString((int)((Float) obj).floatValue());
3776+
if(obj instanceof Double)
3777+
return Long.toHexString((long)((Float) obj).floatValue());
3778+
return "";
3779+
}
3780+
37533781
/**
37543782
* Returns a formatted string using the specified format string and
37553783
* arguments.
@@ -3812,9 +3840,20 @@ public char[] toCharArray() {
38123840
* @diffblue.untested
38133841
*/
38143842
public static String format(String format, Object... args) {
3815-
// DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
3816-
return CProver.nondetWithoutNullForNotModelled();
3843+
// DIFFBLUE MODEL LIBRARY
38173844
// return new Formatter().format(format, args).toString();
3845+
String arg1 = args.length > 0 ? cproverFormatArgument(args[0]) : "";
3846+
String arg2 = args.length > 1 ? cproverFormatArgument(args[1]) : "";
3847+
String arg3 = args.length > 2 ? cproverFormatArgument(args[2]) : "";
3848+
String arg4 = args.length > 3 ? cproverFormatArgument(args[3]) : "";
3849+
String arg5 = args.length > 4 ? cproverFormatArgument(args[4]) : "";
3850+
String arg6 = args.length > 5 ? cproverFormatArgument(args[5]) : "";
3851+
String arg7 = args.length > 6 ? cproverFormatArgument(args[6]) : "";
3852+
String arg8 = args.length > 7 ? cproverFormatArgument(args[7]) : "";
3853+
String arg9 = args.length > 8 ? cproverFormatArgument(args[8]) : "";
3854+
String arg10 = args.length > 9 ? cproverFormatArgument(args[9]) : "";
3855+
return CProverString.format(format, arg1, arg2, arg3, arg4, arg5, arg6,
3856+
arg7, arg8, arg9, arg10);
38183857
}
38193858

38203859
/**

0 commit comments

Comments
 (0)