Skip to content

Commit 6c507b9

Browse files
committed
added support for multiple comment characters
By default, it will now use '/!#'. Fixes #456
1 parent 572d9f5 commit 6c507b9

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/json_initialize_arguments.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ logical(LK),intent(in),optional :: unescape_strings
3232
!! string is returned from [[json_get_string]]
3333
!! and similar routines. If true [default],
3434
!! then the string is returned unescaped.
35-
character(kind=CK,len=1),intent(in),optional :: comment_char
36-
!! If present, this character is used
35+
character(kind=CK,len=*),intent(in),optional :: comment_char
36+
!! If present, these characters are used
3737
!! to denote comments in the JSON file,
3838
!! which will be ignored if present.
39-
!! Example: `!` or `#`. Setting this
39+
!! Example: `!`, `#`, or `/!#`. Setting this
4040
!! to a blank string disables the
41-
!! ignoring of comments. (Default is `!`).
41+
!! ignoring of comments. (Default is `/!#`).
4242
integer(IK),intent(in),optional :: path_mode
4343
!! How the path strings are interpreted in the
4444
!! `get_by_path` routines:

src/json_value_module.F90

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ module json_value_module
216216
!! then the string is returned unescaped.
217217

218218
logical(LK) :: allow_comments = .true. !! if true, any comments will be ignored when
219-
!! parsing a file. The comment token is defined
219+
!! parsing a file. The comment tokens are defined
220220
!! by the `comment_char` character variable.
221-
character(kind=CK,len=1) :: comment_char = CK_'!' !! comment token when
222-
!! `allow_comments` is true.
223-
!! Examples: '`!`' or '`#`'.
221+
character(kind=CK,len=:),allocatable :: comment_char !! comment tokens when
222+
!! `allow_comments` is true.
223+
!! Examples: '`!`' or '`#`'.
224+
!! Default is `CK_'/!#'`.
224225

225226
integer(IK) :: path_mode = 1_IK !! How the path strings are interpreted in the
226227
!! `get_by_path` routines:
@@ -1081,7 +1082,7 @@ subroutine json_initialize(me,&
10811082
! [an empty string disables comments]
10821083
if (present(comment_char)) then
10831084
me%allow_comments = comment_char/=CK_''
1084-
me%comment_char = comment_char
1085+
me%comment_char = trim(adjustl(comment_char))
10851086
end if
10861087

10871088
! path separator:
@@ -9736,6 +9737,12 @@ subroutine json_parse_file(json, file, p, unit)
97369737
! clear any exceptions and initialize:
97379738
call json%initialize()
97389739

9740+
if (json%allow_comments .and. .not. allocated(json%comment_char)) then
9741+
! comments are enabled, but user hasn't set the comment char,
9742+
! so in this case use the default:
9743+
json%comment_char = CK_'/!#'
9744+
end if
9745+
97399746
if ( present(unit) ) then
97409747

97419748
if (unit==0) then
@@ -9846,6 +9853,12 @@ subroutine json_parse_string(json, p, str)
98469853
! clear any exceptions and initialize:
98479854
call json%initialize()
98489855

9856+
if (json%allow_comments .and. .not. allocated(json%comment_char)) then
9857+
! comments are enabled, but user hasn't set the comment char,
9858+
! so in this case use the default:
9859+
json%comment_char = CK_'/!#'
9860+
end if
9861+
98499862
! create the value and associate the pointer
98509863
call json_value_create(p)
98519864

@@ -11435,7 +11448,7 @@ subroutine pop_char(json,unit,str,skip_ws,skip_comments,eof,popped)
1143511448

1143611449
end if
1143711450

11438-
if (ignore_comments .and. (parsing_comment .or. c == json%comment_char) ) then
11451+
if (ignore_comments .and. (parsing_comment .or. scan(c,json%comment_char,kind=IK)>0_IK) ) then
1143911452

1144011453
! skipping the comment
1144111454
parsing_comment = .true.

0 commit comments

Comments
 (0)