Reduce to one (unique) multiple (sequential) occurrences of a characters substring into a string.
For example the string ’ ab-cre-cre-ab’ is reduce to ‘ab-cre-ab’ if the substring is ‘-cre’.
Note
Eventual multiple trailing white space are not reduced to one occurrence.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string |
String to be parsed. |
||
character(len=*), | intent(in) | :: | substring |
Substring which multiple occurences must be reduced to one. |
String parsed.
elemental function unique(string, substring) result(uniq) !< Reduce to one (unique) multiple (sequential) occurrences of a characters substring into a string. !< !< For example the string ' ab-cre-cre-ab' is reduce to 'ab-cre-ab' if the substring is '-cre'. !< @note Eventual multiple trailing white space are not reduced to one occurrence. character(len=*), intent(in) :: string !< String to be parsed. character(len=*), intent(in) :: substring !< Substring which multiple occurences must be reduced to one. character(len=len(string)) :: uniq !< String parsed. integer(I4P) :: Lsub !< Lenght of substring. integer(I4P) :: c1 !< Counter. integer(I4P) :: c2 !< Counter. uniq = string Lsub=len(substring) if (Lsub>len(string)) return c1 = 1 Loop1: do if (c1>=len_trim(uniq)) exit Loop1 if (uniq(c1:c1+Lsub-1)==substring.and.uniq(c1+Lsub:c1+2*Lsub-1)==substring) then c2 = c1 + Lsub Loop2: do if (c2>=len_trim(uniq)) exit Loop2 if (uniq(c2:c2+Lsub-1)==substring) then c2 = c2 + Lsub else exit Loop2 endif enddo Loop2 uniq = uniq(1:c1)//uniq(c2:) else c1 = c1 + Lsub endif enddo Loop1 endfunction unique