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 | Intent | Optional | 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. |
Result of the test.
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