Changeset 4403 for LMDZ6/trunk/libf/misc/strings_mod.F90
- Timestamp:
- Jan 30, 2023, 9:27:31 PM (20 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
LMDZ6/trunk/libf/misc/strings_mod.F90
r4363 r4403 194 194 195 195 !============================================================================================================================== 196 !=== Extract the substring in front of the last (first if lFirst==TRUE) occurrence of separator "sep" in "str"================196 !=== Extract the substring in front of the first (last if lBackward==TRUE) occurrence of "sep" in "str" ================ 197 197 !=== Examples for str='a_b_c' and sep='_' and the bash command with the same effect: ================ 198 198 !=== * strHead(..,.FALSE.) = 'a' ${str%%$sep*} ================ 199 199 !=== * strHead(..,.TRUE.) = 'a_b' ${str%$sep*} ================ 200 200 !============================================================================================================================== 201 CHARACTER(LEN=maxlen) FUNCTION strHead_1(str, sep,lFirst) RESULT(out)201 CHARACTER(LEN=maxlen) FUNCTION strHead_1(str, sep, lBackward) RESULT(out) 202 202 CHARACTER(LEN=*), INTENT(IN) :: str 203 203 CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep 204 LOGICAL, OPTIONAL, INTENT(IN) :: lFirst 205 !------------------------------------------------------------------------------------------------------------------------------ 206 LOGICAL :: lf 207 lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst 204 LOGICAL, OPTIONAL, INTENT(IN) :: lBackward 205 !------------------------------------------------------------------------------------------------------------------------------ 208 206 IF(PRESENT(sep)) THEN 209 out = str(1:INDEX(str,sep,.NOT.lf)-1) 207 IF( PRESENT(lBackWard)) out = str(1:INDEX(str,sep,lBackWard)-1) 208 IF(.NOT.PRESENT(lBackWard)) out = str(1:INDEX(str,sep)-1) 210 209 ELSE 211 out = str(1:INDEX(str,'/',.NOT.lf)-1) 210 IF( PRESENT(lBackWard)) out = str(1:INDEX(str,'/',lBackWard)-1) 211 IF(.NOT.PRESENT(lBackWard)) out = str(1:INDEX(str,'/')-1) 212 212 END IF 213 213 IF(out == '') out = str 214 214 END FUNCTION strHead_1 215 215 !============================================================================================================================== 216 FUNCTION strHead_m(str, sep,lFirst) RESULT(out)216 FUNCTION strHead_m(str, sep, lBackward) RESULT(out) 217 217 CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:) 218 218 CHARACTER(LEN=*), INTENT(IN) :: str(:) 219 219 CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep 220 LOGICAL, OPTIONAL, INTENT(IN) :: lFirst 221 !------------------------------------------------------------------------------------------------------------------------------ 222 LOGICAL :: lf 220 LOGICAL, OPTIONAL, INTENT(IN) :: lBackward 221 !------------------------------------------------------------------------------------------------------------------------------ 223 222 INTEGER :: k 224 lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst225 223 IF(PRESENT(sep)) THEN 226 out = [(strHead_1(str(k), sep, lf), k=1, SIZE(str))] 224 IF( PRESENT(lBackWard)) out = [(strHead_1(str(k), sep, lBackWard), k=1, SIZE(str))] 225 IF(.NOT.PRESENT(lBackWard)) out = [(strHead_1(str(k), sep), k=1, SIZE(str))] 227 226 ELSE 228 out = [(strHead_1(str(k), lFirst=lf), k=1, SIZE(str))] 227 IF( PRESENT(lBackWard)) out = [(strHead_1(str(k), '/', lBackWard), k=1, SIZE(str))] 228 IF(.NOT.PRESENT(lBackWard)) out = [(strHead_1(str(k), '/'), k=1, SIZE(str))] 229 229 END IF 230 230 END FUNCTION strHead_m 231 231 !============================================================================================================================== 232 !=== Extract the substring following the last (first if lFirst==TRUE) occurrence of separator "sep" in "str"================232 !=== Extract the substring following the first (last if lBackward==TRUE) occurrence of "sep" in "str" ================ 233 233 !=== Examples for str='a_b_c' and sep='_' and the bash command with the same effect: ================ 234 !=== * strTail( ..,.FALSE.) = 'c' ${str#*$sep}================235 !=== * strTail( ..,.TRUE.) = 'b_c' ${str##*$sep}================236 !============================================================================================================================== 237 CHARACTER(LEN=maxlen) FUNCTION strTail_1(str, sep,lFirst) RESULT(out)234 !=== * strTail(str, '_', .FALSE.) = 'b_c' ${str#*$sep} ================ 235 !=== * strTail(str, '_', .TRUE.) = 'c' ${str##*$sep} ================ 236 !============================================================================================================================== 237 CHARACTER(LEN=maxlen) FUNCTION strTail_1(str, sep, lBackWard) RESULT(out) 238 238 CHARACTER(LEN=*), INTENT(IN) :: str 239 239 CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep 240 LOGICAL, OPTIONAL, INTENT(IN) :: lFirst 241 !------------------------------------------------------------------------------------------------------------------------------ 242 LOGICAL :: lf 243 lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst 240 LOGICAL, OPTIONAL, INTENT(IN) :: lBackWard 241 !------------------------------------------------------------------------------------------------------------------------------ 244 242 IF(PRESENT(sep)) THEN 245 out = str(INDEX(str,sep,.NOT.lf)+LEN(sep):LEN_TRIM(str)) 243 IF( PRESENT(lBackWard)) out = str(INDEX(str,sep,lBackWard)+LEN(sep):LEN_TRIM(str)) 244 IF(.NOT.PRESENT(lBackWard)) out = str(INDEX(str,sep) +LEN(sep):LEN_TRIM(str)) 246 245 ELSE 247 out = str(INDEX(str,'/',.NOT.lf)+1:LEN_TRIM(str)) 246 IF( PRESENT(lBackWard)) out = str(INDEX(str,'/',lBackWard)+1:LEN_TRIM(str)) 247 IF(.NOT.PRESENT(lBackWard)) out = str(INDEX(str,'/') +1:LEN_TRIM(str)) 248 248 END IF 249 249 IF(out == '') out = str 250 250 END FUNCTION strTail_1 251 251 !============================================================================================================================== 252 FUNCTION strTail_m(str, sep,lFirst) RESULT(out)252 FUNCTION strTail_m(str, sep, lBackWard) RESULT(out) 253 253 CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:) 254 254 CHARACTER(LEN=*), INTENT(IN) :: str(:) 255 255 CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep 256 LOGICAL, OPTIONAL, INTENT(IN) :: lFirst 257 !------------------------------------------------------------------------------------------------------------------------------ 258 LOGICAL :: lf 256 LOGICAL, OPTIONAL, INTENT(IN) :: lBackWard 257 !------------------------------------------------------------------------------------------------------------------------------ 259 258 INTEGER :: k 260 lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst261 259 IF(PRESENT(sep)) THEN 262 out = [(strTail_1(str(k), sep, lf), k=1, SIZE(str))] 260 IF( PRESENT(lBackWard)) out = [(strTail_1(str(k), sep, lBackWard), k=1, SIZE(str))] 261 IF(.NOT.PRESENT(lBackWard)) out = [(strTail_1(str(k), sep), k=1, SIZE(str))] 263 262 ELSE 264 out = [(strTail_1(str(k), lFirst=lf), k=1, SIZE(str))] 263 IF( PRESENT(lBackWard)) out = [(strTail_1(str(k), '/', lBackWard), k=1, SIZE(str))] 264 IF(.NOT.PRESENT(lBackWard)) out = [(strTail_1(str(k), '/'), k=1, SIZE(str))] 265 265 END IF 266 266 END FUNCTION strTail_m
Note: See TracChangeset
for help on using the changeset viewer.