1 | ! ==============================================================================================================================\n |
---|
2 | ! MODULE : |
---|
3 | ! |
---|
4 | ! CONTACT : orchidee-help _at_ listes.ipsl.fr |
---|
5 | ! |
---|
6 | ! LICENCE : IPSL (2006) |
---|
7 | ! This software is governed by the CeCILL licence see ORCHIDEE/ORCHIDEE_CeCILL.LIC |
---|
8 | ! |
---|
9 | !>\BRIEF |
---|
10 | !! |
---|
11 | !! |
---|
12 | !!\n DESCRIPTION : |
---|
13 | !! |
---|
14 | !! RECENT CHANGE(S): None |
---|
15 | !! |
---|
16 | !! REFERENCE(S) : None |
---|
17 | !! |
---|
18 | !! SVN : |
---|
19 | !! $HeadURL: svn://forge.ipsl.jussieu.fr/orchidee/branches/ORCHIDEE-CN-P/ORCHIDEE/src_driver/readdim2.f90 $ |
---|
20 | !! $Date: 2019-01-16 11:11:16 +0100 (mer. 16 janv. 2019) $ |
---|
21 | !! $Revision: 5700 $ |
---|
22 | !! \n |
---|
23 | !_ ================================================================================================================================ |
---|
24 | |
---|
25 | MODULE string |
---|
26 | |
---|
27 | IMPLICIT NONE |
---|
28 | |
---|
29 | PRIVATE |
---|
30 | PUBLIC :: string_endswith, countsubstring, string_split |
---|
31 | |
---|
32 | CONTAINS |
---|
33 | |
---|
34 | !! ==============================================================================================================================\n |
---|
35 | !! SUBROUTINE : |
---|
36 | !! |
---|
37 | !>\BRIEF |
---|
38 | !! |
---|
39 | !!\n DESCRIPTION : string and with 'ending' |
---|
40 | !! true : in_str ends with ENDING |
---|
41 | !! false: in_str does not end with ENDING |
---|
42 | !! |
---|
43 | !! |
---|
44 | !! RECENT CHANGE(S): None |
---|
45 | !! |
---|
46 | !! MAIN OUTPUT VARIABLE(S): |
---|
47 | !! |
---|
48 | !! REFERENCE(S) : |
---|
49 | !! |
---|
50 | !_ ================================================================================================================================ |
---|
51 | FUNCTION string_endswith(in_str, ending) RESULT (is_ending) |
---|
52 | CHARACTER(LEN=*), INTENT(in) :: in_str |
---|
53 | CHARACTER(LEN=*), INTENT(in) :: ending |
---|
54 | |
---|
55 | CHARACTER(LEN=:), ALLOCATABLE :: clean_in_str |
---|
56 | INTEGER :: idx |
---|
57 | |
---|
58 | LOGICAL :: is_ending |
---|
59 | |
---|
60 | is_ending = .FALSE. |
---|
61 | |
---|
62 | clean_in_str =TRIM(in_str) |
---|
63 | idx = INDEX(clean_in_str, ending) |
---|
64 | !WRITE(*,*) "string_endswith:: idx=", idx |
---|
65 | |
---|
66 | ! substring found |
---|
67 | IF (idx .GT. 0) THEN |
---|
68 | ! is it at the back? |
---|
69 | !WRITE(*,*) "string_endswith:: idx+len(ending)=", idx+LEN(ending)-1 |
---|
70 | !WRITE(*,*) "string_endswith:: LEN(clean_in_str)=", LEN(clean_in_str) |
---|
71 | IF (idx + LEN(ending) - 1 .EQ. LEN(clean_in_str) ) THEN |
---|
72 | ! yes |
---|
73 | is_ending = .TRUE. |
---|
74 | ENDIF |
---|
75 | ENDIF ! (idx .EQ. 0) |
---|
76 | |
---|
77 | END FUNCTION string_endswith |
---|
78 | |
---|
79 | |
---|
80 | !! ==============================================================================================================================\n |
---|
81 | !! SUBROUTINE : |
---|
82 | !! |
---|
83 | !>\BRIEF |
---|
84 | !! |
---|
85 | !!\n DESCRIPTION : string and with 'ending' |
---|
86 | !! https://www.rosettacode.org/wiki/Count_occurrences_of_a_substring#Fortran |
---|
87 | !! |
---|
88 | !! |
---|
89 | !! RECENT CHANGE(S): None |
---|
90 | !! |
---|
91 | !! MAIN OUTPUT VARIABLE(S): |
---|
92 | !! |
---|
93 | !! REFERENCE(S) : |
---|
94 | !! |
---|
95 | !_ ================================================================================================================================ |
---|
96 | FUNCTION countsubstring(s1, s2) RESULT(c) |
---|
97 | character(*), intent(in) :: s1, s2 |
---|
98 | integer :: c, p, posn |
---|
99 | |
---|
100 | c = 0 |
---|
101 | if(len(s2) == 0) return |
---|
102 | p = 1 |
---|
103 | do |
---|
104 | posn = index(s1(p:), s2) |
---|
105 | ! WRITE(*,*) "countsubstring:: s1=", s1(p:) |
---|
106 | ! WRITE(*,*) "countsubstring:: pos=", posn |
---|
107 | if(posn == 0) exit |
---|
108 | c = c + 1 |
---|
109 | p = p + posn + len(s2)-1 |
---|
110 | ! WRITE(*,*) "countsubstring:: next p=", s1(p:) |
---|
111 | end do |
---|
112 | END FUNCTION countsubstring |
---|
113 | |
---|
114 | !! ==============================================================================================================================\n |
---|
115 | !! SUBROUTINE : |
---|
116 | !! |
---|
117 | !>\BRIEF |
---|
118 | !! |
---|
119 | !!\n DESCRIPTION : string and with 'ending' |
---|
120 | !! https://www.rosettacode.org/wiki/Count_occurrences_of_a_substring#Fortran |
---|
121 | !! |
---|
122 | !! |
---|
123 | !! RECENT CHANGE(S): None |
---|
124 | !! |
---|
125 | !! MAIN OUTPUT VARIABLE(S): |
---|
126 | !! |
---|
127 | !! REFERENCE(S) : |
---|
128 | !! |
---|
129 | !_ ================================================================================================================================ |
---|
130 | SUBROUTINE string_split(str, token, out_str, stat) |
---|
131 | CHARACTER(*), INTENT(IN) :: str |
---|
132 | CHARACTER(*), INTENT(IN) :: token |
---|
133 | |
---|
134 | CHARACTER(LEN=300), ALLOCATABLE, DIMENSION(:), INTENT(out) :: out_str |
---|
135 | INTEGER, INTENT(out) :: stat |
---|
136 | INTEGER :: c, p, posn, num_tok, startpos, endpos, ier |
---|
137 | |
---|
138 | num_tok = countsubstring(str,token) + 1 |
---|
139 | ! WRITE(*,*) "string_split:: found parts=", num_tok |
---|
140 | |
---|
141 | stat = 0 |
---|
142 | |
---|
143 | IF (.NOT. ALLOCATED(out_str) ) THEN |
---|
144 | ALLOCATE(out_str(num_tok), stat=ier) |
---|
145 | !CALL iplserr(3,"string_split",'Memory allocation error for out_str','Error code=',ier) |
---|
146 | stat = ier |
---|
147 | ELSE |
---|
148 | !IF (SIZE(out_str) .LT. num_tok) THEN |
---|
149 | ! CALL ipslerr(3,'string_split','The size of the allocated output array','is not big enough. found=',num_tok) |
---|
150 | !ENDIF |
---|
151 | stat = -2 |
---|
152 | ENDIF |
---|
153 | |
---|
154 | startpos=1 |
---|
155 | endpos=1 |
---|
156 | ! WRITE(*,*) "string_split:: str=", str |
---|
157 | |
---|
158 | c = 0 |
---|
159 | if(len(token) == 0) return |
---|
160 | do |
---|
161 | ! WRITE(*,*) "" |
---|
162 | ! WRITE(*,*) "string_split:: index=", str(endpos:) |
---|
163 | posn = index(str(endpos:), token) |
---|
164 | ! WRITE(*,*) "string_split:: posn=", posn |
---|
165 | if(posn == 0) exit |
---|
166 | c = c + 1 |
---|
167 | |
---|
168 | ! WRITE(*,*) "string_split:: startpos=", startpos |
---|
169 | ! WRITE(*,*) "string_split:: endpos=", startpos+posn-1 |
---|
170 | out_str(c) = str(startpos:startpos+posn-1) |
---|
171 | ! WRITE(*,*) "string_split:: out_str=", TRIM(out_str(c)) |
---|
172 | |
---|
173 | startpos = startpos + posn |
---|
174 | endpos = endpos + posn + len(token) - 1 |
---|
175 | end do |
---|
176 | |
---|
177 | ! WRITE(*,*) "string_split:: end, startpos=", startpos |
---|
178 | ! WRITE(*,*) "string_split:: end, endpos=", len(str) |
---|
179 | ! last iteration |
---|
180 | out_str(c+1) = str(startpos:len(str)) |
---|
181 | |
---|
182 | ! WRITE(*,*) "string_split:: out_str=", out_str(c+1) |
---|
183 | END SUBROUTINE string_split |
---|
184 | |
---|
185 | END MODULE string |
---|