@@ -5904,13 +5904,18 @@ subroutine json_value_to_string(json,p,str)
59045904
59055905    integer (IK) ::  iloc  ! ! used to keep track of size of str
59065906                         ! ! since it is being allocated in chunks.
5907+     character (kind= CK,len= :),allocatable  ::  tmp  ! ! temporary buffer for trimming `str`
59075908
59085909    str =  repeat (space, print_str_chunk_size)
59095910    iloc =  0_IK 
59105911    call  json% json_value_print(p, iunit= unit2str, str= str, iloc= iloc, indent= 1_IK , colon= .true. )
59115912
59125913    !  trim the string if necessary:
5913-     if  (len (str)>iloc) str =  str(1 :iloc)
5914+     if  (len (str)>iloc) then 
5915+         allocate (character (kind= CK,len= iloc):: tmp)
5916+         tmp(1 :iloc) =  str
5917+         call  move_alloc(tmp, str)
5918+     endif 
59145919
59155920    end  subroutine  json_value_to_string 
59165921! *****************************************************************************************
@@ -6032,10 +6037,11 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
60326037    character (kind= CK,len= :),allocatable  ::  s_indent ! ! the string of spaces for
60336038                                                     ! ! indenting (see `tab` and `spaces`)
60346039    character (kind= CK,len= :),allocatable  ::  s ! ! the string appended to `str`
6040+     character (kind= CK,len= :),allocatable  ::  buf ! ! temporary buffer for extending `str`
60356041    type (json_value),pointer  ::  element ! ! for getting children
60366042    integer (IK) ::  tab           ! ! number of `tabs` for indenting
60376043    integer (IK) ::  spaces        ! ! number of spaces for indenting
6038-     integer (IK) ::  i              ! ! counter
6044+     integer (IK) ::  i,j            ! ! counter
60396045    integer (IK) ::  count         ! ! number of children
60406046    logical (LK) ::  print_comma   ! ! if the comma will be printed after the value
60416047    logical (LK) ::  write_file    ! ! if we are writing to a file
@@ -6376,7 +6382,12 @@ subroutine write_it(advance,comma,space_after_comma)
63766382            if  (room_left < n) then 
63776383                !  need to add another chunk to fit this string:
63786384                n_chunks_to_add =  max (1_IK , ceiling ( real (len (s)- room_left,RK) /  real (chunk_size,RK), IK ) )
6379-                 str =  str //  repeat (space, print_str_chunk_size* n_chunks_to_add)
6385+                 allocate (character (kind= CK, len= len (str)+ print_str_chunk_size* n_chunks_to_add):: buf)
6386+                 buf(1 :len (str)) =  str
6387+                 do  j =  len (str)+ 1 , len (buf)
6388+                     buf(j:j) =  space
6389+                 enddo 
6390+                 call  move_alloc(buf, str)
63806391            end if 
63816392            !  append s to str:
63826393            str(iloc+1 :iloc+ n) =  s
0 commit comments