@@ -5904,13 +5904,18 @@ subroutine json_value_to_string(json,p,str)
5904
5904
5905
5905
integer (IK) :: iloc ! ! used to keep track of size of str
5906
5906
! ! since it is being allocated in chunks.
5907
+ character (kind= CK,len= :),allocatable :: tmp ! ! temporary buffer for trimming `str`
5907
5908
5908
5909
str = repeat (space, print_str_chunk_size)
5909
5910
iloc = 0_IK
5910
5911
call json% json_value_print(p, iunit= unit2str, str= str, iloc= iloc, indent= 1_IK , colon= .true. )
5911
5912
5912
5913
! 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
5914
5919
5915
5920
end subroutine json_value_to_string
5916
5921
! *****************************************************************************************
@@ -6032,10 +6037,11 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
6032
6037
character (kind= CK,len= :),allocatable :: s_indent ! ! the string of spaces for
6033
6038
! ! indenting (see `tab` and `spaces`)
6034
6039
character (kind= CK,len= :),allocatable :: s ! ! the string appended to `str`
6040
+ character (kind= CK,len= :),allocatable :: buf ! ! temporary buffer for extending `str`
6035
6041
type (json_value),pointer :: element ! ! for getting children
6036
6042
integer (IK) :: tab ! ! number of `tabs` for indenting
6037
6043
integer (IK) :: spaces ! ! number of spaces for indenting
6038
- integer (IK) :: i ! ! counter
6044
+ integer (IK) :: i,j ! ! counter
6039
6045
integer (IK) :: count ! ! number of children
6040
6046
logical (LK) :: print_comma ! ! if the comma will be printed after the value
6041
6047
logical (LK) :: write_file ! ! if we are writing to a file
@@ -6376,7 +6382,12 @@ subroutine write_it(advance,comma,space_after_comma)
6376
6382
if (room_left < n) then
6377
6383
! need to add another chunk to fit this string:
6378
6384
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)
6380
6391
end if
6381
6392
! append s to str:
6382
6393
str(iloc+1 :iloc+ n) = s
0 commit comments