@@ -7,16 +7,26 @@ module strings
7
7
! > @brief Count the occurrences of a substring in a string.
8
8
! > @param[in] str - string to count from
9
9
! > @param[in] substr - substring to count
10
+ ! > @param[in] match_case - use case sensitivity (.true.) or not (.false. [DEFAULT]) \b [OPTIONAL]
10
11
! > @return count of \b substr in \b str
11
12
! -------------------------------------------------------------------------------------
12
- function str_count (str ,substr ) result(count)
13
+ function str_count (str ,substr , match_case ) result(count)
13
14
implicit none
14
15
character (len=* ), intent (in ) :: str
15
16
character (len=* ), intent (in ) :: substr
17
+ logical , intent (in ), optional :: match_case
18
+ character (len= :), allocatable :: strtmp,substrtmp
16
19
integer :: i,count
20
+ if (present (match_case).and. match_case)then
21
+ strtmp= str
22
+ substrtmp= substr
23
+ else
24
+ strtmp= str_lower(str)
25
+ substrtmp= str_lower(substr)
26
+ endif
17
27
count= 0
18
- do i= 1 ,len_trim (str )
19
- if (str (i:i+ (len_trim (substr )- 1 )).eq. substr )count= count+1
28
+ do i= 1 ,len_trim (strtmp )
29
+ if (strtmp (i:i+ (len_trim (substrtmp )- 1 )).eq. substrtmp )count= count+1
20
30
end do
21
31
end function str_count
22
32
@@ -208,7 +218,7 @@ end function str_zfill
208
218
! > centered string is a blank character.
209
219
! > @param[in] str - string to work on
210
220
! > @param[in] width - width of centered string
211
- ! > @param[in] fillchar - character to fill centered string. \b[OPTIONAL]
221
+ ! > @param[in] fillchar - character to fill centered string. \b [OPTIONAL]
212
222
! > @return centered string
213
223
! -------------------------------------------------------------------------------------
214
224
function str_center (str ,width ,fillchar ) result(strout)
@@ -254,18 +264,50 @@ end function str_reverse
254
264
! > @brief Return .true. if substr is in str, .false. otherwise
255
265
! > @param[in] str - string to work on
256
266
! > @param[in] substr - string to search for in str
267
+ ! > @param[in] match_case - use case sensitivity (.true.) or not (.false. [DEFAULT]) \b [OPTIONAL]
257
268
! > @return .true. or .false.
258
269
! -------------------------------------------------------------------------------------
259
- function str_test (str ,substr ) result(strtest)
270
+ function str_test (str ,substr , match_case ) result(strtest)
260
271
implicit none
261
272
character (len=* ), intent (in ) :: str
262
273
character (len=* ), intent (in ) :: substr
274
+ logical , intent (in ), optional :: match_case
263
275
logical :: strtest
264
- if (str_count(str,substr).eq. 0 )then
276
+ integer :: cnt
277
+ cnt= 0
278
+ if (present (match_case))then
279
+ cnt= str_count(str,substr,match_case= match_case)
280
+ else
281
+ cnt= str_count(str,substr)
282
+ endif
283
+ if (cnt.eq. 0 )then
265
284
strtest= .false.
266
- elseif (str_count(str,substr) .gt. 0 )then
285
+ elseif (cnt .gt. 0 )then
267
286
strtest= .true.
268
287
endif
269
288
end function str_test
270
289
290
+ ! -------------------------------------------------------------------------------------
291
+ ! Function: str_swapcase
292
+ ! > @brief Return str with case of letters swapped
293
+ ! > @param[in] str - input string
294
+ ! -------------------------------------------------------------------------------------
295
+ function str_swapcase (str ) result(strout)
296
+ implicit none
297
+ character (len=* ), intent (in ) :: str
298
+ character (len= :), allocatable :: strout
299
+ integer :: i,ic
300
+ strout= " "
301
+ do i= 1 ,len (str)
302
+ ic= iachar (str(i:i))
303
+ if (ic.ge. 65.and .ic.le. 90 )then
304
+ strout= strout// achar (ic+32 )
305
+ elseif (ic.ge. 97.and .ic.le. 122 )then
306
+ strout= strout// achar (ic-32 )
307
+ else
308
+ strout= strout// achar (ic)
309
+ endif
310
+ end do
311
+ end function str_swapcase
312
+
271
313
end module strings
0 commit comments