Strings.f90 61.9 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
!>  - @c ANY
!>  - @c ALL
31
32
33
!>
!> @par Module Dependencies
!>  - @ref IntrType "IntrType": @copybrief IntrType
34
35
36
37
38
39
!>
!> @par EXAMPLE
!> @code
!> PROGRAM
!>   USE Strings
!>   IMPLICIT NONE
40
!>
41
42
43
!>   CHARACTER(LEN=20) :: char20
!>   TYPE(StringType) :: myString
!>
44
45
46
47
48
49
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
!>   WRITE(*,*) '"'//TRIM(myString)//'"'
!>   WRITE(*,*) '"'//ADJUSTL(myString)//'"'
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
!>   WRITE(*,*) '"'//LEN(myString)//'"'
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
50
51
52
!>
!>   myString=' hello world'
!>   char20=myString
53
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
54
!>   WRITE(*,*) '"'//char20//'"'
55
!>   WRITE(*,*) '"'//TRIM(CHAR(myString))//'"'
56
!>   WRITE(*,*) '"'//TRIM(char20)//'"'
57
!>   WRITE(*,*) '"'//ADJUSTL(CHAR(myString))//'"'
58
!>   WRITE(*,*) '"'//ADJUSTL(char20)//'"'
59
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
60
!>   WRITE(*,*) '"'//ADJUSTR(char20)//'"'
61
!>   WRITE(*,*) '"'//LEN(myString)//'"'
62
!>   WRITE(*,*) '"'//LEN(char20)//'"'
63
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
64
65
66
!>   WRITE(*,*) '"'//LEN_TRIM(char20)//'"'
!>
!>   myString=char20
67
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
68
!>   WRITE(*,*) '"'//char20//'"'
69
!>   WRITE(*,*) '"'//TRIM(myString)//'"'
70
!>   WRITE(*,*) '"'//TRIM(char20)//'"'
71
!>   WRITE(*,*) '"'//ADJUSTL(myString)//'"'
72
!>   WRITE(*,*) '"'//ADJUSTL(char20)//'"'
73
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
74
!>   WRITE(*,*) '"'//ADJUSTR(char20)//'"'
75
!>   WRITE(*,*) '"'//LEN(myString)//'"'
76
!>   WRITE(*,*) '"'//LEN(char20)//'"'
77
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
78
!>   WRITE(*,*) '"'//LEN_TRIM(char20)//'"'
79
80
81
82
83
84
!>
!> END PROGRAM
!> @endcode
!>
!> @author Brendan Kochunas
!>   @date 07/25/2012
85
86
87
!> @par Revisions:
!>   (10/25/2013) - Dan Jabaay
!>   - Overloaded Instrinsic ALL and ANY operators
88
89
!>   (05/18/2016) - Dan Jabaay
!>   - Added overloading of ALL for 1-D, 2-D, and 3-D arrays.
90
91
92
!>
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
MODULE Strings
93

94
95
  USE IntrType
  IMPLICIT NONE
96
97
  PRIVATE

98
99
! List of Public items
  PUBLIC :: StringType
100
  PUBLIC :: getSubstring
101
  PUBLIC :: CHAR
102
103
104
105
106
  PUBLIC :: LEN
  PUBLIC :: LEN_TRIM
  PUBLIC :: TRIM
  PUBLIC :: ADJUSTL
  PUBLIC :: ADJUSTR
107
  PUBLIC :: INDEX
108
109
  PUBLIC :: ANY
  PUBLIC :: ALL
110
111
112
113
  PUBLIC :: ASSIGNMENT(=)
  PUBLIC :: OPERATOR(//)
  PUBLIC :: OPERATOR(==)
  PUBLIC :: OPERATOR(/=)
114

115
116
  !> Derived type for an arbitrary length string
  TYPE :: StringType
117
118
119
120
    !> The size of the string
    !>
    !> This is needed because SIZE(%s) does not reliably return 0 when
    !> s has been allocated and then unallocated.
121
    INTEGER(SIK) :: n=0
122
123
124
    !> The trim length of the string
    !>
    !> This is needed for the TRIM routine
125
    INTEGER(SIK) :: ntrim=0
126
127
128
129
130
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
    !> 
    CHARACTER(LEN=:),ALLOCATABLE,PRIVATE :: s
    CONTAINS
      !> copybrief StringType::toupper_string
      !> copydetails StringType::toupper_string
      PROCEDURE,PASS :: upper => toupper_string
      !> copybrief StringType::toLower_string
      !> copydetails StringType::toLower_string
      PROCEDURE,PASS :: lower => toLower_string
      !> copybrief StringType::at_string
      !> copydetails StringType::at_string
      PROCEDURE,PASS :: at_string
      !> copybrief StringType::at_string_slice
      !> copydetails StringType::at_string_slice
      PROCEDURE,PASS :: at_string_slice
      GENERIC :: at => at_string, at_string_slice
      !> copybrief StringType::replace_slice
      !> copydetails StringType::replace_slice
      PROCEDURE,PASS :: replace => replace_slice
      !> copybrief StringType:: split_string
      !> copydetails StringType:: split_string
      PROCEDURE,PASS :: split =>  split_string
      !> copybrief StringType::assign_char_to_StringType
      !> copydetails StringType::assign_char_to_StringType
      PROCEDURE,PASS :: assign_char_to_StringType
      !> @copybrief Strings::assign_Short_Integer_to_StringType
      !> @copydetails Strings::assign_Short_Integer_to_StringType
      PROCEDURE assign_Short_Integer_to_StringType
      !> @copybrief Strings::assign_Long_Integer_to_StringType
      !> @copydetails Strings::assign_Long_Integer_to_StringType
      PROCEDURE assign_Long_Integer_to_StringType
      !> @copybrief Strings::assign_Single_Real_to_StringType
      !> @copydetails Strings::assign_Single_Real_to_StringType
      PROCEDURE assign_Single_Real_to_StringType
      !> @copybrief Strings::assign_Double_Real_to_StringType
      !> @copydetails Strings::assign_Double_Real_to_StringType
      PROCEDURE assign_Double_Real_to_StringType
      !> copybrief StringType::assign_Logical_to_StringType
      !> copydetails StringType::assign_Logical_to_StringType
      PROCEDURE 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, &
          assign_Long_Integer_to_StringType
      !> copybrief StringType::str_to_sik
      !> copydetails StringType::str_to_sik
      PROCEDURE,PASS :: str_to_sik
      GENERIC :: stoi => str_to_sik
      !> copybrief StringType::str_to_srk
      !> copydetails StringType::str_to_srk
      PROCEDURE,PASS :: str_to_srk
      GENERIC :: stof => str_to_srk
      !> copybrief StringType::isInteger
      !> copydetails StringType::isInteger
      PROCEDURE,PASS :: isInteger
      !> copybrief StringType::isFloat
      !> copydetails StringType::isFloat
      PROCEDURE,PASS :: isFloat
      !> copybrief StringType::isNumeric_str
      !> copydetails StringType::isNumeric_str
      PROCEDURE,PASS :: isNumeric => isNumeric_str
187
  ENDTYPE StringType
188

189
190
191
192
193
194
195
  !> @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
  ENDINTERFACE
196

197
  !> @brief Overloads the Fortran intrinsic procedure LEN() so
198
199
200
201
202
203
  !> a string type argument may be passed.
  INTERFACE LEN
    !> @copybrief Strings::LEN_StringType
    !> @copydetails Strings::LEN_StringType
    MODULE PROCEDURE LEN_StringType
  ENDINTERFACE
204

205
  !> @brief Overloads the Fortran intrinsic procedure LEN_TRIM() so
206
207
208
209
210
211
  !> 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
212

213
  !> @brief Overloads the Fortran intrinsic procedure TRIM() so
214
215
216
217
218
219
  !> a string type argument may be passed.
  INTERFACE TRIM
    !> @copybrief Strings::TRIM_StringType
    !> @copydetails Strings::TRIM_StringType
    MODULE PROCEDURE TRIM_StringType
  ENDINTERFACE
220
221

  !> @brief Overloads the Fortran intrinsic procedure ADJUSTL() so
222
223
224
225
226
227
  !> a string type argument may be passed.
  INTERFACE ADJUSTL
    !> @copybrief Strings::ADJUSTL_StringType
    !> @copydetails Strings::ADJUSTL_StringType
    MODULE PROCEDURE ADJUSTL_StringType
  ENDINTERFACE
228
229

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

237
238
239
240
241
242
243
244
245
246
247
248
249
  !> @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
250
251

  !> @brief Overloads the Fortran intrinsic procedure ANY() so
252
253
254
255
256
257
258
259
260
261
  !> an array of string type arguments may be passed.
  INTERFACE ANY
    !> @copybrief Strings::ANY_StringTypeArray_char
    !> @copydetails Strings::ANY_StringTypeArray_char
    MODULE PROCEDURE ANY_StringTypeArray_char
    !> @copybrief Strings::ANY_StringTypeArray_StringType
    !> @copydetails Strings::ANY_StringTypeArray_StringType
    MODULE PROCEDURE ANY_StringTypeArray_StringType
    !> @copybrief Strings::ANY_char_StringTypeArray
    !> @copydetails Strings::ANY_char_StringTypeArray
Dan Jabaay's avatar
Dan Jabaay committed
262
263
264
265
    !MODULE PROCEDURE ANY_char_StringTypeArray
    !> @copybrief Strings::ANY_StringTypeStringTypeArray
    !> @copydetails Strings::ANY_StringTypeStringTypeArray
    !MODULE PROCEDURE ANY_StringTypeStringTypeArray
266
  ENDINTERFACE
267
268

  !> @brief Overloads the Fortran intrinsic procedure ALL() so
269
270
271
272
273
274
275
276
277
278
  !> an array of string type arguments may be passed.
  INTERFACE ALL
    !> @copybrief Strings::ALL_StringTypeArray_char
    !> @copydetails Strings::ALL_StringTypeArray_char
    MODULE PROCEDURE ALL_StringTypeArray_char
    !> @copybrief Strings::ALL_StringTypeArray_StringType
    !> @copydetails Strings::ALL_StringTypeArray_StringType
    MODULE PROCEDURE ALL_StringTypeArray_StringType
    !> @copybrief Strings::ALL_char_StringTypeArray
    !> @copydetails Strings::ALL_char_StringTypeArray
Dan Jabaay's avatar
Dan Jabaay committed
279
280
281
282
    !MODULE PROCEDURE ALL_char_StringTypeArray
    !!> @copybrief Strings::ALL_StringType_StringTypeArray
    !!> @copydetails Strings::ALL_StringType_StringTypeArray
    !MODULE PROCEDURE ALL_StringType_StringTypeArray
283
284
285
286
287
288
289
290
291
292
! at some point, add the ALL(1-D == scalar)...
    !> @copybrief Strings::ALL_StringType1a_StringType1a
    !> @copydetails Strings::ALL_StringType1a_StringType1a
    MODULE PROCEDURE ALL_StringType1a_StringType1a
    !> @copybrief Strings::ALL_StringType2a_StringType2a
    !> @copydetails Strings::ALL_StringType2a_StringType2a
    MODULE PROCEDURE ALL_StringType2a_StringType2a
    !> @copybrief Strings::ALL_StringType3a_StringType3a
    !> @copydetails Strings::ALL_StringType3a_StringType3a
    MODULE PROCEDURE ALL_StringType3a_StringType3a
293
  ENDINTERFACE
294

295
296
297
298
299
300
  !> @brief Overloads the assignment operator.
  INTERFACE ASSIGNMENT(=)
    !> @copybrief Strings::assign_StringType_to_char
    !> @copydetails Strings::assign_StringType_to_char
    MODULE PROCEDURE assign_StringType_to_char
  ENDINTERFACE
301

302
303
304
305
306
307
308
309
310
311
312
313
314
  !> @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
315

316
317
318
319
320
321
322
323
324
325
326
327
328
  !> @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
329

330
331
332
333
334
335
336
337
338
339
340
341
342
  !> @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
343
344
345
346
347
!
!===============================================================================
  CONTAINS
!
!-------------------------------------------------------------------------------
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
!> @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
!>
  ELEMENTAL SUBROUTINE assign_char_to_StringType(lhs,rhs)
    CLASS(StringType),INTENT(INOUT) :: lhs
    CHARACTER(LEN=*),INTENT(IN) :: rhs

    lhs%n=LEN(rhs)
    lhs%ntrim=LEN_TRIM(rhs)
    lhs%s = rhs
    
  ENDSUBROUTINE assign_char_to_StringType
!
!-------------------------------------------------------------------------------
!> @brief splits a StringType at specified delimiter and returns the substrings
!> in an array of StringTypes. If the delimiter is not found or the supplied
!> string is empty, the returned array is size 0 with an empty string type inside
!> @param this the string to be split
!> @param sub_str the returned array of substrings
!> @param delim the delimiter for split locations, OPTIONAL:default " "
!>
! The idea of this routine is to mirror the python .split() method as closely
! as possible, currently consecutive delimiters will be treated as one
  FUNCTION split_string(this,delim) RESULT(sub_str)
    CLASS(StringType),INTENT(IN) :: this
    CHARACTER(LEN=*),OPTIONAL,INTENT(IN) :: delim
    TYPE(StringType),ALLOCATABLE :: sub_str(:)
    !
    CHARACTER(LEN=:),ALLOCATABLE :: separator
    INTEGER(SIK) :: iSplit,nSplits,stt,stp,pos

    ! Use user supplied delimiter if provided. DEFAULT = " "
    IF(PRESENT(delim)) THEN
      ALLOCATE(CHARACTER(LEN(delim)) :: separator)
      separator = delim
    ELSE
      ALLOCATE(CHARACTER(1) :: separator)
      separator = " "
    ENDIF

    ! Count the number of splits
    nSplits = 0
    stt = 1
    stp = LEN(this%s)
    IF(stp < 1) THEN
      ALLOCATE(sub_str(0))
      RETURN
    ENDIF !stp < 1
    ! Search until the delimiter isn't found
    DO WHILE(INDEX(this%s(stt:stp),separator) > 0)
    ! If the index is greater than 1 then the first character must be a string
      IF(INDEX(this%s(stt:stp),separator) > 1) THEN
        ! Count the string
        nSplits = nSplits + 1
        ! Increment to the next delimiter
        stt = stt + INDEX(this%s(stt:stp),separator) + LEN(separator) - 1
      ELSE
        ! The first character was the delimiter so skip past it
        stt = stt + LEN(separator)
      ENDIF
    ENDDO
    ! If the string ends with a word, then it wasn't counted before
    IF(.NOT.(this%s(stp-LEN(separator)+1:stp) == separator)) THEN
      nSplits = nSplits + 1
    ENDIF
    ALLOCATE(sub_str(nSplits))

    ! Split along delimiters and store in the provided array
    stt = 1
    iSplit = 0
    DO WHILE(INDEX(this%s(stt:stp),separator) > 0)
      IF(INDEX(this%s(stt:stp),separator) > 1) THEN
        iSplit = iSplit + 1
        ! Strip out the string...subtract 2 (1 for exclusive, 1 for delimiter)
        sub_str(iSplit) = &
            this%s(stt:stt+INDEX(this%s(stt:stp),separator) - 2)
        stt = stt + INDEX(this%s(stt:stp),separator) + LEN(separator) - 1
      ELSE
        stt = stt + LEN(separator)
      ENDIF
    ENDDO
    ! If the string ends with a word, then it wasn't snatched out before
    IF(.NOT.(this%s(stp-LEN(separator)+1:stp) == separator)) THEN
      sub_str(nSplits) = &
          this%s(INDEX(this%s(1:stp),separator,.TRUE.)+LEN(separator):stp)
    ENDIF
  ENDFUNCTION split_string
!
!-------------------------------------------------------------------------------
!> @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
!>
  SUBROUTINE replace_slice(this,stt,stp,in_char)
    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
      
    IF((stt <= stp) .AND. (stt > 0) .AND. (stp <= this%n)) 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)
      ENDIF
      IF(part > 0) THEN
        tmp_str = TRIM(tmp_str)//in_char(1:part)
      ENDIF

      ! Direct substitution of characters
      this%s(stt:stp) = tmp_str(1:step)
    ENDIF
    
  ENDSUBROUTINE replace_slice
!
!-------------------------------------------------------------------------------
!> @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
!>
  FUNCTION at_string(this,pos) RESULT(letter)
    CLASS(StringType),INTENT(IN) :: this
    INTEGER(SIK),INTENT(IN) :: pos
    CHARACTER(LEN=1) :: letter

    IF((pos > 0) .AND. (pos <= this%n)) THEN
      letter = this%s(pos:pos)
    ELSE
      letter = CHAR(0)
    ENDIF
    
  ENDFUNCTION at_string
!
!-------------------------------------------------------------------------------
!> @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
!>
  FUNCTION at_string_slice(this,stt,stp) RESULT(slice)
    CLASS(StringType),INTENT(IN) :: this
    INTEGER(SIK),INTENT(IN) :: stt
    INTEGER(SIK),INTENT(IN) :: stp
    CHARACTER(LEN=:),ALLOCATABLE :: slice
    !
    INTEGER(SIK) :: iCh,i

    IF((stt <= stp) .AND. (stt > 0) .AND. (stp <= this%n)) THEN
      slice = this%s(stt:stp)
    ELSE
      slice = CHAR(0)
    ENDIF
    
  ENDFUNCTION at_string_slice
!
!-------------------------------------------------------------------------------
!> @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
!>
    PURE FUNCTION toLower_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') &
          upper_str%s(i:i)=ACHAR(IACHAR(word%s(i:i))+32)
      ENDDO
    ENDFUNCTION toLower_string
!
!-------------------------------------------------------------------------------
!> @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
!>
    PURE FUNCTION toupper_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') &
          lower_str%s(i:i)=ACHAR(IACHAR(word%s(i:i))-32)
      ENDDO
    ENDFUNCTION toupper_string
!
!-------------------------------------------------------------------------------
!> Returns a StringType object which is a section of the input StringType object
!> @param string the StringType object to get substring from
!> @param substring the substring stored as a StringType object
!> @param stt the index of input string at which the substring starts
!> @param stp the index of input string at which the substring stops
!>
    PURE SUBROUTINE getSubstring(string,substring,stt,stp)
      TYPE(StringType),INTENT(IN) :: string
      TYPE(StringType),INTENT(OUT) :: substring
      INTEGER(SIK),INTENT(IN) :: stt,stp
      INTEGER(SIK) :: i,sublen

      IF(stp >= stt) THEN
        IF(stp <= string%n) THEN
          IF(stt > 0) THEN
            sublen=stp-stt+1
            ALLOCATE(CHARACTER(sublen) :: substring%s)
            substring%n=sublen
            DO i=stt,stp
              substring%s(i-stt+1:i-stt+1)=string%s(i:i)
            ENDDO
            substring%ntrim=substring%n
            DO i=sublen,1,-1
              IF(substring%s(i:i) == ' ') THEN
                substring%ntrim=substring%ntrim-1
              ELSE
                EXIT
              ENDIF
            ENDDO
          ELSE
            substring=string
          ENDIF
        ELSE
          substring=string
        ENDIF
      ELSE
        substring=''
      ENDIF
    ENDSUBROUTINE getSubstring
!
!-------------------------------------------------------------------------------
596
!> @brief Returns the length of the string including all whitespace.
597
!> @param this the string object
598
599
!> @returns n the length of the string
!>
600
601
602
!> Should behave exactly as it would if called directly on a character array
    ELEMENTAL FUNCTION LEN_StringType(this) RESULT(n)
      CLASS(StringType),INTENT(IN) :: this
603
      INTEGER(SIK) :: n
604
      n=LEN(this%s)
605
    ENDFUNCTION LEN_StringType
606
607
!
!-------------------------------------------------------------------------------
608
!> @brief Returns the length of the string excluding trailing whitespace.
609
!> @param this the string object
610
611
!> @returns n the length of the string
!>
612
613
614
!> Should behave exactly as it would if called directly on a character array
    ELEMENTAL FUNCTION LEN_TRIM_StringType(this) RESULT(n)
      CLASS(StringType),INTENT(IN) :: this
615
      INTEGER(SIK) :: n
616
      n=LEN_TRIM(this%s)
617
618
619
620
621
    ENDFUNCTION LEN_TRIM_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable.
622
623
!> @param this the string object
!> @returns str the character type with the value of @c this
624
625
626
!>
!> This routine basically converts a @c StringType to a @c CHARACTER type.
!>
627
628
629
630
631
632
633
634
    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)
635
      ENDIF
636
    ENDFUNCTION CHAR_StringType
637
638
639
640
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string excluding all trailing whitespace
!> as an intrinsic character type variable.
641
642
!> @param this the string object
!> @returns str the character string
643
644
645
646
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c TRIM does for character variables.
!>
647
648
649
650
651
652
653
654
    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)
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
      ENDIF
    ENDFUNCTION TRIM_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable with all preceding whitespace moved to the end.
!> @param thisStr the string object
!> @returns s the character string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ADJUSTL does for character variables.
!>
    PURE FUNCTION ADJUSTL_StringType(thisStr) RESULT(s)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=thisStr%n) :: s
670
      s=ADJUSTL(thisStr%s)
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    ENDFUNCTION ADJUSTL_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable with all trailing whitespace moved to the beginning.
!> @param thisStr the string object
!> @returns s the character string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ADJUSTR does for character variables.
!>
    PURE FUNCTION ADJUSTR_StringType(thisStr) RESULT(s)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=thisStr%n) :: s
685
      s=ADJUSTR(thisStr%s)
686
    ENDFUNCTION ADJUSTR_StringType
687
688
!
!-------------------------------------------------------------------------------
689
!> @brief Returns the starting position of a @c CHARACTER substring within a
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
!> @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
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c INDEX does for character variables.
!>
    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
705
      ipos=INDEX(string%s,substring,back)
706
707
708
    ENDFUNCTION INDEX_StringType_char
!
!-------------------------------------------------------------------------------
709
!> @brief Returns the starting position of a @c StringType substring within a
710
711
712
713
714
715
716
717
718
719
720
!> @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
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c INDEX does for character variables.
!>
    ELEMENTAL FUNCTION INDEX_char_StringType(string,substring,back) RESULT(ipos)
721
      CHARACTER(LEN=*),INTENT(IN) :: string
722
723
724
      CLASS(StringType),INTENT(IN) :: substring
      LOGICAL,INTENT(IN),OPTIONAL :: back
      INTEGER :: ipos
725
      ipos=INDEX(string,substring%s,back)
726
727
728
    ENDFUNCTION INDEX_char_StringType
!
!-------------------------------------------------------------------------------
729
!> @brief Returns the starting position of a @c StringType substring within a
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
!> @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
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c INDEX does for character variables.
!>
    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
745
      ipos=INDEX(string%s,substring%s,back)
746
747
748
    ENDFUNCTION INDEX_StringType_StringType
!
!-------------------------------------------------------------------------------
749
750
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where only one entry in the array need to
751
752
753
754
755
756
757
758
759
760
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ANY_StringTypeArray_char(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
761
      TYPE(StringType),INTENT(IN) :: thisStr(:)
762
      CHARACTER(LEN=*),INTENT(IN) :: s
763
764
765
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i
766

767
768
769
770
771
772
773
774
775
776
777
778
      bool=.FALSE.
      IF(PRESENT(not)) bool=not
      DO i=1,SIZE(thisStr,DIM=1)
        IF(thisStr(i) == s) THEN
          bool=.TRUE.
          IF(PRESENT(not)) bool=.NOT.not
          EXIT
        ENDIF
      ENDDO
    ENDFUNCTION ANY_StringTypeArray_char
!
!-------------------------------------------------------------------------------
779
780
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string s where only one entry in the array need to
781
782
783
784
785
786
787
788
789
790
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ANY_StringTypeArray_StringType(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
791
      TYPE(StringType),INTENT(IN) :: thisStr(:)
792
793
794
      CLASS(StringType),INTENT(IN) :: s
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
795

796
797
      IF(PRESENT(not)) THEN
        bool=ANY_StringTypeArray_char(thisStr,CHAR(s),not)
798
      ELSE
799
        bool=ANY_StringTypeArray_char(thisStr,CHAR(s))
800
      ENDIF
801
802
803
    ENDFUNCTION ANY_StringTypeArray_StringType
!
!-------------------------------------------------------------------------------
804
805
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where only one entry in the array need to
806
807
808
809
810
811
812
813
814
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
Dan Jabaay's avatar
Dan Jabaay committed
815
!    PURE FUNCTION ANY_char_StringTypeArray(s,thisStr,not) RESULT(bool)
816
!      CHARACTER(LEN=*),INTENT(IN) :: s
Dan Jabaay's avatar
Dan Jabaay committed
817
818
819
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
820
!
Dan Jabaay's avatar
Dan Jabaay committed
821
822
823
824
825
826
!      IF(PRESENT(not)) THEN
!        bool=ANY_StringTypeArray_char(thisStr,s,not)
!      ELSE
!        bool=ANY_StringTypeArray_char(thisStr,s)
!      ENDIF
!    ENDFUNCTION ANY_char_StringTypeArray
827
828
!
!-------------------------------------------------------------------------------
829
830
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string s where only one entry in the array need to
831
832
833
834
835
836
837
838
839
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
Dan Jabaay's avatar
Dan Jabaay committed
840
!    PURE FUNCTION ANY_StringTypeStringTypeArray(s,thisStr,not) RESULT(bool)
841
!      CLASS(StringType),INTENT(IN) :: s
Dan Jabaay's avatar
Dan Jabaay committed
842
843
844
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
845
!
Dan Jabaay's avatar
Dan Jabaay committed
846
847
848
849
850
851
!      IF(PRESENT(not)) THEN
!        bool=ANY_StringTypeArray_char(thisStr,CHAR(s),not)
!      ELSE
!        bool=ANY_StringTypeArray_char(thisStr,CHAR(s))
!      ENDIF
!    ENDFUNCTION ANY_StringTypeStringTypeArray
852
853
!
!-------------------------------------------------------------------------------
854
855
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where all entries in the array need to
856
857
858
859
860
861
862
863
864
865
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringTypeArray_char(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
866
      TYPE(StringType),INTENT(IN) :: thisStr(:)
867
      CHARACTER(LEN=*),INTENT(IN) :: s
868
869
870
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i
871

872
873
874
875
876
877
878
879
880
881
882
883
      bool=.TRUE.
      IF(PRESENT(not)) bool=.NOT.not
      DO i=1,SIZE(thisStr,DIM=1)
        IF(thisStr(i) /= s) THEN
          bool=.FALSE.
          IF(PRESENT(not)) bool=not
          EXIT
        ENDIF
      ENDDO
    ENDFUNCTION ALL_StringTypeArray_char
!
!-------------------------------------------------------------------------------
884
885
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
886
887
888
889
890
891
892
893
894
895
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringTypeArray_StringType(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
896
      TYPE(StringType),INTENT(IN) :: thisStr(:)
897
      CLASS(StringType),INTENT(IN) :: s
898
899
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
900

901
902
903
904
905
906
907
908
      IF(PRESENT(not)) THEN
        bool=ALL_StringTypeArray_char(thisStr,CHAR(s),not)
      ELSE
        bool=ALL_StringTypeArray_char(thisStr,CHAR(s))
      ENDIF
    ENDFUNCTION ALL_StringTypeArray_StringType
!
!-------------------------------------------------------------------------------
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringType1a_StringType1a(thisStr,s,not) RESULT(bool)
      TYPE(StringType),INTENT(IN) :: thisStr(:)
      TYPE(StringType),INTENT(IN) :: s(:)
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i

      bool=.FALSE.
      IF(SIZE(thisStr,DIM=1) == SIZE(s,DIM=1)) THEN
        bool=.TRUE.
        IF(PRESENT(not)) THEN
          IF(not) THEN
            DO i=1,SIZE(s,DIM=1)
              bool=bool .AND. (thisStr(i) /= s(i))
            ENDDO
          ELSE
            DO i=1,SIZE(s,DIM=1)
              bool=bool .AND. (thisStr(i) == s(i))
            ENDDO
          ENDIF
        ELSE
          DO i=1,SIZE(s,DIM=1)
            bool=bool .AND. (thisStr(i) == s(i))
          ENDDO
        ENDIF
      ENDIF
    ENDFUNCTION ALL_StringType1a_StringType1a
!
!-------------------------------------------------------------------------------
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringType2a_StringType2a(thisStr,s,not) RESULT(bool)
      TYPE(StringType),INTENT(IN) :: thisStr(:,:)
      TYPE(StringType),INTENT(IN) :: s(:,:)
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i,j

      bool=.FALSE.
      IF((SIZE(thisStr,DIM=1) == SIZE(s,DIM=1)) .AND. &
         (SIZE(thisStr,DIM=2) == SIZE(s,DIM=2))) THEN
        bool=.TRUE.
        IF(PRESENT(not)) THEN
          IF(not) THEN
            DO j=1,SIZE(s,DIM=2)
              DO i=1,SIZE(s,DIM=1)
                bool=bool .AND. (thisStr(i,j) /= s(i,j))
              ENDDO
            ENDDO
          ELSE
            DO j=1,SIZE(s,DIM=2)
              DO i=1,SIZE(s,DIM=1)
                bool=bool .AND. (thisStr(i,j) == s(i,j))
              ENDDO
            ENDDO
          ENDIF
        ELSE
          DO j=1,SIZE(s,DIM=2)
            DO i=1,SIZE(s,DIM=1)
              bool=bool .AND. (thisStr(i,j) == s(i,j))
            ENDDO
          ENDDO
        ENDIF
      ENDIF
    ENDFUNCTION ALL_StringType2a_StringType2a
!
!-------------------------------------------------------------------------------
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
For faster browsing, not all history is shown. View entire blame