functiontop_indexpure
pure integer function top_index(s, pattern)
Position of the first pattern in s outside brackets and strings, or 0.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| s | character(len=*) | in | the text | |
| pattern | character(len=*) | in | the text to find |
Result
| Name | Type | Attributes | Description |
|---|---|---|---|
| top_index | integer |
Source
Show the code
pure integer function top_index(s, pattern)
character(len=*), intent(in) :: s !! the text
character(len=*), intent(in) :: pattern !! the text to find
integer :: k, depth
character :: quote
top_index = 0
depth = 0
quote = ' '
do k = 1, len(s) - len(pattern) + 1
if (quote /= ' ') then
if (s(k:k) == quote) quote = ' '
cycle
end if
if (depth == 0 .and. s(k:k + len(pattern) - 1) == pattern) then
top_index = k
return
end if
select case (s(k:k))
case ('"', "'")
quote = s(k:k)
case ('(', '[')
depth = depth + 1
case (')', ']')
depth = depth - 1
end select
end do
end function top_index