Add a string to the buffer.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(pyplot), | intent(inout) | :: | me |
pyplot handler |
||
| character(len=*), | intent(in) | :: | str |
str to be added to pyplot handler buffer |
subroutine add_str(me,str) class(pyplot), intent(inout) :: me !! pyplot handler character(len=*), intent(in) :: str !! str to be added to pyplot handler buffer integer :: n_old !! current `me%str` length integer :: n_str !! length of input `str` character(len=:),allocatable :: tmp !! tmp string for building the result if (len(str)==0) return ! original !me%str = me%str//str//new_line(' ') ! the above can sometimes cause a stack overflow in the ! intel Fortran compiler, so we replace with this: if (allocated(me%str)) then ! if there is room in the current string, then ! insert str into it. otherwise, allocate a new string ! with enough room and move the old string into it before adding str. if (me%str_len + len(str) + 1 <= len(me%str)) then me%str(me%str_len+1:me%str_len+len(str)) = str me%str_len = me%str_len + len(str) me%str(me%str_len+1:me%str_len+1) = new_line(' ') me%str_len = me%str_len + 1 else n_old = me%str_len n_str = len(str) ! double size to avoid too many allocations allocate(character(len=2*(n_old+n_str)) :: tmp) tmp(1:n_old) = me%str tmp(n_old+1:) = str//new_line(' ') call move_alloc(tmp, me%str) me%str_len = n_old + n_str + 1 end if else allocate(me%str, source = str//new_line(' ')) me%str_len = len(me%str) end if end subroutine add_str