Skip to content

Commit 3b24364

Browse files
Merge pull request #389 from jacobwilliams/388-integer-parse
Fix integer parse issue
2 parents e287c6d + 14b0c30 commit 3b24364

File tree

2 files changed

+115
-41
lines changed

2 files changed

+115
-41
lines changed

src/json_value_module.F90

+34-41
Original file line numberDiff line numberDiff line change
@@ -10985,60 +10985,53 @@ subroutine parse_number(json, unit, str, value)
1098510985
!get the next character:
1098610986
call json%pop_char(unit, str=str, eof=eof, skip_ws=.true., popped=c)
1098710987

10988-
if (eof) then
10989-
call json%throw_exception('Error in parse_number:'//&
10990-
' Unexpected end of file while parsing number.')
10991-
return
10992-
else
10993-
10994-
select case (c)
10995-
case(CK_'-',CK_'+') !note: allowing a '+' as the first character here.
10988+
select case (c)
10989+
case(CK_'-',CK_'+') !note: allowing a '+' as the first character here.
1099610990

10997-
if (is_integer .and. (.not. first)) is_integer = .false.
10991+
if (is_integer .and. (.not. first)) is_integer = .false.
1099810992

10999-
!add it to the string:
11000-
!tmp = tmp // c !...original
11001-
if (ip>len(tmp)) tmp = tmp // blank_chunk
11002-
tmp(ip:ip) = c
11003-
ip = ip + 1
10993+
!add it to the string:
10994+
!tmp = tmp // c !...original
10995+
if (ip>len(tmp)) tmp = tmp // blank_chunk
10996+
tmp(ip:ip) = c
10997+
ip = ip + 1
1100410998

11005-
case(CK_'.',CK_'E',CK_'e') !can be present in real numbers
10999+
case(CK_'.',CK_'E',CK_'e') !can be present in real numbers
1100611000

11007-
if (is_integer) is_integer = .false.
11001+
if (is_integer) is_integer = .false.
1100811002

11009-
!add it to the string:
11010-
!tmp = tmp // c !...original
11011-
if (ip>len(tmp)) tmp = tmp // blank_chunk
11012-
tmp(ip:ip) = c
11013-
ip = ip + 1
11003+
!add it to the string:
11004+
!tmp = tmp // c !...original
11005+
if (ip>len(tmp)) tmp = tmp // blank_chunk
11006+
tmp(ip:ip) = c
11007+
ip = ip + 1
1101411008

11015-
case(CK_'0':CK_'9') !valid characters for numbers
11009+
case(CK_'0':CK_'9') !valid characters for numbers
1101611010

11017-
!add it to the string:
11018-
!tmp = tmp // c !...original
11019-
if (ip>len(tmp)) tmp = tmp // blank_chunk
11020-
tmp(ip:ip) = c
11021-
ip = ip + 1
11011+
!add it to the string:
11012+
!tmp = tmp // c !...original
11013+
if (ip>len(tmp)) tmp = tmp // blank_chunk
11014+
tmp(ip:ip) = c
11015+
ip = ip + 1
1102211016

11023-
case default
11017+
case default
1102411018

11025-
!push back the last character read:
11026-
call json%push_char(c)
11019+
!push back the last character read:
11020+
call json%push_char(c)
1102711021

11028-
!string to value:
11029-
if (is_integer) then
11030-
ival = json%string_to_int(tmp)
11031-
call json%to_integer(value,ival)
11032-
else
11033-
rval = json%string_to_dble(tmp)
11034-
call json%to_real(value,rval)
11035-
end if
11022+
!string to value:
11023+
if (is_integer) then
11024+
ival = json%string_to_int(tmp)
11025+
call json%to_integer(value,ival)
11026+
else
11027+
rval = json%string_to_dble(tmp)
11028+
call json%to_real(value,rval)
11029+
end if
1103611030

11037-
exit !finished
11031+
exit !finished
1103811032

11039-
end select
11033+
end select
1104011034

11041-
end if
1104211035
if (first) first = .false.
1104311036

1104411037
end do

src/tests/jf_test_39.F90

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
!*****************************************************************************************
2+
!> author: Jacob Williams
3+
! date: 1/26/2019
4+
!
5+
! Module for the 39th unit test.
6+
7+
module jf_test_39_mod
8+
9+
use json_module, CK => json_CK, CDK => json_CDK, IK => json_IK
10+
use, intrinsic :: iso_fortran_env , only: error_unit, output_unit
11+
12+
implicit none
13+
14+
private
15+
public :: test_39
16+
17+
contains
18+
19+
subroutine test_39(error_cnt)
20+
21+
!! Test of some weird (but valid) JSON structures
22+
23+
implicit none
24+
25+
integer,intent(out) :: error_cnt !! report number of errors to caller
26+
27+
type(json_file) :: json
28+
integer :: i !! counter
29+
30+
character(kind=CK,len=*),dimension(6),parameter :: tests = ['"42" ',&
31+
'[42] ',&
32+
'42 ',&
33+
'true ',&
34+
'false',&
35+
'null ' ]
36+
37+
write(error_unit,'(A)') ''
38+
write(error_unit,'(A)') '================================='
39+
write(error_unit,'(A)') ' TEST 39'
40+
write(error_unit,'(A)') '================================='
41+
write(error_unit,'(A)') ''
42+
43+
error_cnt = 0
44+
45+
do i = 1, size(tests)
46+
47+
json = json_file(trim(tests(i)),verbose=.true.,stop_on_error=.true.)
48+
call json%print_file(int(error_unit,IK))
49+
write(error_unit,'(A)') ''
50+
if (json%failed()) then
51+
call json%print_error_message(error_unit)
52+
error_cnt = error_cnt + 1
53+
write(error_unit,'(A)') ' FAILED!'
54+
else
55+
write(error_unit,'(A)') ' Success!'
56+
end if
57+
call json%destroy()
58+
write(error_unit,'(A)') ''
59+
60+
end do
61+
62+
end subroutine test_39
63+
64+
end module jf_test_39_mod
65+
!*****************************************************************************************
66+
67+
#ifndef INTERGATED_TESTS
68+
!*****************************************************************************************
69+
program jf_test_39
70+
71+
!! 39th unit test.
72+
73+
use jf_test_39_mod, only: test_39
74+
implicit none
75+
integer :: n_errors
76+
call test_39(n_errors)
77+
if ( n_errors /= 0) stop 1
78+
79+
end program jf_test_39
80+
!*****************************************************************************************
81+
#endif

0 commit comments

Comments
 (0)