end_with Function

private elemental function end_with(self, suffix, start, end, ignore_null_eof)

Return true if a string ends with a specified suffix.

 type(string) :: astring
 logical      :: test_passed(5)
 astring = 'Hello WorLD!'
 test_passed(1) = astring%end_with(suffix='LD!').eqv..true.
 test_passed(2) = astring%end_with(suffix='lD!').eqv..false.
 test_passed(3) = astring%end_with(suffix='orLD!', start=5).eqv..true.
 test_passed(4) = astring%end_with(suffix='orLD!', start=8, end=12).eqv..true.
 test_passed(5) = astring%end_with(suffix='!').eqv..true.
 print '(L1)', all(test_passed)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self

The string.

character(kind=CK, len=*), intent(in) :: suffix

Searched suffix.

integer, intent(in), optional :: start

Start position into the string.

integer, intent(in), optional :: end

End position into the string.

logical, intent(in), optional :: ignore_null_eof

Ignore null character at the end of file.

Return Value logical

Result of the test.


Called by

proc~~end_with~~CalledByGraph proc~end_with string%end_with proc~split_chunked string%split_chunked proc~split_chunked->proc~end_with proc~write_line string%write_line proc~write_line->proc~end_with program~volatile_doctest~105 volatile_doctest program~volatile_doctest~105->proc~end_with proc~write_lines write_lines proc~write_lines->proc~write_line proc~write_lines~2 string%write_lines proc~write_lines~2->proc~write_line program~stringifor_test_parse_large_csv stringifor_test_parse_large_csv program~stringifor_test_parse_large_csv->proc~split_chunked program~volatile_doctest~75 volatile_doctest program~volatile_doctest~75->proc~split_chunked proc~write_file write_file proc~write_file->proc~write_lines proc~write_file~2 string%write_file proc~write_file~2->proc~write_lines~2 program~stringifor_test_csv_naive_parser stringifor_test_csv_naive_parser program~stringifor_test_csv_naive_parser->proc~write_file program~volatile_doctest~17 volatile_doctest program~volatile_doctest~17->proc~write_file program~volatile_doctest~44 volatile_doctest program~volatile_doctest~44->proc~write_file~2

Source Code

   elemental function end_with(self, suffix, start, end, ignore_null_eof)
   !< Return true if a string ends with a specified suffix.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(5)
   !< astring = 'Hello WorLD!'
   !< test_passed(1) = astring%end_with(suffix='LD!').eqv..true.
   !< test_passed(2) = astring%end_with(suffix='lD!').eqv..false.
   !< test_passed(3) = astring%end_with(suffix='orLD!', start=5).eqv..true.
   !< test_passed(4) = astring%end_with(suffix='orLD!', start=8, end=12).eqv..true.
   !< test_passed(5) = astring%end_with(suffix='!').eqv..true.
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self             !< The string.
   character(kind=CK, len=*), intent(in)           :: suffix           !< Searched suffix.
   integer,                   intent(in), optional :: start            !< Start position into the string.
   integer,                   intent(in), optional :: end              !< End position into the string.
   logical,                   intent(in), optional :: ignore_null_eof  !< Ignore null character at the end of file.
   logical                                         :: end_with         !< Result of the test.
   integer                                         :: start_           !< Start position into the string, local variable.
   integer                                         :: end_             !< End position into the string, local variable.
   logical                                         :: ignore_null_eof_ !< Ignore null character at the end of file, local variable.

   end_with = .false.
   if (allocated(self%raw)) then
      start_           = 1             ; if (present(start))           start_           = start
      end_             = len(self%raw) ; if (present(end))             end_             = end
      ignore_null_eof_ = .false.       ; if (present(ignore_null_eof)) ignore_null_eof_ = ignore_null_eof
      if (ignore_null_eof_.and.(self%raw(end_:end_) == char(0))) end_ = end_ - 1
      if (len(suffix) <= len(self%raw(start_:end_))) then
         end_with = self%raw(end_-len(suffix)+1:end_) == suffix
      endif
   endif
   endfunction end_with