fill Function

private elemental function fill(self, width, right, filling_char) result(filled)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self
integer, intent(in) :: width
logical, intent(in), optional :: right
character(kind=CK, len=1), intent(in), optional :: filling_char

Return Value type(string)


Called by

proc~~fill~~CalledByGraph proc~fill string%fill proc~add_stream_attributes xml_tag%add_stream_attributes proc~add_stream_attributes->proc~fill program~volatile_doctest~323 volatile_doctest program~volatile_doctest~323->proc~fill program~volatile_doctest~548 volatile_doctest program~volatile_doctest~548->proc~fill none~add_attributes xml_tag%add_attributes none~add_attributes->proc~add_stream_attributes proc~set xml_tag%set proc~set->proc~add_stream_attributes proc~create_tag_flat create_tag_flat proc~create_tag_flat->proc~set proc~create_tag_nested create_tag_nested proc~create_tag_nested->proc~set program~add_attributes add_attributes program~add_attributes->none~add_attributes program~create_tag create_tag program~create_tag->proc~set interface~xml_tag xml_tag interface~xml_tag->proc~create_tag_flat interface~xml_tag->proc~create_tag_nested

Source Code

   elemental function fill(self, width, right, filling_char) result(filled)
   !< Pad string on the left (or right) with zeros (or other char) to fill width.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(4)
   !< astring = 'this is string example....wow!!!'
   !< test_passed(1) = astring%fill(width=40)//''=='00000000this is string example....wow!!!'
   !< test_passed(2) = astring%fill(width=50)//''=='000000000000000000this is string example....wow!!!'
   !< test_passed(3) = astring%fill(width=50, right=.true.)//''=='this is string example....wow!!!000000000000000000'
   !< test_passed(4) = astring%fill(width=40, filling_char='*')//''=='********this is string example....wow!!!'
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self          !< The string.
   integer,                   intent(in)           :: width         !< Final width of filled string.
   logical,                   intent(in), optional :: right         !< Fill on the right instead of left.
   character(kind=CK, len=1), intent(in), optional :: filling_char  !< Filling character (default "0").
   type(string)                                    :: filled        !< Filled string.
   logical                                         :: right_        !< Fill on the right instead of left, local variable.
   character(kind=CK, len=1)                       :: filling_char_ !< Filling character (default "0"), local variable.

   if (allocated(self%raw)) then
      if (width>len(self%raw)) then
         right_ = .false. ; if (present(right)) right_ = right
         filling_char_ = '0' ; if (present(filling_char)) filling_char_ = filling_char
         if (.not.right_) then
            filled%raw = repeat(filling_char_, width-len(self%raw))//self%raw
         else
            filled%raw = self%raw//repeat(filling_char_, width-len(self%raw))
         endif
      endif
   endif
   endfunction fill