pure function strjoin_strings_array(array, sep, is_col) result(join)
!< Return a string that is a join of columns or rows of an array of strings.
!<
!< The join-separator is set equals to a null string '' if custom separator isn't specified.
!< The is_col is setup the direction of join: within default columns (.true.) or rows(.false.).
!<
!<```fortran
!< type(string), allocatable :: strings_arr(:, :)
!< logical :: test_passed(5)
!<
!< strings_arr = reshape( source = &
!< [string('one'), string('two'), string('three'), &
!< string('ONE'), string('TWO'), string('THREE')], &
!< shape = [3, 2] )
!<
!< test_passed(1) = all( strjoin(array=strings_arr) == &
!< reshape([string('onetwothree'), string('ONETWOTHREE')], &
!< shape = [2]) )
!<
!< test_passed(2) = all( strjoin(array=strings_arr, sep='_') == &
!< reshape([string('one_two_three'), string('ONE_TWO_THREE')], &
!< shape = [2]) )
!<
!< test_passed(3) = all( strjoin(array=strings_arr, is_col=.false.) == &
!< reshape([string('oneONE'), string('twoTWO'), string('threeTHREE')], &
!< shape = [3]) )
!<
!< test_passed(4) = all( strjoin(array=strings_arr, sep='_', is_col=.false.) == &
!< reshape([string('one_ONE'), string('two_TWO'), string('three_THREE')], &
!< shape = [3]) )
!<
!< call strings_arr(2, 1)%free
!< test_passed(5) = all( strjoin(array=strings_arr, sep='_', is_col=.false.) == &
!< reshape([string('one_ONE'), string('TWO'), string('three_THREE')], &
!< shape = [3]) )
!<
!< print '(L1)', all(test_passed)
!<```
!=> T <<<
class(string), intent(in) :: array(1:, 1:) !< Array to be joined.
character(kind=CK, len=*), intent(in), optional :: sep !< Separator.
logical, intent(in), optional :: is_col !< Direction: 'columns' if .true. or 'rows' if .false.
type(string), allocatable :: join(:) !< The join of array.
type(string), allocatable :: slice(:) !< The column or row slice of array
character(kind=CK, len=:), allocatable :: sep_ !< Separator, default value.
logical :: is_col_ !< Direction, default value.
integer :: a, join_size, slice_size !< Counter, sizes of join vector and of slice of array
sep_ = '' ; if (present(sep)) sep_ = sep
is_col_ = .true. ; if (present(is_col)) is_col_ = is_col
if (is_col_) then
join_size = size(array, dim=2)
slice_size = size(array, dim=1)
if (.not.allocated(join)) allocate(join(join_size))
if (.not.allocated(slice)) allocate(slice(slice_size))
do a = 1, join_size
slice(:) = array(:, a)
join(a) = strjoin_strings(slice, sep_)
end do
else
join_size = size(array, dim=1)
slice_size = size(array, dim=2)
if (.not.allocated(join)) allocate(join(join_size))
if (.not.allocated(slice)) allocate(slice(slice_size))
do a = 1, join_size
slice(:) = array(a, :)
join(a) = strjoin_strings(slice, sep_)
end do
endif
endfunction strjoin_strings_array