Strings.f90 39.3 KB
Newer Older
1
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
2
3
4
5
6
7
!                          Futility Development Group                          !
!                             All rights reserved.                             !
!                                                                              !
! Futility is a jointly-maintained, open-source project between the University !
! of Michigan and Oak Ridge National Laboratory.  The copyright and license    !
! can be found in LICENSE.txt in the head directory of this repository.        !
8
9
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
!> @brief Module for specifying a type for arbitrary length strings
10
!>
11
!> This module defines a type for a string which can be an arbitrary length.
12
!> It then provides overloaded operators and intrinsic functions so
13
!> that it can be used almost exactly like a Fortran @c CHARACTER type variable.
14
!>
15
16
17
18
19
20
21
!> The following operators have been overloaded for use with @c StringType
!>  - @c ASSIGNMENT(=)
!>  - @c OPERATOR(//)
!>  - @c OPERATOR(==)
!>  - @c OPERATOR(/=)
!>
!> The following intrinsic Fortran functions have been overloaded for use with
22
!> @c StringType
23
24
25
26
27
!>  - @ref Strings::LEN_StringType "LEN": @copybrief Strings::LEN_StringType
!>  - @ref Strings::LEN_TRIM_StringType "LEN_TRIM": @copybrief Strings::LEN_TRIM_StringType
!>  - @ref Strings::TRIM_StringType "TRIM": @copybrief Strings::TRIM_StringType
!>  - @ref Strings::ADJUSTL_StringType "ADJUSTL": @copybrief Strings::ADJUSTL_StringType
!>  - @ref Strings::ADJUSTR_StringType "ADJUSTR": @copybrief Strings::ADJUSTR_StringType
28
!>  - @c INDEX
29
30
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
MODULE Strings
HendersonSC's avatar
HendersonSC committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
USE ISO_C_BINDING
USE IntrType
IMPLICIT NONE
PRIVATE

PUBLIC :: StringType
PUBLIC :: CHAR
PUBLIC :: LEN
PUBLIC :: LEN_TRIM
PUBLIC :: TRIM
PUBLIC :: ADJUSTL
PUBLIC :: ADJUSTR
PUBLIC :: INDEX
PUBLIC :: ASSIGNMENT(=)
PUBLIC :: OPERATOR(//)
PUBLIC :: OPERATOR(==)
PUBLIC :: OPERATOR(/=)

!> Derived type for an arbitrary length string
TYPE :: StringType
  !> The stored intrinsic character string
  CHARACTER(LEN=:),ALLOCATABLE,PRIVATE :: s
  CONTAINS
    !> copybrief Strings::toupper_string
    !> copydetails Strings::toupper_string
    PROCEDURE,PASS :: upper => toupper_string
    !> copybrief Strings::toLower_string
    !> copydetails Strings::toLower_string
    PROCEDURE,PASS :: lower => toLower_string
    !> copybrief Strings::at_string
    !> copydetails Strings::at_string
    PROCEDURE,PASS :: at => at_string
    !> copybrief Strings::substr_str
    !> copydetails Strings::substr_str
    PROCEDURE,PASS :: substr => substr_str
    !> copybrief Strings::replace_slice
    !> copydetails Strings::replace_slice
    PROCEDURE,PASS,PRIVATE :: replace_slice
    !> copybrief Strings::replace_pattern
    !> copydetails Strings::replace_pattern
    PROCEDURE,PASS,PRIVATE :: replace_pattern
    GENERIC :: replace => replace_slice,replace_pattern
    !> copybrief Strings:: split_string
    !> copydetails Strings:: split_string
    PROCEDURE,PASS,PRIVATE :: split_string
    !> copybrief Strings::split_string_space
    !> copydetails Strings::split_string_space
    PROCEDURE,PASS,PRIVATE :: split_string_space
    GENERIC :: split => split_string, split_string_space
80
81
    !> copybrief Strings::partition
    !> copydetails Strings::partition
82
    PROCEDURE,PASS :: partition
83
84
85
    !> copybrief Strings::rPartition
    !> copydetails Strings::rPartition
    PROCEDURE,PASS :: rPartition
HendersonSC's avatar
HendersonSC committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    !> copybrief Strings::assign_char_to_StringType
    !> copydetails Strings::assign_char_to_StringType
    PROCEDURE,PASS,PRIVATE :: assign_char_to_StringType
    !> @copybrief Strings::assign_Short_Integer_to_StringType
    !> @copydetails Strings::assign_Short_Integer_to_StringType
    PROCEDURE,PASS,PRIVATE :: assign_Short_Integer_to_StringType
    !> @copybrief Strings::assign_Long_Integer_to_StringType
    !> @copydetails Strings::assign_Long_Integer_to_StringType
    PROCEDURE,PASS,PRIVATE :: assign_Long_Integer_to_StringType
    !> @copybrief Strings::assign_Single_Real_to_StringType
    !> @copydetails Strings::assign_Single_Real_to_StringType
    PROCEDURE,PASS,PRIVATE :: assign_Single_Real_to_StringType
    !> @copybrief Strings::assign_Double_Real_to_StringType
    !> @copydetails Strings::assign_Double_Real_to_StringType
    PROCEDURE,PASS,PRIVATE :: assign_Double_Real_to_StringType
    !> copybrief Strings::assign_Logical_to_StringType
    !> copydetails Strings::assign_Logical_to_StringType
    PROCEDURE,PASS,PRIVATE :: assign_Logical_to_StringType
    GENERIC :: ASSIGNMENT(=) => assign_char_to_StringType, &
        assign_Logical_to_StringType,assign_Single_Real_to_StringType, &
        assign_Double_Real_to_StringType,assign_Short_Integer_to_StringType, &
Graham, Aaron's avatar
Graham, Aaron committed
107
            assign_Long_Integer_to_StringType
HendersonSC's avatar
HendersonSC committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    !> copybrief Strings::str_to_sik
    !> copydetails Strings::str_to_sik
    PROCEDURE,PASS :: str_to_sik
    GENERIC :: stoi => str_to_sik
    !> copybrief Strings::str_to_srk
    !> copydetails Strings::str_to_srk
    PROCEDURE,PASS :: str_to_srk
    GENERIC :: stof => str_to_srk
    !> copybrief Strings::isInteger
    !> copydetails Strings::isInteger
    PROCEDURE,PASS :: isInteger
    !> copybrief Strings::isFloat
    !> copydetails Strings::isFloat
    PROCEDURE,PASS :: isFloat
    !> copybrief Strings::isNumeric_str
    !> copydetails Strings::isNumeric_str
    PROCEDURE,PASS :: isNumeric => isNumeric_str
125
126
    !> copybrief Strings::clear_str
    !> copydetails Strings::clear_str
HendersonSC's avatar
HendersonSC committed
127
    PROCEDURE,PASS :: clear => clear_str
128
129
130
    !> copybrief Strings::clean_str
    !> copydetails Strings::clean_str
    !FINAL :: clean_str
HendersonSC's avatar
HendersonSC committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
ENDTYPE StringType

!> @brief Overloads the Fortran intrinsic procedure CHAR() so
!> a string type argument may be passed.
INTERFACE CHAR
  !> @copybrief Strings::CHAR_StringType
  !> @copydetails Strings::CHAR_StringType
  MODULE PROCEDURE CHAR_StringType
  !> copybrief Strings::cchar_to_fchar
  !> copydetails Strings::cchar_to_fchar
  MODULE PROCEDURE cchar_to_fchar
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic procedure LEN() so
!> a string type argument may be passed.
INTERFACE LEN
  !> @copybrief Strings::LEN_StringType
  !> @copydetails Strings::LEN_StringType
  MODULE PROCEDURE LEN_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic procedure LEN_TRIM() so
!> a string type argument may be passed.
INTERFACE LEN_TRIM
  !> @copybrief Strings::LEN_TRIM_StringType
  !> @copydetails Strings::LEN_TRIM_StringType
  MODULE PROCEDURE LEN_TRIM_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic procedure TRIM() so
!> a string type argument may be passed.
INTERFACE TRIM
  !> @copybrief Strings::TRIM_StringType
  !> @copydetails Strings::TRIM_StringType
  MODULE PROCEDURE TRIM_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic procedure ADJUSTL() so
!> a string type argument may be passed.
INTERFACE ADJUSTL
  !> @copybrief Strings::ADJUSTL_StringType
  !> @copydetails Strings::ADJUSTL_StringType
  MODULE PROCEDURE ADJUSTL_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic procedure ADJUSTR() so
!> a string type argument may be passed.
INTERFACE ADJUSTR
  !> @copybrief Strings::ADJUSTR_StringType
  !> @copydetails Strings::ADJUSTR_StringType
  MODULE PROCEDURE ADJUSTR_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic procedure INDEX() so
!> string type arguments may be passed.
INTERFACE INDEX
  !> @copybrief Strings::INDEX_StringType_char
  !> @copydetails Strings::INDEX_StringType_char
  MODULE PROCEDURE INDEX_StringType_char
  !> @copybrief Strings::INDEX_char_StringType
  !> @copydetails Strings::INDEX_char_StringType
  MODULE PROCEDURE INDEX_char_StringType
  !> @copybrief Strings::INDEX_StringType_StringType
  !> @copydetails Strings::INDEX_StringType_StringType
  MODULE PROCEDURE INDEX_StringType_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic operator for concatenating
!> character strings.
INTERFACE OPERATOR(//)
  !> @copybrief Strings::concatenate_char_onto_StringType
  !> @copydetails Strings::concatenate_char_onto_StringType
  MODULE PROCEDURE concatenate_char_onto_StringType
  !> @copybrief Strings::concatenate_StringType_onto_char
  !> @copydetails Strings::concatenate_StringType_onto_char
  MODULE PROCEDURE concatenate_StringType_onto_char
  !> @copybrief Strings::concatenate_StringType_onto_StringType
  !> @copydetails Strings::concatenate_StringType_onto_StringType
  MODULE PROCEDURE concatenate_StringType_onto_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic operator for comparing
!> two variables to see if they are equal
INTERFACE OPERATOR(==)
  !> @copybrief Strings::equalto_char_StringType
  !> @copydetails Strings::equalto_char_StringType
  MODULE PROCEDURE equalto_char_StringType
  !> @copybrief Strings::equalto_StringType_char
  !> @copydetails Strings::equalto_StringType_char
  MODULE PROCEDURE equalto_StringType_char
  !> @copybrief Strings::equalto_StringType_StringType
  !> @copydetails Strings::equalto_StringType_StringType
  MODULE PROCEDURE equalto_StringType_StringType
ENDINTERFACE

!> @brief Overloads the Fortran intrinsic operator for comparing
!> two variables to see if they are not equal
INTERFACE OPERATOR(/=)
  !> @copybrief Strings::notequalto_char_StringType
  !> @copydetails Strings::notequalto_char_StringType
  MODULE PROCEDURE notequalto_char_StringType
  !> @copybrief Strings::notequalto_StringType_char
  !> @copydetails Strings::notequalto_StringType_char
  MODULE PROCEDURE notequalto_StringType_char
  !> @copybrief Strings::notequalto_StringType_StringType
  !> @copydetails Strings::notequalto_StringType_StringType
  MODULE PROCEDURE notequalto_StringType_StringType
ENDINTERFACE
239
240
!
!===============================================================================
HendersonSC's avatar
HendersonSC committed
241
CONTAINS
242
243
244
245
246
247
248
249
250
!!
!!-------------------------------------------------------------------------------
!!> @brief cleans up string objects
!!> @param this the StringType being garbaged collected
!!>
!ELEMENTAL SUBROUTINE clean_str(this)
!  TYPE(StringType),INTENT(INOUT) :: this
!  IF(ALLOCATED(this%s)) DEALLOCATE(this%s)
!ENDSUBROUTINE clean_str
251
252
253
254
255
!
!-------------------------------------------------------------------------------
!> @brief cleans up string objects
!> @param this the StringType being garbaged collected
!>
256
257
258
259
SUBROUTINE clear_str(this)
  CLASS(StringType),INTENT(INOUT) :: this
  IF(ALLOCATED(this%s)) DEALLOCATE(this%s)
ENDSUBROUTINE clear_str
260
261
!
!-------------------------------------------------------------------------------
262
263
!> @brief breaks a string into partitions at the first instance of a delimiter
!> @param this the string being partitioned
264
265
266
!> @param sep the delimiter used for partitioning
!> @returns tokens size 3 array containing the characters before the delimiter,
!> the delimiter, and the characters after the delimiter in that order
267
!>
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
PURE FUNCTION partition(this,sep) RESULT(tokens)
  CLASS(StringType),INTENT(IN) :: this
  CHARACTER(LEN=*),INTENT(IN) :: sep
  TYPE(StringType) :: tokens(3)
  INTEGER(SIK) :: sepPos
  sepPos = MERGE(0,INDEX(this%s,sep),sep == '')
  IF(sepPos > 0) THEN
    tokens(1) = this%s(1:sepPos-1)
    tokens(3) = this%s(sepPos+LEN(SEP):LEN(this%s))
  ELSE
    tokens(1) = this%s
    tokens(3) = ''
  ENDIF
  tokens(2) = sep
ENDFUNCTION partition
!
!-------------------------------------------------------------------------------
!> @brief breaks a string into partitions at the last instance of a delimiter
!> @param this the string being partitioned
287
288
289
!> @param sep the delimiter used for partitioning
!> @returns tokens size 3 array containing the characters before the delimiter,
!> the delimiter, and the characters after the delimiter in that order
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
!>
PURE FUNCTION rPartition(this,sep) RESULT(tokens)
  CLASS(StringType),INTENT(IN) :: this
  CHARACTER(LEN=*),INTENT(IN) :: sep
  TYPE(StringType) :: tokens(3)
  INTEGER(SIK) :: sepPos
  sepPos = MERGE(0,INDEX(this%s,sep,.TRUE.),sep == '')
  IF(sepPos > 0) THEN
    tokens(1) = this%s(1:sepPos-1)
    tokens(3) = this%s(sepPos+LEN(SEP):LEN(this%s))
  ELSE
    tokens(1) = ''
    tokens(3) = this%s
  ENDIF
  tokens(2) = sep
ENDFUNCTION rPartition
!
!-------------------------------------------------------------------------------
308
309
310
311
!> @brief Assigns an intrinsic character array to a string
!> @param lhs the string type receiving the characters
!> @param rhs the character string that will be assigned
!>
HendersonSC's avatar
HendersonSC committed
312
313
314
315
316
317
318
319
ELEMENTAL SUBROUTINE assign_char_to_StringType(lhs,rhs)
  CLASS(StringType),INTENT(INOUT) :: lhs
  CHARACTER(LEN=*),INTENT(IN) :: rhs
  IF(ALLOCATED(lhs%s)) DEALLOCATE(lhs%s)
  IF(LEN(rhs)>0) THEN
    lhs%s = rhs
  ENDIF
ENDSUBROUTINE assign_char_to_StringType
320
321
322
323
!
!-------------------------------------------------------------------------------
!> @brief converts a c-style character array to an intrinsic Fortran array
!>
HendersonSC's avatar
HendersonSC committed
324
325
326
327
FUNCTION cchar_to_fchar(cchar) RESULT(fchar)
  CHARACTER(KIND=C_CHAR),TARGET,INTENT(IN) :: cchar(:)
  CHARACTER(LEN=:),ALLOCATABLE :: fchar
  INTEGER(SIK) :: i,n
328

HendersonSC's avatar
HendersonSC committed
329
330
331
332
333
334
  n = SIZE(cchar)
  ALLOCATE(CHARACTER(n) :: fchar)
  DO i=1,n
    fchar(i:i) = cchar(i)
  ENDDO
ENDFUNCTION cchar_to_fchar
335
336
!
!-------------------------------------------------------------------------------
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
!> @brief splits a StringType using a single space as the delimiter. This
!> routine consumes consecutive spaces as a if they were a single space. The
!> @param this the StringType being split
!> @returns sub_str an array of substings
!>
!> The returned array of strings will not contain any empty strings. If the
!> string being split is empty the returned array will be size 0. If there were
!> no separators found to split on the array will be size 1, and contain the
!> original string.
!>
FUNCTION split_string_space(this) RESULT(sub_str)
  CLASS(StringType),INTENT(IN) :: this
  TYPE(StringType),ALLOCATABLE :: sub_str(:)
  !
  INTEGER(SIK) :: iSplit,nSplits,stt,stp,sepLoc

  stp = LEN(this%s)
  sepLoc = MERGE(INDEX(this%s,' '),0,stp > 1)
  IF(sepLoc == 0) THEN
    !This indicates that either the string or the separator were empty, or
    !the separator was not found in the string.
    ALLOCATE(sub_str(1))
    sub_str(1) = this%s
    RETURN !For these conditions the original string is returned
  ENDIF

  ! Search until the delimiter isn't found
  nSplits = 0
  stt = 1
  DO WHILE(sepLoc > 0)
  ! If the index is greater than 1 then the first character must be a string
    IF(sepLoc > 1) THEN
      ! Count the string
      nSplits = nSplits + 1
    ENDIF
    ! Increment to the next delimiter
    stt = stt + sepLoc
    sepLoc = INDEX(this%s(stt:stp),' ')
  ENDDO
  !Account for strings that don't end in a delimiter
  IF(stt <= stp) nSplits = nSplits + 1
  ALLOCATE(sub_str(nSplits))

  ! Split along delimiters and store in the provided array
  stt = 1
  iSplit = 0
  sepLoc = INDEX(this%s(stt:stp),' ')
  DO WHILE(sepLoc > 0)
    IF(sepLoc > 1) THEN
      iSplit = iSplit + 1
      ! Strip out the string...subtract 2 (1 for exclusive, 1 for delimiter)
      sub_str(iSplit) = this%s(stt:stt+sepLoc-2)
    ENDIF
    stt = stt + sepLoc
    sepLoc = INDEX(this%s(stt:stp),' ')
  ENDDO
  ! If the string ends with a word, then it wasn't snatched out before
  IF(.NOT.(this%s(stp:stp) == ' ')) THEN
    sub_str(nSplits) = &
        this%s(INDEX(this%s(1:stp),' ',.TRUE.)+1:stp)
  ENDIF
ENDFUNCTION split_string_space
!
!-------------------------------------------------------------------------------
401
!> @brief splits a StringType at specified delimiter and returns the substrings
402
!> in an array of StringTypes.
403
!> @param this the string to be split
404
405
!> @param separator the delimiter for split locations
!> @returns sub_str the returned array of substrings
406
!>
HendersonSC's avatar
HendersonSC committed
407
408
FUNCTION split_string(this,separator) RESULT(sub_str)
  CLASS(StringType),INTENT(IN) :: this
Graham, Aaron's avatar
Graham, Aaron committed
409
  CHARACTER(LEN=*),INTENT(IN) :: separator
HendersonSC's avatar
HendersonSC committed
410
411
412
  TYPE(StringType),ALLOCATABLE :: sub_str(:)
  !
  INTEGER(SIK) :: iSplit,nSplits,stt,stp,sepLoc
413

HendersonSC's avatar
HendersonSC committed
414
415
416
417
418
419
420
421
422
  stp = LEN(this%s)
  sepLoc = MERGE(INDEX(this%s,separator),0,stp > 1 .OR. LEN(separator) > 0)
  IF(sepLoc == 0) THEN
    !This indicates that either the string or the separator were empty, or
    !the separator was not found in the string.
    ALLOCATE(sub_str(1))
    sub_str(1) = this%s
    RETURN !For these conditions the original string is returned
  ENDIF
423

HendersonSC's avatar
HendersonSC committed
424
425
426
427
428
429
430
431
432
433
434
  ! Count the number of splits
  nSplits = 0
  stt = 1
  DO WHILE(sepLoc > 0)
    nSplits = nSplits + 1
    stt = stt + sepLoc + LEN(separator) - 1
    sepLoc = INDEX(this%s(stt:stp),separator)
  ENDDO
  !Account for strings that don't end in a delimiter
  IF(stt <= stp) nSplits = nSplits + 1
  ALLOCATE(sub_str(nSplits))
435

HendersonSC's avatar
HendersonSC committed
436
437
438
439
440
441
442
  stt = 1
  iSplit = 0
  sepLoc = INDEX(this%s(stt:stp),separator)
  DO WHILE(sepLoc > 0)
    iSplit = iSplit + 1
    sub_str(iSplit) = this%s(stt:stt+sepLoc-2)
    stt = stt + sepLoc + LEN(separator) - 1
443
    sepLoc = INDEX(this%s(stt:stp),separator)
HendersonSC's avatar
HendersonSC committed
444
445
446
447
448
449
450
  ENDDO
  ! If the string ends with a word, then it wasn't snatched out before
  IF(iSplit < nSplits) THEN
    sub_str(nSplits) = &
        this%s(INDEX(this%s(1:stp),separator,.TRUE.)+LEN(separator):stp)
  ENDIF
ENDFUNCTION split_string
451
452
!
!-------------------------------------------------------------------------------
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
!> @brief returns a copy of the string with every occurrence of a specified
!> search pattern with the designated replacement pattern.
!> @param this the StringType being operated on
!> @param oldPat the pattern being replaced
!> @param newPat the pattern being inserted
!> @returns retStr the newly formed string
!> This routine will automatically account for size changes of the underlying
!> character array. If the search pattern is not found, then the original string
!> is returned
!>
FUNCTION replace_pattern(this,oldPat,newPat) RESULT(retStr)
  CLASS(StringType),INTENT(IN) :: this
  CHARACTER(LEN=*),INTENT(IN) :: oldPat
  CHARACTER(LEN=*),INTENT(IN) :: newPat
  CHARACTER(LEN=:),ALLOCATABLE :: retStr
  !
  TYPE(StringType),ALLOCATABLE :: tokens(:)
  INTEGER(SIK) :: iToken

  !Split out the parts to be retained
  tokens = this%split(oldPat)

  !Set-up the new string
  IF(SIZE(tokens) > 1) THEN
    retStr = ''
  ELSE
    retStr = char(tokens(1))
    RETURN
  ENDIF

  DO iToken=1,SIZE(tokens)-1
    IF(tokens(iToken) == '') THEN
      retStr = retStr//newPat
    ELSE
      retStr = retStr//tokens(iToken)//newPat
    ENDIF
  ENDDO
  IF(tokens(SIZE(tokens)) /= '') THEN
    retStr = retStr//tokens(SIZE(tokens))
  ELSE
    retStr = retStr//newPat
  ENDIF
ENDFUNCTION replace_pattern
!
!-------------------------------------------------------------------------------
498
499
500
501
502
503
504
!> @brief replaces a slice from a string with the designated character(s). If
!> the replacement characters are not the same size as the slice, the characters
!> are repeated until the slice is filled, any excess is truncated
!> @param this the string where the substitution is occurring
!> @param stt the start of the slice to be replaced
!> @param stp the end of the slice to be replaced
!> @param in_char the characters that will be subbed in
505
!> @returns retStr the newly formed string
506
!>
HendersonSC's avatar
HendersonSC committed
507
508
509
510
511
512
513
514
515
FUNCTION replace_slice(this,stt,stp,in_char) RESULT(retStr)
  CLASS(StringType),INTENT(INOUT) :: this
  INTEGER(SIK),INTENT(IN) :: stt
  INTEGER(SIK),INTENT(IN) :: stp
  CHARACTER(LEN=*),INTENT(IN) :: in_char
  !
  CHARACTER(LEN=stp-stt+1) :: tmp_str
  INTEGER(SIK) :: full,part,step
  CHARACTER(LEN=:),ALLOCATABLE :: retStr
516

HendersonSC's avatar
HendersonSC committed
517
518
519
520
521
522
523
524
525
526
527
528
  IF((stt <= stp) .AND. (stt > 0) .AND. (stp <= LEN(this))) THEN
    ! Find the number of characters being replaced
    step = stp-stt+1
    ! Find the number of full replacement strings required
    full = LEN(in_char)/step
    ! Find the length of any partial replacements
    part = MOD(LEN(in_char),step)

    !Construct the replacement string
    tmp_str(1:step) = " "
    IF(full > 0) THEN
      tmp_str = REPEAT(in_char,full)
529
    ENDIF
HendersonSC's avatar
HendersonSC committed
530
531
532
533
534
535
536
537
538
539
540
    IF(part > 0) THEN
      tmp_str = TRIM(tmp_str)//in_char(1:part)
    ENDIF

    ! Direct substitution of characters
    retStr = this%s
    retStr(stt:stp) = tmp_str(1:step)
  ELSE
    retStr =this%s
  ENDIF
ENDFUNCTION replace_slice
541
542
543
544
545
546
!
!-------------------------------------------------------------------------------
!> @brief Returns the character at the index provided, if the index is out of
!> or the string is not allocated then the ascii null character is returned.
!> @param this the string being indexed into
!> @param pos the index the character should be taken from
547
!> @returns letter the character being returned
548
!>
HendersonSC's avatar
HendersonSC committed
549
550
551
552
PURE FUNCTION at_string(this,pos) RESULT(letter)
  CLASS(StringType),INTENT(IN) :: this
  INTEGER(SIK),INTENT(IN) :: pos
  CHARACTER(LEN=:),ALLOCATABLE :: letter
553

554
  IF(ALLOCATED(this%s) .AND. (pos > 0) .AND. (pos <= LEN(this))) THEN
HendersonSC's avatar
HendersonSC committed
555
556
557
558
559
    letter = this%s(pos:pos)
  ELSE
    ALLOCATE(CHARACTER(0) :: letter)
  ENDIF
ENDFUNCTION at_string
560
561
562
563
564
565
566
!
!-------------------------------------------------------------------------------
!> @brief Returns the characters in the slice indicated, if the either index is
!> out of range, then the ascii null character is returned
!> @param this the string being sliced
!> @param stt the first index of the slice
!> @param stp the last index of the slice OPTIONAL: Default = LEN(this)
567
!> @returns slice the character(s) being returned
568
!
HendersonSC's avatar
HendersonSC committed
569
570
571
572
573
574
PURE FUNCTION substr_str(this,stt,stp) RESULT(slice)
  CLASS(StringType),INTENT(IN) :: this
  INTEGER(SIK),INTENT(IN) :: stt
  INTEGER(SIK),INTENT(IN),OPTIONAL :: stp
  CHARACTER(LEN=:),ALLOCATABLE :: slice
  INTEGER(SIK) :: sub_stp
575

HendersonSC's avatar
HendersonSC committed
576
577
578
579
580
  IF(PRESENT(stp)) THEN
    sub_stp = stp
  ELSE
    sub_stp = LEN(this)
  ENDIF
581
  IF(ALLOCATED(this%s) .AND. (stt > 0) .AND. (sub_stp <= LEN(this))) THEN
HendersonSC's avatar
HendersonSC committed
582
583
584
585
586
    slice = this%s(stt:sub_stp)
  ELSE
    ALLOCATE(CHARACTER(0) :: slice)
  ENDIF
ENDFUNCTION substr_str
587
588
589
590
591
!
!-------------------------------------------------------------------------------
!> @brief Utility function takes a string and converts all upper case letters to
!> lower case letters.
!> @param word input is a string, output has all lower case letters
592
!> @lower_str the lower cased version of this
593
!>
HendersonSC's avatar
HendersonSC committed
594
595
596
597
598
599
600
601
PURE FUNCTION toLower_string(word) RESULT(lower_str)
  CLASS(StringType),INTENT(IN) :: word
  INTEGER(SIK) :: i
  !
  TYPE(StringType) :: lower_str
  lower_str = word
  DO i=1,LEN(word)
    IF('A' <= word%s(i:i) .AND. word%s(i:i) <= 'Z') &
Graham, Aaron's avatar
Graham, Aaron committed
602
        lower_str%s(i:i)=ACHAR(IACHAR(word%s(i:i))+32)
HendersonSC's avatar
HendersonSC committed
603
604
  ENDDO
ENDFUNCTION toLower_string
605
606
607
608
609
!
!-------------------------------------------------------------------------------
!> @brief Utility function takes a string and converts all lower case letters to
!> upper case letters.
!> @param word input is a string, output has all upper case letters
610
!> @upper_str the upper cased version of this
611
!>
HendersonSC's avatar
HendersonSC committed
612
613
614
615
616
617
618
619
PURE FUNCTION toupper_string(word) RESULT(upper_str)
  CLASS(StringType),INTENT(IN) :: word
  INTEGER(SIK) :: i
  !
  TYPE(StringType) :: upper_str
  upper_str = word
  DO i=1,LEN(word)
    IF('a' <= word%s(i:i) .AND. word%s(i:i) <= 'z') &
Graham, Aaron's avatar
Graham, Aaron committed
620
        upper_str%s(i:i)=ACHAR(IACHAR(word%s(i:i))-32)
HendersonSC's avatar
HendersonSC committed
621
622
  ENDDO
ENDFUNCTION toupper_string
623
624
!
!-------------------------------------------------------------------------------
625
!> @brief Returns the length of the string including all whitespace.
626
!> @param this the string object
627
628
!> @returns n the length of the string
!>
629
!> Should behave exactly as it would if called directly on a character array
HendersonSC's avatar
HendersonSC committed
630
631
632
ELEMENTAL FUNCTION LEN_StringType(this) RESULT(n)
  CLASS(StringType),INTENT(IN) :: this
  INTEGER(SIK) :: n
633
634
635
636
637
  IF(ALLOCATED(this%s)) THEN
    n=LEN(this%s)
  ELSE
    n = 0
  ENDIF
HendersonSC's avatar
HendersonSC committed
638
ENDFUNCTION LEN_StringType
639
640
!
!-------------------------------------------------------------------------------
641
!> @brief Returns the length of the string excluding trailing whitespace.
642
!> @param this the string object
643
644
!> @returns n the length of the string
!>
645
!> Should behave exactly as it would if called directly on a character array
HendersonSC's avatar
HendersonSC committed
646
647
648
649
650
651
652
653
654
ELEMENTAL FUNCTION LEN_TRIM_StringType(this) RESULT(n)
  CLASS(StringType),INTENT(IN) :: this
  INTEGER(SIK) :: n
  IF(ALLOCATED(this%s)) THEN
    n=LEN_TRIM(this%s)
  ELSE
    n=0
  ENDIF
ENDFUNCTION LEN_TRIM_StringType
655
656
657
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
658
659
660
661
!> variable. If the string is unallocated a zero-length character is returned
!> @param this the string object
!> @returns str the character type with the value of @c this
!>
HendersonSC's avatar
HendersonSC committed
662
663
664
665
666
667
668
669
670
PURE FUNCTION CHAR_StringType(this) RESULT(str)
  CLASS(StringType),INTENT(IN) :: this
  CHARACTER(LEN=:),ALLOCATABLE :: str
  IF(ALLOCATED(this%s)) THEN
    str = this%s
  ELSE
    ALLOCATE(CHARACTER(0) :: str)
  ENDIF
ENDFUNCTION CHAR_StringType
671
672
673
674
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string excluding all trailing whitespace
!> as an intrinsic character type variable.
675
!> @param this the string object
676
!> @returns str this trimmed of trailing whitespace
677
!>
HendersonSC's avatar
HendersonSC committed
678
679
680
681
682
683
684
685
686
PURE FUNCTION TRIM_StringType(this) RESULT(str)
  CLASS(StringType),INTENT(IN) :: this
  CHARACTER(LEN=:),ALLOCATABLE :: str
  IF(ALLOCATED(this%s)) THEN
    str = TRIM(this%s)
  ELSE
    ALLOCATE(CHARACTER(0) :: str)
  ENDIF
ENDFUNCTION TRIM_StringType
687
688
689
690
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable with all preceding whitespace moved to the end.
691
!> @param this the string object
692
!> @returns s left aligned version of this
693
!>
694
695
PURE FUNCTION ADJUSTL_StringType(this) RESULT(s)
  CLASS(StringType),INTENT(IN) :: this
HendersonSC's avatar
HendersonSC committed
696
  CHARACTER(LEN=:),ALLOCATABLE :: s
697
698
699
700
701
  IF(ALLOCATED(this%s)) THEN
    s = ADJUSTL(this%s)
  ELSE
    s = ''
  ENDIF
HendersonSC's avatar
HendersonSC committed
702
ENDFUNCTION ADJUSTL_StringType
703
704
705
706
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable with all trailing whitespace moved to the beginning.
707
!> @param this the string object
708
!> @returns s right aligned version of this
709
!>
710
711
PURE FUNCTION ADJUSTR_StringType(this) RESULT(s)
  CLASS(StringType),INTENT(IN) :: this
HendersonSC's avatar
HendersonSC committed
712
  CHARACTER(LEN=:),ALLOCATABLE :: s
713
714
715
716
717
  IF(ALLOCATED(this%s)) THEN
    s = ADJUSTR(this%s)
  ELSE
    s = ''
  ENDIF
HendersonSC's avatar
HendersonSC committed
718
ENDFUNCTION ADJUSTR_StringType
719
720
!
!-------------------------------------------------------------------------------
721
!> @brief Returns the starting position of a @c CHARACTER substring within a
722
723
724
725
726
727
728
!> @c StringType string.
!> @param string the @c StringType object in which @c substring is located
!> @param substring the @c CHARACTER string to locate within @c string
!> @param back a logical indicating whether or not to return the position of
!>        the first or last instance of the @c substring within @c string
!> @returns ipos the position in @c string of the @c substring
!>
HendersonSC's avatar
HendersonSC committed
729
730
731
732
733
ELEMENTAL FUNCTION INDEX_StringType_char(string,substring,back) RESULT(ipos)
  CLASS(StringType),INTENT(IN) :: string
  CHARACTER(LEN=*),INTENT(IN) :: substring
  LOGICAL,INTENT(IN),OPTIONAL :: back
  INTEGER :: ipos
734
735
736
737
738
  IF(ALLOCATED(string%s) .AND. LEN(substring) > 0) THEN
    ipos=INDEX(string%s,substring,back)
  ELSE
    ipos = 0
  ENDIF
HendersonSC's avatar
HendersonSC committed
739
ENDFUNCTION INDEX_StringType_char
740
741
!
!-------------------------------------------------------------------------------
742
!> @brief Returns the starting position of a @c StringType substring within a
743
744
745
746
747
748
749
!> @c CHARACTER string.
!> @param string the @c CHARACTER string in which @c substring is located
!> @param substring the @c StringType object to locate within @c string
!> @param back a logical indicating whether or not to return the position of
!>        the first or last instance of the @c substring within @c string
!> @returns ipos the position in @c string of the @c substring
!>
HendersonSC's avatar
HendersonSC committed
750
751
752
753
754
ELEMENTAL FUNCTION INDEX_char_StringType(string,substring,back) RESULT(ipos)
  CHARACTER(LEN=*),INTENT(IN) :: string
  CLASS(StringType),INTENT(IN) :: substring
  LOGICAL,INTENT(IN),OPTIONAL :: back
  INTEGER :: ipos
755
756
757
758
759
  IF(ALLOCATED(substring%s) .AND. LEN(string) > 0) THEN
    ipos=INDEX(string,substring%s,back)
  ELSE
    ipos = 0
  ENDIF
HendersonSC's avatar
HendersonSC committed
760
ENDFUNCTION INDEX_char_StringType
761
762
!
!-------------------------------------------------------------------------------
763
!> @brief Returns the starting position of a @c StringType substring within a
764
765
766
767
768
769
770
!> @c StringType string.
!> @param string the @c StringType object in which @c substring is located
!> @param substring the @c StringType object to locate within @c string
!> @param back a logical indicating whether or not to return the position of
!>        the first or last instance of the @c substring within @c string
!> @returns ipos the position in @c string of the @c substring
!>
HendersonSC's avatar
HendersonSC committed
771
772
773
774
775
ELEMENTAL FUNCTION INDEX_StringType_StringType(string,substring,back) RESULT(ipos)
  CLASS(StringType),INTENT(IN) :: string
  CLASS(StringType),INTENT(IN) :: substring
  LOGICAL,INTENT(IN),OPTIONAL :: back
  INTEGER :: ipos
776
777
778
779
780
  IF(ALLOCATED(string%s) .AND. ALLOCATED(substring%s)) THEN
    ipos=INDEX(string%s,substring%s,back)
  ELSE
    ipos = 0
  ENDIF
HendersonSC's avatar
HendersonSC committed
781
ENDFUNCTION INDEX_StringType_StringType
782
783
!
!-------------------------------------------------------------------------------
784
!> @brief Assigns a short integer to a StringType.
785
!> @param lhs the string object
786
!> @param rhs short integer
787
!>
HendersonSC's avatar
HendersonSC committed
788
789
790
791
792
793
794
ELEMENTAL SUBROUTINE assign_Short_Integer_to_StringType(lhs,rhs)
  CLASS(StringType),INTENT(INOUT) :: lhs
  INTEGER(SNK),INTENT(IN) :: rhs
  CHARACTER(LEN=11) :: l
  WRITE(l,'(i11)') rhs
  lhs = TRIM(ADJUSTL(l))
ENDSUBROUTINE assign_Short_Integer_to_StringType
795
796
797
!
!-------------------------------------------------------------------------------
!> @brief Assigns a long integer to a StringType.
798
799
!> @param lhs the string object
!> @param rhs long integer
800
!>
HendersonSC's avatar
HendersonSC committed
801
802
803
804
805
806
807
ELEMENTAL SUBROUTINE assign_Long_Integer_to_StringType(lhs,rhs)
  CLASS(StringType),INTENT(INOUT) ::lhs
  INTEGER(SLK),INTENT(IN) :: rhs
  CHARACTER(LEN=20) :: l
  WRITE(l,'(i20)') rhs
  lhs = TRIM(ADJUSTL(l))
ENDSUBROUTINE assign_Long_Integer_to_StringType
808
809
!
!-------------------------------------------------------------------------------
djabaay's avatar
djabaay committed
810
!> @brief Assigns a single real to a StringType.
811
!> @param lhs the string object
812
!> @param rhs single real
djabaay's avatar
djabaay committed
813
!>
HendersonSC's avatar
HendersonSC committed
814
815
816
817
818
819
820
ELEMENTAL SUBROUTINE assign_Single_Real_to_StringType(lhs,rhs)
  CLASS(StringType),INTENT(INOUT) :: lhs
  REAL(SSK),INTENT(IN) :: rhs
  CHARACTER(LEN=15) :: l
  WRITE(l,'(es15.5)') rhs
  lhs = TRIM(ADJUSTL(l))
ENDSUBROUTINE assign_Single_Real_to_StringType
djabaay's avatar
djabaay committed
821
822
823
!
!-------------------------------------------------------------------------------
!> @brief Assigns a double real to a StringType.
824
!> @param lhs the string object
825
!> @param rhs double real
djabaay's avatar
djabaay committed
826
!>
HendersonSC's avatar
HendersonSC committed
827
828
829
830
831
832
833
ELEMENTAL SUBROUTINE assign_Double_Real_to_StringType(lhs,rhs)
  CLASS(StringType),INTENT(INOUT) :: lhs
  REAL(SDK),INTENT(IN) :: rhs
  CHARACTER(LEN=22) :: l
  WRITE(l,'(es22.15)') rhs
  lhs = TRIM(ADJUSTL(l))
ENDSUBROUTINE assign_Double_Real_to_StringType
djabaay's avatar
djabaay committed
834
835
!
!-------------------------------------------------------------------------------
836
837
!> @brief Assigns a logical to a StringType.
!> @param lhs the string object
838
!> @param rhs the logical to be written
839
!>
HendersonSC's avatar
HendersonSC committed
840
841
842
843
844
845
846
SUBROUTINE assign_Logical_to_StringType(lhs,rhs)
  CLASS(StringType),INTENT(INOUT) :: lhs
  LOGICAL(SBK),INTENT(IN) :: rhs
  CHARACTER(LEN=1) :: tmp
  WRITE(tmp,'(L1)') rhs
  lhs = tmp
ENDSUBROUTINE assign_Logical_to_StringType
847
848
!
!-------------------------------------------------------------------------------
849
!> @brief Concatenates an intrinsic character type variable with a
850
851
852
!> @c StringType.
!> @param thisStr the string object
!> @param s the length of the string
853
854
!> @returns newstring a character string that is a concatenation of a
!> @c StringType and character string
855
!>
HendersonSC's avatar
HendersonSC committed
856
857
858
859
860
861
PURE FUNCTION concatenate_StringType_onto_char(lhs,rhs) RESULT(out_str)
  CHARACTER(LEN=*),INTENT(IN) :: lhs
  CLASS(StringType),INTENT(IN) :: rhs
  CHARACTER(LEN=:),ALLOCATABLE :: out_str
  out_str = (lhs//CHAR(rhs))
ENDFUNCTION concatenate_StringType_onto_char
862
863
!
!-------------------------------------------------------------------------------
864
!> @brief Concatenates an intrinsic character type variable with a
865
866
!> @c StringType.
!> @param s the length of the string
867
868
869
!> @param thisStr the string object
!> @returns newstring a character string that is a concatenation of a
!> @c StringType and character string
870
!>
HendersonSC's avatar
HendersonSC committed
871
872
873
874
875
876
PURE FUNCTION concatenate_char_onto_StringType(lhs,rhs) RESULT(out_str)
  CLASS(StringType),INTENT(IN) :: lhs
  CHARACTER(LEN=*),INTENT(IN) :: rhs
  CHARACTER(LEN=:),ALLOCATABLE :: out_str
  out_str = (CHAR(lhs)//rhs)
ENDFUNCTION concatenate_char_onto_StringType
877
878
!
!-------------------------------------------------------------------------------
879
880
881
882
883
!> @brief Concatenates a @c StringType type variable with a @c StringType.
!> @param s1 the string object
!> @param s2 the string object
!> @returns newstring a character string that is a concatenation of a
!> two @c StringTypes
884
!>
HendersonSC's avatar
HendersonSC committed
885
886
887
888
889
890
PURE FUNCTION concatenate_StringType_onto_StringType(lhs,rhs) RESULT(out_str)
  CLASS(StringType),INTENT(IN) :: lhs
  CLASS(StringType),INTENT(IN) :: rhs
  CHARACTER(LEN=:),ALLOCATABLE :: out_str
  out_str = (CHAR(lhs)//CHAR(rhs))
ENDFUNCTION concatenate_StringType_onto_StringType
891
892
893
894
!
!-------------------------------------------------------------------------------
!> @brief Performs an equal to operation of a @c CHARACTER and a
!> @c StringType.
895
896
!> @param lhs a @c CHARACTER type
!> @param rhs a @c StringType object
897
898
!> @returns bool the result of the == operation
!>
HendersonSC's avatar
HendersonSC committed
899
900
901
902
903
904
ELEMENTAL FUNCTION equalto_char_StringType(lhs,rhs) RESULT(bool)
  CHARACTER(LEN=*),INTENT(IN) :: lhs
  CLASS(StringType),INTENT(IN) ::rhs
  LOGICAL(SBK) :: bool
  bool = (lhs == CHAR(rhs))
ENDFUNCTION equalto_char_StringType
905
906
907
908
!
!-------------------------------------------------------------------------------
!> @brief Performs an equal to operation of a @c CHARACTER and a
!> @c StringType.
909
910
!> @param lhs a @c StringType object
!> @param rhs a @c CHARACTER type
911
912
!> @returns bool the result of the == operation
!>
HendersonSC's avatar
HendersonSC committed
913
914
915
916
917
918
ELEMENTAL FUNCTION equalto_StringType_char(lhs,rhs) RESULT(bool)
  CLASS(StringType),INTENT(IN) ::lhs
  CHARACTER(LEN=*),INTENT(IN) :: rhs
  LOGICAL(SBK) :: bool
    bool =  (CHAR(lhs) == rhs)
ENDFUNCTION equalto_StringType_char
919
920
921
922
!
!-------------------------------------------------------------------------------
!> @brief Performs an equal to operation of a @c StringType and a
!> @c StringType.
923
924
!> @param lhs a @c StringType object
!> @param rhs another @c StringType object
925
926
!> @returns bool the result of the == operation
!>
HendersonSC's avatar
HendersonSC committed
927
928
929
930
931
932
ELEMENTAL FUNCTION equalto_StringType_StringType(lhs,rhs) RESULT(bool)
  CLASS(StringType),INTENT(IN) :: lhs
  CLASS(StringType),INTENT(IN) :: rhs
  LOGICAL(SBK) :: bool
  bool = (CHAR(lhs) == CHAR(rhs))
ENDFUNCTION equalto_StringType_StringType
933
934
935
936
!
!-------------------------------------------------------------------------------
!> @brief Performs a not equal to operation of a @c CHARACTER and a
!> @c StringType.
937
938
!> @param lhs a @c CHARACTER type
!> @param rhs a @c StringType object
939
940
!> @returns bool the result of the /= operation
!>
HendersonSC's avatar
HendersonSC committed
941
942
943
944
945
946
ELEMENTAL FUNCTION notequalto_char_StringType(lhs,rhs) RESULT(bool)
  CHARACTER(LEN=*),INTENT(IN) :: lhs
  CLASS(StringType),INTENT(IN) :: rhs
  LOGICAL(SBK) :: bool
  bool = (lhs /= CHAR(rhs))
ENDFUNCTION notequalto_char_StringType
947
948
949
950
!
!-------------------------------------------------------------------------------
!> @brief Performs a not equal to operation of a @c CHARACTER and a
!> @c StringType.
951
952
!> @param lhs a @c StringType object
!> @param rhs a @c CHARACTER type
953
954
!> @returns bool the result of the /= operation
!>
HendersonSC's avatar
HendersonSC committed
955
956
957
958
959
960
ELEMENTAL FUNCTION notequalto_StringType_char(lhs,rhs) RESULT(bool)
  CLASS(StringType),INTENT(IN) :: lhs
  CHARACTER(LEN=*),INTENT(IN) :: rhs
  LOGICAL(SBK) :: bool
  bool = (CHAR(lhs) /= rhs)
ENDFUNCTION notequalto_StringType_char
961
962
963
964
!
!-------------------------------------------------------------------------------
!> @brief Performs a not equal to operation of a @c StringType and a
!> @c StringType.
965
966
!> @param lhs a @c StringType object
!> @param rhs another @c StringType object
967
968
!> @returns bool the result of the /= operation
!>
HendersonSC's avatar
HendersonSC committed
969
970
971
972
973
974
ELEMENTAL FUNCTION notequalto_StringType_StringType(lhs,rhs) RESULT(bool)
  CLASS(StringType),INTENT(IN) :: lhs
  CLASS(StringType),INTENT(IN) :: rhs
  LOGICAL(SBK) :: bool
  bool = (CHAR(lhs) /= CHAR(rhs))
ENDFUNCTION notequalto_StringType_StringType
975
!
976
977
978
979
980
!-------------------------------------------------------------------------------
!> @brief convert a string to an int
!> @param this the string being converted
!> @param stt position for starting conversion
!> @param stp end of conversion
981
!> @returns i the integer that will be output
982
!>
HendersonSC's avatar
HendersonSC committed
983
984
985
986
987
988
989
FUNCTION str_to_sik(this,stt,stp) RESULT(i)
  CLASS(StringType),INTENT(IN) :: this
  INTEGER(SIK),INTENT(IN),OPTIONAL :: stt
  INTEGER(SIK),INTENT(IN),OPTIONAL :: stp
  INTEGER(SIK) :: i
  !
  CHARACTER(LEN=:),ALLOCATABLE :: scratch
990

HendersonSC's avatar
HendersonSC committed
991
992
993
994
995
  IF(PRESENT(stt) .OR. PRESENT(stp)) THEN
    scratch = this%s(stt:stp)
  ELSE
    scratch = this%s
  ENDIF
996

HendersonSC's avatar
HendersonSC committed
997
998
999
1000
  IF(LEN(scratch) > 0) THEN
    READ(scratch,*) i
  ELSE
    i = -HUGE(i)
For faster browsing, not all history is shown. View entire blame