start_with Function

private elemental function start_with(self, prefix, start, end)

Return true if a string starts with a specified prefix.

 type(string) :: astring
 logical      :: test_passed(4)
 astring = 'Hello WorLD!'
 test_passed(1) = astring%start_with(prefix='Hello').eqv..true.
 test_passed(2) = astring%start_with(prefix='hell').eqv..false.
 test_passed(3) = astring%start_with(prefix='llo Wor', start=3).eqv..true.
 test_passed(4) = astring%start_with(prefix='lo W', start=4, end=7).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) :: prefix

Searched prefix.

integer, intent(in), optional :: start

Start position into the string.

integer, intent(in), optional :: end

End position into the string.

Return Value logical

Result of the test.


Called by

proc~~start_with~~CalledByGraph proc~start_with stringifor_string_t::string%start_with proc~split_chunked stringifor_string_t::string%split_chunked proc~split_chunked->proc~start_with

Contents

Source Code


Source Code

   elemental function start_with(self, prefix, start, end)
   !< Return true if a string starts with a specified prefix.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(4)
   !< astring = 'Hello WorLD!'
   !< test_passed(1) = astring%start_with(prefix='Hello').eqv..true.
   !< test_passed(2) = astring%start_with(prefix='hell').eqv..false.
   !< test_passed(3) = astring%start_with(prefix='llo Wor', start=3).eqv..true.
   !< test_passed(4) = astring%start_with(prefix='lo W', start=4, end=7).eqv..true.
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self       !< The string.
   character(kind=CK, len=*), intent(in)           :: prefix     !< Searched prefix.
   integer,                   intent(in), optional :: start      !< Start position into the string.
   integer,                   intent(in), optional :: end        !< End position into the string.
   logical                                         :: start_with !< Result of the test.
   integer                                         :: start_     !< Start position into the string, local variable.
   integer                                         :: end_       !< End position into the string, local variable.

   start_with = .false.
   if (allocated(self%raw)) then
      start_ = 1             ; if (present(start)) start_ = start
      end_   = len(self%raw) ; if (present(end))   end_   = end
      if (len(prefix)<=len(self%raw(start_:end_))) then
         start_with = index(self%raw(start_:end_), prefix)==1
      endif
   endif
   endfunction start_with