Skip to content

Commit 242aaff

Browse files
authored
Merge pull request #31 from diffblue/smowton/feature/use-is-valid-int
Introduce and use CProverString.isValidInt/Long
2 parents d8e8b42 + d0d583a commit 242aaff

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ public static int parseInt(String s, int radix)
410410
" greater than Character.MAX_RADIX");
411411
}
412412

413+
if(!CProverString.isValidInt(s, radix)) {
414+
throw new NumberFormatException(s + " isn't a valid integer");
415+
}
416+
413417
return CProverString.parseInt(s, radix);
414418
}
415419

@@ -431,11 +435,7 @@ public static int parseInt(String s, int radix)
431435
* parsable integer.
432436
*/
433437
public static int parseInt(String s) throws NumberFormatException {
434-
if (s == null) {
435-
throw new NumberFormatException("null");
436-
}
437-
438-
return CProverString.parseInt(s, 10);
438+
return parseInt(s, 10);
439439
}
440440

441441
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ public static long parseLong(String s, int radix)
473473
throw new NumberFormatException("radix " + radix +
474474
" greater than Character.MAX_RADIX");
475475
}
476+
if (!CProverString.isValidLong(s, radix)) {
477+
throw new NumberFormatException(s + " isn't a valid long");
478+
}
476479

477480
return CProverString.parseLong(s, radix);
478481
}
@@ -502,10 +505,7 @@ public static long parseLong(String s, int radix)
502505
* parsable {@code long}.
503506
*/
504507
public static long parseLong(String s) throws NumberFormatException {
505-
if (s == null) {
506-
throw new NumberFormatException("null");
507-
}
508-
return CProverString.parseLong(s, 10);
508+
return parseLong(s, 10);
509509
}
510510

511511
/**

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,38 @@ public static String toString(double d) {
442442
}
443443

444444
/**
445-
* Exactly as Integer.parseInt, except s is already checked non-null and the
446-
* radix is already checked in-range.
445+
* Exactly as Integer.parseInt, except s is already checked non-null, the
446+
* radix is already checked in-range and 's' is known to be a valid integer
447+
* according to isValidInt below
447448
*/
448449
public static int parseInt(String s, int radix) {
449450
return CProver.nondetInt();
450451
}
451452

452453
/**
453-
* Exactly as Long.parseLong, except s is already checked non-null and the
454-
* radix is already checked in-range.
454+
* Exactly as Long.parseLong, except s is already checked non-null, the
455+
* radix is already checked in-range and 's' is known to be a valid integer
456+
* according to isValidLong below
455457
*/
456458
public static long parseLong(String s, int radix) {
457459
return CProver.nondetLong();
458460
}
461+
462+
/**
463+
* Returns true if string 's' is a valid integer: contains at least one
464+
* digit, perhaps a leading '+' or '-', and doesn't contain invalid chars
465+
* for the given radix.
466+
*/
467+
public static boolean isValidInt(String s, int radix) {
468+
return CProver.nondetBoolean();
469+
}
470+
471+
/**
472+
* Returns true if string 's' is a valid long: contains at least one
473+
* digit, perhaps a leading '+' or '-', and doesn't contain invalid chars
474+
* for the given radix.
475+
*/
476+
public static boolean isValidLong(String s, int radix) {
477+
return CProver.nondetBoolean();
478+
}
459479
}

0 commit comments

Comments
 (0)