@@ -3884,6 +3884,50 @@ public char[] toCharArray() {
3884
3884
return result ;
3885
3885
}
3886
3886
3887
+ /**
3888
+ * Helper function for the {@code format} function.
3889
+ * Serialize potential String.format argument to a String.
3890
+ * The returned String is never null.
3891
+ */
3892
+ static String cproverFormatArgument (Object obj ) {
3893
+ if (obj == null ) {
3894
+ return "null" ;
3895
+ }
3896
+ if (obj instanceof String ) {
3897
+ return (String ) obj ;
3898
+ }
3899
+
3900
+ // All primitive types are cast to a long
3901
+ long longValue = 0 ;
3902
+ if (obj instanceof Integer ) {
3903
+ longValue = (Integer ) obj ;
3904
+ } else if (obj instanceof Long ) {
3905
+ longValue = (Long ) obj ;
3906
+ } else if (obj instanceof Float ) {
3907
+ longValue = (long ) ((Float ) obj ).doubleValue ();
3908
+ } else if (obj instanceof Double ) {
3909
+ longValue = (long ) ((Double ) obj ).doubleValue ();
3910
+ } else if (obj instanceof Character ) {
3911
+ char charValue = (Character ) obj ;
3912
+ longValue = (long ) charValue ;
3913
+ } else if (obj instanceof Byte ) {
3914
+ byte byteValue = ((Byte ) obj );
3915
+ longValue = (long ) byteValue ;
3916
+ } else if (obj instanceof Boolean ) {
3917
+ longValue = ((Boolean ) obj ) ? 1 : 0 ;
3918
+ } else {
3919
+ return CProver .nondetWithoutNull ("" );
3920
+ }
3921
+
3922
+ // The long value is encoded using a string of 4 characters
3923
+ StringBuilder builder = new StringBuilder ();
3924
+ builder .append ((char ) (longValue >> 48 & 0xFFFF ));
3925
+ builder .append ((char ) (longValue >> 32 & 0xFFFF ));
3926
+ builder .append ((char ) (longValue >> 16 & 0xFFFF ));
3927
+ builder .append ((char ) (longValue & 0xFFFF ));
3928
+ return builder .toString ();
3929
+ }
3930
+
3887
3931
/**
3888
3932
* Returns a formatted string using the specified format string and
3889
3933
* arguments.
@@ -3942,13 +3986,32 @@ public char[] toCharArray() {
3942
3986
* <li>
3943
3987
* having 5 arguments or more makes the solver slow
3944
3988
* </li>
3989
+ * <li>
3990
+ * the string "null" is interpreted the same way {@code null} would be by
3991
+ * the JDK String.format function, which is correct for the %s format
3992
+ * specifier but not %b for instance.
3993
+ * </li>
3945
3994
* </ul>
3946
3995
* @diffblue.untested
3947
3996
*/
3948
3997
public static String format (String format , Object ... args ) {
3949
- // DIFFBLUE MODEL LIBRARY This is treated internally in CBMC
3950
- return CProver .nondetWithoutNullForNotModelled ();
3951
3998
// return new Formatter().format(format, args).toString();
3999
+ // DIFFBLUE MODEL LIBRARY
4000
+ if (args .length > 10 ) {
4001
+ return CProver .nondetWithoutNull ("" );
4002
+ }
4003
+ String arg0 = args .length > 0 ? cproverFormatArgument (args [0 ]) : "" ;
4004
+ String arg1 = args .length > 1 ? cproverFormatArgument (args [1 ]) : "" ;
4005
+ String arg2 = args .length > 2 ? cproverFormatArgument (args [2 ]) : "" ;
4006
+ String arg3 = args .length > 3 ? cproverFormatArgument (args [3 ]) : "" ;
4007
+ String arg4 = args .length > 4 ? cproverFormatArgument (args [4 ]) : "" ;
4008
+ String arg5 = args .length > 5 ? cproverFormatArgument (args [5 ]) : "" ;
4009
+ String arg6 = args .length > 6 ? cproverFormatArgument (args [6 ]) : "" ;
4010
+ String arg7 = args .length > 7 ? cproverFormatArgument (args [7 ]) : "" ;
4011
+ String arg8 = args .length > 8 ? cproverFormatArgument (args [8 ]) : "" ;
4012
+ String arg9 = args .length > 9 ? cproverFormatArgument (args [9 ]) : "" ;
4013
+ return CProverString .format (format , arg0 , arg1 , arg2 , arg3 , arg4 , arg5 ,
4014
+ arg6 , arg7 , arg8 , arg9 );
3952
4015
}
3953
4016
3954
4017
/**
@@ -3988,12 +4051,12 @@ public static String format(String format, Object... args) {
3988
4051
* @see java.util.Formatter
3989
4052
* @since 1.5
3990
4053
*
3991
- * @diffblue.noSupport
4054
+ * @diffblue.limitedSupport The locale argument is ignored and it has the
4055
+ * same limitations as {@link #format(String, Object...)}.
3992
4056
*/
3993
4057
public static String format (Locale l , String format , Object ... args ) {
3994
4058
// return new Formatter(l).format(format, args).toString();
3995
- CProver .notModelled ();
3996
- return CProver .nondetWithoutNullForNotModelled ();
4059
+ return format (format , args );
3997
4060
}
3998
4061
3999
4062
/**
0 commit comments