fill Function

private 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.

 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)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
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”).

Return Value type(string)

Filled string.


Contents

Source Code


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