Fortran Wiki
String_Functions
MODULE String_Functions
IMPLICIT NONE
INTERFACE Copy
MODULE PROCEDURE copy_a2s, copy_s2a
END INTERFACE Copy
CONTAINS
PURE FUNCTION Copy_a2s(a) RESULT (s)
CHARACTER,INTENT(IN) :: a(:)
CHARACTER(SIZE(a)) :: s
INTEGER :: i
DO i = 1,SIZE(a)
s(i:i) = a(i)
END DO
END FUNCTION Copy_a2s
PURE FUNCTION Copy_s2a(s) RESULT (a)
CHARACTER(*),INTENT(IN) :: s
CHARACTER :: a(LEN(s))
INTEGER :: i
DO i = 1,LEN(s)
a(i) = s(i:i)
END DO
END FUNCTION Copy_s2a
PURE INTEGER FUNCTION Clen(s)
CHARACTER(*),INTENT(IN) :: s
INTEGER :: i
Clen = LEN(s)
i = LEN_TRIM(s)
IF (s(i:i) == CHAR(0)) Clen = i-1
END FUNCTION Clen
PURE INTEGER FUNCTION Clen_trim(s)
CHARACTER(*),INTENT(IN) :: s
INTEGER :: i
i = LEN_TRIM(s) ; Clen_trim = i
IF (s(i:i) == CHAR(0)) Clen_trim = Clen(s)
END FUNCTION Clen_trim
FUNCTION Ctrim(s1) RESULT(s2)
CHARACTER(*),INTENT(IN) :: s1
CHARACTER(Clen_trim(s1)) :: s2
s2 = s1
END FUNCTION Ctrim
INTEGER FUNCTION Count_Items(s1)
CHARACTER(*) :: s1
CHARACTER(Clen(s1)) :: s
INTEGER :: i, k
s = s1
k = 0 ; IF (s /= ' ') k = 1
DO i = 1,LEN_TRIM(s)-1
IF (s(i:i) /= ' '.AND.s(i:i) /= ',' &
.AND.s(i+1:i+1) == ' '.OR.s(i+1:i+1) == ',') k = k+1
END DO
Count_Items = k
END FUNCTION Count_Items
FUNCTION Reduce_Blanks(s) RESULT (outs)
CHARACTER(*) :: s
CHARACTER(LEN_TRIM(s)) :: outs
INTEGER :: i, k, n
n = 0 ; k = LEN_TRIM(s)
DO i = 1,k-1
n = n+1 ; outs(n:n) = s(i:i)
IF (s(i:i+1) == ' ') n = n-1
END DO
n = n+1 ; outs(n:n) = s(k:k)
IF (n < k) outs(n+1:) = ' '
END FUNCTION Reduce_Blanks
FUNCTION Replace_Text (s,text,rep) RESULT(outs)
CHARACTER(*) :: s,text,rep
CHARACTER(LEN(s)+100) :: outs
INTEGER :: i, nt, nr
outs = s ; nt = LEN_TRIM(text) ; nr = LEN_TRIM(rep)
DO
i = INDEX(outs,text(:nt)) ; IF (i == 0) EXIT
outs = outs(:i-1) // rep(:nr) // outs(i+nt:)
END DO
END FUNCTION Replace_Text
FUNCTION Spack (s,ex) RESULT (outs)
CHARACTER(*) :: s,ex
CHARACTER(LEN(s)) :: outs
CHARACTER :: aex(LEN(ex))
INTEGER :: i, n
n = 0 ; aex = Copy(ex)
DO i = 1,LEN(s)
IF (.NOT.ANY(s(i:i) == aex)) CYCLE
n = n+1 ; outs(n:n) = s(i:i)
END DO
outs(n+1:) = ' '
END FUNCTION Spack
INTEGER FUNCTION Tally (s,text)
CHARACTER(*) :: s, text
INTEGER :: i, nt
Tally = 0 ; nt = LEN_TRIM(text)
DO i = 1,LEN(s)-nt+1
IF (s(i:i+nt-1) == text(:nt)) Tally = Tally+1
END DO
END FUNCTION Tally
FUNCTION Translate(s1,codes) RESULT (s2)
CHARACTER(*) :: s1, codes(2)
CHARACTER(LEN(s1)) :: s2
CHARACTER :: ch
INTEGER :: i, j
DO i = 1,LEN(s1)
ch = s1(i:i)
j = INDEX(codes(1),ch) ; IF (j > 0) ch = codes(2)(j:j)
s2(i:i) = ch
END DO
END FUNCTION Translate
FUNCTION Upper(s1) RESULT (s2)
CHARACTER(*) :: s1
CHARACTER(LEN(s1)) :: s2
CHARACTER :: ch
INTEGER,PARAMETER :: DUC = ICHAR('A') - ICHAR('a')
INTEGER :: i
DO i = 1,LEN(s1)
ch = s1(i:i)
IF (ch >= 'a'.AND.ch <= 'z') ch = CHAR(ICHAR(ch)+DUC)
s2(i:i) = ch
END DO
END FUNCTION Upper
FUNCTION Lower(s1) RESULT (s2)
CHARACTER(*) :: s1
CHARACTER(LEN(s1)) :: s2
CHARACTER :: ch
INTEGER,PARAMETER :: DUC = ICHAR('A') - ICHAR('a')
INTEGER :: i
DO i = 1,LEN(s1)
ch = s1(i:i)
IF (ch >= 'A'.AND.ch <= 'Z') ch = CHAR(ICHAR(ch)-DUC)
s2(i:i) = ch
END DO
END FUNCTION Lower
END MODULE String_Functions
Created on August 30, 2013 00:43:41
by
Jason Blevins
(174.101.45.6)
(5634 characters / 2.0 pages)