count the substring in utf8_string overlaps are not considered e.g. utf8_count(“AUAUAUAUAUAUAU”,”AUA”) returns 3
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(utf8_string), | intent(in) | :: | utf8 | |||
character(kind=c_char,len=*), | intent(in) | :: | substring |
pure function utf8_count(utf8, substring) result(count)
class(utf8_string), intent(in) :: utf8
character(len=*, kind=c_char), intent(in) :: substring
integer :: count
if (.not. utf8_is_valid(substring) .or. len(substring) == 0) then
count = 0; return
end if
count = count_internal(utf8%str, substring)
contains
pure recursive function count_internal(full, sub) result(c)
character(len=*, kind=c_char), intent(in) :: full
character(len=*, kind=c_char), intent(in) :: sub
integer :: c
integer :: l, idx
l = len(sub)
idx = index(full, sub)
if (idx == 0) then
c = 0; return
else
c = count_internal(full(idx + l:), sub) + 1
end if
end function count_internal
end function utf8_count